diff options
Diffstat (limited to 'app/tests')
-rw-r--r-- | app/tests/conftest.py | 16 | ||||
-rw-r--r-- | app/tests/test_admin.py | 23 | ||||
-rw-r--r-- | app/tests/test_cli.py | 9 | ||||
-rw-r--r-- | app/tests/test_fastapi.py | 22 | ||||
-rw-r--r-- | app/tests/test_forms.py | 37 | ||||
-rw-r--r-- | app/tests/test_models.py | 16 | ||||
-rw-r--r-- | app/tests/test_views.py | 222 |
7 files changed, 179 insertions, 166 deletions
diff --git a/app/tests/conftest.py b/app/tests/conftest.py index 0fd1ef1..71eb677 100644 --- a/app/tests/conftest.py +++ b/app/tests/conftest.py @@ -5,14 +5,18 @@ from app import db import tempfile import os + @pytest.fixture def app(): - flask_app.config['WTF_CSRF_ENABLED'] = False - flask_app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///" + tempfile.mkstemp(suffix='.db')[-1] - flask_app.config['MAIL_BACKEND'] = "file" #"locmem" - flask_app.config["MAIL_FILE_PATH"] = '/tmp/app-messages' - db.create_all() - yield flask_app + flask_app.config["WTF_CSRF_ENABLED"] = False + flask_app.config["SQLALCHEMY_DATABASE_URI"] = ( + "sqlite:///" + tempfile.mkstemp(suffix=".db")[-1] + ) + flask_app.config["MAIL_BACKEND"] = "file" # "locmem" + flask_app.config["MAIL_FILE_PATH"] = "/tmp/app-messages" + db.create_all() + yield flask_app + @pytest.fixture def client(app): diff --git a/app/tests/test_admin.py b/app/tests/test_admin.py index 1a5df07..ae20f2b 100644 --- a/app/tests/test_admin.py +++ b/app/tests/test_admin.py @@ -1,14 +1,19 @@ from app import database_cli + def test_admin(app, client): - runner = app.test_cli_runner() - assert runner.invoke(database_cli,["admin-create"]).exit_code == 0 + runner = app.test_cli_runner() + assert runner.invoke(database_cli, ["admin-create"]).exit_code == 0 - res = client.post("/signin",data=dict( - email=app.config["ADMIN_EMAIL"], - password=app.config["ADMIN_PASSWORD"]),follow_redirects=True) + res = client.post( + "/signin", + data=dict( + email=app.config["ADMIN_EMAIL"], password=app.config["ADMIN_PASSWORD"] + ), + follow_redirects=True, + ) - print(res.data) - assert b"Supersu" in res.data - res = client.get("/admin/user/") - assert res.status_code == 200
\ No newline at end of file + print(res.data) + assert b"Supersu" in res.data + res = client.get("/admin/user/") + assert res.status_code == 200 diff --git a/app/tests/test_cli.py b/app/tests/test_cli.py index aec4cee..194dd64 100644 --- a/app/tests/test_cli.py +++ b/app/tests/test_cli.py @@ -1,7 +1,8 @@ from app import database_cli + def test_database_commands(app, client): - runner = app.test_cli_runner() - assert runner.invoke(database_cli,["delete"]).exit_code == 0 - assert runner.invoke(database_cli,["create"]).exit_code == 0 - assert runner.invoke(database_cli,["admin-create"]).exit_code == 0
\ No newline at end of file + runner = app.test_cli_runner() + assert runner.invoke(database_cli, ["delete"]).exit_code == 0 + assert runner.invoke(database_cli, ["create"]).exit_code == 0 + assert runner.invoke(database_cli, ["admin-create"]).exit_code == 0 diff --git a/app/tests/test_fastapi.py b/app/tests/test_fastapi.py index 89ae368..f8c304e 100644 --- a/app/tests/test_fastapi.py +++ b/app/tests/test_fastapi.py @@ -4,17 +4,19 @@ from api import app as fastapi_app fastapi_client = TestClient(fastapi_app) + def test_fastapi(app, client): - res = fastapi_client.get("/version") - assert res.status_code == 200 - assert res.json() == {"message":fastapi_app.version} + res = fastapi_client.get("/version") + assert res.status_code == 200 + assert res.json() == {"message": fastapi_app.version} + def test_fastapi_user_details(app, client): - # TODO Investigate why this is failing - #res = fastapi_client.get("/v1/user-details?email={}".format("admin@example.com")) - #assert res.status_code == 200 - #assert res.json()["first_name"] == "Supersu" + # TODO Investigate why this is failing + # res = fastapi_client.get("/v1/user-details?email={}".format("admin@example.com")) + # assert res.status_code == 200 + # assert res.json()["first_name"] == "Supersu" - res = fastapi_client.get("/v1/user-details?email={}".format("notadmin@example.com")) - assert res.status_code == 404 - assert res.json()["detail"] == "User Not Found" + res = fastapi_client.get("/v1/user-details?email={}".format("notadmin@example.com")) + assert res.status_code == 404 + assert res.json()["detail"] == "User Not Found" diff --git a/app/tests/test_forms.py b/app/tests/test_forms.py index 2da2c36..1a26635 100644 --- a/app/tests/test_forms.py +++ b/app/tests/test_forms.py @@ -1,19 +1,24 @@ -def test_incorrect_forms(app,client): - res = client.post("/signin",data={"email":"123"}) - assert b"This field is required." in res.data +def test_incorrect_forms(app, client): + res = client.post("/signin", data={"email": "123"}) + assert b"This field is required." in res.data - res = client.post("/signup",data={"email":"123"}) - assert b"This field is required." in res.data + res = client.post("/signup", data={"email": "123"}) + assert b"This field is required." in res.data - res = client.post("/ContactUs",data={"email":123}) - assert b"This field is required." in res.data + res = client.post("/ContactUs", data={"email": 123}) + assert b"This field is required." in res.data -def test_contactus(app,client): - res = client.post("/ContactUs",data={ - "name": "Test User", - "subject": "Test Contact", - "email": "testemail@email.com", - "body": "Test Message" - }, follow_redirects=True) - assert res.status_code == 200 - assert b"Wuhu" in res.data
\ No newline at end of file + +def test_contactus(app, client): + res = client.post( + "/ContactUs", + data={ + "name": "Test User", + "subject": "Test Contact", + "email": "testemail@email.com", + "body": "Test Message", + }, + follow_redirects=True, + ) + assert res.status_code == 200 + assert b"Wuhu" in res.data diff --git a/app/tests/test_models.py b/app/tests/test_models.py index 513f332..c84df92 100644 --- a/app/tests/test_models.py +++ b/app/tests/test_models.py @@ -1,11 +1,9 @@ from app.models import User -def test_usermodel(app,client): - user = User( - first_name="John", - email="test@example.com", - password="pass") - assert user.full_name == "John None" - assert user.get_role() == None - assert user.get_team() == None - assert user.is_paid() == None
\ No newline at end of file + +def test_usermodel(app, client): + user = User(first_name="John", email="test@example.com", password="pass") + assert user.full_name == "John None" + assert user.get_role() == None + assert user.get_team() == None + assert user.is_paid() == None diff --git a/app/tests/test_views.py b/app/tests/test_views.py index 0abd5e4..811815f 100644 --- a/app/tests/test_views.py +++ b/app/tests/test_views.py @@ -1,122 +1,120 @@ from itsdangerous.url_safe import URLSafeSerializer from app import app as flask_app + ts = URLSafeSerializer(flask_app.config["SECRET_KEY"]) data2check_visitors = { - "/index": { - "code": 200, "data": b"Nice Tagline" - }, - "/": { - "code": 200, "data": b"Nice Tagline" - }, - "/ContactUs":{ - "code": 200, "data": b"send us a message." - }, - "/doesnotexists":{ - "code": 404, "data": b"Page Not Found" - }, - "/logout":{ - "code": 200, "data": b"You have been logged out." - }, - "/dashboard":{ - "code":401,"data":b"You need to be logged in to access this resource" - }, - "/signup":{ - "code":200,"data":b"Register your account." - }, - "/signin":{ - "code":200,"data":b"Sign in to your account." - }, - "/Simulate500":{ - "code":500,"data":b"Server Could Not Process This." - }, - "/admin/user/":{ - "code":403,"data":b"Forbidden" - }, - "/confirm":{ - "code":200,"data":b"Token not provided in URL Parameter" - }, - "/confirm?confirmation_token=123":{ - "code":200,"data":b"Bad Token Provided" - } + "/index": {"code": 200, "data": b"Nice Tagline"}, + "/": {"code": 200, "data": b"Nice Tagline"}, + "/ContactUs": {"code": 200, "data": b"send us a message."}, + "/doesnotexists": {"code": 404, "data": b"Page Not Found"}, + "/logout": {"code": 200, "data": b"You have been logged out."}, + "/dashboard": { + "code": 401, + "data": b"You need to be logged in to access this resource", + }, + "/signup": {"code": 200, "data": b"Register your account."}, + "/signin": {"code": 200, "data": b"Sign in to your account."}, + "/Simulate500": {"code": 500, "data": b"Server Could Not Process This."}, + "/admin/user/": {"code": 403, "data": b"Forbidden"}, + "/confirm": {"code": 200, "data": b"Token not provided in URL Parameter"}, + "/confirm?confirmation_token=123": {"code": 200, "data": b"Bad Token Provided"}, } + def test_visitors(app, client): - for page in data2check_visitors: - res = client.get(page) - print("Testing %s",page) - assert res.status_code == data2check_visitors[page]["code"] - assert data2check_visitors[page]["data"] in res.data + for page in data2check_visitors: + res = client.get(page) + print("Testing %s", page) + assert res.status_code == data2check_visitors[page]["code"] + assert data2check_visitors[page]["data"] in res.data + def test_user_auth_flow(app, client): - res = client.post("/signup",data=dict( - email="test@example.com", - first_name="John", - password="testpassword", - ), follow_redirects=True) - - assert res.status_code == 200 - assert b"confirm your email" in res.data - - res = client.post("/signin",data=dict( - email="test@example.com", - password="testpassword"), - follow_redirects=True) - assert res.status_code == 200 - assert b"Please Confirm Your Email First." in res.data - - confirmation_token = ts.dumps("test@example.com",salt="email-confirm-key") - res = client.get("/confirm?confirmation_token={}".format(confirmation_token), - follow_redirects=True) - print(res.data) - assert b"Succesfully Verified" in res.data - - res = client.post("/signin",data=dict( - email="test@example.com", - password="testpassword"), - follow_redirects=True) - assert res.status_code == 200 - assert b"Hi John" in res.data - - res = client.get("/logout", follow_redirects=True) - assert res.status_code == 200 - assert b"You have been logged out." in res.data - - res = client.post("/signin",data=dict( - email="test@example.com", - password="testpassword"), - follow_redirects=True) - assert res.status_code == 200 - assert b"Hi John" in res.data - - res = client.get("/signin",follow_redirects=True) - assert res.status_code == 200 - assert b"Hi John" in res.data - - res = client.get("/signup",follow_redirects=True) - assert res.status_code == 200 - assert b"Hi John" in res.data - - res = client.get("/admin/user/") - assert res.status_code == 403 - - res = client.get("/logout") - res = client.post("/signin",data=dict( - email="testtest@example.com", - password="123456"),follow_redirects=True) - assert res.status_code == 200 - assert b"Incorrect Email" in res.data - res = client.post("/signin",data=dict( - email="test@example.com", - password="incorrectpassword"), - follow_redirects = True) - assert res.status_code == 200 - assert b"Incorrect Password" in res.data - - res = client.post("/signup",data=dict( - email="test@example.com", - first_name="John", - password="testpassword", - ), follow_redirects=True) - assert res.status_code == 200 - assert b"Oops! An account with that email already exists" in res.data
\ No newline at end of file + res = client.post( + "/signup", + data=dict( + email="test@example.com", + first_name="John", + password="testpassword", + ), + follow_redirects=True, + ) + + assert res.status_code == 200 + assert b"confirm your email" in res.data + + res = client.post( + "/signin", + data=dict(email="test@example.com", password="testpassword"), + follow_redirects=True, + ) + assert res.status_code == 200 + assert b"Please Confirm Your Email First." in res.data + + confirmation_token = ts.dumps("test@example.com", salt="email-confirm-key") + res = client.get( + "/confirm?confirmation_token={}".format(confirmation_token), + follow_redirects=True, + ) + print(res.data) + assert b"Succesfully Verified" in res.data + + res = client.post( + "/signin", + data=dict(email="test@example.com", password="testpassword"), + follow_redirects=True, + ) + assert res.status_code == 200 + assert b"Hi John" in res.data + + res = client.get("/logout", follow_redirects=True) + assert res.status_code == 200 + assert b"You have been logged out." in res.data + + res = client.post( + "/signin", + data=dict(email="test@example.com", password="testpassword"), + follow_redirects=True, + ) + assert res.status_code == 200 + assert b"Hi John" in res.data + + res = client.get("/signin", follow_redirects=True) + assert res.status_code == 200 + assert b"Hi John" in res.data + + res = client.get("/signup", follow_redirects=True) + assert res.status_code == 200 + assert b"Hi John" in res.data + + res = client.get("/admin/user/") + assert res.status_code == 403 + + res = client.get("/logout") + res = client.post( + "/signin", + data=dict(email="testtest@example.com", password="123456"), + follow_redirects=True, + ) + assert res.status_code == 200 + assert b"Incorrect Email" in res.data + res = client.post( + "/signin", + data=dict(email="test@example.com", password="incorrectpassword"), + follow_redirects=True, + ) + assert res.status_code == 200 + assert b"Incorrect Password" in res.data + + res = client.post( + "/signup", + data=dict( + email="test@example.com", + first_name="John", + password="testpassword", + ), + follow_redirects=True, + ) + assert res.status_code == 200 + assert b"Oops! An account with that email already exists" in res.data |