From 2593da860266c31e95e46e931c1c171151252ba1 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi <256895@studenti.unimore.it> Date: Tue, 11 May 2021 00:16:02 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20bug=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/backend/nest_backend/test/conftest.py | 4 +-- .../test/fixtures/flask_client.py | 29 +++++++++---------- code/backend/nest_backend/test/test_user.py | 21 +++++++------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/code/backend/nest_backend/test/conftest.py b/code/backend/nest_backend/test/conftest.py index 1a6dcc4..8d964cd 100644 --- a/code/backend/nest_backend/test/conftest.py +++ b/code/backend/nest_backend/test/conftest.py @@ -4,6 +4,4 @@ Pytest configuration file. Import global fixtures here. """ -from fixtures.flask_client import flask_client, admin_access_token, admin_headers, user_access_token, user_headers, user_create - - +from fixtures.flask_client import flask_client, admin_access_token, admin_headers, user_access_token, user_headers, user_exists diff --git a/code/backend/nest_backend/test/fixtures/flask_client.py b/code/backend/nest_backend/test/fixtures/flask_client.py index 9d1edfd..27bfd1e 100644 --- a/code/backend/nest_backend/test/fixtures/flask_client.py +++ b/code/backend/nest_backend/test/fixtures/flask_client.py @@ -40,7 +40,7 @@ def flask_client(): ext.engine.execute(f"""DROP SCHEMA "{uniq_schema}" CASCADE;""") -@pytest.fixture() +@pytest.fixture(scope="package") def admin_access_token(flask_client): response = flask_client.post("/api/v1/login", json={ "email": "admin@admin.com", @@ -55,8 +55,17 @@ def admin_access_token(flask_client): return data["access_token"] -@pytest.fixture() -def user_access_token(flask_client): +@pytest.fixture(scope="package") +def user_exists(admin_headers, flask_client): + flask_client.post(f'/api/v1/users/', headers=admin_headers, json={ + 'email': 'utente_test@nest.com', + 'password': 'password', + 'username': 'utente_test' + }) + + +@pytest.fixture(scope="package") +def user_access_token(flask_client, user_exists): response = flask_client.post("/api/v1/login", json={ "email": "utente_test@nest.com", "password": "password" @@ -70,23 +79,13 @@ def user_access_token(flask_client): return data["access_token"] -@pytest.fixture() +@pytest.fixture(scope="package") def admin_headers(admin_access_token): admin_headers = {'Authorization': f"Bearer {admin_access_token}"} return admin_headers -@pytest.fixture() +@pytest.fixture(scope="package") def user_headers(user_access_token): user_headers = {'Authorization': f"Bearer {user_access_token}"} return user_headers - - -@pytest.fixture() -def user_create(admin_headers, flask_client): - r = flask_client.post(f'/api/v1/users/', headers=admin_headers, json={ - 'email': 'utente_test@nest.com', - 'password': 'password', - 'username': 'utente_test' - }) - diff --git a/code/backend/nest_backend/test/test_user.py b/code/backend/nest_backend/test/test_user.py index 4d4a81f..db8572e 100644 --- a/code/backend/nest_backend/test/test_user.py +++ b/code/backend/nest_backend/test/test_user.py @@ -4,20 +4,25 @@ from flask.testing import Client class TestUserGet: - def test_for_success(self, flask_client: Client, admin_headers): + def test_admin_user(self, flask_client: Client, admin_headers): r = flask_client.get(f'/api/v1/users/admin@admin.com', headers=admin_headers) - assert b'success' in r.data + assert r.json["result"] == "success" + assert r.json["data"]["email"] == "admin@admin.com" + assert r.json["data"]["isAdmin"] is True + assert r.json["data"]["username"] == "admin" + def test_non_existing_user(self, flask_client: Client, admin_headers): + r = flask_client.get(f'/api/v1/users/ciccio@dev.com', headers=admin_headers) + assert r.json["result"] == "failure" + assert r.json["msg"] == "Could not locate the user." # ritorna i dati di tutti gli utenti registrati class TestUserGetAll: - def test_for_success(self, flask_client: Client, admin_headers, user_create): + def test_for_success(self, flask_client: Client, admin_headers): r = flask_client.get(f'/api/v1/users/', headers=admin_headers) assert b'success' in r.data - # FIXME AssertionError in flask_client at line 63. Il test non riesce ad andare a buon fine def test_for_failure(self, flask_client: Client, user_headers): - r = flask_client.get(f'/api/v1/users/', headers=user_headers) assert b'failure' in r.data @@ -47,14 +52,12 @@ class TestUserDelete: # the admin tries to commit suicide def test_for_failure(self, flask_client: Client, admin_headers): - r = flask_client.delete(f'/api/v1/users/admin@admin.com', headers=admin_headers) assert b'failure' in r.data class TestUserPatch: - def test_for_success(self, flask_client: Client, admin_headers, user_create): - + def test_for_success(self, flask_client: Client, admin_headers): r = flask_client.patch(f'/api/v1/users/admin@admin.com', headers=admin_headers, json={ 'username': 'admin_patched' }) @@ -66,5 +69,3 @@ class TestUserPatch: 'username': 'admin_patched' }) assert b'failure' in r.data - -