def test_getToken_and_verify_token(self): res = self.app.post("/api/getToken", data=json.dumps(self.default_user), content_type='application/json') token = json.loads(res.data.decode("utf-8"))["token"] self.assertTrue(auth.verify_token(token)) self.assertEqual(res.status_code, 200) res2 = self.app.post("/api/is_token_valid", data=json.dumps({"token": token}), content_type='application/json') self.assertTrue(json.loads(res2.data.decode("utf-8")), ["token_is_valid"]) res3 = self.app.post("/api/is_token_valid", data=json.dumps( {"token": token + "something-else"}), content_type='application/json') self.assertEqual(res3.status_code, 403) res4 = self.app.post("/api/getToken", data=json.dumps(self.some_user), content_type='application/json') self.assertEqual(res4.status_code, 403)
def test_get_token_and_verify_token(self): res = self.app.post( "/api/get_token", data=json.dumps(self.default_user), content_type='application/json' ) token = json.loads(res.data)["token"] self.assertTrue(auth.verify_token(token)) self.assertEqual(res.status_code, 200) res2 = self.app.post( "/api/is_token_valid", data=json.dumps({"token": token}), content_type='application/json' ) self.assertTrue(json.loads(res2.data), ["token_is_valid"]) res3 = self.app.post( "/api/is_token_valid", data=json.dumps({"token": token + "something-else"}), content_type='application/json' ) self.assertEqual(res3.status_code, 403) res4 = self.app.post( "/api/get_token", data=json.dumps(self.some_user), content_type='application/json' ) self.assertEqual(res4.status_code, 403)
def is_token_valid(): """ Validate token :return: return valid message in JSON if valid; if not, return false in JSON """ incoming = request.get_json() is_valid = verify_token(incoming["token"]) if is_valid: return jsonify(token_is_valid=True) else: return jsonify(token_is_valid=False), 403