def test_givenUserWithPass_whenAddDB_thenEncodeAuthToken(self):
     """User model encode auth token test """
     user = User(email='*****@*****.**', password='******')
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id, user.email)
     self.assertTrue(isinstance(auth_token, bytes))
 def test_givenUserWithPass_whenAddDB_thenDecodeAuthToken(self):
     """User model decode auth token test """
     user = User(email='*****@*****.**', password='******')
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id, user.email)
     self.assertTrue(isinstance(auth_token, bytes))
     payload = User.decode_auth_token(auth_token.decode("utf-8"))
     self.assertEqual('*****@*****.**', payload['user_email'])
Esempio n. 3
0
 def test_encode_auth_token(self):
     user = User(email='*****@*****.**',
                 password='******',
                 registered_on=datetime.datetime.utcnow())
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     print(type(auth_token))
     self.assertTrue(isinstance(
         auth_token, bytes))  # auth_token:bytes表明编码成功 因为jwt用base64编码
Esempio n. 4
0
 def test_decode_auth_token(self):
     user = User(email='*****@*****.**',
                 password='******',
                 registered_on=datetime.datetime.utcnow())
     db.session.add(user)
     db.session.commit()
     auth_token = user.encode_auth_token(user.id)
     self.assertTrue(isinstance(auth_token, bytes))
     self.assertTrue(
         User.decode_auth_token(
             auth_token.decode("utf-8")) == 1)  # first record id will be 1
Esempio n. 5
0
 def get_logged_in_user(new_request):
     # get the auth token
     data = new_request.headers.get('Authorization')
     if data:
         auth_token = data.split(" ")[1]
     else:
         auth_token = ''
     if auth_token:
         print(auth_token)
         resp = User.decode_auth_token(auth_token)  # user_id
         if not isinstance(resp, str):
             user = User.query.filter_by(id=resp).first()
             response_object = {
                 'status': 'success',
                 'data': {
                     'user_id': user.id,
                     'email': user.email,
                     'admin': user.admin,
                     'username': user.username,
                     'registered_on': str(user.registered_on)
                 }
             }
             return response_object, 200  # 返回user对象
         response_object = {
             'status': 'fail',
             'message': resp
         }
         return response_object, 401
     else:
         response_object = {
             'status': 'fail',
             'message': 'Provide a valid auth token.'
         }
         return response_object, 401
Esempio n. 6
0
    def test_givenUser_whenAccessParameter_thenGetUser(self):
        """User Model parameters test """
        actual_user = User(id="322",
                           email='*****@*****.**',
                           name='Mesut',
                           surname='Tekin',
                           password='******')

        self.assertEqual("322", actual_user.id)
        self.assertEqual("*****@*****.**", actual_user.email)
        self.assertEqual("Mesut", actual_user.name)
        self.assertEqual("Tekin", actual_user.surname)
        self.assertTrue(actual_user.check_password("test"))

        with self.assertRaises(AttributeError):
            actual_user.password()
    def test_givenUser_whenUserAdded_thenGetTheUser(self):
        """Add and get a user to DB """
        expected_user = User(email='*****@*****.**',
                             name='Mesut',
                             surname='Tekin',
                             password='******')
        db.session.add(expected_user)
        db.session.commit()
        actual_user = User.query.filter_by(email='*****@*****.**').first()

        self.assertEqual(expected_user.email, actual_user.email)
        self.assertEqual(expected_user.name, actual_user.name)
        self.assertEqual(expected_user.surname, actual_user.surname)
def save_new_user(data):
    user = User.query.filter_by(email=data['email']).first()
    if not user:
        new_user = User(
            email=data['email'],
            name=data['name'],
            surname=data['surname'],
            password=data['password'],
        )
        save_changes(new_user)
        return generate_token(new_user)
    else:
        response_object = {
            'status': 'fail',
            'message': 'User already exists. Please Log in.',
        }
        return response_object, 409
Esempio n. 9
0
def save_new_user(data):
    user = User.query.filter_by(email=data['email']).first()
    if not user:
        new_user = User(public_id=str(uuid.uuid4()),
                        email=data['email'],
                        username=data['username'],
                        password=data['password'],
                        registered_on=datetime.utcnow())
        save_changes(new_user)

        return generate_token(new_user)
    else:
        # 用户名已存在
        response_object = {
            'status': 'fail',
            'message': 'User already exists.Please Log in.',
        }
        return response_object, 409
Esempio n. 10
0
    def logout_user(data):
        if data:
            auth_token = data.split(" ")[1]  # Bearer xxx.xxx.xxx
        else:
            auth_token = ''
        if auth_token:
            resp = User.decode_auth_token(auth_token)  # user_id or error msg
            if not isinstance(resp, str):
                # mark the token as blacklisted to disabled it
                return save_token(token=auth_token)
            else:
                response_object = {
                    'status': 'fail',
                    'message': resp
                }
                return response_object, 401

        else:
            response_object = {
                'status': 'fail',
                'message': 'Provide a valid auth token.'
            }
            return response_object, 403