Skip to content

Commit

Permalink
Added tests for API of user app
Browse files Browse the repository at this point in the history
  • Loading branch information
EXG1O committed Jan 22, 2024
1 parent bd997b7 commit e6b0e40
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 11 deletions.
6 changes: 3 additions & 3 deletions constructor_telegram_bots/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
path('i18n/', include('django.conf.urls.i18n')),
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),

path('api/', include([
path('', include('user.urls')),
path('api/', include(([
path('user/', include('user.urls')),
path('telegram-bots/', include('telegram_bot.urls')),
path('team/', include('team.urls')),
path('updates/', include('updates.urls')),
path('donations/', include('donation.urls')),
path('instruction/', include('instruction.urls')),
path('privacy-policy/', include('privacy_policy.urls')),
])),
], 'api'))),
]

if settings.DEBUG:
Expand Down
92 changes: 92 additions & 0 deletions user/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from django.test import TestCase
from django.http import HttpResponse
from django.urls import reverse

from rest_framework.test import APIClient
from rest_framework.authtoken.models import Token

from user.models import User


class CustomTestCase(TestCase):
def setUp(self) -> None:
self.client: APIClient = APIClient()
self.user: User = User.objects.create(
telegram_id=123456789,
first_name='exg1o',
)
self.token: Token = Token.objects.create(user=self.user)

class UserAPIViewTests(CustomTestCase):
url: str = reverse('api:user:index')

def test_get_method(self) -> None:
response: HttpResponse = self.client.get(self.url)
self.assertEqual(response.status_code, 401)

self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')

response: HttpResponse = self.client.get(self.url) # type: ignore [no-redef]
self.assertEqual(response.status_code, 200)

def test_delete_method(self) -> None:
response: HttpResponse = self.client.delete(self.url)
self.assertEqual(response.status_code, 401)

self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')

response: HttpResponse = self.client.delete(self.url) # type: ignore [no-redef]
self.assertEqual(response.status_code, 200)

try:
self.user.refresh_from_db()
raise self.failureException('User has not been deleted from database!')
except User.DoesNotExist:
pass

class UserLoginAPIViewTests(CustomTestCase):
url: str = reverse('api:user:login')

def test_post_method(self) -> None:
response: HttpResponse = self.client.post(
self.url,
data={
'user_id': 0,
'confirm_code': self.user.confirm_code,
},
)
self.assertEqual(response.status_code, 404)

response: HttpResponse = self.client.post( # type: ignore [no-redef]
self.url,
data={
'user_id': self.user.id,
'confirm_code': '',
},
)
self.assertEqual(response.status_code, 400)

response: HttpResponse = self.client.post( # type: ignore [no-redef]
self.url,
data={
'user_id': self.user.id,
'confirm_code': self.user.confirm_code,
},
)
self.assertEqual(response.status_code, 200)

self.user.refresh_from_db()

self.assertTrue(self.user.last_login)

class UserLogoutAPIViewTests(CustomTestCase):
url: str = reverse('api:user:logout')

def test_post_method(self) -> None:
response: HttpResponse = self.client.post(self.url)
self.assertEqual(response.status_code, 401)

self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')

response: HttpResponse = self.client.post(self.url) # type: ignore [no-redef]
self.assertEqual(response.status_code, 200)
11 changes: 5 additions & 6 deletions user/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from django.urls import path, include
from django.urls import path

from .views import UserAPIView, UserLoginAPIView, UserLogoutAPIView


app_name = 'user'
urlpatterns = [
path('user/', include([
path('', UserAPIView.as_view()),
path('login/', UserLoginAPIView.as_view()),
path('logout/', UserLogoutAPIView.as_view()),
])),
path('', UserAPIView.as_view(), name='index'),
path('login/', UserLoginAPIView.as_view(), name='login'),
path('logout/', UserLogoutAPIView.as_view(), name='logout'),
]
5 changes: 3 additions & 2 deletions user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.contrib.auth import login, logout

from rest_framework.views import APIView
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.request import Request
from rest_framework.response import Response
Expand All @@ -17,7 +18,7 @@


class UserAPIView(APIView):
authentication_classes = [CookiesTokenAuthentication]
authentication_classes = [CookiesTokenAuthentication, TokenAuthentication]
permission_classes = [IsAuthenticated]

def get(self, request: Request) -> Response:
Expand Down Expand Up @@ -64,7 +65,7 @@ def post(self, request: Request) -> CustomResponse:
return response

class UserLogoutAPIView(APIView):
authentication_classes = [CookiesTokenAuthentication]
authentication_classes = [CookiesTokenAuthentication, TokenAuthentication]
permission_classes = [IsAuthenticated]

def post(self, request: Request) -> CustomResponse:
Expand Down

0 comments on commit e6b0e40

Please sign in to comment.