From acffcad48b83fc555bfbf6135d4ed895a22fe12e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20L=C3=B6fgren?= <victor.l0fgr3n@gmail.com> Date: Tue, 16 Feb 2021 11:20:59 +0100 Subject: [PATCH] #3: Add initial pytest tests --- server/requirements.txt | Bin 1728 -> 1828 bytes server/test.py | 69 --------------------------------------- server/tests/__init__.py | 18 ++++++++++ server/tests/test_app.py | 42 ++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 69 deletions(-) delete mode 100644 server/test.py create mode 100644 server/tests/__init__.py create mode 100644 server/tests/test_app.py diff --git a/server/requirements.txt b/server/requirements.txt index 15f3fb46c43a7c2d2eee3959e3fce08db255efc2..31bb42fb147e77cea0e83c9af0230046efacb0b4 100644 GIT binary patch delta 99 zcmX@WyM%AUDn`*{hJ1!HhE#?khD3&RAZ-hTrkfWs#xq-|G88kEfCbDL^cal5n3sWz dp@5+hC=QX;Wq=p}(gspt2!ud#^H-MBi~uFz6X5^= delta 17 ZcmZ3&cYt@pD#pzx7{i!1r?4Ji1OPwR28{p! diff --git a/server/test.py b/server/test.py deleted file mode 100644 index f69c5a7b..00000000 --- a/server/test.py +++ /dev/null @@ -1,69 +0,0 @@ -from app import create_app, db -import unittest -import json - - -class Test(unittest.TestCase): - def setUp(self): - """Before each test, set up a blank database""" - self.app = create_app("configmodule.TestingConfig") - self.app.testing = True - - self.client = self.app.test_client() - - with self.app.app_context(): - db.drop_all() - db.create_all() - - # Called after every test - def tearDown(self): - with self.app.app_context(): - db.session.remove() - db.drop_all() - - def test_user(self): - # Create user - rv = self.client.post( - "/api/users/", - data=json.dumps({"email": "test@test.se", "password": "abc123"}), - ) - rv_dict = json.loads(rv.data.decode()) - - assert rv.status_code == 200 - assert rv_dict["id"] == 1 - assert "password" not in rv_dict - assert rv_dict["email"] == "test@test.se" - - # Try loggin with wrong PASSWORD - rv = self.client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc1234"})) - assert rv.status_code == 401 - - # Try loggin with wrong Email - rv = self.client.post("/api/users/login", data=json.dumps({"email": "test1@test.se", "password": "abc1234"})) - assert rv.status_code == 401 - - # Try loggin with right PASSWORD - rv = self.client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc123"})) - rv_dict = json.loads(rv.data.decode()) - assert rv.status_code == 200 - headers = {"Authorization": "Bearer " + rv_dict["access_token"]} - - # Get the current user - rv = self.client.get("/api/users/", headers=headers) - rv_dict = json.loads(rv.data.decode()) - assert rv.status_code == 200 - assert rv_dict["email"] == "test@test.se" - - rv = self.client.put("/api/users/", data=json.dumps({"name": "carl carlsson"}), headers=headers) - rv_dict = json.loads(rv.data.decode()) - assert rv.status_code == 200 - assert rv_dict["name"] == "Carl Carlsson" - - def test_empty(self): - # Try loggin withou any users - rv = self.client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc123"})) - assert rv.status_code == 401 - - -if __name__ == "__main__": - unittest.main() diff --git a/server/tests/__init__.py b/server/tests/__init__.py new file mode 100644 index 00000000..0d8c1354 --- /dev/null +++ b/server/tests/__init__.py @@ -0,0 +1,18 @@ +import pytest +from app import create_app, db + + +@pytest.fixture +def app(): + app = create_app("configmodule.TestingConfig") + + with app.app_context(): + db.drop_all() + db.create_all() + + return app + + +@pytest.fixture +def client(app): + return app.test_client() diff --git a/server/tests/test_app.py b/server/tests/test_app.py new file mode 100644 index 00000000..2ee26647 --- /dev/null +++ b/server/tests/test_app.py @@ -0,0 +1,42 @@ +from tests import app, client +import json + + +def test_app(client): + + # Create user + rv = client.post( + "/api/users/", + data=json.dumps({"email": "test@test.se", "password": "abc123"}), + ) + rv_dict = json.loads(rv.data.decode()) + + assert rv.status_code == 200 + assert rv_dict["id"] == 1 + assert "password" not in rv_dict + assert rv_dict["email"] == "test@test.se" + + # Try loggin with wrong PASSWORD + rv = client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc1234"})) + assert rv.status_code == 401 + + # Try loggin with wrong Email + rv = client.post("/api/users/login", data=json.dumps({"email": "test1@test.se", "password": "abc1234"})) + assert rv.status_code == 401 + + # Try loggin with right PASSWORD + rv = client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc123"})) + rv_dict = json.loads(rv.data.decode()) + assert rv.status_code == 200 + headers = {"Authorization": "Bearer " + rv_dict["access_token"]} + + # Get the current user + rv = client.get("/api/users/", headers=headers) + rv_dict = json.loads(rv.data.decode()) + assert rv.status_code == 200 + assert rv_dict["email"] == "test@test.se" + + rv = client.put("/api/users/", data=json.dumps({"name": "carl carlsson"}), headers=headers) + rv_dict = json.loads(rv.data.decode()) + assert rv.status_code == 200 + assert rv_dict["name"] == "Carl Carlsson" -- GitLab