def dataverse_add_user_account(auth, **kwargs): """Verifies new external account credentials and adds to user's list""" user = auth.user provider = DataverseProvider() host = request.json.get('host').rstrip('/') api_token = request.json.get('api_token') # Verify that credentials are valid client.connect_or_error(host, api_token) # Note: `DataverseSerializer` expects display_name to be a URL try: provider.account = ExternalAccount( provider=provider.short_name, provider_name=provider.name, display_name=host, # no username; show host oauth_key=host, # hijacked; now host oauth_secret=api_token, # hijacked; now api_token provider_id=api_token, # Change to username if Dataverse allows ) provider.account.save() except KeyExistsException: # ... or get the old one provider.account = ExternalAccount.find_one( Q('provider', 'eq', provider.short_name) & Q('provider_id', 'eq', api_token) ) assert provider.account is not None if provider.account not in user.external_accounts: user.external_accounts.append(provider.account) user_addon = auth.user.get_addon('dataverse') if not user_addon: user.add_addon('dataverse') user.save() # Need to ensure that the user has dataverse enabled at this point user.get_or_add_addon('dataverse', auth=auth) user.save() return {}
class TestDataverseSerializerConfig(DataverseAddonTestCase): def setUp(self): super(TestDataverseSerializerConfig, self).setUp() self.provider = DataverseProvider() def test_default(self): assert_is_none(self.provider.account) @mock.patch('website.addons.dataverse.client._connect') def test_add_user_auth(self, mock_connect): mock_connect.return_value = create_mock_connection() external_account = create_external_account() self.user.external_accounts.append(external_account) self.user.save() self.provider.add_user_auth( self.node_settings, self.user, external_account._id, ) assert_equal(self.node_settings.external_account, external_account) assert_equal(self.node_settings.user_settings, self.user_settings) def test_add_user_auth_not_in_user_external_accounts(self): external_account = create_external_account() with assert_raises(HTTPError) as e: self.provider.add_user_auth( self.node_settings, self.user, external_account._id, ) assert_equal(e.status_code, http.FORBIDDEN)
def dataverse_remove_user_auth(auth, node_addon, **kwargs): """Remove Dataverse authorization and settings from node""" provider = DataverseProvider() return provider.remove_user_auth(node_addon, auth.user)
def dataverse_add_user_auth(auth, node_addon, **kwargs): """Allows for importing existing auth to AddonDataverseNodeSettings""" provider = DataverseProvider() external_account_id = request.get_json().get('external_account_id') return provider.add_user_auth(node_addon, auth.user, external_account_id)
def setUp(self): super(TestDataverseSerializerConfig, self).setUp() self.provider = DataverseProvider()