Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kalys committed Apr 20, 2024
1 parent ce1dbdd commit ce335a7
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 7 deletions.
13 changes: 13 additions & 0 deletions ashar_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@
# 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
}

SWAGGER_SETTINGS = {
'SECURITY_DEFINITIONS': {
'Basic': {
'type': 'basic'
},
'Bearer': {
'type': 'apiKey',
'name': 'Authorization',
'in': 'header'
}
}
}

# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

Expand Down
2 changes: 1 addition & 1 deletion term/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class IsAuthorPermission(BasePermission):
def has_object_permission(self, request, view, obj):
return request.user.is_authenticated and obj.author == request.user
return request.user.is_authenticated and obj.author == request.user
3 changes: 3 additions & 0 deletions term/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class Meta:
fields = ('id', 'other_lang_examples', 'term', 'description', 'category')

def validate(self, attrs):
category = attrs.get('category')
if not category:
return attrs
category_slug = attrs.get('category').slug
if not Category.objects.filter(slug=category_slug).exists():
raise serializers.ValidationError("category not found")
Expand Down
22 changes: 22 additions & 0 deletions term/tests/test_categories_listing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from rest_framework.test import APITestCase
from assertpy import assert_that

import json

from . import factories

class CategoryListingTestCase(APITestCase):
def test_list_terms(self):
factories.CategoryFactory()
response = self.client.get('/api/v1/categories/')

expected_response = [
{
'slug': 'programmaloo',
'title': 'Программалоо'
},
]

self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content), expected_response)

27 changes: 27 additions & 0 deletions term/tests/test_term_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
from rest_framework.test import APITestCase
from assertpy import assert_that

import json

from . import factories

class TermDetailsTestCase(APITestCase):

def test_term_details(self):
user = factories.UserFactory()
category = factories.CategoryFactory()
term = factories.TermFactory(author=user, category=category)

response = self.client.get(f'/api/v1/term/{term.id}/')
assert_that(response.status_code).is_equal_to(200)
print(response.data)
assert_that(response.data).is_equal_to({
'id': term.id,
'term': term.term,
'description': term.description,
'category': {
'slug': category.slug,
'title': category.title,
}
}, ignore=['other_language_examples','translation_suggestions'])
114 changes: 114 additions & 0 deletions term/tests/test_term_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import unittest
from rest_framework.test import APITestCase
from assertpy import assert_that

from rest_framework_simplejwt.tokens import RefreshToken
import json

from . import factories

class TermUpdateTestCase(APITestCase):
def setUp(self):
self.current_user = factories.UserFactory()
refresh = RefreshToken.for_user(self.current_user)
access_token = str(refresh.access_token)
self.client.credentials(HTTP_AUTHORIZATION = f'JWT {access_token}')
self.term = factories.TermFactory(author=self.current_user)


def test_update_fields(self):
data = {
'term': 'new term',
'description': 'new description',
'active': True
}
path = f'/api/v1/terms/{self.term.id}/'
response = self.client.patch(path, data, format='json')
expected_data = {
'id': self.term.id,
'term': 'new term',
'description': 'new description',
'other_lang_examples': [],
'category': self.term.category.slug,
'other_language_examples': [],
'translation_suggestions': []
}

assert_that(response.status_code).is_equal_to(200)
assert_that(json.loads(response.content)).is_equal_to(expected_data, ignore='id')

def test_update_category(self):
category = factories.CategoryFactory(title='new category')
data = {
'category': category.slug
}
path = f'/api/v1/terms/{self.term.id}/'
response = self.client.patch(path, data, format='json')
expected_data = {
'id': self.term.id,
'term': self.term.term,
'description': self.term.description,
'other_lang_examples': [],
'category': category.slug,
'other_language_examples': [],
'translation_suggestions': []
}

assert_that(response.status_code).is_equal_to(200)
assert_that(json.loads(response.content)).is_equal_to(expected_data, ignore='id')

def test_wrong_data(self):
data = {
'term': '',
'description': 'new description',
'active': 'True'
}
path = f'/api/v1/terms/{self.term.id}/'
response = self.client.patch(path, data, format='json')


expected_data = {
'term': ['This field may not be blank.']
}

assert_that(response.status_code).is_equal_to(400)
assert_that(json.loads(response.content)).is_equal_to(expected_data)


def test_unauthenticated(self):
self.client.credentials()
data = {
'term': 'new term',
'description': 'new description',
'active': True
}
path = f'/api/v1/terms/{self.term.id}/'
response = self.client.patch(path, data, format='json')

expected_data = {
'detail': 'Authentication credentials were not provided.'
}
assert_that(response.status_code).is_equal_to(401)
assert_that(json.loads(response.content)).is_equal_to(expected_data)

def test_unauthorized(self):
another_user = factories.UserFactory()

another_term = factories.TermFactory(
author=another_user,
category=self.term.category
)

data = {
'term': 'new term',
'description': 'new description',
'active': True
}
path = f'/api/v1/terms/{another_term.id}/'
response = self.client.patch(path, data, format='json')

expected_data = {
'detail': 'You do not have permission to perform this action.'
}
assert_that(response.status_code).is_equal_to(403)
assert_that(json.loads(response.content)).is_equal_to(expected_data)
2 changes: 1 addition & 1 deletion term/tests/test_terms_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def setUp(self):
self.client.credentials(HTTP_AUTHORIZATION = f'JWT {access_token}')


def test_unauthorized(self):
def test_unauthenticated(self):
self.client.credentials(HTTP_AUTHORIZATION = f'JWT wrong_access_token')

data = {
Expand Down
37 changes: 34 additions & 3 deletions term/tests/test_terms_listing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import unittest
from rest_framework.test import APITestCase
from assertpy import assert_that

Expand All @@ -6,15 +7,17 @@
from . import factories

class TermListingTestCase(APITestCase):
def setUp(self):
self.term = factories.TermFactory()

def test_list_terms(self):
term = factories.TermFactory()
response = self.client.get('/api/v1/terms/')

expected_response = [
{
'category': {'slug': 'programmaloo', 'title': 'Программалоо'},
'description': term.description,
'id': term.id,
'description': self.term.description,
'id': self.term.id,
'other_language_examples': [],
'term': 'процессор',
'translation_suggestions': []
Expand All @@ -23,3 +26,31 @@ def test_list_terms(self):

self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content), expected_response)


def test_filter_by_category(self):
category = factories.CategoryFactory(title='администрлөө')
factories.TermFactory(category=category)

response = self.client.get('/api/v1/terms/?filter=programmaloo')

expected_response = [
{
'category': {'slug': 'programmaloo', 'title': 'Программалоо'},
'description': self.term.description,
'id': self.term.id,
'other_language_examples': [],
'term': 'процессор',
'translation_suggestions': []
}
]
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content), expected_response)

@unittest.skip
def test_search_by_term(self):
print('not implemented')

@unittest.skip
def test_search_by_description(self):
print('not implemented')
4 changes: 2 additions & 2 deletions term/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
urlpatterns = [
path('categories/', views.CategoryListView.as_view()),
path('terms/', views.TermListCreateView.as_view()),
path('term-detail/<int:pk>/', views.TermDetailView.as_view()),
path('term/<int:pk>/', views.TermUpdateDeleteView.as_view()),
path('terms/<int:pk>/', views.TermUpdateDeleteView.as_view()),
path('term/<int:pk>/', views.TermDetailView.as_view()),
path('suggestion/', views.SuggestionView.as_view()),
]
1 change: 1 addition & 0 deletions term/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def get_serializer_context(self):


class TermDetailView(generics.RetrieveAPIView):
permission_classes = (permissions.AllowAny, )
queryset = Term.objects.select_related('author', 'category')
serializer_class = TermSerializer

Expand Down

0 comments on commit ce335a7

Please sign in to comment.