Skip to content

Commit 3ce4852

Browse files
author
Emmanouil Konstantinidis
committed
Tests
1 parent 55da88e commit 3ce4852

File tree

9 files changed

+149
-3
lines changed

9 files changed

+149
-3
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ cache:
2727

2828
install:
2929
- pip install tox
30+
- pip install codecov
3031

3132
script:
3233
- tox -e $TOX_ENV
34+
35+
after_success:
36+
- codecov

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ Add 'rest_framework_api_key' to your `INSTALLED_APPS` setting:
2222
'rest_framework_api_key',
2323
)
2424

25+
Finally set the django-rest-framework permissions under your django settings:
26+
27+
REST_FRAMEWORK = {
28+
'DEFAULT_PERMISSION_CLASSES': (
29+
'rest_framework_api_key.permissions.HasAPIAccess',
30+
)
31+
}
32+
33+
34+
### Example Request
35+
36+
```python
37+
response = requests.get(
38+
url="http://uhs-api.ctf.sh:8080/catalogue/categories/",
39+
headers={
40+
"Api-Key": "fd8b4a98c8f53035aeab410258430e2d86079c93",
41+
},
42+
)
43+
```
44+
2545

2646
[travis-image]: https://travis-ci.org/ekonstantinidis/django-rest-framework-api-key.svg?branch=master
2747
[travis-url]: https://travis-ci.org/ekonstantinidis/django-rest-framework-api-key

codecov.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
coverage:
2+
precision: 2
3+
round: down
4+
range: "70...100"
5+
6+
status:
7+
project: false
8+
patch: false
9+
changes: false
10+
11+
comment: off

runtests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
FLAKE8_ARGS = ['rest_framework_api_key', 'tests/', '--ignore=E501']
8-
PYTEST_ARGS = ['tests', '--cov=rest_framework_api_key', '--tb=short']
8+
PYTEST_ARGS = ['tests', '--cov=rest_framework_api_key', '--tb=short', '-rw']
99

1010

1111
def exit_on_failure(command, message=None):

tests/settings.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'django.contrib.admin',
1717
'django.contrib.auth',
1818
'django.contrib.contenttypes',
19+
'django.contrib.sessions',
1920
'django.contrib.staticfiles',
2021

2122
# External Packages
@@ -26,4 +27,33 @@
2627
"tests"
2728
]
2829

30+
MIDDLEWARE_CLASSES = (
31+
'django.contrib.sessions.middleware.SessionMiddleware',
32+
'django.middleware.common.CommonMiddleware',
33+
'django.middleware.csrf.CsrfViewMiddleware',
34+
'django.contrib.auth.middleware.AuthenticationMiddleware',
35+
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
36+
'django.contrib.messages.middleware.MessageMiddleware',
37+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
38+
'django.middleware.security.SecurityMiddleware',
39+
)
40+
2941
ROOT_URLCONF = 'tests.urls'
42+
43+
STATIC_URL = '/static/'
44+
45+
TEMPLATES = [
46+
{
47+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
48+
'DIRS': [],
49+
'APP_DIRS': True,
50+
'OPTIONS': {
51+
'context_processors': [
52+
'django.template.context_processors.debug',
53+
'django.template.context_processors.request',
54+
'django.contrib.auth.context_processors.auth',
55+
'django.contrib.messages.context_processors.messages',
56+
],
57+
},
58+
},
59+
]

tests/test_admin.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from django.contrib.auth.models import User
2+
from django.core.urlresolvers import reverse
3+
from django.test import TestCase
4+
from rest_framework_api_key.models import APIKey
5+
from rest_framework_api_key.helpers import generate_key
6+
7+
8+
class LoggedInAdminTestCase(TestCase):
9+
10+
USERNAME = "martymcfly"
11+
12+
PASSWORD = "password"
13+
14+
def setUp(self):
15+
super(LoggedInAdminTestCase, self).setUp()
16+
17+
self.user = User.objects.create_superuser(self.USERNAME, self.EMAIL, self.PASSWORD)
18+
self.assertTrue(self.user.is_active)
19+
self.assertTrue(self.user.is_superuser)
20+
self.client.login(username=self.USERNAME, password=self.PASSWORD)
21+
22+
23+
class APIAuthenticatedTestCase(TestCase):
24+
25+
APP_NAME = 'Project Tests'
26+
27+
def setUp(self):
28+
self.app_key = APIKey.objects.create(name=self.APP_NAME, key=generate_key())
29+
self.header = {'HTTP_API_KEY': self.app_key.key}
30+
31+
32+
class AdminTestCase(LoggedInAdminTestCase, APIAuthenticatedTestCase):
33+
def setUp(self):
34+
super(AdminTestCase, self).setUp()
35+
self.add_app_url = reverse('admin:rest_framework_api_key_apikey_add')
36+
37+
def test_admin_create_app_access(self):
38+
response = self.client.get(self.add_app_url)
39+
self.assertEqual(response.status_code, 200)
40+
41+
def test_admin_create_app(self):
42+
self.assertEqual(APIKey.objects.all().count(), 1)
43+
44+
response = self.client.post(self.add_app_url, data={"name": "Hello"}, follow=True)
45+
46+
self.assertEqual(response.status_code, 200)
47+
self.assertContains(response, "Please note it since you will not be able to see it again.")
48+
self.assertContains(response, "was added successfully.")
49+
50+
self.assertEqual(APIKey.objects.all().count(), 2)
51+
52+
def test_admin_change_app(self):
53+
self.change_app_url = reverse('admin:rest_framework_api_key_apikey_change', args=(self.app_key.id,))
54+
55+
response = self.client.get(self.change_app_url,)
56+
57+
self.assertEqual(response.status_code, 200)
58+
self.assertContains(response, "Hidden")

tests/test_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from rest_framework_api_key.models import APIKey
33

44

5-
class APIKeyAdminTestCase(TestCase):
5+
class ModelTestCase(TestCase):
66

77
def setUp(self):
8-
super(APIKeyAdminTestCase, self).setUp()
8+
super(ModelTestCase, self).setUp()
99

1010
def test_admin_create_object(self):
1111
self.assertEqual(APIKey.objects.all().count(), 0)

tests/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.conf.urls import include, url
2+
from django.contrib import admin
3+
from tests.views import TestView
4+
5+
6+
urlpatterns = [
7+
8+
url(r'^admin/', include(admin.site.urls)),
9+
url(r'^test/$', TestView.as_view(), name="test-view"),
10+
11+
]

tests/views.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from rest_framework import status
2+
from rest_framework.response import Response
3+
from rest_framework.views import APIView
4+
5+
6+
class TestView(APIView):
7+
"""
8+
A dummy view used only for testing.
9+
"""
10+
11+
def get(self, request):
12+
return Response({"msg": "Hello World!"}, status=status.HTTP_200_OK)

0 commit comments

Comments
 (0)