class TestNoInstitutionAddonAllowView(AdminTestCase): def setUp(self): super(TestNoInstitutionAddonAllowView, self).setUp() self.user = AuthUserFactory() self.external_account = ExternalAccountFactory() self.rdm_addon_option = rdm_addon_factories.RdmAddonNoInstitutionFactoryOption( ) self.rdm_addon_option.external_accounts.add(self.external_account) self.rdm_addon_option.save() self.user.external_accounts.add(self.external_account) self.user.save() self.request = RequestFactory().get('/fake_path') self.view = views.AddonAllowView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = { 'addon_name': self.rdm_addon_option.provider, 'institution_id': MAGIC_INSTITUTION_ID, 'allowed': '1', } def tearDown(self): super(TestNoInstitutionAddonAllowView, self).tearDown() if self.user.external_accounts.filter( pk=self.external_account.id).exists(): self.user.external_accounts.remove(self.external_account) self.user.delete() self.rdm_addon_option.external_accounts.remove(self.external_account) self.rdm_addon_option.delete() self.external_account.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_get(self, *args, **kwargs): rdm_addon_option = utils.get_rdm_addon_option( MAGIC_INSTITUTION_ID, self.view.kwargs['addon_name']) nt.assert_true(rdm_addon_option.is_allowed) nt.assert_equal(rdm_addon_option.provider, self.view.kwargs['addon_name']) def test_get_disallowed(self, *args, **kwargs): self.view.kwargs['allowed'] = False self.view.get(self.request, *args, **self.view.kwargs) rdm_addon_option = utils.get_rdm_addon_option( MAGIC_INSTITUTION_ID, self.view.kwargs['addon_name']) nt.assert_equal(rdm_addon_option.is_allowed, False) nt.assert_equal(rdm_addon_option.provider, self.view.kwargs['addon_name']) nt.assert_equal( self.user.external_accounts.filter( pk=self.external_account.id).exists(), False)
class TestInstitutionListView(AdminTestCase): def setUp(self): super(TestInstitutionListView, self).setUp() self.user = AuthUserFactory() self.institutions = [InstitutionFactory(), InstitutionFactory()] self.request = RequestFactory().get('/fake_path') self.view = views.InstitutionListView() self.view = setup_user_view(self.view, self.request, user=self.user) def tearDown(self): super(TestInstitutionListView, self).tearDown() self.user.delete() for institution in self.institutions: institution.delete() def test_super_admin_login(self, *args, **kwargs): """test superuser login""" self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_registered = False nt.assert_equal(self.view.test_func(), False) def test_super_admin_get(self, *args, **kwargs): """test superuser GET method""" self.request.user.is_superuser = True self.request.user.is_staff = True res = self.view.get(self.request, *args, **kwargs) nt.assert_equal(res.status_code, 200) def test_admin_get(self, *args, **kwargs): """test institution administrator GET method""" self.request.user.is_superuser = False self.request.user.is_staff = True res = self.view.get(self.request, *args, **kwargs) nt.assert_equal(res.status_code, 302)
class TestAddonListView(AdminTestCase): def setUp(self): super(TestAddonListView, self).setUp() self.user = AuthUserFactory() self.institution1 = InstitutionFactory() self.institution2 = InstitutionFactory() self.user.affiliated_institutions.add(self.institution1) self.request = RequestFactory().get('/fake_path') self.view = views.AddonListView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = {'institution_id': self.institution1.id} def tearDown(self): super(TestAddonListView, self).tearDown() self.user.affiliated_institutions.remove(self.institution1) self.user.delete() self.institution1.delete() self.institution2.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_registered = False nt.assert_equal(self.view.test_func(), False) def test_non_affiliated_institution_user_login(self): """test user unaffiliated institution login""" self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs = {'institution_id': self.institution1.id + 1} nt.assert_equal(self.view.test_func(), False)
class TestSendView(AdminTestCase): def setUp(self): super(TestSendView, self).setUp() self.user = AuthUserFactory() self.request = RequestFactory().get('/fake_path') self.view = views.SendView() self.view = setup_user_view(self.view, self.request, user=self.user) def tearDown(self): super(TestSendView, self).tearDown() self.user.delete() def test_super_admin_login(self, *args, **kwargs): """test superuser login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False self.request.user.is_registered = True self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_active = True self.request.user.is_registered = False self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False)
class TestCreateCSV(AdminTestCase): """test ImageView""" def setUp(self): super(TestCreateCSV, self).setUp() self.user = AuthUserFactory() self.request = RequestFactory().get('/fake_path') self.institution1 = InstitutionFactory() self.user.affiliated_institutions.add(self.institution1) self.kwargs = {'institution_id': self.institution1.id} self.rdm_statistics = rdm_statistics_factories.RdmStatisticsFactory.create( institution=self.institution1, provider='s3', owaner=self.user) self.rdm_statistics.save() def tearDown(self): super(TestCreateCSV, self).tearDown() self.user.affiliated_institutions.remove(self.institution1) self.user.delete() self.institution1.delete()
class TestRdmUserKey(OsfTestCase): def setUp(self): super(TestRdmUserKey, self).setUp() self.user = AuthUserFactory() def tearDown(self): super(TestRdmUserKey, self).tearDown() osfuser_id = Guid.objects.get(_id=self.user._id).object_id key_exists_check = userkey_generation_check(self.user._id) if key_exists_check: rdmuserkey_pvt_key = RdmUserKey.objects.get( guid=osfuser_id, key_kind=api_settings.PRIVATE_KEY_VALUE) pvt_key_path = os.path.join(api_settings.KEY_SAVE_PATH, rdmuserkey_pvt_key.key_name) os.remove(pvt_key_path) rdmuserkey_pvt_key.delete() rdmuserkey_pub_key = RdmUserKey.objects.get( guid=osfuser_id, key_kind=api_settings.PUBLIC_KEY_VALUE) pub_key_path = os.path.join(api_settings.KEY_SAVE_PATH, rdmuserkey_pub_key.key_name) os.remove(pub_key_path) rdmuserkey_pub_key.delete() self.user.delete() def test_userkey_generation_check_return_true(self): userkey_generation(self.user._id) nt.assert_true(userkey_generation_check(self.user._id)) def test_userkey_generation_check_return_false(self): nt.assert_false(userkey_generation_check(self.user._id)) def test_userkey_generation(self): osfuser_id = Guid.objects.get(_id=self.user._id).object_id userkey_generation(self.user._id) rdmuserkey_pvt_key = RdmUserKey.objects.filter( guid=osfuser_id, key_kind=api_settings.PRIVATE_KEY_VALUE) nt.assert_equal(rdmuserkey_pvt_key.count(), 1) rdmuserkey_pub_key = RdmUserKey.objects.filter( guid=osfuser_id, key_kind=api_settings.PUBLIC_KEY_VALUE) nt.assert_equal(rdmuserkey_pub_key.count(), 1)
class TestCompleteView(AdminTestCase): def setUp(self): super(TestCompleteView, self).setUp() self.user = AuthUserFactory() self.request = RequestFactory().get('/fake_path') self.view = views.CompleteView() self.view = setup_user_view(self.view, self.request, user=self.user) def tearDown(self): super(TestCompleteView, self).tearDown() self.user.delete() def test_login(self): """login test""" nt.assert_true(self.view.test_func()) def test_get_context_data(self, *args, **kwargs): res = views.CompleteView.as_view()(self.request) nt.assert_equal(res.status_code, 200)
class TestIconView(AdminTestCase): def setUp(self): super(TestIconView, self).setUp() self.user = AuthUserFactory() self.request = RequestFactory().get('/fake_path') self.view = views.IconView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = {'addon_name': 's3'} def tearDown(self): super(TestIconView, self).tearDown() self.user.delete() def test_login_user(self): nt.assert_true(self.view.test_func()) def test_valid_get(self, *args, **kwargs): res = self.view.get(self.request, *args, **self.view.kwargs) nt.assert_equal(res.status_code, 200)
class ApiAddonTestCase(ApiTestCase): """Base `TestCase` for tests that require interaction with addons. """ DISABLE_OUTGOING_CONNECTIONS = True @abc.abstractproperty def short_name(self): pass @abc.abstractproperty def addon_type(self): pass @abc.abstractmethod def _apply_auth_configuration(self): pass @abc.abstractmethod def _set_urls(self): pass def _settings_kwargs(self, node, user_settings): return { 'user_settings': self.user_settings, 'folder_id': '1234567890', 'owner': self.node } def setUp(self): super(ApiAddonTestCase, self).setUp() from osf_tests.factories import ( ProjectFactory, AuthUserFactory, ) from addons.base.models import ( BaseOAuthNodeSettings, BaseOAuthUserSettings ) assert self.addon_type in ('CONFIGURABLE', 'OAUTH', 'UNMANAGEABLE', 'INVALID') self.account = None self.node_settings = None self.user_settings = None self.user = AuthUserFactory() self.auth = Auth(self.user) self.node = ProjectFactory(creator=self.user) if self.addon_type not in ('UNMANAGEABLE', 'INVALID'): if self.addon_type in ('OAUTH', 'CONFIGURABLE'): self.account = self.AccountFactory() self.user.external_accounts.add(self.account) self.user.save() self.user_settings = self.user.get_or_add_addon(self.short_name) self.node_settings = self.node.get_or_add_addon(self.short_name, auth=self.auth) if self.addon_type in ('OAUTH', 'CONFIGURABLE'): self.node_settings.set_auth(self.account, self.user) self._apply_auth_configuration() if self.addon_type in ('OAUTH', 'CONFIGURABLE'): assert isinstance(self.node_settings, BaseOAuthNodeSettings) assert isinstance(self.user_settings, BaseOAuthUserSettings) self.node_settings.reload() self.user_settings.reload() self.account_id = self.account._id if self.account else None self.set_urls() def tearDown(self): super(ApiAddonTestCase, self).tearDown() self.user.delete() self.node.delete() if self.account: self.account.delete()
class ApiAddonTestCase(ApiTestCase): """Base `TestCase` for tests that require interaction with addons. """ DISABLE_OUTGOING_CONNECTIONS = True @abc.abstractproperty def short_name(self): pass @abc.abstractproperty def addon_type(self): pass @abc.abstractmethod def _apply_auth_configuration(self): pass @abc.abstractmethod def _set_urls(self): pass def _settings_kwargs(self, node, user_settings): return { 'user_settings': self.user_settings, 'folder_id': '1234567890', 'owner': self.node } def setUp(self): super(ApiAddonTestCase, self).setUp() from osf_tests.factories import ( ProjectFactory, AuthUserFactory, ) from addons.base.models import (BaseOAuthNodeSettings, BaseOAuthUserSettings) assert self.addon_type in ('CONFIGURABLE', 'OAUTH', 'UNMANAGEABLE', 'INVALID') self.account = None self.node_settings = None self.user_settings = None self.user = AuthUserFactory() self.auth = Auth(self.user) self.node = ProjectFactory(creator=self.user) if self.addon_type not in ('UNMANAGEABLE', 'INVALID'): if self.addon_type in ('OAUTH', 'CONFIGURABLE'): self.account = self.AccountFactory() self.user.external_accounts.add(self.account) self.user.save() self.user_settings = self.user.get_or_add_addon(self.short_name) self.node_settings = self.node.get_or_add_addon(self.short_name, auth=self.auth) if self.addon_type in ('OAUTH', 'CONFIGURABLE'): self.node_settings.set_auth(self.account, self.user) self._apply_auth_configuration() if self.addon_type in ('OAUTH', 'CONFIGURABLE'): assert isinstance(self.node_settings, BaseOAuthNodeSettings) assert isinstance(self.user_settings, BaseOAuthUserSettings) self.node_settings.reload() self.user_settings.reload() self.account_id = self.account._id if self.account else None self.set_urls() def tearDown(self): super(ApiAddonTestCase, self).tearDown() self.user.delete() self.node.delete() if self.account: self.account.delete()
class TestConnectView(AdminTestCase): def setUp(self): super(TestConnectView, self).setUp() self.user = AuthUserFactory() self.external_account = ExternalAccountFactory() self.institution = InstitutionFactory() self.user.affiliated_institutions.add(self.institution) self.provider = MockOAuth2Provider(self.external_account) self.request = RequestFactory().get('/fake_path') add_session_to_request(self.request) self.view = views.ConnectView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = { 'addon_name': self.external_account.provider, 'institution_id': self.institution.id, } def tearDown(self): super(TestConnectView, self).tearDown() self.user.affiliated_institutions.remove(self.institution) self.user.delete() self.institution.delete() self.external_account.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_registered = False nt.assert_equal(self.view.test_func(), False) def test_non_affiliated_institution_user_login(self): """test user unaffiliated institution login""" self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs = {'institution_id': self.institution.id + 1} nt.assert_equal(self.view.test_func(), False) def test_get(self, *args, **kwargs): self.request.user.is_superuser = False self.request.user.is_staff = True res = self.view.get(self.request, *args, **self.view.kwargs) nt.assert_equal(res.status_code, 302)
class TestIndexView(AdminTestCase): def setUp(self): super(TestIndexView, self).setUp() self.user = AuthUserFactory() self.request = RequestFactory().get('/fake_path') self.view = views.IndexView() self.view = setup_user_view(self.view, self.request, user=self.user) def tearDown(self): super(TestIndexView, self).tearDown() self.user.delete() def test_super_admin_login(self, *args, **kwargs): """test superuser login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False self.request.user.is_registered = True self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_active = True self.request.user.is_registered = False self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_super_admin_get(self, *args, **kwargs): """test superuser GET method""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True self.request.user.is_staff = True res = self.view.get(self.request, *args, **kwargs) nt.assert_equal(res.status_code, 200) def test_admin_get(self, *args, **kwargs): """test institution administrator GET method""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True res = self.view.get(self.request, *args, **kwargs) nt.assert_equal(res.status_code, 200) def test_get_context_data(self): res = self.view.get_context_data() nt.assert_is_instance(res, dict) nt.assert_equal(res['form'], PreviewForm) def test_post_option_check_okay(self): mod_data = dict(option_data) mod_data.update({'user_id': self.user.id}) test_option = RdmAnnouncementOption.objects.create(**mod_data) test_option.save() self.form = PreviewForm(data) ret = self.view.option_check(data) nt.assert_is_instance(test_option, RdmAnnouncementOption) nt.assert_true(self.form.is_valid()) nt.assert_true(ret) def test_post_option_check_raise(self): mod_data = dict(option_data) mod_data.update({'user_id': self.user.id, 'twitter_api_key': None}) test_option = RdmAnnouncementOption.objects.create(**mod_data) test_option.save() mod_data2 = dict(data) mod_data2.update({'announcement_type': 'SNS (Twitter)'}) self.form = PreviewForm(mod_data2) ret = self.view.option_check(mod_data2) nt.assert_is_instance(test_option, RdmAnnouncementOption) nt.assert_true(self.form.is_valid()) nt.assert_false(ret) ''' disable #GRDM-6902
class TestSettingsView(AdminTestCase): def setUp(self): super(TestSettingsView, self).setUp() self.user = AuthUserFactory() self.request = RequestFactory().get('/fake_path') self.view = views.SettingsView() self.view = setup_user_view(self.view, self.request, user=self.user) def tearDown(self): super(TestSettingsView, self).tearDown() self.user.delete() def test_super_admin_login(self, *args, **kwargs): """test superuser login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administraror login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False self.request.user.is_registered = True self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """login at unregisterd user""" self.request.user.is_active = True self.request.user.is_registered = False self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_super_admin_get(self, *args, **kwargs): """test superuser GET method""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True self.request.user.is_staff = True res = self.view.get(self.request, *args, **kwargs) nt.assert_equal(res.status_code, 200) def test_admin_get(self, *args, **kwargs): """test institution administrator GET method""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True res = self.view.get(self.request, *args, **kwargs) nt.assert_equal(res.status_code, 200) def test_get_context_data(self): res = self.view.get_context_data() nt.assert_is_instance(res, dict) nt.assert_equal(res['form'], SettingsForm) def test_get_exist_option_set(self): """test superuser""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True self.request.user.is_staff = True new_user = AuthUserFactory() new_user.is_active = True new_user.is_registered = True new_user.is_superuser = True new_user.is_staff = True mod_data = dict(option_data) mod_data.update({'user_id': new_user.id}) new_user_option = RdmAnnouncementOption.objects.create(**mod_data) new_user_option.save() ret = self.view.get_exist_option_set() nt.assert_is_instance(new_user_option, RdmAnnouncementOption) nt.assert_true(ret) def test_get_exist_option_set2(self): """test institution administrator""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True new_user = AuthUserFactory() new_user.is_active = True new_user.is_registered = True new_user.is_superuser = False new_user.is_staff = True mod_data = dict(option_data) mod_data.update({'user_id': new_user.id}) new_user_option = RdmAnnouncementOption.objects.create(**mod_data) new_user_option.save() ret = self.view.get_exist_option_set() nt.assert_is_instance(new_user_option, RdmAnnouncementOption) nt.assert_true(ret) def test_get_exist_option_set3(self): """test user not superuser or institution administrator""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = False new_user = AuthUserFactory() new_user.is_active = True new_user.is_registered = True new_user.is_superuser = False new_user.is_staff = False mod_data = dict(option_data) mod_data.update({'user_id': new_user.id}) new_user_option = RdmAnnouncementOption.objects.create(**mod_data) new_user_option.save() ret = self.view.get_exist_option_set() nt.assert_is_instance(new_user_option, RdmAnnouncementOption) nt.assert_equal(ret, 'False')
class TestOAuthView(AdminTestCase): def setUp(self): super(TestOAuthView, self).setUp() self.user = AuthUserFactory() self.external_account = ExternalAccountFactory() self.rdm_addon_option = rdm_addon_factories.RdmAddonOptionFactory() self.rdm_addon_option.provider = self.external_account.provider self.rdm_addon_option.external_accounts.add(self.external_account) self.rdm_addon_option.save() self.user.affiliated_institutions.add(self.rdm_addon_option.institution) self.user.external_accounts.add(self.external_account) self.user.save() self.request = RequestFactory().get('/fake_path') self.view = views.OAuthView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = { 'external_account_id': self.external_account._id, 'institution_id': self.rdm_addon_option.institution.id, } def tearDown(self): super(TestOAuthView, self).tearDown() institution = self.rdm_addon_option.institution self.user.affiliated_institutions.remove(institution) if self.user.external_accounts.filter(pk=self.external_account.id).exists(): self.user.external_accounts.remove(self.external_account) self.user.delete() if self.rdm_addon_option.external_accounts.filter(pk=self.external_account.id).exists(): self.rdm_addon_option.external_accounts.remove(self.external_account) self.rdm_addon_option.delete() institution.delete() self.external_account.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login """ self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_registered = False nt.assert_equal(self.view.test_func(), False) def test_non_affiliated_institution_user_login(self): """test unaffiliated institution user login""" self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs['institution_id'] = self.rdm_addon_option.institution.id + 1 nt.assert_equal(self.view.test_func(), False) self.view.kwargs['institution_id'] = self.rdm_addon_option.institution.id def test_delete(self, *args, **kwargs): self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_equal(self.user.external_accounts.count(), 1) nt.assert_equal(self.rdm_addon_option.external_accounts.count(), 1) self.view.delete(self.request, *args, **self.view.kwargs) nt.assert_equal(self.user.external_accounts.count(), 0) nt.assert_equal(self.rdm_addon_option.external_accounts.count(), 0) def test_delete_dummy(self, *args, **kwargs): self.view.kwargs['external_account_id'] = self.external_account._id + 'dummy' with self.assertRaises(Http404): self.view.delete(self.request, *args, **self.view.kwargs) self.view.kwargs['external_account_id'] = self.external_account._id def test_delete_empty(self, *args, **kwargs): self.rdm_addon_option.external_accounts.remove(self.external_account) with self.assertRaises(Http404): self.view.delete(self.request, *args, **self.view.kwargs)
class TestAccountsView(AdminTestCase): def setUp(self): super(TestAccountsView, self).setUp() self.user = AuthUserFactory() self.external_account = ExternalAccountFactory() self.rdm_addon_option = rdm_addon_factories.RdmAddonOptionFactory() self.rdm_addon_option.provider = self.external_account.provider self.rdm_addon_option.external_accounts.add(self.external_account) self.rdm_addon_option.save() self.user.affiliated_institutions.add(self.rdm_addon_option.institution) self.user.external_accounts.add(self.external_account) self.user.save() self.request = RequestFactory().get('/fake_path') self.view = views.AccountsView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = { 'addon_name': self.external_account.provider, 'institution_id': self.rdm_addon_option.institution.id, } def tearDown(self): super(TestAccountsView, self).tearDown() institution = self.rdm_addon_option.institution self.user.affiliated_institutions.remove(institution) if self.user.external_accounts.filter(pk=self.external_account.id).exists(): self.user.external_accounts.remove(self.external_account) self.user.delete() if self.rdm_addon_option.external_accounts.filter(pk=self.external_account.id).exists(): self.rdm_addon_option.external_accounts.remove(self.external_account) self.rdm_addon_option.delete() institution.delete() self.external_account.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_registered = False nt.assert_equal(self.view.test_func(), False) def test_non_affiliated_institution_user_login(self): """test user unaffiliated institution login""" self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs = {'institution_id': self.rdm_addon_option.institution.id + 1} nt.assert_equal(self.view.test_func(), False) def test_get(self, *args, **kwargs): res = self.view.get(self.request, *args, **self.view.kwargs) nt.assert_equal(res.status_code, 200) content = json.loads(res.content) nt.assert_equal(len(content['accounts']), 1) def test_post_empty(self, *args, **kwargs): self.request = RequestFactory().post( '/fake', data=json.dumps({}), content_type='application/json' ) self.view.kwargs['addon_name'] = 'dummy' res = self.view.post(self.request, *args, **self.view.kwargs) nt.assert_equal(res.status_code, 400) def test_post_fake_s3_account(self, *args, **kwargs): self.request = RequestFactory().post( '/fake', data=json.dumps({'access_key': 'aaa', 'secret_key': 'bbb'}), content_type='application/json' ) self.view.kwargs['addon_name'] = 's3' res = self.view.post(self.request, *args, **self.view.kwargs) nt.assert_equal(res.status_code, 400)
class TestSettingsView(AdminTestCase): def setUp(self): super(TestSettingsView, self).setUp() self.user = AuthUserFactory() self.institution = InstitutionFactory() self.user.affiliated_institutions.add(self.institution) self.request = RequestFactory().get('/fake_path') self.view = views.SettingsView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = { 'addon_name': 'dataverse', 'institution_id': self.institution.id, } def tearDown(self): super(TestSettingsView, self).tearDown() self.user.affiliated_institutions.remove() self.user.delete() self.institution.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_registered = False nt.assert_equal(self.view.test_func(), False) def test_non_affiliated_institution_user_login(self): """test user unaffiliated institution login""" self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs = {'institution_id': self.institution.id + 1} nt.assert_equal(self.view.test_func(), False) def test_get_dataverse(self, *args, **kwargs): self.request.user.is_superuser = False self.request.user.is_staff = True res = self.view.get(self.request, *args, **self.view.kwargs) nt.assert_equal(res.status_code, 200) nt.assert_true('result' in res.content) def test_get_dummy_addon(self, *args, **kwargs): self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs['addon_name'] = 'dummy' res = self.view.get(self.request, *args, **self.view.kwargs) nt.assert_equal(res.status_code, 200) self.assertJSONEqual(res.content, {})
class TestCallbackView(AdminTestCase): def setUp(self): super(TestCallbackView, self).setUp() self.user = AuthUserFactory() self.external_account = ExternalAccountFactory() self.institution = InstitutionFactory() self.provider = MockOAuth2Provider(self.external_account) self.user.affiliated_institutions.add(self.institution) app = flask.Flask(__name__) make_url_map(app) app.config['SECRET_KEY'] = 'aaaaa' self.ctx = app.test_request_context() self.ctx.push() self.request = RequestFactory().get('/fake_path') add_session_to_request(self.request) self.view0 = views.ConnectView() self.view0 = setup_user_view(self.view0, self.request, user=self.user) self.view0.kwargs = { 'addon_name': self.external_account.provider, 'institution_id': self.institution.id, } self.view = views.CallbackView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = { 'addon_name': self.external_account.provider, 'institution_id': self.institution.id, } def tearDown(self): super(TestCallbackView, self).tearDown() self.user.affiliated_institutions.remove(self.institution) self.user.delete() self.institution.delete() self.external_account.delete() try: self.ctx.pop() except AssertionError: pass def test_super_admin_login(self): """test superuser login""" self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_registered = False nt.assert_equal(self.view.test_func(), False)
class TestAddonForceView(AdminTestCase): def setUp(self): super(TestAddonForceView, self).setUp() self.user = AuthUserFactory() self.external_account = ExternalAccountFactory() self.rdm_addon_option = rdm_addon_factories.RdmAddonOptionFactory() self.rdm_addon_option.external_accounts.add(self.external_account) self.rdm_addon_option.save() self.user.affiliated_institutions.add( self.rdm_addon_option.institution) self.user.external_accounts.add(self.external_account) self.user.save() self.request = RequestFactory().get('/fake_path') self.view = views.AddonForceView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = { 'addon_name': self.rdm_addon_option.provider, 'institution_id': self.rdm_addon_option.institution.id, 'forced': '1', } def tearDown(self): super(TestAddonForceView, self).tearDown() institution = self.rdm_addon_option.institution self.user.affiliated_institutions.remove(institution) if self.user.external_accounts.filter( pk=self.external_account.id).exists(): self.user.external_accounts.remove(self.external_account) self.user.delete() self.rdm_addon_option.external_accounts.remove(self.external_account) self.rdm_addon_option.delete() institution.delete() self.external_account.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_registered = False nt.assert_equal(self.view.test_func(), False) def test_non_affiliated_institution_user_login(self): """test user unaffiliated institution login""" self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs = { 'institution_id': self.rdm_addon_option.institution.id + 1 } nt.assert_equal(self.view.test_func(), False) def test_get(self, *args, **kwargs): self.view.get(self.request, *args, **self.view.kwargs) rdm_addon_option = utils.get_rdm_addon_option( self.rdm_addon_option.institution.id, self.view.kwargs['addon_name']) nt.assert_true(rdm_addon_option.is_forced) nt.assert_equal(rdm_addon_option.provider, self.view.kwargs['addon_name']) def test_get_not_forced(self, *args, **kwargs): self.view.kwargs['forced'] = False self.view.get(self.request, *args, **self.view.kwargs) rdm_addon_option = utils.get_rdm_addon_option( self.rdm_addon_option.institution.id, self.view.kwargs['addon_name']) nt.assert_equal(rdm_addon_option.is_forced, False) nt.assert_equal(rdm_addon_option.provider, self.view.kwargs['addon_name']) nt.assert_true( self.user.external_accounts.filter( pk=self.external_account.id).exists())
class TestImageView(AdminTestCase): """test ImageView""" def setUp(self): super(TestImageView, self).setUp() self.user = AuthUserFactory() self.request = RequestFactory().get('/fake_path') self.institution1 = InstitutionFactory() self.user.affiliated_institutions.add(self.institution1) self.rdm_statistics = rdm_statistics_factories.RdmStatisticsFactory.create( institution=self.institution1, provider='s3', owner=self.user) self.rdm_statistics.save() self.view = views.ImageView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = {'graph_type': 'ext'} self.view.kwargs = {'provider': 's3'} self.view.kwargs = {'institution_id': self.institution1.id} def tearDown(self): super(TestImageView, self).tearDown() self.user.affiliated_institutions.remove(self.institution1) self.user.delete() self.institution1.delete() self.rdm_statistics.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False self.request.user.is_registered = True self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_active = True self.request.user.is_registered = False self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_non_affiliated_institution_user_login(self): """test user unaffiliated institution login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs = {'institution_id': self.institution1.id + 1} nt.assert_equal(self.view.test_func(), False) self.view.kwargs = {'institution_id': self.institution1.id}
class TestclassStatisticsView(AdminTestCase): """test StatisticsView""" def setUp(self): super(TestclassStatisticsView, self).setUp() self.user = AuthUserFactory() self.institution1 = InstitutionFactory() self.user.affiliated_institutions.add(self.institution1) self.request = RequestFactory().get('/fake_path') self.view = views.StatisticsView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = {'institution_id': self.institution1.id} def tearDown(self): super(TestclassStatisticsView, self).tearDown() self.user.affiliated_institutions.remove(self.institution1) self.user.delete() self.institution1.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False self.request.user.is_registered = True self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_active = True self.request.user.is_registered = False self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_non_affiliated_institution_user_login(self): """test user unaffiliated institution login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs = {'institution_id': self.institution1.id + 1} nt.assert_equal(self.view.test_func(), False) self.view.kwargs = {'institution_id': self.institution1.id} def test_get_context_data(self, **kwargs): """contextのテスト""" ctx = self.view.get_context_data(**self.view.kwargs) nt.assert_is_instance(ctx['institution'], Institution) nt.assert_equal(ctx['institution'].id, self.institution1.id) nt.assert_true('current_date' in ctx) nt.assert_true('user' in ctx) nt.assert_true('provider_data_array' in ctx) nt.assert_true('token' in ctx)
class TestSendView(AdminTestCase): """test SendView""" def setUp(self): super(TestSendView, self).setUp() self.user = AuthUserFactory() self.request = RequestFactory().get('/fake_path') self.user = AuthUserFactory() self.institution1 = InstitutionFactory() self.user.affiliated_institutions.add(self.institution1) self.view = views.SendView() self.view = setup_user_view(self.view, self.request, user=self.user) self.view.kwargs = {'institution_id': self.institution1.id} def tearDown(self): super(TestSendView, self).tearDown() self.user.affiliated_institutions.remove(self.institution1) self.user.delete() self.institution1.delete() def test_super_admin_login(self): """test superuser login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = True nt.assert_true(self.view.test_func()) def test_admin_login(self): """test institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True nt.assert_true(self.view.test_func()) def test_non_admin_login(self): """test user not superuser or institution administrator login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = False nt.assert_equal(self.view.test_func(), False) def test_non_active_user_login(self): """test invalid user login""" self.request.user.is_active = False self.request.user.is_registered = True self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_non_registered_user_login(self): """test unregistered user login""" self.request.user.is_active = True self.request.user.is_registered = False self.request.user.is_superuser = True self.request.user.is_staff = True nt.assert_equal(self.view.test_func(), False) def test_non_affiliated_institution_user_login(self): """test user unaffiliated institution login""" self.request.user.is_active = True self.request.user.is_registered = True self.request.user.is_superuser = False self.request.user.is_staff = True self.view.kwargs = {'institution_id': self.institution1.id + 1} nt.assert_equal(self.view.test_func(), False) self.view.kwargs = {'institution_id': self.institution1.id} def test_valid_get(self, *args, **kwargs): """test valid GET method""" res = self.view.get(self.request, *args, **self.view.kwargs) nt.assert_equal(res.status_code, 200) def test_invalid_get(self, *args, **kwargs): """test invalid GET method""" self.view.kwargs = {'institution_id': 100} res = self.view.get(self.request, *args, **self.view.kwargs) nt.assert_equal(res.status_code, 200)