Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: upading edx-enterprise version + adding tests #35924

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions common/djangoapps/third_party_auth/tests/specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ def test_canceling_authentication_redirects_to_account_settings_when_auth_entry_
def test_canceling_authentication_redirects_to_root_when_auth_entry_not_set(self):
self.assert_exception_redirect_looks_correct('/')

@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
@mock.patch('common.djangoapps.third_party_auth.pipeline.segment.track')
def test_full_pipeline_succeeds_for_linking_account(self, _mock_segment_track):
# First, create, the GET request and strategy that store pipeline state,
Expand Down Expand Up @@ -632,6 +633,7 @@ def test_full_pipeline_succeeds_for_linking_account(self, _mock_segment_track):
self.assert_social_auth_exists_for_user(get_request.user, strategy)
self.assert_account_settings_context_looks_correct(account_settings_context(get_request), linked=True)

@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
def test_full_pipeline_succeeds_for_unlinking_account(self):
# First, create, the GET request and strategy that store pipeline state,
# configure the backend, and mock out wire traffic.
Expand Down Expand Up @@ -702,6 +704,7 @@ def test_linking_already_associated_account_raises_auth_already_associated(self)
# pylint: disable=protected-access
actions.do_complete(backend, social_views._do_login, user=unlinked_user, request=strategy.request)

@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
def test_already_associated_exception_populates_dashboard_with_error(self):
# Instrument the pipeline with an exception. We test that the
# exception is raised correctly separately, so it's ok that we're
Expand Down Expand Up @@ -737,6 +740,7 @@ def test_already_associated_exception_populates_dashboard_with_error(self):
self.assert_account_settings_context_looks_correct(
account_settings_context(post_request), duplicate=True, linked=True)

@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
@mock.patch('common.djangoapps.third_party_auth.pipeline.segment.track')
def test_full_pipeline_succeeds_for_signing_in_to_existing_active_account(self, _mock_segment_track):
# First, create, the GET request and strategy that store pipeline state,
Expand Down Expand Up @@ -866,6 +870,7 @@ def test_pipeline_redirects_to_requested_url(self):
requested_redirect_url,
)

@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
def test_full_pipeline_succeeds_registering_new_account(self):
# First, create, the request and strategy that store pipeline state.
# Mock out wire traffic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def instrument_course_progress_render(

@ddt.data(*itertools.product(('no_overrides', 'ccx'), list(range(1, 4)), (True, False), (True, False)))
@ddt.unpack
@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
@override_settings(
XBLOCK_FIELD_DATA_WRAPPERS=[],
MODULESTORE_FIELD_OVERRIDE_PROVIDERS=[],
Expand Down
1 change: 1 addition & 0 deletions lms/djangoapps/grades/tests/test_course_grade_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_course_grade_no_access(self):
grade = CourseGradeFactory().read(self.request.user, invisible_course)
assert grade.percent == 0

@patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
def test_read_and_update(self):
grade_factory = CourseGradeFactory()

Expand Down
2 changes: 2 additions & 0 deletions lms/envs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
# the one in cms/envs/test.py
FEATURES['ENABLE_DISCUSSION_SERVICE'] = False

FEATURES['ENABLE_ENTERPRISE_INTEGRATION'] = True

FEATURES['ENABLE_SERVICE_STATUS'] = True

FEATURES['ENABLE_VERIFIED_CERTIFICATES'] = True
Expand Down
Empty file added make
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ def setUp(self): # pylint: disable=arguments-differ
self.configure_google_provider(enabled=True, visible=True)
self.configure_facebook_provider(enabled=True, visible=True)

# Python-social saves auth failure notifcations in Django messages.
# Python-social saves auth failure notifications in Django messages.
# See pipeline.get_duplicate_provider() for details.
self.request.COOKIES = {}
MessageMiddleware(get_response=lambda request: None).process_request(self.request)
messages.error(self.request, 'Facebook is already in use.', extra_tags='Auth facebook')

@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
@mock.patch('openedx.features.enterprise_support.api.enterprise_customer_for_request')
def test_context(self, mock_enterprise_customer_for_request):
self.request.site = SiteFactory.create()
Expand Down Expand Up @@ -109,6 +110,7 @@ def test_context(self, mock_enterprise_customer_for_request):
expected_beta_language = {'code': 'lt-lt', 'name': settings.LANGUAGE_DICT.get('lt-lt')}
assert context['beta_language'] == expected_beta_language

@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
@with_site_configuration(
configuration={
'extended_profile_fields': ['work_experience']
Expand All @@ -124,6 +126,7 @@ def test_context_extended_profile(self):
assert extended_pofile_field['field_name'] == 'work_experience'
assert extended_pofile_field['field_label'] == 'Work experience'

@mock.patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
@mock.patch('openedx.core.djangoapps.user_api.accounts.settings_views.enterprise_customer_for_request')
@mock.patch('openedx.features.enterprise_support.utils.third_party_auth.provider.Registry.get')
def test_context_for_enterprise_learner(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def test_login_success(self):

FEATURES_WITH_AUTHN_MFE_ENABLED = settings.FEATURES.copy()
FEATURES_WITH_AUTHN_MFE_ENABLED['ENABLE_AUTHN_MICROFRONTEND'] = True
FEATURES_WITH_AUTHN_MFE_ENABLED['ENABLE_ENTERPRISE_INTEGRATION'] = False

@override_settings(MARKETING_EMAILS_OPT_IN=True)
def test_login_success_with_opt_in_flag_enabled(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from django.urls import reverse
from pytz import UTC
from unittest.mock import patch

from openedx.core.djangoapps.waffle_utils.testutils import WAFFLE_TABLES
from openedx.features.content_type_gating.models import ContentTypeGatingConfig
Expand Down Expand Up @@ -40,6 +41,7 @@ def test_view(self):
self.assertContains(response, 'First Message')
self.assertContains(response, 'Second Message')

@patch.dict("django.conf.settings.FEATURES", {"ENABLE_ENTERPRISE_INTEGRATION": False})
def test_queries(self):
ContentTypeGatingConfig.objects.create(enabled=True, enabled_as_of=datetime(2018, 1, 1, tzinfo=UTC))
self.create_course_update('First Message')
Expand Down
70 changes: 69 additions & 1 deletion openedx/features/enterprise_support/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import ddt
import httpretty
import json
import pytest
from testfixtures import LogCapture
from consent.models import DataSharingConsent
Expand All @@ -14,9 +15,11 @@
from django.http import HttpResponseRedirect
from django.test.utils import override_settings
from django.urls import reverse
from django.utils.http import urlencode
from edx_django_utils.cache import get_cache_key, TieredCache
from enterprise.models import EnterpriseCustomerUser # lint-amnesty, pylint: disable=wrong-import-order
from requests.exceptions import HTTPError
from rest_framework.test import APIClient
from six.moves.urllib.parse import parse_qs

from common.djangoapps.student.tests.factories import UserFactory
Expand Down Expand Up @@ -56,6 +59,7 @@
from openedx.features.enterprise_support.tests import FEATURES_WITH_ENTERPRISE_ENABLED
from openedx.features.enterprise_support.tests.factories import (
EnterpriseCourseEnrollmentFactory,
EnterpriseCustomerFactory,
EnterpriseCustomerIdentityProviderFactory,
EnterpriseCustomerUserFactory,
)
Expand Down Expand Up @@ -89,9 +93,18 @@ def setUpTestData(cls):
username=settings.ENTERPRISE_SERVICE_WORKER_USERNAME,
email='[email protected]',
password='password123',
is_staff=True
)
cls.enterprise_customer = EnterpriseCustomerFactory()
super().setUpTestData()

def setUp(self):
super().setUp()
self.client = APIClient()
self.client.login(
username=settings.ENTERPRISE_SERVICE_WORKER_USERNAME,
password='password123')

def _assert_api_service_client(self, api_client, mocked_jwt_builder):
"""
Verify that the provided api client uses the enterprise service user to generate
Expand Down Expand Up @@ -151,7 +164,7 @@ def _assert_get_enterprise_customer_with_cache(self, api_client, enterprise_cust
@mock.patch('openedx.features.enterprise_support.api.create_jwt_for_user')
def test_enterprise_api_client_with_service_user(self, mock_jwt_builder):
"""
Verify that enterprise API service client uses enterprcreate_jwt_for_userise service user
Verify that enterprise API service client uses enterprise service user
by default to authenticate and access enterprise API.
"""
self._assert_api_service_client(EnterpriseApiServiceClient, mock_jwt_builder)
Expand Down Expand Up @@ -1405,3 +1418,58 @@ def test_unlink_enterprise_user_from_idp_no_customer_user(self, mock_customer_fr
unlink_enterprise_user_from_idp(request, self.user, idp_backend_name='the-backend-name')

assert 0 == EnterpriseCustomerUser.objects.filter(user_id=self.user.id).count()

@mock.patch('openedx.features.enterprise_support.api.create_jwt_for_user')
def test_list_members_filtered(self, mock_jwt_builder):
"""
Test that the enterprise customer members endpoint works and can be filtered
by user query
"""
user_1 = UserFactory(first_name="Rhaenyra", last_name="Targaryen")
user_2 = UserFactory(first_name="Alicent", last_name="Hightower")
user_3 = UserFactory(first_name="Jace", last_name="Targaryen")
user_4 = UserFactory(first_name="Bad", last_name="Name")
user_4.profile.name = "Helaena Targaryen"
user_4.profile.save()

enterprise_customer = EnterpriseCustomerFactory()
EnterpriseCustomerUserFactory(
user_id=user_1.id,
enterprise_customer=enterprise_customer
)
EnterpriseCustomerUserFactory(
user_id=user_2.id,
enterprise_customer=enterprise_customer
)
EnterpriseCustomerUserFactory(
user_id=user_3.id,
enterprise_customer=enterprise_customer
)
EnterpriseCustomerUserFactory(
user_id=user_4.id,
enterprise_customer=enterprise_customer
)

# Test valid UUID
url = reverse('enterprise-customer-members', kwargs={'enterprise_uuid': enterprise_customer.uuid})
response = self.client.get(url)
assert response.status_code == 200
results = json.loads(response.content.decode("utf-8"))['results']

# list should be sorted alphabetically by name
assert len(results) == 4
assert results[0]['enterprise_customer_user']['name'] == (user_2.first_name + ' ' + user_2.last_name)
assert results[1]['enterprise_customer_user']['name'] == (user_4.profile.name)
assert results[2]['enterprise_customer_user']['name'] == (user_3.first_name + ' ' + user_3.last_name)
assert results[3]['enterprise_customer_user']['name'] == (user_1.first_name + ' ' + user_1.last_name)

# use user query to filter by name
query_params = {'user_query': user_2.first_name}
url = reverse(
'enterprise-customer-members',
kwargs={'enterprise_uuid': enterprise_customer.uuid}) + '?' + urlencode(query_params)
response = self.client.get(url)
assert response.status_code == 200
results = (json.loads(response.content.decode("utf-8"))['results'])
assert len(results) == 1
assert results[0]['enterprise_customer_user']['name'] == (user_2.first_name + ' ' + user_2.last_name)
Loading