def test_fetch_organization(self): """ Unit Test: test_fetch_organization""" organization1 = OrganizationFactory.create() organization2 = OrganizationFactory.create() with self.assertNumQueries(2): self.assertEqual(data.fetch_organization(organization1.id)['id'], organization1.id) self.assertEqual(data.fetch_organization(organization2.id)['id'], organization2.id)
def test_saml_provider_not_found(self): """ Test an exception is thrown if no SAML provider exists for this program's organization """ OrganizationFactory.create(short_name=self.organization_key) with pytest.raises(ProviderDoesNotExistException): get_user_by_program_id(self.external_user_id, self.program_uuid)
def test_saml_provider_not_found(self): """ Test that Prov exception is thrown if no SAML provider exists for this program's organization. """ OrganizationFactory.create(short_name=self.organization_key) with self.assertRaises(ProviderDoesNotExistException): get_users_by_external_keys(self.program_uuid, [])
def test_fetch_organization(self): """ Unit Test: test_fetch_organization""" organization1 = OrganizationFactory.create() organization2 = OrganizationFactory.create() with self.assertNumQueries(2): self.assertEqual( data.fetch_organization(organization1.id)['id'], organization1.id) self.assertEqual( data.fetch_organization(organization2.id)['id'], organization2.id)
def test_course_org_db_logo_association(self): """ Suppose we created a course and incorrectly called it `MITX/Demo/2017` while we want Hsoub logo on it? This ensures that we can override it using the `OrganizationCourse` model. """ org_id = 'PSUT' course_key = 'PSUT/Demo/Fall2018' with utils.OrganizationLogo(org_id, course_key) as prev_logo: self.assertEquals('psut.png', os.path.basename( prev_logo.name)) # Should use the PSUT legacy logo moe_logo = 'moe.png' with open(os.path.join(STATIC_DIR, moe_logo), 'rb') as updated_logo: wanted_org = OrganizationFactory.create( name='Ministry of Education', short_name='MoE', logo=SimpleUploadedFile(moe_logo, content=updated_logo.read()), ) # Associate the course with a different organization OrganizationCourse.objects.create(organization=wanted_org, course_id=course_key) with utils.OrganizationLogo(org_id, course_key) as overridden_logo: self.assertRegexpMatches( os.path.basename(overridden_logo.name), r'.*moe.*.\.png*') # Now it's an MoE course!
def test_user_has_document_permissions(): user = UserFactory.create() organization = OrganizationFactory.create() project = ProjectFactory.create(organization=organization) document = DocumentFactory.create(project=project) document.add_create(user) assert len(user.get_organizations()) == 1 assert len(user.get_projects()) == 1 assert len(user.get_documents()) == 1 assert organization.can_manage(user) == False assert organization.can_invite(user) == False assert organization.can_create(user) == False assert project.can_invite(user) == False assert project.can_create(user) == False assert project.can_manage(user) == False assert document.can_create(user) == True assert document.can_invite(user) == False assert document.can_manage(user) == False document.delete_create(user) document.add_invite(user) assert document.can_create(user) == True assert document.can_invite(user) == True assert document.can_manage(user) == False document.delete_invite(user) document.add_manage(user) assert document.can_create(user) == True assert document.can_invite(user) == True assert document.can_manage(user) == True
def test_course_org_db_logo_override_legacy(self): """ Want to re-upload an organization logo? You can do it! Just create an organization with and upload the logo. """ org_id = 'MITX' course_key = 'MITX/Demo/2017' with utils.OrganizationLogo(org_id, course_key) as prev_logo: # Should provide the legacy logo self.assertEquals('edx.png', os.path.basename(prev_logo.name)) hsoub_logo = 'hsoub.png' # Sorry we're using this all over the place! with open(os.path.join(STATIC_DIR, hsoub_logo), 'rb') as updated_logo: org = OrganizationFactory.create( short_name='MITX', logo=SimpleUploadedFile(hsoub_logo, content=updated_logo.read()), ) with utils.OrganizationLogo(org_id, course_key) as updated_logo: # Should use the database logo self.assertRegexpMatches(os.path.basename(updated_logo.name), r'.*hsoub.*\.png.*')
def test_multiple_saml_providers(self, second_config_enabled): """ If multiple samlprovider records exist with the same organization an exception is raised """ organization = OrganizationFactory.create( short_name=self.organization_key) provider = SAMLProviderConfigFactory.create(organization=organization) self.create_social_auth_entry(self.user, provider, self.external_user_id) # create a second active config for the same organization SAMLProviderConfigFactory.create(organization=organization, slug='foox', enabled=second_config_enabled) try: get_user_by_program_id(self.external_user_id, self.program_uuid) except ProviderConfigurationException: self.assertTrue(second_config_enabled, 'Unexpected error when second config is disabled') else: self.assertFalse( second_config_enabled, 'Expected error was not raised when second config is enabled')
def setup(): user = UserFactory.create() organization = OrganizationFactory.create() project = ProjectFactory.create(organization=organization) document = DocumentFactory.create(project=project) return user, organization, project, document, Client()
def test_same_user_key_in_multiple_organizations(self): uox_program_enrollment = self._create_waiting_program_enrollment() second_organization = OrganizationFactory.create() SAMLProviderConfigFactory.create(organization=second_organization, slug='aiu') catalog_org = CatalogOrganizationFactory.create(key=second_organization.short_name) program_uuid = self._create_catalog_program(catalog_org)['uuid'] # aiu enrollment with the same student key as our uox user aiu_program_enrollment = ProgramEnrollmentFactory.create( user=None, external_user_key=self.external_id, program_uuid=program_uuid ) UserSocialAuth.objects.create( user=UserFactory.create(), uid='{0}:{1}'.format('not_used', self.external_id), ) UserSocialAuth.objects.create( user=self.user, uid='{0}:{1}'.format(self.provider_slug, self.external_id), ) self._assert_program_enrollment_user(uox_program_enrollment, self.user) aiu_user = UserFactory.create() UserSocialAuth.objects.create( user=aiu_user, uid='{0}:{1}'.format('aiu', self.external_id), ) self._assert_program_enrollment_user(aiu_program_enrollment, aiu_user)
def test_same_user_key_in_multiple_organizations(self): uox_program_enrollment = self._create_waiting_program_enrollment() second_organization = OrganizationFactory.create() SAMLProviderConfigFactory.create(organization=second_organization, slug='aiu') catalog_org = CatalogOrganizationFactory.create( key=second_organization.short_name) program_uuid = self._create_catalog_program(catalog_org)['uuid'] # aiu enrollment with the same student key as our uox user aiu_program_enrollment = ProgramEnrollmentFactory.create( user=None, external_user_key=self.external_id, program_uuid=program_uuid) UserSocialAuth.objects.create( user=UserFactory.create(), uid='{0}:{1}'.format('not_used', self.external_id), ) UserSocialAuth.objects.create( user=self.user, uid='{0}:{1}'.format(self.provider_slug, self.external_id), ) self._assert_program_enrollment_user(uox_program_enrollment, self.user) aiu_user = UserFactory.create() UserSocialAuth.objects.create( user=aiu_user, uid='{0}:{1}'.format('aiu', self.external_id), ) self._assert_program_enrollment_user(aiu_program_enrollment, aiu_user)
def test_multiple_saml_providers(self): """ Test that get_users_by_external_keys returns the expected mapping of external keys to users when multiple saml providers are configured. """ organization = OrganizationFactory.create( short_name=self.organization_key) provider_1 = SAMLProviderConfigFactory.create( organization=organization) provider_2 = SAMLProviderConfigFactory.create( organization=organization, slug='test-shib-2', enabled=True) self.create_social_auth_entry(self.user_0, provider_1, 'ext-user-0') self.create_social_auth_entry(self.user_1, provider_1, 'ext-user-1') self.create_social_auth_entry(self.user_1, provider_2, 'ext-user-1') self.create_social_auth_entry(self.user_2, provider_2, 'ext-user-2') requested_keys = {'ext-user-1', 'ext-user-2', 'ext-user-3'} actual = get_users_by_external_keys(self.program_uuid, requested_keys) # ext-user-0 not requested, ext-user-3 doesn't exist, # ext-user-2 is authorized with secondary provider # ext-user-1 has an entry in both providers expected = { 'ext-user-1': self.user_1, 'ext-user-2': self.user_2, 'ext-user-3': None, } assert actual == expected
def setUp(self): super(TestOrganizationsView, self).setUp() self.user_password = '******' self.user = UserFactory(password=self.user_password) self.organization = OrganizationFactory.create() self.organization_list_url = reverse('v0:organization-list') self.client.login(username=self.user.username, password=self.user_password)
def setUp(self): super(TestOrganizationsView, self).setUp() self.user_password = '******' self.user = UserFactory(password=self.user_password, is_superuser=True) self.organization = OrganizationFactory.create() self.organization_list_url = reverse('v0:organization-list') self.client.login(username=self.user.username, password=self.user_password)
def test_empty_request(self): """ Test that requesting no external keys does not cause an exception. """ organization = OrganizationFactory.create(short_name=self.organization_key) SAMLProviderConfigFactory.create(organization=organization) actual = get_users_by_external_keys(self.program_uuid, set()) assert actual == {}
def test_no_double_organization_course(self): """ Apparently edX allows multiple organizations per course, but my limited imagination wouldn't grok it! """ org_id = 'PSUT' course_key = 'PSUT/Demo/Fall2018' wanted_org = OrganizationFactory.create(short_name='MITX') OrganizationCourse.objects.create(organization=wanted_org, course_id=course_key) unwanted_org = OrganizationFactory.create(short_name='AnythingX') OrganizationCourse.objects.create(organization=unwanted_org, course_id=course_key) with self.assertRaisesRegexp(Exception, '.*multiple organizations.*'): with utils.OrganizationLogo(org_id, course_key): self.fail('Should fail when having multiple organizations')
def test_lms_organization_not_found(self): """ Test an OrganizationDoesNotExistException is thrown if the LMS has no organization matching the catalog program's authoring_organization """ organization = OrganizationFactory.create(short_name='some_other_org') SAMLProviderConfigFactory.create(organization=organization) with self.assertRaises(OrganizationDoesNotExistException): get_users_by_external_keys(self.program_uuid, [])
def test_social_auth_user_not_created(self): """ None should be returned if no lms user exists for an external id """ organization = OrganizationFactory.create(short_name=self.organization_key) SAMLProviderConfigFactory.create(organization=organization) user = get_user_by_program_id(self.external_user_id, self.program_uuid) self.assertIsNone(user)
def test_get_user_success(self): """ Test lms user is successfully found """ organization = OrganizationFactory.create(short_name=self.organization_key) provider = SAMLProviderConfigFactory.create(organization=organization) self.create_social_auth_entry(self.user, provider, self.external_user_id) user = get_user_by_program_id(self.external_user_id, self.program_uuid) self.assertEquals(user, self.user)
def create_course(self): # Make the `may_certify()` true! past_week = datetime.now(pytz.UTC) - timedelta(days=7) course = CourseFactory.create( start=past_week, end=past_week, ) from edraak_certificates.utils import STATIC_DIR from django.core.files.uploadedfile import SimpleUploadedFile # Enforce a DB logo logo_file_name = 'moe.png' with open(os.path.join(STATIC_DIR, logo_file_name), 'rb') as logo: OrganizationFactory.create( short_name=course.org, logo=SimpleUploadedFile(logo_file_name, content=logo.read()), ) return course
def test_lms_organization_not_found(self): """ Test an OrganizationDoesNotExistException is thrown if the LMS has no organization matching the catalog program's authoring_organization """ organization = OrganizationFactory.create(short_name='some_other_org') provider = SAMLProviderConfigFactory.create(organization=organization) self.create_social_auth_entry(self.user, provider, self.external_user_id) with pytest.raises(OrganizationDoesNotExistException): get_user_by_program_id(self.external_user_id, self.program_uuid)
def test_user_has_superuser(): user = UserFactory.create(is_superuser=True) organization = OrganizationFactory.create() project = ProjectFactory.create(organization=organization) DocumentFactory.create(project=project) UserFactory.create() assert len(user.get_organizations()) == 1 assert len(user.get_projects()) == 1 assert len(user.get_documents()) == 1 assert len(user.get_users()) == 2
def test_extra_saml_provider_disabled(self): """ If multiple samlprovider records exist with the same organization, but the extra record is disabled, no exception is raised. """ organization = OrganizationFactory.create(short_name=self.organization_key) SAMLProviderConfigFactory.create(organization=organization) # create a second active config for the same organization, NOT enabled SAMLProviderConfigFactory.create( organization=organization, slug='foox', enabled=False ) get_users_by_external_keys(self.program_uuid, [])
def test_user_has_no_permissions(): user = UserFactory.create() # Create some objects that the user should not be able to see organization = OrganizationFactory.create() project = ProjectFactory.create(organization=organization) DocumentFactory.create(project=project) UserFactory.create() assert len(user.get_organizations()) == 0 assert len(user.get_projects()) == 0 assert len(user.get_documents()) == 0 assert len(user.get_users()) == 0
def test_extra_saml_provider_enabled(self): """ If multiple enabled samlprovider records exist with the same organization an exception is raised. """ organization = OrganizationFactory.create(short_name=self.organization_key) SAMLProviderConfigFactory.create(organization=organization) # create a second active config for the same organizationm, IS enabled SAMLProviderConfigFactory.create( organization=organization, slug='foox', enabled=True ) with self.assertRaises(ProviderConfigurationException): get_users_by_external_keys(self.program_uuid, [])
def setUp(self): """ Set up test data """ super(WritingProgramEnrollmentTest, self).setUp() catalog_org = CatalogOrganizationFactory.create( key=self.organization_key) program = ProgramFactory.create(uuid=self.program_uuid_x, authoring_organizations=[catalog_org]) organization = OrganizationFactory.create( short_name=self.organization_key) SAMLProviderConfigFactory.create(organization=organization) cache.set(PROGRAM_CACHE_KEY_TPL.format(uuid=self.program_uuid_x), program, None)
def test_course_org_db_logo(self): """ You know what? We can add logos to HogwartsX from the DB from now on! Ref: https://en.wikipedia.org/wiki/Hogwarts """ org_id = 'HogwartsX' course_key = 'HogwartsX/Wizard_101/1814' with utils.OrganizationLogo(org_id, course_key) as logo: self.assertIsNone(logo) logo_file_name = 'hsoub.png' with open(os.path.join(STATIC_DIR, logo_file_name), 'rb') as logo: OrganizationFactory.create( short_name='HogwartsX', logo=SimpleUploadedFile(logo_file_name, content=logo.read()), ) with utils.OrganizationLogo(org_id, course_key) as logo: self.assertRegexpMatches(os.path.basename(logo.name), r'.*hsoub.*\.png.*')
def test_multiple_active_saml_providers(self): """ If multiple samlprovider records exist with the same organization an exception is raised """ organization = OrganizationFactory.create(short_name=self.organization_key) provider = SAMLProviderConfigFactory.create(organization=organization) self.create_social_auth_entry(self.user, provider, self.external_user_id) # create a second active config for the same organization SAMLProviderConfigFactory.create(organization=organization, slug='foox') with pytest.raises(UserLookupException): get_user_by_program_id(self.external_user_id, self.program_uuid)
def setUpClass(cls): super(SocialAuthEnrollmentCompletionSignalTest, cls).setUpClass() cls.external_id = '0000' cls.provider_slug = 'uox' cls.course_keys = [ CourseKey.from_string('course-v1:edX+DemoX+Test_Course'), CourseKey.from_string('course-v1:edX+DemoX+Another_Test_Course'), ] cls.organization = OrganizationFactory.create(short_name='UoX') cls.user = UserFactory.create() for course_key in cls.course_keys: CourseOverviewFactory(id=course_key) cls.provider_config = SAMLProviderConfigFactory.create( organization=cls.organization, slug=cls.provider_slug)
def test_catalog_program_missing_org(self): """ Test OrganizationDoesNotExistException is thrown if the cached program does not have an authoring organization. """ program = ProgramFactory.create( uuid=self.program_uuid, authoring_organizations=[] ) cache.set(PROGRAM_CACHE_KEY_TPL.format(uuid=self.program_uuid), program, None) organization = OrganizationFactory.create(short_name=self.organization_key) provider = SAMLProviderConfigFactory.create(organization=organization) self.create_social_auth_entry(self.user, provider, self.external_user_id) with pytest.raises(OrganizationDoesNotExistException): get_user_by_program_id(self.external_user_id, self.program_uuid)
def setUpClass(cls): super(SocialAuthEnrollmentCompletionSignalTest, cls).setUpClass() cls.external_id = '0000' cls.provider_slug = 'uox' cls.course_keys = [ CourseKey.from_string('course-v1:edX+DemoX+Test_Course'), CourseKey.from_string('course-v1:edX+DemoX+Another_Test_Course'), ] cls.organization = OrganizationFactory.create( short_name='UoX' ) cls.user = UserFactory.create() for course_key in cls.course_keys: CourseOverviewFactory(id=course_key) cls.provider_config = SAMLProviderConfigFactory.create(organization=cls.organization, slug=cls.provider_slug)
def test_multiple_active_saml_providers(self): """ If multiple samlprovider records exist with the same organization an exception is raised """ organization = OrganizationFactory.create( short_name=self.organization_key) provider = SAMLProviderConfigFactory.create(organization=organization) self.create_social_auth_entry(self.user, provider, self.external_user_id) # create a second active config for the same organization SAMLProviderConfigFactory.create(organization=organization, slug='foox') with pytest.raises(UserLookupException): get_user_by_program_id(self.external_user_id, self.program_uuid)
def test_user_has_organization_permissions(): user = UserFactory.create() organization = OrganizationFactory.create() organization.add_change(user) project = ProjectFactory.create(organization=organization) document = DocumentFactory.create(project=project) assert len(user.get_organizations()) == 1 assert len(user.get_projects()) == 1 assert len(user.get_documents()) == 1 assert organization.has_change(user) == True assert organization.has_delete(user) == False assert organization.has_create(user) == False assert project.has_delete(user) == False assert project.has_create(user) == False assert project.has_change(user) == True assert document.has_delete(user) == False assert document.has_create(user) == False assert document.has_change(user) == True organization.add_delete(user) assert project.has_delete(user) == True assert project.has_create(user) == False assert project.has_change(user) == True assert document.has_delete(user) == True assert document.has_create(user) == False assert document.has_change(user) == True organization.add_create(user) assert project.has_delete(user) == True assert project.has_create(user) == True assert project.has_change(user) == True assert document.has_delete(user) == True assert document.has_create(user) == True assert document.has_change(user) == True
def test_happy_path(self): """ Test that get_users_by_external_keys returns the expected mapping of external keys to users. """ organization = OrganizationFactory.create(short_name=self.organization_key) provider = SAMLProviderConfigFactory.create(organization=organization) self.create_social_auth_entry(self.user_0, provider, 'ext-user-0') self.create_social_auth_entry(self.user_1, provider, 'ext-user-1') self.create_social_auth_entry(self.user_2, provider, 'ext-user-2') requested_keys = {'ext-user-1', 'ext-user-2', 'ext-user-3'} actual = get_users_by_external_keys(self.program_uuid, requested_keys) # ext-user-0 not requested, ext-user-3 doesn't exist expected = { 'ext-user-1': self.user_1, 'ext-user-2': self.user_2, 'ext-user-3': None, } assert actual == expected
def setUpClass(cls): """ Set up test data """ super().setUpClass() catalog_org = CatalogOrganizationFactory.create( key=cls.organization_key) cls.program = ProgramFactory.create( uuid=cls.program_uuid, authoring_organizations=[catalog_org]) organization = OrganizationFactory.create( short_name=cls.organization_key) SAMLProviderConfigFactory.create(organization=organization) catalog_course_id_str = 'course-v1:edX+ToyX' course_run_id_str = f'{catalog_course_id_str}+Toy_Course' cls.course_id = CourseKey.from_string(course_run_id_str) CourseOverviewFactory(id=cls.course_id) course_run = CourseRunFactory(key=course_run_id_str) cls.course = CourseFactory(key=catalog_course_id_str, course_runs=[course_run]) cls.student_1 = UserFactory(username='******') cls.student_2 = UserFactory(username='******')
def setup(): user = UserFactory.create() organization = OrganizationFactory.create() return user, organization, Client()
def setUp(self): super(TestOrganizationSerializer, self).setUp() self.organization = OrganizationFactory.create()
def test_authenticated_user(self): """ Verify that the authenticated user gets data.""" OrganizationFactory.create() response = self.client.get(self.organization_list_url) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 2)
def setUp(self): super(TestOrganizationModel, self).setUp() self.organization = OrganizationFactory.create()