diff --git a/backend/test/conftest.py b/backend/test/conftest.py deleted file mode 100644 index 58bdd70a..00000000 --- a/backend/test/conftest.py +++ /dev/null @@ -1,67 +0,0 @@ -import pytest -import json -from flask import url_for, current_app - -from app import create_app -from core.models import TTokens - -@pytest.fixture() -def app(): - app = create_app() - app.config.update({"TESTING": True}) - - # other setup can go here - - yield app - - # clean up / reset resources here - - -@pytest.fixture() -def client(app): - return app.test_client() - - -@pytest.fixture() -def runner(app): - return app.test_cli_runner() - - -mimetype = "application/json" -headers = {"Content-Type": mimetype, "Accept": mimetype} - - -def get_token(client): - data = {"email": EMAIL} - - response = client.post( - url_for("app_routes.send_login_email"), data=json.dumps(data), headers=headers - ) - assert response.status_code == 204 - - # Get token manually - token = ( - TTokens.query.filter_by(used=False) - .filter_by(email=EMAIL) - .order_by(TTokens.created_at.desc()) - .first() - ) - - response = client.post( - url_for("app_routes.login"), - data=json.dumps({"login_token": token.token}), - headers=headers, - ) - assert response.status_code == 200 - rdata = json.loads(response.data) - assert rdata["is_admin"] == True - - -def post_json(client, url, json_dict): - """Send dictionary json_dict as a json to the specified url""" - return client.post(url, data=json.dumps(json_dict), content_type="application/json") - - -def json_of_response(response): - """Decode json from response""" - return json.loads(response.data.decode("utf8")) diff --git a/backend/test/fixtures.py b/backend/test/fixtures.py index 7fc1bb5f..8bf25aa6 100644 --- a/backend/test/fixtures.py +++ b/backend/test/fixtures.py @@ -1,11 +1,15 @@ import pytest +import json + from datetime import date from flask import url_for from sqlalchemy.sql import text -from core.models import TTokens, GTEvents +from app import create_app +from core.models import TTokens, GTEvents from core.env import db + headers = {"Content-type": "application/json", "Accept": "application/json"} events_data = [ @@ -19,11 +23,72 @@ "x": 765227.4922990737, "y": 6365673.938623513, "published_fr": True, - "published_en" : True + "published_en": True, } ] +ADMIN_EMAIL = "test.test@test.fr" + + +@pytest.fixture() +def app(): + app = create_app() + app.config.update({"TESTING": True, "ADMIN_EMAILS": [ADMIN_EMAIL]}) + + # other setup can go here + + yield app + + # clean up / reset resources here + + +@pytest.fixture() +def client(app): + return app.test_client() + + +@pytest.fixture() +def runner(app): + return app.test_cli_runner() + + +def get_token(client): + data = {"email": ADMIN_EMAIL} + + response = client.post( + url_for("app_routes.send_login_email"), data=json.dumps(data), headers=headers + ) + assert response.status_code == 204 + + # Get token manually + token = ( + TTokens.query.filter_by(used=False) + .filter_by(email=ADMIN_EMAIL) + .order_by(TTokens.created_at.desc()) + .first() + ) + + response = client.post( + url_for("app_routes.login"), + data=json.dumps({"login_token": token.token}), + headers=headers, + ) + assert response.status_code == 200 + rdata = json.loads(response.data) + assert rdata["is_admin"] == True + + +def post_json(client, url, json_dict): + """Send dictionary json_dict as a json to the specified url""" + return client.post(url, data=json.dumps(json_dict), content_type="application/json") + + +def json_of_response(response): + """Decode json from response""" + return json.loads(response.data.decode("utf8")) + + @pytest.fixture(scope="function") def events(): with db.session.begin_nested(): diff --git a/backend/test/test_api.py b/backend/test/test_api.py index 4926f7b0..21b59f7b 100644 --- a/backend/test/test_api.py +++ b/backend/test/test_api.py @@ -1,5 +1,5 @@ from flask import url_for, current_app -from .conftest import get_token, json_of_response, post_json + import pytest import json import logging @@ -7,11 +7,19 @@ from core.models import GTEvents, TReservations, TAnimationsBilans from core.models import TTokens -from .fixtures import events +from .fixtures import ( + events, + ADMIN_EMAIL, + get_token, + json_of_response, + post_json, + app, + headers, +) LOGGER = logging.getLogger(__name__) -EMAIL='test.test@test.fr' + TEST_RESERVATION = { "nom": "BLAIR", "prenom": "Eric", @@ -35,11 +43,9 @@ "nb_plus_12_ans": 3, } -headers = {"Content-type": "application/json", "Accept": "application/json"} - def login(client): - data = {"email": EMAIL} + data = {"email": ADMIN_EMAIL} client.post( url_for("app_routes.send_login_email"), data=json.dumps(data), headers=headers @@ -47,7 +53,7 @@ def login(client): # Get token manually token = ( TTokens.query.filter_by(used=False) - .filter_by(email=EMAIL) + .filter_by(email=ADMIN_EMAIL) .order_by(TTokens.created_at.desc()) .first() ) @@ -61,7 +67,7 @@ def login(client): @pytest.mark.usefixtures("client_class") class TestAPI: def test_login(self): - data = {"email": EMAIL} + data = {"email": ADMIN_EMAIL} response = self.client.post( url_for("app_routes.send_login_email"), @@ -73,7 +79,7 @@ def test_login(self): # Get token manually token = ( TTokens.query.filter_by(used=False) - .filter_by(email=EMAIL) + .filter_by(email=ADMIN_EMAIL) .order_by(TTokens.created_at.desc()) .first() )