Skip to content

Commit

Permalink
task(api/save): limita a quantidade de grades que o usuário pode salv…
Browse files Browse the repository at this point in the history
…ar (#156)

* api(search): Aumenta o número de disciplinas retornadas na busca
- Agora a api retorna até 15 disciplinas por busca

* api(save_schedule): Define limite de grades que podem ser salvas
- O usuário só pode salvar até 10 grades

* api(save_schedule): adiciona testes para limite de salvamento de grades
- Foram feitas modificações necessárias na classe TestScheduleSaveAPI de teste para tornar o teste funcional
- Foram adicionados testes para verificar se o limite de salvamento de grades está funcionando corretamente
- Adicionado mensagem de erro para quando o limite for atingido
  • Loading branch information
GabrielCastelo-31 authored Dec 9, 2023
1 parent 4be57e1 commit f2eeafb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 10 deletions.
70 changes: 60 additions & 10 deletions api/api/tests/test_schedule_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

from ..views.save_schedule import SCHEDULES_LIMIT, SCHEDULES_LIMIT_ERROR_MSG

from utils import db_handler as dbh

from api.serializers import ClassSerializerSchedule
Expand Down Expand Up @@ -43,16 +45,16 @@ def setClassInfos(self):
(['LUIS FILOMENO DE JESUS FERNANDES'], 'FGA - Sala I6', '46M34',
['Quarta-feira 10:00 às 11:50', 'Sexta-feira 10:00 às 11:50'],
'2', [], self.disciplines['discipline_FGA0003_2023_2']),
(['RICARDO RAMOS FRAGELLI'], 'FGA - I9', '246M34',
['Segunda-feira 10:00 às 11:50', 'Quarta-feira 10:00 às 11:50',
'Sexta-feira 10:00 às 11:50'], '24', [],
self.disciplines['discipline_MAT0025_2023_2']),
(['EDSON ALVES DA COSTA JUNIOR'], 'FGA - I8', '35T23',
['Terça-feira 14:00 às 15:50', 'Quinta-feira 14:00 às 15:50'],
'1', [], self.disciplines['discipline_FGA0003_2024_1']),
(['LUIS FILOMENO DE JESUS FERNANDES'], 'FGA - Sala I6', '46M34',
['Quarta-feira 10:00 às 11:50', 'Sexta-feira 10:00 às 11:50'],
'2', [], self.disciplines['discipline_FGA0003_2024_1']),
(['RICARDO RAMOS FRAGELLI'], 'FGA - I9', '246M34',
['Segunda-feira 10:00 às 11:50', 'Quarta-feira 10:00 às 11:50',
'Sexta-feira 10:00 às 11:50'], '24', [],
self.disciplines['discipline_MAT0025_2023_2']),
(['RICARDO RAMOS FRAGELLI'], 'FGA - I9', '246M34',
['Segunda-feira 10:00 às 11:50', 'Quarta-feira 10:00 às 11:50',
'Sexta-feira 10:00 às 11:50'], '24', [],
Expand All @@ -61,6 +63,23 @@ def setClassInfos(self):
['Segunda-feira 10:00 às 11:50', 'Quarta-feira 10:00 às 11:50',
'Quinta-feira 14:00 às 15:50'], '25', [],
self.disciplines['discipline_MAT0025_2024_1']),
(['VINICIUS RISPOLI'], 'FGA - I9', '245M34',
['Segunda-feira 10:00 às 11:50', 'Quarta-feira 10:00 às 11:50',
'Quinta-Feira 10:00 às 11:50'], '24', [],
self.disciplines['discipline_MAT0025_2024_1']),
(['MATEUS VIEIRA ROCHA'], 'FGA - I9', '236M34',
['Segunda-feira 10:00 às 11:50', 'Terça-feira 10:00 às 11:50',
'Sexta-feira 10:00 às 11:50'], '24', [],
self.disciplines['discipline_MAT0025_2024_1']),
(['LUIZA YOKO'], 'FGA - I9', '346M34',
['Terça-feira 10:00 às 11:50', 'Quarta-feira 10:00 às 11:50',
'Sexta-feira 10:00 às 11:50'], '24', [],
self.disciplines['discipline_MAT0025_2024_1']),
(['LUIZA YOKO'], 'FGA - I9', '456M34',
['Quarta-feira 10:00 às 11:50', 'Quinta-feira 10:00 às 11:50',
'Sexta-feira 10:00 às 11:50'], '24', [],
self.disciplines['discipline_MAT0025_2024_1']),

]

def generate_schedule_structure(self, classes: list[Class]) -> list:
Expand Down Expand Up @@ -147,11 +166,9 @@ def test_save_correct_schedule(self):
"""
schedule = self.generate_schedule_structure([
self.classes['class_0_2023_2'],
self.classes['class_4_2023_2']
self.classes['class_2_2023_2']
])

response = self.make_post_request(schedule=schedule)

self.assertEqual(len(self.user.schedules.all()), 1)
self.assertEqual(response.status_code, 201)

Expand Down Expand Up @@ -206,7 +223,7 @@ def test_save_incorrect_schedule_with_compatible_classes(self):
- Status code (400 BAD REQUEST)
"""
schedule = self.generate_schedule_structure([
self.classes['class_2_2024_1'],
self.classes['class_3_2024_1'],
self.classes['class_6_2024_1']
])

Expand All @@ -225,7 +242,7 @@ def test_save_correct_schedule_without_auth(self):
"""
schedule = self.generate_schedule_structure([
self.classes['class_0_2023_2'],
self.classes['class_4_2023_2']
self.classes['class_2_2023_2']
])

response = self.make_post_request(auth=False, schedule=schedule)
Expand All @@ -241,10 +258,43 @@ def test_save_correct_schedule_with_incorrect_auth_token(self):
"""
schedule = self.generate_schedule_structure([
self.classes['class_0_2023_2'],
self.classes['class_4_2023_2']
self.classes['class_2_2023_2']
])

token = 'incorrect_token'
response = self.make_post_request(auth=token, schedule=schedule)

self.assertEqual(response.status_code, 403)

def test_save_limit_reached(self):
"""
Testa o salvamento de uma grade horária quando o limite de grades horárias é atingido.
Salva todas as grades horárias possíveis e tenta salvar mais uma.
Tests:
- Status code (400 BAD REQUEST)
"""

for i in range(3):
schedule = self.generate_schedule_structure([
self.classes[f'class_{i}_2023_2']
])

response = self.make_post_request(schedule=schedule)
self.assertEqual(response.status_code, 201)

for i in range(3, SCHEDULES_LIMIT):
schedule = self.generate_schedule_structure([
self.classes[f'class_{i}_2024_1']
])
response = self.make_post_request(schedule=schedule)
self.assertEqual(response.status_code, 201)

schedule = self.generate_schedule_structure([
self.classes[f'class_{SCHEDULES_LIMIT-1}_2024_1']
])

response = self.make_post_request(schedule=schedule)
self.assertEqual(response.data.get('errors'),
SCHEDULES_LIMIT_ERROR_MSG)
self.assertEqual(response.status_code, 400)
14 changes: 14 additions & 0 deletions api/api/views/save_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from api.views.utils import handle_400_error
from api import serializers

SCHEDULES_LIMIT = 10
SCHEDULES_LIMIT_ERROR_MSG = f"you have reached the limit of {SCHEDULES_LIMIT} schedules"


class SaveSchedule():
@swagger_auto_schema(
Expand All @@ -23,6 +26,9 @@ class SaveSchedule():
}
)
def post(self, request: request.Request, *args, **kwargs) -> response.Response:
if not check_permission_to_save(request.user):
return handle_400_error(SCHEDULES_LIMIT_ERROR_MSG)

classes = request.data

try:
Expand Down Expand Up @@ -204,3 +210,11 @@ def validate_received_schedule(classes_id: list[int]) -> list[Class]:
raise ValueError("the classes are not compatible")

return schedules[0]


def check_permission_to_save(user) -> bool:
schedules = dbh.get_schedules(user)

if len(schedules) >= SCHEDULES_LIMIT:
return False
return True

0 comments on commit f2eeafb

Please sign in to comment.