-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
218 additions
and
7 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
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,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) | ||
|
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,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']) |
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,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) |
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
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