def test_another_user_with_certs_shared_custom(self, auth_type, scopes_enforced): """ Returns 200 with cert list for OAuth, Session, and JWT auth. Returns 200 for jwt_restricted and user:me filter unset. """ self.student.profile.year_of_birth = 1977 self.student.profile.save() UserPreferenceFactory.build( user=self.student, key='account_privacy', value='custom', ).save() UserPreferenceFactory.build( user=self.student, key='visibility.course_certificates', value='all_users', ).save() with ENFORCE_JWT_SCOPES.override(active=scopes_enforced): resp = self.get_response(auth_type, requesting_user=self.other_student) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(len(resp.data), 1)
def test_preferences(self): lang_pref = UserPreferenceFactory(user=self.user, key=LANGUAGE_KEY, value="language pref test value") UserPreferenceFactory(user=self.user, key="non_included_key") result = self._get_detail() self.assertEqual( result["preferences"], { NOTIFICATION_PREF_KEY: self.notification_pref.value, LANGUAGE_KEY: lang_pref.value, })
def test_another_user_with_certs_shared_public(self, auth_type): """ Returns 200 with cert list for OAuth, Session, and JWT auth. Returns 200 for jwt_restricted and user:me filter unset. """ self.student.profile.year_of_birth = 1977 self.student.profile.save() UserPreferenceFactory.build( user=self.student, key='account_privacy', value='all_users', ).save() resp = self.get_response(auth_type, requesting_user=self.global_staff) assert resp.status_code == status.HTTP_200_OK assert len(resp.data) == 1
def test_another_user_with_certs_shared_public(self, auth_type, scopes_enforced): """ Returns 200 with cert list for OAuth, Session, and JWT auth. Returns 200 for jwt_restricted and user:me filter unset. """ self.student.profile.year_of_birth = 1977 self.student.profile.save() UserPreferenceFactory.build( user=self.student, key='account_privacy', value='all_users', ).save() with ENFORCE_JWT_SCOPES.override(active=scopes_enforced): resp = self.get_response(auth_type, requesting_user=self.other_student) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(len(resp.data), 1)
def test_add_user_preferred_language_to_template_slug_successfully( slug, language, expected): """ Assert that Mandrill function returns template slug as per user selected language """ user = UserFactory() UserPreferenceFactory(user=user, key='pref-lang', value=language) assert add_user_preferred_language_to_template_slug(slug, user.email) == expected
def test_db_access(self): for _ in range(10): new_user = UserFactory() UserPreferenceFactory(user=new_user, key=NOTIFICATION_PREF_KEY) # The number of queries is one for the users plus one for each prefetch # in NotifierUsersViewSet (roles__permissions does one for each table). with self.assertNumQueries(6): self._get_list()
def test_owner_can_access_its_certs(self): """ Tests the owner of the certs can access the certificate list api """ self.student.profile.year_of_birth = 1977 self.student.profile.save() UserPreferenceFactory.build( user=self.student, key='visibility.course_certificates', value='private', ).save() resp = self.get_response(AuthType.session, requesting_user=self.student) assert resp.status_code == status.HTTP_200_OK # verifies that other than owner cert list api is not accessible resp = self.get_response(AuthType.session, requesting_user=self.other_student) assert resp.status_code == status.HTTP_403_FORBIDDEN
def test_public_profile_certs_is_accessible(self): """ Tests the public profile certs can be accessed by all users """ self.student.profile.year_of_birth = 1977 self.student.profile.save() UserPreferenceFactory.build( user=self.student, key='visibility.course_certificates', value='all_users', ).save() resp = self.get_response(AuthType.session, requesting_user=self.student) assert resp.status_code == status.HTTP_200_OK resp = self.get_response(AuthType.session, requesting_user=self.other_student) assert resp.status_code == status.HTTP_200_OK resp = self.get_response(AuthType.session, requesting_user=self.global_staff) assert resp.status_code == status.HTTP_200_OK
def test_context(self, mock_enterprise_customer_for_request): self.request.site = SiteFactory.create() UserPreferenceFactory(user=self.user, key='pref-lang', value='lt-lt') DarkLangConfig(released_languages='en', changed_by=self.user, enabled=True, beta_languages='lt-lt', enable_beta_languages=True).save() mock_enterprise_customer_for_request.return_value = {} with override_settings(LANGUAGES=[EN, LT_LT], LANGUAGE_CODE='en'): context = account_settings_context(self.request) user_accounts_api_url = reverse( "accounts_api", kwargs={'username': self.user.username}) self.assertEqual(context['user_accounts_api_url'], user_accounts_api_url) user_preferences_api_url = reverse( 'preferences_api', kwargs={'username': self.user.username}) self.assertEqual(context['user_preferences_api_url'], user_preferences_api_url) for attribute in self.FIELDS: self.assertIn(attribute, context['fields']) self.assertEqual( context['user_accounts_api_url'], reverse("accounts_api", kwargs={'username': self.user.username})) self.assertEqual( context['user_preferences_api_url'], reverse('preferences_api', kwargs={'username': self.user.username})) self.assertEqual(context['duplicate_provider'], 'facebook') self.assertEqual(context['auth']['providers'][0]['name'], 'Facebook') self.assertEqual(context['auth']['providers'][1]['name'], 'Google') self.assertEqual(context['sync_learner_profile_data'], False) self.assertEqual(context['edx_support_url'], settings.SUPPORT_SITE_LINK) self.assertEqual(context['enterprise_name'], None) self.assertEqual(context['enterprise_readonly_account_fields'], { 'fields': list(get_enterprise_readonly_account_fields(self.user)) }) expected_beta_language = { 'code': 'lt-lt', 'name': settings.LANGUAGE_DICT.get('lt-lt') } self.assertEqual(context['beta_language'], expected_beta_language)
def setUp(self): super(NotifierUsersViewSetTest, self).setUp() self.courses = [] self.cohorts = [] self.user = UserFactory() self.notification_pref = UserPreferenceFactory( user=self.user, key=NOTIFICATION_PREF_KEY, value="notification pref test value") self.list_view = NotifierUsersViewSet.as_view({"get": "list"}) self.detail_view = NotifierUsersViewSet.as_view({"get": "retrieve"})
def test_multiple_users(self): other_user = UserFactory() other_notification_pref = UserPreferenceFactory( user=other_user, key=NOTIFICATION_PREF_KEY, value="other value" ) self._set_up_course(is_course_cohorted=True, is_user_cohorted=True, is_moderator=False) self._set_up_course(is_course_cohorted=False, is_user_cohorted=False, is_moderator=False) # Users have different sets of enrollments CourseEnrollmentFactory(user=other_user, course_id=self.courses[0].id) result_map = {result["id"]: result for result in self._get_list()} self.assertEqual(set(result_map.keys()), {self.user.id, other_user.id}) for user in [self.user, other_user]: self._assert_basic_user_info_correct(user, result_map[user.id]) self.assertEqual( result_map[self.user.id]["preferences"], {NOTIFICATION_PREF_KEY: self.notification_pref.value} ) self.assertEqual( result_map[other_user.id]["preferences"], {NOTIFICATION_PREF_KEY: other_notification_pref.value} ) self.assertEqual( result_map[self.user.id]["course_info"], { six.text_type(self.courses[0].id): { "cohort_id": self.cohorts[0].id, "see_all_cohorts": False, }, six.text_type(self.courses[1].id): { "cohort_id": None, "see_all_cohorts": True, }, } ) self.assertEqual( result_map[other_user.id]["course_info"], { six.text_type(self.courses[0].id): { "cohort_id": None, "see_all_cohorts": False, }, } )
def test_pagination(self, page_size): num_users = 12 users = [self.user] while len(users) < num_users: new_user = UserFactory() users.append(new_user) UserPreferenceFactory(user=new_user, key=NOTIFICATION_PREF_KEY) num_pages = (num_users - 1) / page_size + 1 result_list = [] for i in range(1, num_pages + 1): result_list.extend(self._get_list(page=i, page_size=page_size)) result_map = {result["id"]: result for result in result_list} self.assertEqual(len(result_list), num_users) for user in users: self._assert_basic_user_info_correct(user, result_map[user.id]) self.assertEqual( self._make_list_request(page=(num_pages + 1), page_size=page_size).status_code, 404)