class TestExternalAccount(OsfTestCase): # Test the ExternalAccount object and associated views. # # Functionality not specific to the OAuth version used by the # ExternalProvider should go here. def setUp(self): super(TestExternalAccount, self).setUp() self.user = AuthUserFactory() self.provider = MockOAuth2Provider() def tearDown(self): ExternalAccount._clear_caches() ExternalAccount.remove() self.user.remove() super(TestExternalAccount, self).tearDown() def test_disconnect(self): # Disconnect an external account from a user external_account = ExternalAccountFactory( provider='mock2', provider_id='mock_provider_id', provider_name='Mock Provider', ) self.user.external_accounts.append(external_account) self.user.save() # If the external account isn't attached, this test has no meaning assert_equal(ExternalAccount.find().count(), 1) assert_in( external_account, self.user.external_accounts, ) response = self.app.delete(api_url_for( 'oauth_disconnect', external_account_id=external_account._id), auth=self.user.auth) # Request succeeded assert_equal( response.status_code, http.OK, ) self.user.reload() # external_account.reload() # External account has been disassociated with the user assert_not_in( external_account, self.user.external_accounts, ) # External account is still in the database assert_equal(ExternalAccount.find().count(), 1) def test_disconnect_with_multiple_connected(self): # Disconnect an account connected to multiple users from one user external_account = ExternalAccountFactory( provider='mock2', provider_id='mock_provider_id', provider_name='Mock Provider', ) self.user.external_accounts.append(external_account) self.user.save() other_user = UserFactory() other_user.external_accounts.append(external_account) other_user.save() response = self.app.delete(api_url_for( 'oauth_disconnect', external_account_id=external_account._id), auth=self.user.auth) # Request succeeded assert_equal( response.status_code, http.OK, ) self.user.reload() # External account has been disassociated with the user assert_not_in( external_account, self.user.external_accounts, ) # External account is still in the database assert_equal(ExternalAccount.find().count(), 1) other_user.reload() # External account is still associated with the other user assert_in( external_account, other_user.external_accounts, )
class ApiAddonTestCase(ApiTestCase): """Base `TestCase` for tests that require interaction with addons. """ @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 tests.factories import ProjectFactory, AuthUserFactory from website.addons.base import ( AddonOAuthNodeSettingsBase, AddonNodeSettingsBase, AddonOAuthUserSettingsBase, AddonUserSettingsBase, ) 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.append(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, AddonOAuthNodeSettingsBase) assert isinstance(self.user_settings, AddonOAuthUserSettingsBase) self.account_id = self.account._id if self.account else None self.set_urls() def tearDown(self): super(ApiAddonTestCase, self).tearDown() self.user.remove() self.node.remove() if self.node_settings: self.node_settings.remove() if self.user_settings: self.user_settings.remove() if self.account: self.account.remove()
class TestExternalAccount(OsfTestCase): # Test the ExternalAccount object and associated views. # # Functionality not specific to the OAuth version used by the # ExternalProvider should go here. def setUp(self): super(TestExternalAccount, self).setUp() self.user = AuthUserFactory() self.provider = MockOAuth2Provider() def tearDown(self): ExternalAccount._clear_caches() ExternalAccount.remove() self.user.remove() super(TestExternalAccount, self).tearDown() def test_disconnect(self): # Disconnect an external account from a user external_account = ExternalAccountFactory( provider='mock2', provider_id='mock_provider_id', provider_name='Mock Provider', ) self.user.external_accounts.append(external_account) self.user.save() # If the external account isn't attached, this test has no meaning assert_equal(ExternalAccount.find().count(), 1) assert_in( external_account, self.user.external_accounts, ) response = self.app.delete( api_url_for('oauth_disconnect', external_account_id=external_account._id), auth=self.user.auth ) # Request succeeded assert_equal( response.status_code, http.OK, ) self.user.reload() # external_account.reload() # External account has been disassociated with the user assert_not_in( external_account, self.user.external_accounts, ) # External account is still in the database assert_equal(ExternalAccount.find().count(), 1) def test_disconnect_with_multiple_connected(self): # Disconnect an account connected to multiple users from one user external_account = ExternalAccountFactory( provider='mock2', provider_id='mock_provider_id', provider_name='Mock Provider', ) self.user.external_accounts.append(external_account) self.user.save() other_user = UserFactory() other_user.external_accounts.append(external_account) other_user.save() response = self.app.delete( api_url_for('oauth_disconnect', external_account_id=external_account._id), auth=self.user.auth ) # Request succeeded assert_equal( response.status_code, http.OK, ) self.user.reload() # External account has been disassociated with the user assert_not_in( external_account, self.user.external_accounts, ) # External account is still in the database assert_equal(ExternalAccount.find().count(), 1) other_user.reload() # External account is still associated with the other user assert_in( external_account, other_user.external_accounts, )
class ApiAddonTestCase(ApiTestCase): """Base `TestCase` for tests that require interaction with addons. """ @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 tests.factories import ( ProjectFactory, AuthUserFactory, ) from website.addons.base import (AddonOAuthNodeSettingsBase, AddonNodeSettingsBase, AddonOAuthUserSettingsBase, AddonUserSettingsBase) 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.append(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, AddonOAuthNodeSettingsBase) assert isinstance(self.user_settings, AddonOAuthUserSettingsBase) self.account_id = self.account._id if self.account else None self.set_urls() def tearDown(self): super(ApiAddonTestCase, self).tearDown() self.user.remove() self.node.remove() if self.node_settings: self.node_settings.remove() if self.user_settings: self.user_settings.remove() if self.account: self.account.remove()