Skip to content

Commit

Permalink
Feature/unit tests (#10)
Browse files Browse the repository at this point in the history
* adding tests and adding timezones to fixtures

* adding more tests

* moving tests to a tests folder

* refactoring the way tests are stored

* adding some more asserts

* adding tests for permissions

* testing delete method

* remove debug

* rename tests

* adding test for field limiter

* adding some crud test cases for recipes

* adding a test for filtering

* fixing test cases for create and update

* adding file creation

* fixing tests

* use django unit test class

* adding some comments to teh login test cases

* adding some comments to teh common test cases

* removing un-needed fixtures
  • Loading branch information
RyanNoelk authored Apr 7, 2018
1 parent 2b41bdc commit 7c626e3
Show file tree
Hide file tree
Showing 22 changed files with 876 additions and 261 deletions.
2 changes: 2 additions & 0 deletions v1/accounts/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# encoding: utf-8
13 changes: 8 additions & 5 deletions v1/accounts/tests.py → v1/accounts/tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from django.test import TestCase
from rest_framework_jwt.settings import api_settings

jwt_decode_handler = api_settings.JWT_DECODE_HANDLER


class AccountTests(TestCase):
fixtures=['test/users.json']
fixtures = ['test/users.json']

def setUp(self):
self.jwt_decode_handler = api_settings.JWT_DECODE_HANDLER

def test_obtain_authtoken_success(self):
""" Try and login and confirm that the login was successful """
resp = self.client.post(
'/api/v1/accounts/obtain-auth-token/',
{
Expand All @@ -21,11 +23,12 @@ def test_obtain_authtoken_success(self):

self.assertEqual(resp.status_code, 200)
self.assertTrue(resp.json()['id'] == 1)
decoded_token = jwt_decode_handler(resp.json()['token'])
decoded_token = self.jwt_decode_handler(resp.json()['token'])
self.assertTrue(decoded_token.get('user_id') == 1)
self.assertTrue(decoded_token.get('username') == 'testuser1')

def test_obtain_authtoken_wrong_password(self):
""" Try and login and confirm that the login was unsuccessful """
resp = self.client.post(
'/api/v1/accounts/obtain-auth-token/',
{
Expand All @@ -34,4 +37,4 @@ def test_obtain_authtoken_wrong_password(self):
}
)

self.assertEqual(resp.status_code, 400)
self.assertEqual(resp.status_code, 400)
2 changes: 2 additions & 0 deletions v1/common/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
# encoding: utf-8
76 changes: 76 additions & 0 deletions v1/common/tests/test_permission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python
# encoding: utf-8

from django.contrib.auth.models import AnonymousUser, User
from django.test import TestCase, RequestFactory
from v1.common.permissions import IsAdminOrReadOnly, IsOwnerOrReadOnly


class PermissionTest(TestCase):
def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()
# Create a staff user.
self.user = User.objects.create_user(
username='jacob', email='[email protected]', password='top_secret', is_staff=True
)

def test_is_owner_or_read_only(self):
# Try and access something as an admin user.
# Both get and post should have access.
request = self.factory.get('/admin')
request.user = self.user
self.assertTrue(
IsOwnerOrReadOnly().has_permission(request, None)
)
self.assertTrue(
IsOwnerOrReadOnly().has_object_permission(request, None, None)
)
request = self.factory.post('/admin')
request.user = self.user
self.assertTrue(
IsOwnerOrReadOnly().has_permission(request, None)
)

# Try and access something as an anonymous user.
# Both get should have access but post shouldn't.
request = self.factory.get('/admin')
request.user = AnonymousUser()
self.assertTrue(
IsOwnerOrReadOnly().has_permission(request, None)
)
self.assertTrue(
IsOwnerOrReadOnly().has_object_permission(request, None, None)
)
request = self.factory.post('/admin')
request.user = AnonymousUser()
self.assertFalse(
IsOwnerOrReadOnly().has_permission(request, None)
)

def test_is_admin_or_read_only(self):
# Try and access something as an admin user.
# Both get and post should have access.
request = self.factory.get('/admin')
request.user = self.user
self.assertTrue(
IsAdminOrReadOnly().has_permission(request, None)
)
request = self.factory.post('/admin')
request.user = self.user
self.assertTrue(
IsAdminOrReadOnly().has_permission(request, None)
)

# Try and access something as an anonymous user.
# Both get should have access but post shouldn't.
request = self.factory.get('/admin')
request.user = AnonymousUser()
self.assertTrue(
IsAdminOrReadOnly().has_permission(request, None)
)
request = self.factory.post('/admin')
request.user = AnonymousUser()
self.assertFalse(
IsAdminOrReadOnly().has_permission(request, None)
)
36 changes: 36 additions & 0 deletions v1/common/tests/test_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
# encoding: utf-8

from django.test import TestCase
from v1.recipe.models import Recipe
from v1.common.recipe_search import get_search_results


class GetSearchResultsTests(TestCase):
fixtures = [
'test/users.json',
'course_data.json',
'cuisine_data.json',
'recipe_data.json',
'ing_data.json'
]

def test_get_search_results(self):
""" Run a search that will return data """
query = get_search_results(
['title', 'ingredient_groups__ingredients__title', 'tags__title'],
Recipe.objects,
'chili'
).distinct()

self.assertTrue(len(query.all()) > 0)

def test_get_search_no_results(self):
""" Run a search that will return no data """
query = get_search_results(
['title', 'ingredient_groups__ingredients__title', 'tags__title'],
Recipe.objects,
'blue berry'
).distinct()

self.assertTrue(len(query.all()) == 0)
2 changes: 1 addition & 1 deletion v1/fixtures/news_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"fields": {
"content": "OpenEats2 is an open source recipe management site. You can share recipes with friends, rate recipes, store your favorite recipes to find easily, and more. If you want to run your own personal OpenEats site visit the <a href=\"https://github.com/ryannoelk/OpenEats\" target=\"_blank\">Github page</a>",
"image": "",
"pub_date": "2010-10-10 13:32:47",
"pub_date": "2010-10-10 13:32:47+00:00",
"slug": "default-news-entry",
"title": "OpenEats 2",
"frontpage": 1
Expand Down
Loading

0 comments on commit 7c626e3

Please sign in to comment.