def test_decode_token_invalid_token(self, mock_decode):
        mock_decode.side_effect = jwt.InvalidTokenError

        token = self.user.encode_token(self.user.id)
        self.assertTrue(isinstance(token, bytes))
        self.assertTrue(User.decode_token(token.decode("utf-8")) ==
                       "Invalid token. Please log in again.")
    def test_decode_token_expired_token(self, mock_decode):
        mock_decode.side_effect = jwt.ExpiredSignatureError

        token = self.user.encode_token(self.user.id)
        self.assertTrue(isinstance(token, bytes))
        self.assertTrue(User.decode_token(token.decode("utf-8")) ==
                        "Signature expired. Please log in again.")
Ejemplo n.º 3
0
 def post(self):
     # get auth token
     auth_header = request.headers.get('Authorization')
     if auth_header:
         token = auth_header.split(" ")[1]
     else:
         token = ''
     if token:
         resp = User.decode_token(token)
         if not isinstance(resp, str):
             # mark the token as blacklisted
             blacklist_token = BlacklistToken(token=token)
             try:
                 # insert the token
                 db.session.add(blacklist_token)
                 db.session.commit()
                 responseObject = {
                     'status': 'success',
                     'message': 'Successfully logged out.'
                 }
                 return make_response(jsonify(responseObject)), 200
             except Exception as e:
                 responseObject = {'status': 'fail', 'message': str(e)}
                 return make_response(jsonify(responseObject)), 200
         else:
             responseObject = {'status': 'fail', 'message': resp}
             return make_response(jsonify(responseObject)), 401
     else:
         responseObject = {
             'status': 'fail',
             'message': 'Provide a valid auth token.'
         }
         return make_response(jsonify(responseObject)), 403
Ejemplo n.º 4
0
 def get(self):
     # get the auth token
     auth_header = request.headers.get('Authorization')
     if auth_header:
         token = auth_header.split(" ")[1]
     else:
         token = ''
     if token:
         resp = User.decode_token(token)
         if not isinstance(resp, str):
             user = User.query.filter_by(id=resp).first()
             responseObject = {
                 'status': 'success',
                 'data': {
                     'user_id': user.id,
                     'username': user.username,
                     'admin': user.admin,
                     'registered_on': user.registered_on
                 }
             }
             return make_response(jsonify(responseObject)), 200
         responseObject = {'status': 'fail', 'message': resp}
         return make_response(jsonify(responseObject)), 401
     else:
         responseObject = {
             'status': 'fail',
             'message': 'Provide a valid auth token.'
         }
         return make_response(jsonify(responseObject)), 401
    def test_decode_token_blacklisted_token(self, mock_check_blacklist):
        is_blacklisted_token = True

        mock_check_blacklist.return_value = is_blacklisted_token

        token = self.user.encode_token(self.user.id)
        self.assertTrue(isinstance(token, bytes))
        self.assertTrue(User.decode_token(token.decode("utf-8")) ==
                        "Token blacklisted. Please log in again.")
 def test_decode_token(self):
     token = self.user.encode_token(self.user.id)
     self.assertTrue(isinstance(token, bytes))
     self.assertTrue(User.decode_token(token.decode("utf-8")) == 1)