Esempio n. 1
0
 def test_authenticate_error_wrong_user_or_passwd(self,
                                                  mock_get_user_from_db,
                                                  mock_load_db_manager):
     auth = Authenticate()
     try:
         auth.authenticate(self.user, self.pw)
         # To be sure that the exception is raised
         self.assertTrue(1 == 0)
     except Exception:
         self.assertTrue(1 == 1)
     mock_load_db_manager.assert_called_once()
     mock_get_user_from_db.assert_called_once_with(self.user, self.pw)
Esempio n. 2
0
    def test_authenticate_ok(self, mock_get_user_from_db,
                             mock_get_token_user_id, mock_is_valid_token,
                             mock_create_new_token, mock_load_db_manager):
        """Test to check authentication process is working"""
        auth = Authenticate()

        token = auth.authenticate(self.user, self.pw)
        self.assertFalse(mock_create_new_token.called)
        mock_load_db_manager.assert_called_once()
        mock_get_user_from_db.assert_called_once_with(self.user, self.pw)
        mock_get_token_user_id.assert_called_once_with(0)
        mock_is_valid_token.assert_called_once_with("token-0")
        self.assertEqual("token-0", token)
Esempio n. 3
0
    def test_authenticate_ok_create_new_token_from_invalid_token(
            self, mock_get_user_from_db, mock_get_token_user_id,
            mock_is_valid_token, mock_create_new_token, mock_load_db_manager):
        """Test to check authentication process is working creating a new token because previous token expired"""
        auth = Authenticate()

        token = auth.authenticate(self.user, self.pw)
        mock_load_db_manager.assert_called_once()
        mock_get_user_from_db.assert_called_once_with(self.user, self.pw)
        mock_get_token_user_id.assert_called_once_with(exp_user.id)
        mock_create_new_token.assert_called_once_with(exp_user.id)
        mock_is_valid_token.assert_called_once_with(exp_token.token)
        self.assertEqual(exp_token.token, token.token)
Esempio n. 4
0
    def authenticate(self, data_json):
        if 'authenticate' not in data_json:
            return AuthenticateBadRequest('\'authenticate\' key is missing')
        if 'username' not in data_json['authenticate']:
            return AuthenticateBadRequest(
                '\'username\' key is missing in \'authenticate\' dict')
        if 'password' not in data_json['authenticate']:
            return AuthenticateBadRequest(
                '\'password\' key is missing in \'authenticate\' dict')

        try:
            auth_obj = Authenticate(self.get_config().read_database())
        except Exception as ex:
            return AuthenticateBadRequest(ex.message)
        try:
            token = auth_obj.authenticate(
                data_json['authenticate']['username'],
                data_json['authenticate']['password'])
            response = Success('Authenticated')
            response.add_extra_fields({'token': token})
            return response
        except Exception as ex:
            return Unauthorized(ex.message)