コード例 #1
0
 def test_create_new_token(self, mock_db_init, mock_create_new_token):
     """Test get token from a user id that has token"""
     with mock.patch(
             "cheetahapi.core.authenticate.Authenticate.load_db_manager"):
         auth = Authenticate()
     token_ret = auth.create_new_token(exp_user.id)
     self.assertEqual(exp_token.token, token_ret)
     mock_db_init.assert_called_once()
     mock_create_new_token.assert_called_once_with(exp_user.id)
コード例 #2
0
 def test_get_user_from_db(self, mock_db_init, mock_get_user_from_db):
     """Test to get a user from the database using username and password"""
     with mock.patch(
             "cheetahapi.core.authenticate.Authenticate.load_db_manager"):
         auth = Authenticate()
     user_ret = auth.get_user_from_db(exp_user.username, exp_user.pw)
     self.assertEqual(exp_user.username, user_ret.username)
     mock_db_init.assert_called_once()
     mock_get_user_from_db.assert_called_once_with(exp_user.username,
                                                   exp_user.pw)
コード例 #3
0
    def test_token_has_not_valid_date(self, mock_get_today_date):
        """Test to check when the token has not a valid date and exceeds the number of days when it's valid"""
        token_creation_date_string = "2018-03-22 00:00:00.0"
        with mock.patch(
                "cheetahapi.core.authenticate.Authenticate.load_db_manager"):
            auth = Authenticate()
        auth.set_token_days_valid(1)

        ret = auth.token_date_not_expired(token_creation_date_string)
        mock_get_today_date.assert_called_once()
        self.assertFalse(ret)
コード例 #4
0
 def test_create_new_token_not_found(self, mock_db_init,
                                     mock_create_new_token):
     """Test get token from a user id that has token"""
     with mock.patch(
             "cheetahapi.core.authenticate.Authenticate.load_db_manager"):
         auth = Authenticate()
     user_id = 99999
     token_ret = auth.create_new_token(user_id)
     self.assertEqual(None, token_ret)
     mock_db_init.assert_called_once()
     mock_create_new_token.assert_called_once_with(user_id)
コード例 #5
0
 def test_get_token_user_id_not_found(self, mock_db_init,
                                      mock_get_token_user_id):
     """Test None token from a user id that has NOT token"""
     with mock.patch(
             "cheetahapi.core.authenticate.Authenticate.load_db_manager"):
         auth = Authenticate()
     user_id = 99999
     token_ret = auth.get_token_user_id(user_id)
     self.assertEqual(None, token_ret)
     mock_db_init.assert_called_once()
     mock_get_token_user_id.assert_called_once_with(user_id)
コード例 #6
0
 def test_get_token_from_db(self, mock_db_init):
     """Test get token from database filtering by token string, which is unique"""
     with mock.patch(
             "cheetahapi.core.authenticate.Authenticate.load_db_manager"):
         auth = Authenticate()
     with mock.patch("cheetahapi.core.db.db_authenticate.DbAuthenticate.get_token", return_value=exp_token)\
             as mock_get_token:
         ret = auth.get_token_from_db(exp_token.token)
         self.assertEqual(exp_token.token, ret.token)
         mock_get_token.assert_called_once_with(exp_token.token)
         mock_db_init.assert_called_once()
コード例 #7
0
 def test_is_invalid_token(self, mock_get_today_date):
     """Test a token is not valid"""
     auth = Authenticate()
     auth.set_token_days_valid(1)
     exp_get_token_from_db = Token()
     exp_get_token_from_db.created = "2018-03-22 00:00:00.0"
     with mock.patch(
             "cheetahapi.core.authenticate.Authenticate.get_token_from_db",
             return_value=exp_get_token_from_db):
         is_valid = auth.is_valid_token(exp_token)
         mock_get_today_date.assert_called_once()
     self.assertFalse(is_valid)
コード例 #8
0
 def test_get_user_from_db_not_found(self, mock_db_init,
                                     mock_get_user_from_db):
     """Test return None user when the username or password is wrong"""
     with mock.patch(
             "cheetahapi.core.authenticate.Authenticate.load_db_manager"):
         auth = Authenticate()
     username = "******"
     pw = "bar"
     user_ret = auth.get_user_from_db(username, pw)
     self.assertEqual(None, user_ret)
     mock_db_init.assert_called_once()
     mock_get_user_from_db.assert_called_once_with(username, pw)
コード例 #9
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)
コード例 #10
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)
コード例 #11
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)
コード例 #12
0
 def test_is_valid_token(self, mock_get_today_date):
     """Test a token is valid"""
     with mock.patch(
             "cheetahapi.core.authenticate.Authenticate.load_db_manager"):
         auth = Authenticate()
     auth.set_token_days_valid(1)
     exp_get_token_from_db = Token()
     exp_get_token_from_db.created = "2018-03-23 00:00:01.377000"
     with mock.patch(
             "cheetahapi.core.authenticate.Authenticate.get_token_from_db",
             return_value=exp_get_token_from_db):
         is_valid = auth.is_valid_token(exp_token)
     mock_get_today_date.assert_called_once()
     self.assertTrue(is_valid)
コード例 #13
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)