Skip to content

Commit

Permalink
Fix tests with new JWT validation
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaweedbrainCY committed Nov 9, 2024
1 parent 13bec0b commit ebb67ed
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
4 changes: 4 additions & 0 deletions api/tests/unit/test_ZKE_encrypted_key_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from app import app
import unittest
from database.db import db
import controllers
from unittest.mock import patch
from zero_totp_db_model.model import ZKE_encryption_key, User
Expand All @@ -18,6 +19,9 @@ def setUp(self):
self.jwtCookie = jwt_func.generate_jwt(1)
self.client = self.application.test_client()
self.endpoint = "/api/v1/zke_encrypted_key"
with self.application.app.app_context():
db.create_all()
db.session.commit()


self.get_zke_enc = patch("database.zke_repo.ZKE.getByUserId").start()
Expand Down
4 changes: 4 additions & 0 deletions api/tests/unit/test_all_secret_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from app import app
from database.db import db
import controllers
from unittest.mock import patch
from zero_totp_db_model.model import User, TOTP_secret
Expand All @@ -17,6 +18,9 @@ def setUp(self):
self.jwtCookie = jwt_func.generate_jwt(1)
self.client = self.application.test_client()
self.endpoint = "/api/v1/all_secrets"
with self.application.app.app_context():
db.create_all()
db.session.commit()


self.get_all_secret = patch("database.totp_secret_repo.TOTP_secret.get_all_enc_secret_by_user_id").start()
Expand Down
4 changes: 4 additions & 0 deletions api/tests/unit/test_encrypted_secret_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from app import app
from database.db import db
import controllers
from unittest.mock import patch
from zero_totp_db_model.model import User, TOTP_secret
Expand All @@ -19,6 +20,9 @@ def setUp(self):
self.jwtCookie = jwt_func.generate_jwt(1)
self.client = self.application.test_client()
self.endpoint = "/api/v1/encrypted_secret/uuid"
with self.application.app.app_context():
db.create_all()
db.session.commit()


self.getEncSecretByUUID = patch("database.totp_secret_repo.TOTP_secret.get_enc_secret_by_uuid").start()
Expand Down
4 changes: 4 additions & 0 deletions api/tests/unit/test_get_auth_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from app import app
from database.db import db
from environment import conf
from CryptoClasses.jwt_func import generate_jwt
from controllers import flask
Expand All @@ -18,6 +19,9 @@ def setUp(self):

self.get_authorization_url_patch = patch("Oauth.oauth_flow.get_authorization_url").start()
self.get_authorization_url_patch.return_value = "https://www.google.com", "state"
with self.application.app.app_context():
db.create_all()
db.session.commit()


def test_get_auth_flow(self):
Expand Down
7 changes: 7 additions & 0 deletions api/tests/unit/test_jwt_func.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import jwt
import unittest
from app import app
from database.db import db
from CryptoClasses.jwt_func import verify_jwt, generate_jwt
import datetime
from environment import conf
from connexion.exceptions import Forbidden, Unauthorized
from uuid import uuid4


class TestJWT(unittest.TestCase):
Expand All @@ -17,9 +20,13 @@ def setUp(self):
"iat": datetime.datetime.utcnow(),
"nbf": datetime.datetime.utcnow(),
"exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1),
"jti": str(uuid4())
}
self.secret = conf.api.jwt_secret
self.algorithm = "HS256"
with app.app.app_context():
db.create_all()
db.session.commit()

def test_verify_jwt_correct(self):
validJWT = jwt.encode(self.validPayload, self.secret, algorithm=self.algorithm)
Expand Down
18 changes: 5 additions & 13 deletions api/tests/unit/test_refresh_auth_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,15 @@ def setUp(self):
self.endpoint = "/api/v1/auth/refresh"
self.user_id = 1
self.jwt_token = generate_jwt(self.user_id)
self.jti = verify_jwt(self.jwt_token)["jti"]
self.refresh_token = str(uuid4())
self.hashed_refresh_token = sha256(self.refresh_token.encode('utf-8')).hexdigest()



user = UserModel(id=self.user_id,username="user", mail="user@user.com", password="pass", derivedKeySalt="AAA", isVerified = False, passphraseSalt = "AAAA", createdAt="01/01/2001")





with self.flask_application.app.app_context():
user = UserModel(id=self.user_id,username="user", mail="user@user.com", password="pass", derivedKeySalt="AAA", isVerified = False, passphraseSalt = "AAAA", createdAt="01/01/2001")
db.create_all()
db.session.add(user)
db.session.commit()

self.jti = verify_jwt(self.jwt_token)["jti"]
self.refresh_token = str(uuid4())
self.hashed_refresh_token = sha256(self.refresh_token.encode('utf-8')).hexdigest()


def tearDown(self):
with self.flask_application.app.app_context():
Expand Down
4 changes: 4 additions & 0 deletions api/tests/unit/test_update_email_controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from app import app
from database.db import db
import unittest
import controllers
from unittest.mock import patch
Expand All @@ -17,6 +18,9 @@ def setUp(self):
self.jwtCookie = jwt_func.generate_jwt(1)
self.client = self.application.test_client()
self.endpoint = "/api/v1/update/email"
with self.application.app.app_context():
db.create_all()
db.session.commit()


self.update_email = patch("database.user_repo.User.update_email").start()
Expand Down

0 comments on commit ebb67ed

Please sign in to comment.