-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task(delete-schedule): deleta grades do usuário e altera rotas (#151)
* utils(db-handler): add delete_schedule function * api(delete-schedules): delete schedule endpoint * test(deleteschedule): add test for delete schedule * test(delete-schedule): add test for invalid token * Delete api/api/tests/test_schedules_api.py * django(api): rename and delete useless files * api(urls): update urls * utils(test): update test urls * api(save_schedule): remove auth save_schedule * api(views): add schedule view * api(views): rename view generate schedule * fix(typo): nome da classe p/ testar delete --------- Co-authored-by: Mateus Vieira <[email protected]>
- Loading branch information
1 parent
16813ab
commit 25375c9
Showing
11 changed files
with
187 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer | ||
from rest_framework.test import APITestCase | ||
from rest_framework.reverse import reverse | ||
|
||
import utils.db_handler as dbh | ||
|
||
from users.models import User | ||
|
||
import json | ||
|
||
|
||
class TestDeleteSchedules(APITestCase): | ||
def setUp(self): | ||
self.department = dbh.get_or_create_department('518', '2023', '2') | ||
self.discipline = dbh.get_or_create_discipline( | ||
'CÁLCULO 1', 'MAT0025', self.department) | ||
self._class = dbh.create_class(['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.discipline) | ||
|
||
body = json.dumps( | ||
{ | ||
"classes": [self._class.id] | ||
} | ||
) | ||
|
||
self.user, _ = User.objects.get_or_create( | ||
first_name='Aroldo', | ||
last_name='Silva', | ||
picture_url='https://www.photo.com', | ||
email="[email protected]" | ||
) | ||
self.user.save() | ||
|
||
tokens = TokenObtainPairSerializer.get_token(self.user) | ||
self.access_token = tokens.access_token | ||
|
||
self.url = "api:delete-schedule" | ||
self.content_type = 'application/json' | ||
|
||
self.schedules = self.client.post( | ||
reverse('api:generate-schedules'), body, content_type=self.content_type).data | ||
|
||
self.schedule_json = json.dumps(self.schedules[0]) | ||
|
||
self.headers = { | ||
'Authorization': 'Bearer ' + str(self.access_token) | ||
} | ||
|
||
|
||
def save_schedule(self): | ||
self.client.post(reverse('api:schedules'), | ||
self.schedule_json, content_type=self.content_type, headers=self.headers) | ||
|
||
def get_user_schedules(self): | ||
return self.client.get( | ||
reverse('api:schedules'), headers=self.headers) | ||
|
||
def test_delete_non_existent_schedule(self): | ||
""" | ||
Testa a deleção de uma grade horária que não existe | ||
""" | ||
|
||
content = self.client.delete( | ||
reverse(self.url, kwargs={'id': 1}), headers=self.headers) | ||
|
||
self.assertEqual(content.status_code, 400) | ||
|
||
def test_delete_schedule(self): | ||
""" | ||
Testa a deleção de uma grade horária | ||
""" | ||
|
||
self.save_schedule() | ||
|
||
user_schedules = self.get_user_schedules().data | ||
schedule_id = user_schedules[0].get('id') | ||
|
||
content = self.client.delete( | ||
reverse(self.url, kwargs={'id': schedule_id}), headers=self.headers) | ||
|
||
self.assertEqual(content.status_code, 204) | ||
self.assertEqual(len(self.get_user_schedules().data), 0) | ||
|
||
def test_delete_schedule_with_invalid_token(self): | ||
""" | ||
Testa a deleção de uma grade horária com um token inválido | ||
""" | ||
|
||
content = self.client.delete( | ||
reverse(self.url, kwargs={'id': 12}), headers={'Authorization': 'Bearer ' + str('invalid_token')}) | ||
|
||
self.assertEqual(content.status_code, 403) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
from django.urls import path | ||
from api.views import save_schedule, get_schedules, views | ||
from api.views import schedules, delete_schedule, views | ||
|
||
app_name = 'api' | ||
|
||
urlpatterns = [ | ||
path('', views.Search.as_view(), name="search"), | ||
path('year-period/', views.YearPeriod.as_view(), name="year-period"), | ||
path('schedule/save/', save_schedule.SaveSchedule.as_view(), name="save-schedule"), | ||
path('schedule/', views.Schedule.as_view(), name="schedule"), | ||
path('schedules/', get_schedules.GetSchedules.as_view(), name="get-schedules") | ||
path('schedules/', schedules.Schedules.as_view(), name="schedules"), | ||
path('schedules/<int:id>/', delete_schedule.DeleteSchedule.as_view(), name="delete-schedule"), | ||
path('schedules/generate/', views.GenerateSchedule.as_view(), name="generate-schedules"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from utils import db_handler as dbh | ||
|
||
from drf_yasg.utils import swagger_auto_schema | ||
from drf_yasg import openapi | ||
|
||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.views import APIView | ||
from rest_framework import status, request, response | ||
|
||
from api.swagger import Errors | ||
|
||
|
||
class DeleteSchedule(APIView): | ||
|
||
permission_classes = [IsAuthenticated] | ||
|
||
@swagger_auto_schema( | ||
operation_description="Deleta a grade horária do usuário logado.", | ||
security=[{'Bearer': []}], | ||
responses={ | ||
204: openapi.Response('NO CONTENT'), | ||
**Errors([400, 401, 403]).retrieve_erros() | ||
} | ||
) | ||
def delete(self, request: request.Request, id: int) -> response.Response: | ||
"""Delete a grade horária do usuário logado.""" | ||
user = request.user | ||
|
||
return response.Response(status=status.HTTP_204_NO_CONTENT) if dbh.delete_schedule(user, id) else response.Response(status=status.HTTP_400_BAD_REQUEST) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from utils.schedule_generator import ScheduleGenerator | ||
from utils import db_handler as dbh | ||
|
||
from drf_yasg.utils import swagger_auto_schema | ||
from drf_yasg import openapi | ||
|
||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.views import APIView | ||
from rest_framework import status, request, response | ||
|
||
from api.models import Class | ||
from api.swagger import Errors | ||
from api.views.utils import handle_400_error | ||
from api import serializers | ||
|
||
from .save_schedule import SaveSchedule | ||
|
||
class Schedules(APIView, SaveSchedule): | ||
permission_classes = [IsAuthenticated] | ||
|
||
def get_permissions(self): | ||
""" | ||
Instancia e retorna a lista de permissões que serão usadas por esta view. | ||
""" | ||
if self.request.method in ['GET', 'POST']: | ||
return [permission() for permission in self.permission_classes] | ||
else: # pragma: no cover | ||
return [] | ||
|
||
@swagger_auto_schema( | ||
operation_description="Retorna as grades horárias do usuário logado.", | ||
security=[{'Bearer': []}], | ||
responses={ | ||
200: openapi.Response('OK', serializers.ScheduleSerializer(many=True)), | ||
**Errors([401, 403]).retrieve_erros() | ||
} | ||
) | ||
def get(self, request: request.Request, **kwargs) -> response.Response: | ||
"""Retorna as grades horárias do usuário logado.""" | ||
|
||
user = request.user | ||
schedules = dbh.get_schedules(user) | ||
data = serializers.ScheduleSerializer(schedules, many=True).data | ||
|
||
return response.Response(status=status.HTTP_200_OK, data=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters