Skip to content

Commit f1e96be

Browse files
committed
Merge pull request #133 from Tivix/cleanup_tests
Reorganized test files
2 parents b8c3ae9 + afcf2a5 commit f1e96be

File tree

10 files changed

+242
-233
lines changed

10 files changed

+242
-233
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ env:
77
install:
88
- pip install -q Django==$DJANGO --use-mirrors
99
- pip install coveralls
10-
- pip install -r test_requirements.pip
10+
- pip install -r rest_auth/tests/requirements.pip
1111
script:
1212
- coverage run --source=rest_auth setup.py test
1313
after_success:

rest_auth/tests/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.

test_settings.py renamed to rest_auth/tests/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33

44
PROJECT_ROOT = os.path.abspath(os.path.split(os.path.split(__file__)[0])[0])
5+
56
ROOT_URLCONF = 'urls'
67
STATIC_URL = '/static/'
78
STATIC_ROOT = '%s/staticserve' % PROJECT_ROOT

rest_auth/tests.py renamed to rest_auth/tests/test_api.py

Lines changed: 2 additions & 228 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,13 @@
1-
import json
2-
3-
from django.conf import settings
41
from django.core.urlresolvers import reverse
5-
from django.test.client import Client, MULTIPART_CONTENT
62
from django.test import TestCase
73
from django.contrib.auth import get_user_model
84
from django.core import mail
95
from django.test.utils import override_settings
10-
from django.contrib.sites.models import Site
116
from django.utils.encoding import force_text
127

13-
from allauth.socialaccount.models import SocialApp
14-
from allauth.socialaccount.providers.facebook.provider import GRAPH_API_URL
15-
import responses
16-
178
from rest_framework import status
189

19-
20-
class APIClient(Client):
21-
22-
def patch(self, path, data='', content_type=MULTIPART_CONTENT, follow=False, **extra):
23-
return self.generic('PATCH', path, data, content_type, **extra)
24-
25-
def options(self, path, data='', content_type=MULTIPART_CONTENT, follow=False, **extra):
26-
return self.generic('OPTIONS', path, data, content_type, **extra)
27-
28-
29-
class BaseAPITestCase(object):
30-
31-
"""
32-
base for API tests:
33-
* easy request calls, f.e.: self.post(url, data), self.get(url)
34-
* easy status check, f.e.: self.post(url, data, status_code=200)
35-
"""
36-
def send_request(self, request_method, *args, **kwargs):
37-
request_func = getattr(self.client, request_method)
38-
status_code = None
39-
if 'content_type' not in kwargs and request_method != 'get':
40-
kwargs['content_type'] = 'application/json'
41-
if 'data' in kwargs and request_method != 'get' and kwargs['content_type'] == 'application/json':
42-
data = kwargs.get('data', '')
43-
kwargs['data'] = json.dumps(data) # , cls=CustomJSONEncoder
44-
if 'status_code' in kwargs:
45-
status_code = kwargs.pop('status_code')
46-
47-
# check_headers = kwargs.pop('check_headers', True)
48-
if hasattr(self, 'token'):
49-
kwargs['HTTP_AUTHORIZATION'] = 'Token %s' % self.token
50-
51-
self.response = request_func(*args, **kwargs)
52-
is_json = bool(
53-
[x for x in self.response._headers['content-type'] if 'json' in x])
54-
if is_json and self.response.content:
55-
self.response.json = json.loads(force_text(self.response.content))
56-
else:
57-
self.response.json = {}
58-
if status_code:
59-
self.assertEqual(self.response.status_code, status_code)
60-
return self.response
61-
62-
def post(self, *args, **kwargs):
63-
return self.send_request('post', *args, **kwargs)
64-
65-
def get(self, *args, **kwargs):
66-
return self.send_request('get', *args, **kwargs)
67-
68-
def patch(self, *args, **kwargs):
69-
return self.send_request('patch', *args, **kwargs)
70-
71-
# def put(self, *args, **kwargs):
72-
# return self.send_request('put', *args, **kwargs)
73-
74-
# def delete(self, *args, **kwargs):
75-
# return self.send_request('delete', *args, **kwargs)
76-
77-
# def options(self, *args, **kwargs):
78-
# return self.send_request('options', *args, **kwargs)
79-
80-
# def post_file(self, *args, **kwargs):
81-
# kwargs['content_type'] = MULTIPART_CONTENT
82-
# return self.send_request('post', *args, **kwargs)
83-
84-
# def get_file(self, *args, **kwargs):
85-
# content_type = None
86-
# if 'content_type' in kwargs:
87-
# content_type = kwargs.pop('content_type')
88-
# response = self.send_request('get', *args, **kwargs)
89-
# if content_type:
90-
# self.assertEqual(
91-
# bool(filter(lambda x: content_type in x, response._headers['content-type'])), True)
92-
# return response
93-
94-
def init(self):
95-
settings.DEBUG = True
96-
self.client = APIClient()
97-
98-
self.login_url = reverse('rest_login')
99-
self.logout_url = reverse('rest_logout')
100-
self.password_change_url = reverse('rest_password_change')
101-
self.register_url = reverse('rest_register')
102-
self.password_reset_url = reverse('rest_password_reset')
103-
self.user_url = reverse('rest_user_details')
104-
self.veirfy_email_url = reverse('rest_verify_email')
105-
self.fb_login_url = reverse('fb_login')
106-
107-
def _login(self):
108-
payload = {
109-
"username": self.USERNAME,
110-
"password": self.PASS
111-
}
112-
self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK)
113-
114-
def _logout(self):
115-
self.post(self.logout_url, status=status.HTTP_200_OK)
116-
117-
118-
# -----------------------
119-
# T E S T H E R E
120-
# -----------------------
10+
from .test_base import BaseAPITestCase
12111

12212

12313
class APITestCase1(TestCase, BaseAPITestCase):
@@ -127,7 +17,7 @@ class APITestCase1(TestCase, BaseAPITestCase):
12717
- custom registration: backend defined
12818
"""
12919

130-
urls = 'rest_auth.test_urls'
20+
urls = 'tests.urls'
13121

13222
USERNAME = 'person'
13323
PASS = 'person'
@@ -421,119 +311,3 @@ def test_registration_with_email_verification(self):
421311
# try to login again
422312
self._login()
423313
self._logout()
424-
425-
426-
class TestSocialAuth(TestCase, BaseAPITestCase):
427-
428-
urls = 'rest_auth.test_urls'
429-
430-
USERNAME = 'person'
431-
PASS = 'person'
432-
433-
REGISTRATION_DATA = {
434-
"username": USERNAME,
435-
"password1": PASS,
436-
"password2": PASS,
437-
"email": EMAIL
438-
}
439-
440-
def setUp(self):
441-
self.init()
442-
443-
social_app = SocialApp.objects.create(
444-
provider='facebook',
445-
name='Facebook',
446-
client_id='123123123',
447-
secret='321321321',
448-
)
449-
site = Site.objects.get_current()
450-
social_app.sites.add(site)
451-
self.graph_api_url = GRAPH_API_URL + '/me'
452-
453-
@responses.activate
454-
def test_failed_social_auth(self):
455-
# fake response
456-
responses.add(
457-
responses.GET,
458-
self.graph_api_url,
459-
body='',
460-
status=400,
461-
content_type='application/json'
462-
)
463-
464-
payload = {
465-
'access_token': 'abc123'
466-
}
467-
self.post(self.fb_login_url, data=payload, status_code=400)
468-
469-
@responses.activate
470-
def test_social_auth(self):
471-
# fake response for facebook call
472-
resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\/\\/www.facebook.com\\/john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true}' # noqa
473-
responses.add(
474-
responses.GET,
475-
self.graph_api_url,
476-
body=resp_body,
477-
status=200,
478-
content_type='application/json'
479-
)
480-
481-
users_count = get_user_model().objects.all().count()
482-
payload = {
483-
'access_token': 'abc123'
484-
}
485-
486-
self.post(self.fb_login_url, data=payload, status_code=200)
487-
self.assertIn('key', self.response.json.keys())
488-
self.assertEqual(get_user_model().objects.all().count(), users_count + 1)
489-
490-
# make sure that second request will not create a new user
491-
self.post(self.fb_login_url, data=payload, status_code=200)
492-
self.assertIn('key', self.response.json.keys())
493-
self.assertEqual(get_user_model().objects.all().count(), users_count + 1)
494-
495-
@responses.activate
496-
@override_settings(
497-
ACCOUNT_EMAIL_VERIFICATION='mandatory',
498-
ACCOUNT_EMAIL_REQUIRED=True,
499-
REST_SESSION_LOGIN=False
500-
)
501-
def test_edge_case(self):
502-
resp_body = '{"id":"123123123123","first_name":"John","gender":"male","last_name":"Smith","link":"https:\\/\\/www.facebook.com\\/john.smith","locale":"en_US","name":"John Smith","timezone":2,"updated_time":"2014-08-13T10:14:38+0000","username":"john.smith","verified":true,"email":"%s"}' # noqa
503-
responses.add(
504-
responses.GET,
505-
self.graph_api_url,
506-
body=resp_body % self.EMAIL,
507-
status=200,
508-
content_type='application/json'
509-
)
510-
511-
# test empty payload
512-
self.post(self.register_url, data={}, status_code=400)
513-
514-
self.post(
515-
self.register_url,
516-
data=self.REGISTRATION_DATA,
517-
status_code=201
518-
)
519-
new_user = get_user_model().objects.latest('id')
520-
self.assertEqual(new_user.username, self.REGISTRATION_DATA['username'])
521-
522-
# verify email
523-
email_confirmation = new_user.emailaddress_set.get(email=self.EMAIL)\
524-
.emailconfirmation_set.order_by('-created')[0]
525-
self.post(
526-
self.veirfy_email_url,
527-
data={"key": email_confirmation.key},
528-
status_code=status.HTTP_200_OK
529-
)
530-
531-
self._login()
532-
self._logout()
533-
534-
payload = {
535-
'access_token': 'abc123'
536-
}
537-
538-
self.post(self.fb_login_url, data=payload, status_code=200)
539-
self.assertIn('key', self.response.json.keys())

rest_auth/tests/test_base.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import json
2+
3+
from django.conf import settings
4+
from django.core.urlresolvers import reverse
5+
from django.test.client import Client, MULTIPART_CONTENT
6+
from django.utils.encoding import force_text
7+
8+
from rest_framework import status
9+
10+
11+
class APIClient(Client):
12+
13+
def patch(self, path, data='', content_type=MULTIPART_CONTENT, follow=False, **extra):
14+
return self.generic('PATCH', path, data, content_type, **extra)
15+
16+
def options(self, path, data='', content_type=MULTIPART_CONTENT, follow=False, **extra):
17+
return self.generic('OPTIONS', path, data, content_type, **extra)
18+
19+
20+
class BaseAPITestCase(object):
21+
22+
"""
23+
base for API tests:
24+
* easy request calls, f.e.: self.post(url, data), self.get(url)
25+
* easy status check, f.e.: self.post(url, data, status_code=200)
26+
"""
27+
def send_request(self, request_method, *args, **kwargs):
28+
request_func = getattr(self.client, request_method)
29+
status_code = None
30+
if 'content_type' not in kwargs and request_method != 'get':
31+
kwargs['content_type'] = 'application/json'
32+
if 'data' in kwargs and request_method != 'get' and kwargs['content_type'] == 'application/json':
33+
data = kwargs.get('data', '')
34+
kwargs['data'] = json.dumps(data) # , cls=CustomJSONEncoder
35+
if 'status_code' in kwargs:
36+
status_code = kwargs.pop('status_code')
37+
38+
# check_headers = kwargs.pop('check_headers', True)
39+
if hasattr(self, 'token'):
40+
kwargs['HTTP_AUTHORIZATION'] = 'Token %s' % self.token
41+
42+
self.response = request_func(*args, **kwargs)
43+
is_json = bool(
44+
[x for x in self.response._headers['content-type'] if 'json' in x])
45+
if is_json and self.response.content:
46+
self.response.json = json.loads(force_text(self.response.content))
47+
else:
48+
self.response.json = {}
49+
if status_code:
50+
self.assertEqual(self.response.status_code, status_code)
51+
return self.response
52+
53+
def post(self, *args, **kwargs):
54+
return self.send_request('post', *args, **kwargs)
55+
56+
def get(self, *args, **kwargs):
57+
return self.send_request('get', *args, **kwargs)
58+
59+
def patch(self, *args, **kwargs):
60+
return self.send_request('patch', *args, **kwargs)
61+
62+
# def put(self, *args, **kwargs):
63+
# return self.send_request('put', *args, **kwargs)
64+
65+
# def delete(self, *args, **kwargs):
66+
# return self.send_request('delete', *args, **kwargs)
67+
68+
# def options(self, *args, **kwargs):
69+
# return self.send_request('options', *args, **kwargs)
70+
71+
# def post_file(self, *args, **kwargs):
72+
# kwargs['content_type'] = MULTIPART_CONTENT
73+
# return self.send_request('post', *args, **kwargs)
74+
75+
# def get_file(self, *args, **kwargs):
76+
# content_type = None
77+
# if 'content_type' in kwargs:
78+
# content_type = kwargs.pop('content_type')
79+
# response = self.send_request('get', *args, **kwargs)
80+
# if content_type:
81+
# self.assertEqual(
82+
# bool(filter(lambda x: content_type in x, response._headers['content-type'])), True)
83+
# return response
84+
85+
def init(self):
86+
settings.DEBUG = True
87+
self.client = APIClient()
88+
89+
self.login_url = reverse('rest_login')
90+
self.logout_url = reverse('rest_logout')
91+
self.password_change_url = reverse('rest_password_change')
92+
self.register_url = reverse('rest_register')
93+
self.password_reset_url = reverse('rest_password_reset')
94+
self.user_url = reverse('rest_user_details')
95+
self.veirfy_email_url = reverse('rest_verify_email')
96+
self.fb_login_url = reverse('fb_login')
97+
98+
def _login(self):
99+
payload = {
100+
"username": self.USERNAME,
101+
"password": self.PASS
102+
}
103+
self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK)
104+
105+
def _logout(self):
106+
self.post(self.logout_url, status=status.HTTP_200_OK)

0 commit comments

Comments
 (0)