Skip to content

Commit

Permalink
[OpenClassrooms-Student-Center#2] - user use more points than availab…
Browse files Browse the repository at this point in the history
…le - Tests Improvement

- tests: conftest.py: config fixture to avoid repetition in test_server.py
- UI: welcome.html: added a span to localize more efficiently the 'club_points_left'
  • Loading branch information
rafaRemote committed Dec 23, 2021
1 parent 3820986 commit 9ae5a14
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 58 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ lib
.envrc
__pycache__
env/
x.py
x.py
3 changes: 2 additions & 1 deletion templates/booking.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Booking for {{competition['name']}} || GUDLFT</title>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
</head>
<body>
<h2>{{competition['name']}}</h2>
Expand All @@ -16,7 +17,7 @@ <h2>{{competition['name']}}</h2>
</ul>
{% endif%}
{% endwith %}
<form action="/purchase-places" method="post">
<form action="/purchase-places" method="POST">
<input type="hidden" name="club_name" value="{{club['name']}}">
<input type="hidden" name="competition_name" value="{{competition['name']}}">
<label for="places">How many places?</label><input type="number" name="places" id="" min="1" max="{{club["points"]}}"/>
Expand Down
1 change: 1 addition & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GUDLFT Registration</title>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
</head>
<body>
<h1>Welcome to the GUDLFT Registration Portal!</h1>
Expand Down
1 change: 1 addition & 0 deletions templates/not_found_404.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Not found</title>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
</head>
<body>
<h2>Not found</h2>
Expand Down
5 changes: 3 additions & 2 deletions templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Summary | GUDLFT Registration</title>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
</head>
<body>
<h2>Welcome, {{club['email']}} </h2><a href="{{url_for('logout')}}">Logout</a>
Expand All @@ -14,8 +15,8 @@ <h2>Welcome, {{club['email']}} </h2><a href="{{url_for('logout')}}">Logout</a>
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif%}
Points Available: {{club['points']}}
{% endif %}
Points Available: <span id="club_points_left">{{club['points']}}</span>
<h3>Competitions:</h3>
<ul>
{% for comp in competitions%}
Expand Down
46 changes: 19 additions & 27 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
import datetime as dt
import server


Expand All @@ -9,6 +8,22 @@ def client():
yield client


@pytest.fixture
def club():
club = {"name": "test club 1", "email": "test1@test.com", "points": "100"}
return club


@pytest.fixture
def clubs():
clubs = [
{"name": "test club 1", "email": "test1@test.com", "points": "100"},
{"name": "test club 2", "email": "test2@test.com", "points": "20"},
{"name": "test club 3", "email": "test3@test.com", "points": "30"},
]
return clubs


@pytest.fixture
def competition():
competition = {
Expand Down Expand Up @@ -41,25 +56,6 @@ def competitions():
return competitions


@pytest.fixture
def past_competition():
last_year = str(int(dt.datetime.now().strftime("%Y")) - 1)
competition = [
{
"name": "test future competition",
"date": dt.datetime.now().strftime(f"%{last_year}-%m-%d %H:%M:%S"),
"numberOfPlaces": "100",
}
]
return competition


@pytest.fixture
def club():
club = {"name": "test club 1", "email": "test1@test.com", "points": "100"}
return club


@pytest.fixture
def unlisted_club():
unlisted_club = {
Expand All @@ -71,10 +67,6 @@ def unlisted_club():


@pytest.fixture
def clubs():
clubs = [
{"name": "test club 1", "email": "test1@test.com", "points": "100"},
{"name": "test club 2", "email": "test2@test.com", "points": "20"},
{"name": "test club 3", "email": "test3@test.com", "points": "30"},
]
return clubs
def config(mocker, clubs, competitions):
mocker.patch.object(server, "clubs", clubs)
mocker.patch.object(server, "competitions", competitions)
37 changes: 10 additions & 27 deletions tests/unit/test_server.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import random
import server
import pytest


Expand All @@ -22,12 +21,8 @@ def test_access_unauthenticated_user_should_405(


class TestLogin:
def test_login_listed_email_should_200(
self, client, mocker, club, clubs, competitions
):
def test_login_listed_email_should_200(self, client, club, config):
"""Checks response when authenticated user request"""
mocker.patch.object(server, "clubs", clubs)
mocker.patch.object(server, "competitions", competitions)
data = {"email": club["email"]}
response = client.post("/show-summary", data=data, follow_redirects=True)
assert response.status_code == 200
Expand All @@ -40,47 +35,34 @@ def test_login_listed_email_should_200(
({"email": "$%^"}, 404),
],
)
def test_login_unlisted_mails_should_404(
self, client, mocker, email, status_code, clubs, competitions
):
def test_login_unlisted_mails_should_404(self, client, email, status_code, config):
"""Checks response when unauthenticated user request"""
mocker.patch.object(server, "clubs", clubs)
mocker.patch.object(server, "competitions", competitions)
response = client.post("/show-summary", data=email, follow_redirects=True)
assert response.status_code == status_code

def test_login_bad_request(self, client, mocker, club, clubs, competitions):
"""Checks response when bad request"""
mocker.patch.object(server, "clubs", clubs)
mocker.patch.object(server, "competitions", competitions)
data = {"address": club["email"]}
response = client.post("/show-summary", data=data, follow_redirects=True)
assert response.status_code == 400


class TestPurchase:
def test_should_not_use_more_points_than_have(
self, client, mocker, club, competition, clubs, competitions
self, client, club, competition, config
):
"""Checks response when user try to use more points than his club has"""
mocker.patch.object(server, "clubs", clubs)
mocker.patch.object(server, "competitions", competitions)
places = int(competition["numberOfPlaces"]) + 1
data = {
"club_name": club["name"],
"competition_name": competition["name"],
"places": str(places),
"places": str(int(club["points"]) + 1),
}
response = client.post("/purchase-places", data=data, follow_redirects=True)
data = response.data.decode("utf-8").split()
data = response.data.decode()
assert "cannot" in data

def test_should_deduct_points_from_club(
self, client, mocker, club, competition, clubs, competitions
):
def test_should_deduct_points_from_club(self, client, club, competition, config):
"""Check club points in response after user book places"""
mocker.patch.object(server, "clubs", clubs)
mocker.patch.object(server, "competitions", competitions)
places = int(competition["numberOfPlaces"])
club_points = int(club["points"])
if places > 0:
Expand All @@ -95,6 +77,7 @@ def test_should_deduct_points_from_club(
"places": str(places_required),
}
response = client.post("/purchase-places", data=data, follow_redirects=True)
data = response.data.decode("utf-8").split()
club_points_left = int(data[(data.index("Available:") + 1)])
assert club_points_left == club_points - places_required
data = response.data.decode().split()
chunk = [i for i in data if "club_points_left" in i][0]
points_left = int("".join([i for i in chunk if i.isdigit()]))
assert points_left == club_points - places_required

0 comments on commit 9ae5a14

Please sign in to comment.