def test_connect_or_401_forbidden(self, mock_connection): mock_connection.side_effect = UnauthorizedError() with assert_raises(HTTPError) as cm: connect_or_401('My token') mock_connection.assert_called_once_with(settings.HOST, 'My token') assert_equal(cm.exception.code, 401)
def test_connect_or_error_returns_401_when_client_raises_unauthorized_error(self, mock_connection): mock_connection.side_effect = UnauthorizedError() with assert_raises(HTTPError) as cm: connect_or_error(self.host, self.token) mock_connection.assert_called_once_with(self.host, self.token) assert_equal(cm.exception.code, 401)
def test_set_user_config_fail(self, mock_connection): mock_connection.side_effect = UnauthorizedError('Bad credentials!') # Create a user with no settings user = AuthUserFactory() user.add_addon('dataverse') user_settings = user.get_addon('dataverse') url = api_url_for('dataverse_set_user_config') params = {'api_token': 'wrong-info'} # Post incorrect credentials to existing user res = self.app.post_json(url, params, auth=self.user.auth, expect_errors=True) self.user_settings.reload() # Original user's info has not changed assert_equal(res.status_code, http.UNAUTHORIZED) assert_equal(self.user_settings.api_token, 'snowman-frosty') # Post incorrect credentials to new user res = self.app.post_json(url, params, auth=user.auth, expect_errors=True) user_settings.reload() # New user's incorrect credentials were not saved assert_equal(res.status_code, http.UNAUTHORIZED) assert_equal(user_settings.api_token, None)
def test_connect_from_settings_or_401_forbidden(self, mock_connection): mock_connection.side_effect = UnauthorizedError() node_settings = AddonDataverseNodeSettings() node_settings.external_account = create_external_account( self.host, self.token, ) with assert_raises(HTTPError) as e: connect_from_settings_or_401(node_settings) mock_connection.assert_called_once_with(self.host, self.token) assert_equal(e.exception.code, 401)
def test_connect_from_settings_or_401_forbidden(self, mock_connection): mock_connection.side_effect = UnauthorizedError() user_settings = AddonDataverseUserSettings() user_settings.api_token = 'Something ridiculous' with assert_raises(HTTPError) as e: connect_from_settings_or_401(user_settings) mock_connection.assert_called_once_with( settings.HOST, user_settings.api_token, ) assert_equal(e.exception.code, 401)
def test_dataverse_add_external_account_fail(self, mock_connection): mock_connection.side_effect = UnauthorizedError('Bad credentials!') host = 'myfakehost.data.verse' token = 'api-token-here' url = api_url_for('dataverse_add_user_account') params = {'host': host, 'api_token': token} res = self.app.post_json( url, params, auth=self.user.auth, expect_errors=True, ) self.user.reload() assert_equal(len(self.user.external_accounts), 0) assert_equal(res.status_code, http.UNAUTHORIZED)
def test_connect_fail(self, mock_connection): mock_connection.side_effect = UnauthorizedError() with assert_raises(UnauthorizedError): _connect(self.host, self.token) mock_connection.assert_called_once_with(self.host, self.token)
def test_connect_fail(self, mock_connection): mock_connection.side_effect = UnauthorizedError() with assert_raises(UnauthorizedError): _connect('My token', 'My host') mock_connection.assert_called_once_with('My host', 'My token')