Skip to content

Commit

Permalink
Clean tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amandine-sahl committed Jun 27, 2023
1 parent 154364a commit 87fa50b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 78 deletions.
67 changes: 0 additions & 67 deletions backend/test/conftest.py

This file was deleted.

69 changes: 67 additions & 2 deletions backend/test/fixtures.py
Original file line number Diff line number Diff line change
@@ -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 = [
Expand All @@ -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():
Expand Down
24 changes: 15 additions & 9 deletions backend/test/test_api.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
from flask import url_for, current_app
from .conftest import get_token, json_of_response, post_json

import pytest
import json
import logging

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",
Expand All @@ -35,19 +43,17 @@
"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
)
# 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()
)
Expand All @@ -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"),
Expand All @@ -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()
)
Expand Down

0 comments on commit 87fa50b

Please sign in to comment.