Skip to content

Commit 8a004bb

Browse files
committed
Increased test coverage
+ minor fixes
1 parent 3189a5c commit 8a004bb

File tree

7 files changed

+105
-80
lines changed

7 files changed

+105
-80
lines changed

.coveragerc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# .coveragerc to control coverage.py
2+
[run]
3+
omit=*site-packages*,*distutils*,*migrations*
4+
5+
[report]
6+
# Regexes for lines to exclude from consideration
7+
exclude_lines =
8+
# Have to re-enable the standard pragma
9+
pragma: no cover
10+
11+
# Don't complain about missing debug-only code:
12+
def __repr__
13+
if self\.debug
14+
15+
# Don't complain if tests don't hit defensive assertion code:
16+
raise AssertionError
17+
raise NotImplementedError
18+
19+
# Don't complain if non-runnable code isn't run:
20+
if 0:
21+
if __name__ == .__main__.:
22+
23+
ignore_errors = True
24+
25+
[html]
26+
directory = coverage_html

rest_auth/registration/views.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ class VerifyEmailView(APIView, ConfirmEmailView):
6868
permission_classes = (AllowAny,)
6969
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
7070

71-
def get(self, *args, **kwargs):
72-
raise MethodNotAllowed('GET')
73-
7471
def post(self, request, *args, **kwargs):
7572
serializer = VerifyEmailSerializer(data=request.data)
7673
serializer.is_valid(raise_exception=True)

rest_auth/tests/django_urls.py

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,67 +13,11 @@
1313
from django.views.decorators.cache import never_cache
1414

1515

16-
class CustomRequestAuthenticationForm(AuthenticationForm):
17-
def __init__(self, request, *args, **kwargs):
18-
assert isinstance(request, HttpRequest)
19-
super(CustomRequestAuthenticationForm, self).__init__(request, *args, **kwargs)
20-
21-
22-
@never_cache
23-
def remote_user_auth_view(request):
24-
"""
25-
Dummy view for remote user tests
26-
"""
27-
t = Template("Username is {{ user }}.")
28-
c = RequestContext(request, {})
29-
return HttpResponse(t.render(c))
30-
31-
32-
def auth_processor_no_attr_access(request):
33-
render(request, 'context_processors/auth_attrs_no_access.html')
34-
# *After* rendering, we check whether the session was accessed
35-
return render(request,
36-
'context_processors/auth_attrs_test_access.html',
37-
{'session_accessed': request.session.accessed})
38-
39-
40-
def auth_processor_attr_access(request):
41-
render(request, 'context_processors/auth_attrs_access.html')
42-
return render(request,
43-
'context_processors/auth_attrs_test_access.html',
44-
{'session_accessed': request.session.accessed})
45-
46-
47-
def auth_processor_user(request):
48-
return render(request, 'context_processors/auth_attrs_user.html')
49-
50-
51-
def auth_processor_perms(request):
52-
return render(request, 'context_processors/auth_attrs_perms.html')
53-
54-
55-
def auth_processor_perm_in_perms(request):
56-
return render(request, 'context_processors/auth_attrs_perm_in_perms.html')
57-
58-
59-
def auth_processor_messages(request):
60-
info(request, "Message 1")
61-
return render(request, 'context_processors/auth_attrs_messages.html')
62-
63-
64-
def userpage(request):
65-
pass
66-
67-
68-
def custom_request_auth_login(request):
69-
return views.login(request, authentication_form=CustomRequestAuthenticationForm)
70-
7116
# special urls for auth test cases
7217
urlpatterns += [
7318
url(r'^logout/custom_query/$', views.logout, dict(redirect_field_name='follow')),
7419
url(r'^logout/next_page/$', views.logout, dict(next_page='/somewhere/')),
7520
url(r'^logout/next_page/named/$', views.logout, dict(next_page='password_reset')),
76-
url(r'^remote_user/$', remote_user_auth_view),
7721
url(r'^password_reset_from_email/$', views.password_reset, dict(from_email='[email protected]')),
7822
url(r'^password_reset/custom_redirect/$', views.password_reset, dict(post_reset_redirect='/custom/')),
7923
url(r'^password_reset/custom_redirect/named/$', views.password_reset, dict(post_reset_redirect='password_reset')),
@@ -90,16 +34,4 @@ def custom_request_auth_login(request):
9034
url(r'^admin_password_reset/$', views.password_reset, dict(is_admin_site=True)),
9135
url(r'^login_required/$', login_required(views.password_reset)),
9236
url(r'^login_required_login_url/$', login_required(views.password_reset, login_url='/somewhere/')),
93-
94-
url(r'^auth_processor_no_attr_access/$', auth_processor_no_attr_access),
95-
url(r'^auth_processor_attr_access/$', auth_processor_attr_access),
96-
url(r'^auth_processor_user/$', auth_processor_user),
97-
url(r'^auth_processor_perms/$', auth_processor_perms),
98-
url(r'^auth_processor_perm_in_perms/$', auth_processor_perm_in_perms),
99-
url(r'^auth_processor_messages/$', auth_processor_messages),
100-
url(r'^custom_request_auth_login/$', custom_request_auth_login),
101-
url(r'^userpage/(.+)/$', userpage, name="userpage"),
102-
103-
# This line is only required to render the password reset with is_admin=True
104-
url(r'^admin/', include(admin.site.urls)),
10537
]

rest_auth/tests/settings.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@
4545
"allauth.socialaccount.context_processors.socialaccount",
4646
]
4747

48+
# avoid deprecation warnings during tests
49+
TEMPLATES = [
50+
{
51+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
52+
'DIRS': [
53+
# insert your TEMPLATE_DIRS here
54+
],
55+
'APP_DIRS': True,
56+
'OPTIONS': {
57+
'context_processors': TEMPLATE_CONTEXT_PROCESSORS,
58+
},
59+
},
60+
]
61+
4862
REST_FRAMEWORK = {
4963
'DEFAULT_AUTHENTICATION_CLASSES': (
5064
'rest_framework.authentication.SessionAuthentication',
@@ -79,3 +93,10 @@
7993
SECRET_KEY = "38dh*skf8sjfhs287dh&^hd8&3hdg*j2&sd"
8094
ACCOUNT_ACTIVATION_DAYS = 1
8195
SITE_ID = 1
96+
97+
AUTHENTICATION_BACKENDS = (
98+
# Needed to login by username in Django admin, regardless of `allauth`
99+
'django.contrib.auth.backends.ModelBackend',
100+
# `allauth` specific authentication methods, such as login by e-mail
101+
'allauth.account.auth_backends.AuthenticationBackend',
102+
)

rest_auth/tests/test_api.py

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
from django.core.urlresolvers import reverse
2-
from django.test import TestCase
2+
from django.test import TestCase, override_settings
33
from django.contrib.auth import get_user_model
44
from django.core import mail
55
from django.conf import settings
6-
from django.test.utils import override_settings
76
from django.utils.encoding import force_text
87

98
from rest_framework import status
10-
9+
from allauth.account import app_settings as account_app_settings
1110
from .test_base import BaseAPITestCase
1211

1312

13+
@override_settings(ROOT_URLCONF="tests.urls")
1414
class APITestCase1(TestCase, BaseAPITestCase):
1515
"""
1616
Case #1:
1717
- user profile: defined
1818
- custom registration: backend defined
1919
"""
2020

21-
urls = 'tests.urls'
21+
# urls = 'tests.urls'
2222

2323
USERNAME = 'person'
2424
PASS = 'person'
@@ -57,7 +57,36 @@ def _generate_uid_and_token(self, user):
5757
result['token'] = default_token_generator.make_token(user)
5858
return result
5959

60-
def test_login(self):
60+
@override_settings(ACCOUNT_AUTHENTICATION_METHOD=account_app_settings.AuthenticationMethod.EMAIL)
61+
def test_login_failed_email_validation(self):
62+
payload = {
63+
"email": '',
64+
"password": self.PASS
65+
}
66+
67+
resp = self.post(self.login_url, data=payload, status_code=400)
68+
self.assertEqual(resp.json['non_field_errors'][0], u'Must include "email" and "password".')
69+
70+
@override_settings(ACCOUNT_AUTHENTICATION_METHOD=account_app_settings.AuthenticationMethod.USERNAME)
71+
def test_login_failed_username_validation(self):
72+
payload = {
73+
"username": '',
74+
"password": self.PASS
75+
}
76+
77+
resp = self.post(self.login_url, data=payload, status_code=400)
78+
self.assertEqual(resp.json['non_field_errors'][0], u'Must include "username" and "password".')
79+
80+
@override_settings(ACCOUNT_AUTHENTICATION_METHOD=account_app_settings.AuthenticationMethod.USERNAME_EMAIL)
81+
def test_login_failed_username_email_validation(self):
82+
payload = {
83+
"password": self.PASS
84+
}
85+
86+
resp = self.post(self.login_url, data=payload, status_code=400)
87+
self.assertEqual(resp.json['non_field_errors'][0], u'Must include either "username" or "email" and "password".')
88+
89+
def test_allauth_login_with_username(self):
6190
payload = {
6291
"username": self.USERNAME,
6392
"password": self.PASS
@@ -91,6 +120,22 @@ def test_login(self):
91120
# test empty payload
92121
self.post(self.login_url, data={}, status_code=400)
93122

123+
@override_settings(ACCOUNT_AUTHENTICATION_METHOD=account_app_settings.AuthenticationMethod.EMAIL)
124+
def test_allauth_login_with_email(self):
125+
payload = {
126+
"email": self.EMAIL,
127+
"password": self.PASS
128+
}
129+
# there is no users in db so it should throw error (400)
130+
self.post(self.login_url, data=payload, status_code=400)
131+
132+
self.post(self.password_change_url, status_code=403)
133+
134+
# create user
135+
user = get_user_model().objects.create_user(self.EMAIL, email=self.EMAIL, password=self.PASS)
136+
137+
self.post(self.login_url, data=payload, status_code=200)
138+
94139
@override_settings(REST_USE_JWT=True)
95140
def test_login_jwt(self):
96141
payload = {
@@ -148,6 +193,9 @@ def test_login_by_email(self):
148193
# test empty payload
149194
self.post(self.login_url, data={}, status_code=400)
150195

196+
# bring back allauth
197+
settings.INSTALLED_APPS.append('allauth')
198+
151199
def test_password_change(self):
152200
login_payload = {
153201
"username": self.USERNAME,

rest_auth/tests/test_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ def send_request(self, request_method, *args, **kwargs):
4545
self.response = request_func(*args, **kwargs)
4646
is_json = bool(
4747
[x for x in self.response._headers['content-type'] if 'json' in x])
48+
49+
self.response.json = {}
4850
if is_json and self.response.content:
4951
self.response.json = json.loads(force_text(self.response.content))
50-
else:
51-
self.response.json = {}
52+
5253
if status_code:
5354
self.assertEqual(self.response.status_code, status_code)
55+
5456
return self.response
5557

5658
def post(self, *args, **kwargs):

rest_auth/tests/test_social.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
from .test_base import BaseAPITestCase
1313

1414

15+
@override_settings(ROOT_URLCONF="tests.urls")
1516
class TestSocialAuth(TestCase, BaseAPITestCase):
1617

17-
urls = 'tests.urls'
18-
1918
USERNAME = 'person'
2019
PASS = 'person'
2120

0 commit comments

Comments
 (0)