Skip to content

Commit

Permalink
Merge pull request #149 from tdameros/148-delete-registration-deadlin…
Browse files Browse the repository at this point in the history
…e-in-tournament

Remove registration deadline in tournament
  • Loading branch information
EthanDelage authored Feb 3, 2024
2 parents afe9940 + 79b849b commit 5319547
Show file tree
Hide file tree
Showing 10 changed files with 3 additions and 155 deletions.
5 changes: 0 additions & 5 deletions tournament/doc/tournament-api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Create a new tournament

- Tournament name must be between 3 and 20 characters and can only contain alnum and space
- Players must be between 2 and 16 (optional, default = 16 players)
- Registration deadline (optional)
- Nickname for the tournament (optional)
- A boolean that specifies if tournament is private
- A password for the tournament (if is-private is true)
Expand All @@ -53,7 +52,6 @@ Create a new tournament
> {
> "name": "World Championship",
> "max-players": 16,
> "registration-deadline": "2024-02-17T10:53",
> "is-private": true,
> "password": "Password1%"
> "nickname": "Player"
Expand Down Expand Up @@ -119,7 +117,6 @@ Body
> "user_id": 2
> }
> ],
> "registration-deadline": "2024-02-17T10:53",
> "is-private": true,
> "status": "created",
> "admin": "edelage",
Expand Down Expand Up @@ -161,15 +158,13 @@ Update tournament settings
- Tournament name must be between 3 and 20 characters and can only contain alnum and space (optional)
- Players must be between 2 and 16 (optional)
- Registration deadline (optional)
- A boolean that specifies if tournament is private (optional)
- A password for the tournament (optional)
> ```javascript
> {
> "name": "World Championship",
> "max-players": 16,
> "registration-deadline": "2024-02-17T10:53",
> "is-private": true,
> "password": "Password1%"
> }
Expand Down
3 changes: 0 additions & 3 deletions tournament/src/api/error_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
TOO_MANY_SLOTS = f'Tournament must contain less or equal than {settings.MAX_PLAYERS} slots'
NOT_ENOUGH_SLOTS = f'Tournament must contain at least {settings.MIN_PLAYERS} slots'

NOT_ISO_8601 = 'Registration deadline not in ISO 8601 date and time format'
DEADLINE_PASSED = 'Registration deadline has passed'

IS_PRIVATE_MISSING = 'Missing is-private field'
IS_PRIVATE_NOT_BOOL = 'Is-private must be a boolean'

Expand Down
1 change: 0 additions & 1 deletion tournament/src/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class Tournament(models.Model):

name = models.CharField(max_length=settings.MAX_TOURNAMENT_NAME_LENGTH)
max_players = models.IntegerField(default=16, blank=True)
registration_deadline = models.DateTimeField(blank=True, null=True)
is_private = models.BooleanField(default=False)
status = models.IntegerField(default=CREATED)
admin_id = models.BigIntegerField(default=0)
Expand Down
26 changes: 1 addition & 25 deletions tournament/src/api/tests/test_manage_tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GetTournamentTest(TestCase):
def setUp(self):
Tournament.objects.create(id=1, name='Test1', admin_id=1)
Tournament.objects.create(id=2, name='Test2', admin_id=2)
Tournament.objects.create(id=3, name='Test3', admin_id=3, registration_deadline='2020-01-01T00:00:00Z')
Tournament.objects.create(id=3, name='Test3', admin_id=3)
Tournament.objects.create(id=4, name='finished', status=Tournament.FINISHED, admin_id=4)

for i in range(1, 14):
Expand Down Expand Up @@ -49,30 +49,6 @@ def test_get_tournament(self, mock_authenticate_request, mock_get_username_by_id
'user-id': i
} for i in range(1, 14)])

@patch('api.views.manage_tournament_views.get_username_by_id')
@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_get_tournament_with_deadline(self, mock_authenticate_request, mock_get_username_by_id):
user = {'id': 1, 'username': 'admin'}
mock_authenticate_request.return_value = (True, user, None)
mock_get_username_by_id.return_value = user['username']
response, body = self.get_tournament(3)

self.assertEqual(response.status_code, 200)
self.assertEqual(body['id'], 3)
self.assertEqual(body['name'], 'Test3')
self.assertEqual(body['max-players'], 16)
self.assertEqual(body['nb-players'], 2)
self.assertEqual(body['is-private'], False)
self.assertEqual(body['status'], 'Created')
self.assertEqual(body['registration-deadline'], '2020-01-01T00:00:00Z')
self.assertEqual(body['players'], [
{
'nickname': f'Player{i}',
'rank': None,
'user-id': i
} for i in range(1, 3)
])

@patch('api.views.manage_tournament_views.get_username_by_id')
@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_get_tournament_not_found(self, mock_authenticate_request, mock_get_username_by_id):
Expand Down
32 changes: 0 additions & 32 deletions tournament/src/api/tests/test_tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ def test_tournament_creation(self, mock_get):
data = {
'name': 'World Championship',
'max-players': 16,
'registration-deadline': '2027-02-17T10:53:00Z',
'is-private': False
}

Expand All @@ -250,7 +249,6 @@ def test_tournament_creation(self, mock_get):
self.assertEqual(response.status_code, 201)
self.assertEqual(body['name'], data['name'])
self.assertEqual(body['max_players'], data['max-players'])
self.assertEqual(body['registration_deadline'], data['registration-deadline'])
self.assertEqual(body['is_private'], data['is-private'])
self.assertEqual(body['admin_id'], 1)

Expand All @@ -262,7 +260,6 @@ def test_private_tournament_creation(self, mock_get):
data = {
'name': 'World Championship',
'max-players': 16,
'registration-deadline': '2027-02-17T10:53:00Z',
'is-private': True,
'password': 'test'
}
Expand All @@ -276,7 +273,6 @@ def test_private_tournament_creation(self, mock_get):
self.assertEqual(response.status_code, 201)
self.assertEqual(body['name'], data['name'])
self.assertEqual(body['max_players'], data['max-players'])
self.assertEqual(body['registration_deadline'], data['registration-deadline'])
self.assertEqual(body['is_private'], data['is-private'])
self.assertEqual(body['admin_id'], 1)
self.assertEqual(len(players), 0)
Expand All @@ -300,7 +296,6 @@ def test_create_tournament_auto_subscribe(self, mock_get):
self.assertEqual(response.status_code, 201)
self.assertEqual(body['name'], data['name'])
self.assertEqual(body['max_players'], data['max-players'])
self.assertEqual(body['registration_deadline'], None)
self.assertEqual(body['is_private'], data['is-private'])
self.assertEqual(body['admin_id'], 1)
self.assertEqual(len(players), 1)
Expand All @@ -315,7 +310,6 @@ def test_tournament_different_timezone(self, mock_get):
data = {
'name': 'World Championship',
'max-players': 16,
'registration-deadline': '2027-01-06T07:38:51-07:00',
'is-private': False
}

Expand All @@ -326,7 +320,6 @@ def test_tournament_different_timezone(self, mock_get):
self.assertEqual(response.status_code, 201)
self.assertEqual(body['name'], data['name'])
self.assertEqual(body['max_players'], data['max-players'])
self.assertEqual(body['registration_deadline'], '2027-01-06T14:38:51Z')
self.assertEqual(body['is_private'], data['is-private'])
self.assertEqual(body['admin_id'], 1)

Expand All @@ -347,7 +340,6 @@ def test_tournament_without_optional_info(self, mock_get):
self.assertEqual(response.status_code, 201)
self.assertEqual(body['name'], data['name'])
self.assertEqual(body['max_players'], settings.MAX_PLAYERS)
self.assertEqual(body['registration_deadline'], None)
self.assertEqual(body['is_private'], data['is-private'])
self.assertEqual(body['admin_id'], 1)

Expand Down Expand Up @@ -428,30 +420,6 @@ def test_invalid_type(self, mock_get):

self.send_tournament_bad_request(mock_get, data, expected_errors)

@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_invalid_iso_8601(self, mock_get):
expected_errors = [error.NOT_ISO_8601]

data = {
'name': 'Test',
'is-private': False,
'registration-deadline': '2030-07-1222:30:00'
}

self.send_tournament_bad_request(mock_get, data, expected_errors)

@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_passed_deadline(self, mock_get):
expected_errors = [error.DEADLINE_PASSED]

data = {
'name': 'Test',
'is-private': False,
'registration-deadline': '2022-07-12T00:00:00Z'
}

self.send_tournament_bad_request(mock_get, data, expected_errors)

@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_name_too_long(self, mock_get):
expected_errors = [error.NAME_TOO_LONG]
Expand Down
13 changes: 0 additions & 13 deletions tournament/src/api/tests/test_tournament_players.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def setUp(self):
name='deadline passed',
admin_id=1,
max_players=16,
registration_deadline='2021-01-01 00:00:00+00:00'
)
Tournament.objects.create(
id=4,
Expand Down Expand Up @@ -196,18 +195,6 @@ def test_invalid_tournament(self, mock_authenticate_request):
self.assertEqual(response.status_code, 404)
self.assertEqual(body['errors'], [f'tournament with id `{tournament_id}` does not exist'])

@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_deadline_passed(self, mock_authenticate_request):
user = {'id': 1}
mock_authenticate_request.return_value = (True, user, None)
tournament_id = 3
body = json.dumps({'nickname': 'player 1'})

response, body = self.post_tournament_players(tournament_id, body)

self.assertEqual(response.status_code, 403)
self.assertEqual(body['errors'], ['The registration phase is over'])

@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_already_register_to_another_tournament(self, mock_authenticate_request):
user = {'id': 1}
Expand Down
25 changes: 1 addition & 24 deletions tournament/src/api/tests/test_update_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PatchTournamentTest(TestCase):
def setUp(self):
Tournament.objects.create(id=1, name='Test1', admin_id=1)
Tournament.objects.create(id=2, name='Test2', admin_id=1)
Tournament.objects.create(id=3, name='Test3', admin_id=1, registration_deadline='2028-01-01T00:00:00Z')
Tournament.objects.create(id=3, name='Test3', admin_id=1)
Tournament.objects.create(id=4, name='in_progress', status=Tournament.IN_PROGRESS, admin_id=1)
Tournament.objects.create(id=5, name='finished', status=Tournament.FINISHED, admin_id=1)
Tournament.objects.create(id=6, name='8players', max_players=14, admin_id=1)
Expand Down Expand Up @@ -133,29 +133,6 @@ def test_patch_invalid_name(self, mock_authenticate_request):
self.assertEqual(response.status_code, 400)
self.assertEqual(body['errors'], [error.NAME_INVALID_CHAR])

@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_patch_passed_deadline(self, mock_authenticate_request):
user = {'id': 1, 'username': 'admin'}
mock_authenticate_request.return_value = (True, user, None)
response, body = self.patch_tournament(3, {'registration-deadline': '2022-01-01T00:00:00Z'})

self.assertEqual(response.status_code, 400)
self.assertEqual(body['errors'], [error.DEADLINE_PASSED])

@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_patch_deadline(self, mock_authenticate_request):
user = {'id': 1, 'username': 'admin'}
mock_authenticate_request.return_value = (True, user, None)
response, body = self.patch_tournament(3, {'registration-deadline': '2029-01-01T00:00:00Z'})

self.assertEqual(response.status_code, 200)
self.assertEqual(body['id'], 3)
self.assertEqual(body['name'], 'Test3')
self.assertEqual(body['max-players'], 16)
self.assertEqual(body['is-private'], False)
self.assertEqual(body['status'], 'Created')
self.assertEqual(body['registration-deadline'], '2029-01-01T00:00:00Z')

@patch('common.src.jwt_managers.UserAccessJWTDecoder.authenticate')
def test_patch_no_password(self, mock_get):
user = {'id': 1, 'username': 'admin'}
Expand Down
18 changes: 0 additions & 18 deletions tournament/src/api/views/manage_tournament_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ def get(request: HttpRequest, tournament_id: int) -> JsonResponse:
except Exception as e:
return JsonResponse({'errors': [str(e)]}, status=500)

if tournament.registration_deadline is not None:
tournament_data['registration-deadline'] = tournament.registration_deadline

return JsonResponse(tournament_data, status=200)

@staticmethod
Expand Down Expand Up @@ -180,16 +177,13 @@ def update_tournament_settings(body: dict, tournament: Tournament, tournament_pl

new_name_errors = ManageTournamentView.update_tournament_name(body, tournament)
new_max_players_error = ManageTournamentView.update_max_players(body, tournament, tournament_players)
new_deadline_error = ManageTournamentView.update_registration_deadline(body, tournament)
new_is_private_error = ManageTournamentView.update_is_private(body, tournament)
new_password_error = ManageTournamentView.update_password(body, tournament)

if new_name_errors is not None:
update_errors.extend(new_name_errors)
if new_max_players_error is not None:
update_errors.append(new_max_players_error)
if new_deadline_error is not None:
update_errors.append(new_deadline_error)
if new_is_private_error is not None:
update_errors.append(new_is_private_error)
if new_password_error is not None:
Expand Down Expand Up @@ -220,17 +214,6 @@ def update_max_players(body: dict, tournament: Tournament, tournament_players) -
tournament.max_players = new_max_players
return None

@staticmethod
def update_registration_deadline(body: dict, tournament: Tournament) -> Optional[str]:
new_deadline = body.get('registration-deadline')
if new_deadline is not None:
valid_registration_deadline, registration_deadline_error = TournamentView.is_valid_deadline(new_deadline)
if not valid_registration_deadline:
return registration_deadline_error
else:
tournament.registration_deadline = new_deadline
return None

@staticmethod
def update_is_private(body: dict, tournament: Tournament) -> Optional[str]:
new_is_private = body.get('is-private')
Expand Down Expand Up @@ -270,7 +253,6 @@ def get_tournament_data(tournament: Tournament) -> dict[str, Any]:
'name': tournament.name,
'max-players': tournament.max_players,
'is-private': tournament.is_private,
'registration-deadline': tournament.registration_deadline,
'status': TournamentView.status_to_string(tournament.status),
}
return tournament_data
5 changes: 1 addition & 4 deletions tournament/src/api/views/tournament_players_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from datetime import datetime, timezone
from typing import Optional

from django.contrib.auth.hashers import check_password
Expand Down Expand Up @@ -134,9 +133,7 @@ def player_can_join_tournament(new_player: Player, password: Optional[str], tour
except Exception as e:
return False, [f'An unexpected error occurred : {e}', 500]

if tournament.status != Tournament.CREATED or (
tournament.registration_deadline is not None
and tournament.registration_deadline < datetime.now(timezone.utc)):
if tournament.status != Tournament.CREATED:
return False, ['The registration phase is over', 403]

if tournament.is_private and password is None:
Expand Down
Loading

0 comments on commit 5319547

Please sign in to comment.