2014-05-03 01:47:28 +04:00
|
|
|
from django.core.urlresolvers import reverse
|
2014-04-30 23:52:05 +04:00
|
|
|
from django.test import TestCase
|
2014-05-02 00:32:30 +04:00
|
|
|
from django.contrib.auth import get_user_model
|
2014-05-06 02:53:06 +04:00
|
|
|
from django.core import mail
|
2015-12-13 23:43:33 +03:00
|
|
|
from django.conf import settings
|
2014-10-01 17:31:10 +04:00
|
|
|
from django.test.utils import override_settings
|
2015-11-19 11:38:57 +03:00
|
|
|
from django.utils.encoding import force_text
|
2014-10-08 14:19:15 +04:00
|
|
|
|
2014-10-01 17:31:10 +04:00
|
|
|
from rest_framework import status
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2015-12-04 11:32:03 +03:00
|
|
|
from .test_base import BaseAPITestCase
|
2014-05-02 00:32:30 +04:00
|
|
|
|
|
|
|
|
2014-07-08 13:36:59 +04:00
|
|
|
class APITestCase1(TestCase, BaseAPITestCase):
|
2014-04-30 23:52:05 +04:00
|
|
|
"""
|
2014-07-08 13:36:59 +04:00
|
|
|
Case #1:
|
|
|
|
- user profile: defined
|
|
|
|
- custom registration: backend defined
|
2014-04-30 23:52:05 +04:00
|
|
|
"""
|
|
|
|
|
2015-12-04 11:32:03 +03:00
|
|
|
urls = 'tests.urls'
|
2014-10-08 14:19:15 +04:00
|
|
|
|
2014-04-30 23:52:05 +04:00
|
|
|
USERNAME = 'person'
|
|
|
|
PASS = 'person'
|
2014-05-06 02:53:06 +04:00
|
|
|
EMAIL = "person1@world.com"
|
|
|
|
NEW_PASS = 'new-test-pass'
|
2014-07-08 13:36:59 +04:00
|
|
|
REGISTRATION_VIEW = 'rest_auth.runtests.RegistrationView'
|
|
|
|
|
|
|
|
# data without user profile
|
2014-10-01 16:13:21 +04:00
|
|
|
REGISTRATION_DATA = {
|
2014-07-08 13:36:59 +04:00
|
|
|
"username": USERNAME,
|
2014-10-01 16:13:21 +04:00
|
|
|
"password1": PASS,
|
|
|
|
"password2": PASS
|
2014-07-08 13:36:59 +04:00
|
|
|
}
|
|
|
|
|
2014-10-01 17:31:10 +04:00
|
|
|
REGISTRATION_DATA_WITH_EMAIL = REGISTRATION_DATA.copy()
|
|
|
|
REGISTRATION_DATA_WITH_EMAIL['email'] = EMAIL
|
|
|
|
|
2014-07-08 13:36:59 +04:00
|
|
|
BASIC_USER_DATA = {
|
|
|
|
'first_name': "John",
|
|
|
|
'last_name': 'Smith',
|
|
|
|
'email': EMAIL
|
|
|
|
}
|
|
|
|
USER_DATA = BASIC_USER_DATA.copy()
|
|
|
|
USER_DATA['newsletter_subscribe'] = True
|
2014-05-02 00:32:30 +04:00
|
|
|
|
2014-04-30 23:52:05 +04:00
|
|
|
def setUp(self):
|
|
|
|
self.init()
|
2014-10-02 13:40:07 +04:00
|
|
|
|
|
|
|
def _generate_uid_and_token(self, user):
|
|
|
|
result = {}
|
|
|
|
from django.utils.encoding import force_bytes
|
|
|
|
from django.contrib.auth.tokens import default_token_generator
|
2015-11-24 00:17:32 +03:00
|
|
|
from django.utils.http import urlsafe_base64_encode
|
|
|
|
|
|
|
|
result['uid'] = urlsafe_base64_encode(force_bytes(user.pk))
|
2014-10-02 13:40:07 +04:00
|
|
|
result['token'] = default_token_generator.make_token(user)
|
|
|
|
return result
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
def test_login(self):
|
|
|
|
payload = {
|
|
|
|
"username": self.USERNAME,
|
|
|
|
"password": self.PASS
|
|
|
|
}
|
2014-10-01 17:31:10 +04:00
|
|
|
# there is no users in db so it should throw error (400)
|
|
|
|
self.post(self.login_url, data=payload, status_code=400)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2014-05-02 00:32:30 +04:00
|
|
|
self.post(self.password_change_url, status_code=403)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
# create user
|
2015-04-13 16:19:54 +03:00
|
|
|
user = get_user_model().objects.create_user(self.USERNAME, '', self.PASS)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
self.post(self.login_url, data=payload, status_code=200)
|
|
|
|
self.assertEqual('key' in self.response.json.keys(), True)
|
2014-05-02 00:32:30 +04:00
|
|
|
self.token = self.response.json['key']
|
|
|
|
|
|
|
|
self.post(self.password_change_url, status_code=400)
|
|
|
|
|
|
|
|
# test inactive user
|
|
|
|
user.is_active = False
|
|
|
|
user.save()
|
2014-10-01 17:31:10 +04:00
|
|
|
self.post(self.login_url, data=payload, status_code=400)
|
2014-05-02 00:32:30 +04:00
|
|
|
|
|
|
|
# test wrong username/password
|
|
|
|
payload = {
|
2014-10-01 16:13:21 +04:00
|
|
|
"username": self.USERNAME + '?',
|
2014-05-02 00:32:30 +04:00
|
|
|
"password": self.PASS
|
|
|
|
}
|
2014-10-01 17:31:10 +04:00
|
|
|
self.post(self.login_url, data=payload, status_code=400)
|
2014-05-02 00:32:30 +04:00
|
|
|
|
|
|
|
# test empty payload
|
|
|
|
self.post(self.login_url, data={}, status_code=400)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2015-12-13 23:43:33 +03:00
|
|
|
def test_login_by_email(self):
|
|
|
|
# starting test without allauth app
|
|
|
|
settings.INSTALLED_APPS.remove('allauth')
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
"email": self.EMAIL.lower(),
|
|
|
|
"password": self.PASS
|
|
|
|
}
|
|
|
|
# there is no users in db so it should throw error (400)
|
|
|
|
self.post(self.login_url, data=payload, status_code=400)
|
|
|
|
|
|
|
|
self.post(self.password_change_url, status_code=403)
|
|
|
|
|
|
|
|
# create user
|
|
|
|
user = get_user_model().objects.create_user(self.USERNAME, self.EMAIL, self.PASS)
|
|
|
|
|
|
|
|
# test auth by email
|
|
|
|
self.post(self.login_url, data=payload, status_code=200)
|
|
|
|
self.assertEqual('key' in self.response.json.keys(), True)
|
|
|
|
self.token = self.response.json['key']
|
|
|
|
|
|
|
|
# test auth by email in different case
|
|
|
|
payload = {
|
|
|
|
"email": self.EMAIL.upper(),
|
|
|
|
"password": self.PASS
|
|
|
|
}
|
|
|
|
self.post(self.login_url, data=payload, status_code=200)
|
|
|
|
self.assertEqual('key' in self.response.json.keys(), True)
|
|
|
|
self.token = self.response.json['key']
|
|
|
|
|
|
|
|
# test inactive user
|
|
|
|
user.is_active = False
|
|
|
|
user.save()
|
|
|
|
self.post(self.login_url, data=payload, status_code=400)
|
|
|
|
|
|
|
|
# test wrong email/password
|
|
|
|
payload = {
|
|
|
|
"email": 't' + self.EMAIL,
|
|
|
|
"password": self.PASS
|
|
|
|
}
|
|
|
|
self.post(self.login_url, data=payload, status_code=400)
|
|
|
|
|
|
|
|
# test empty payload
|
|
|
|
self.post(self.login_url, data={}, status_code=400)
|
|
|
|
|
2014-05-02 00:32:30 +04:00
|
|
|
def test_password_change(self):
|
|
|
|
login_payload = {
|
|
|
|
"username": self.USERNAME,
|
|
|
|
"password": self.PASS
|
|
|
|
}
|
2015-04-13 16:19:54 +03:00
|
|
|
get_user_model().objects.create_user(self.USERNAME, '', self.PASS)
|
2014-05-02 00:32:30 +04:00
|
|
|
self.post(self.login_url, data=login_payload, status_code=200)
|
2014-04-30 23:52:05 +04:00
|
|
|
self.token = self.response.json['key']
|
|
|
|
|
2014-05-02 00:32:30 +04:00
|
|
|
new_password_payload = {
|
|
|
|
"new_password1": "new_person",
|
|
|
|
"new_password2": "new_person"
|
|
|
|
}
|
2015-04-28 11:22:08 +03:00
|
|
|
self.post(
|
|
|
|
self.password_change_url,
|
|
|
|
data=new_password_payload,
|
|
|
|
status_code=200
|
|
|
|
)
|
2014-05-02 00:32:30 +04:00
|
|
|
|
|
|
|
# user should not be able to login using old password
|
2014-10-01 17:31:10 +04:00
|
|
|
self.post(self.login_url, data=login_payload, status_code=400)
|
2014-05-02 00:32:30 +04:00
|
|
|
|
|
|
|
# new password should work
|
|
|
|
login_payload['password'] = new_password_payload['new_password1']
|
|
|
|
self.post(self.login_url, data=login_payload, status_code=200)
|
|
|
|
|
|
|
|
# pass1 and pass2 are not equal
|
|
|
|
new_password_payload = {
|
|
|
|
"new_password1": "new_person1",
|
|
|
|
"new_password2": "new_person"
|
|
|
|
}
|
2015-04-28 11:22:08 +03:00
|
|
|
self.post(
|
|
|
|
self.password_change_url,
|
|
|
|
data=new_password_payload,
|
|
|
|
status_code=400
|
|
|
|
)
|
2014-05-02 00:32:30 +04:00
|
|
|
|
|
|
|
# send empty payload
|
|
|
|
self.post(self.password_change_url, data={}, status_code=400)
|
|
|
|
|
2014-11-12 13:51:22 +03:00
|
|
|
@override_settings(OLD_PASSWORD_FIELD_ENABLED=True)
|
|
|
|
def test_password_change_with_old_password(self):
|
|
|
|
login_payload = {
|
|
|
|
"username": self.USERNAME,
|
|
|
|
"password": self.PASS
|
|
|
|
}
|
2015-04-13 16:19:54 +03:00
|
|
|
get_user_model().objects.create_user(self.USERNAME, '', self.PASS)
|
2014-11-12 13:51:22 +03:00
|
|
|
self.post(self.login_url, data=login_payload, status_code=200)
|
|
|
|
self.token = self.response.json['key']
|
|
|
|
|
|
|
|
new_password_payload = {
|
|
|
|
"old_password": "%s!" % self.PASS, # wrong password
|
|
|
|
"new_password1": "new_person",
|
|
|
|
"new_password2": "new_person"
|
|
|
|
}
|
2015-04-28 11:22:08 +03:00
|
|
|
self.post(
|
|
|
|
self.password_change_url,
|
|
|
|
data=new_password_payload,
|
|
|
|
status_code=400
|
|
|
|
)
|
2014-11-12 13:51:22 +03:00
|
|
|
|
|
|
|
new_password_payload = {
|
|
|
|
"old_password": self.PASS,
|
|
|
|
"new_password1": "new_person",
|
|
|
|
"new_password2": "new_person"
|
|
|
|
}
|
2015-04-28 11:22:08 +03:00
|
|
|
self.post(
|
|
|
|
self.password_change_url,
|
|
|
|
data=new_password_payload,
|
|
|
|
status_code=200
|
|
|
|
)
|
2014-11-12 13:51:22 +03:00
|
|
|
|
|
|
|
# user should not be able to login using old password
|
|
|
|
self.post(self.login_url, data=login_payload, status_code=400)
|
|
|
|
|
|
|
|
# new password should work
|
|
|
|
login_payload['password'] = new_password_payload['new_password1']
|
|
|
|
self.post(self.login_url, data=login_payload, status_code=200)
|
|
|
|
|
2014-05-06 02:53:06 +04:00
|
|
|
def test_password_reset(self):
|
2015-04-13 16:19:54 +03:00
|
|
|
user = get_user_model().objects.create_user(self.USERNAME, self.EMAIL, self.PASS)
|
2014-05-06 02:53:06 +04:00
|
|
|
|
|
|
|
# call password reset
|
|
|
|
mail_count = len(mail.outbox)
|
|
|
|
payload = {'email': self.EMAIL}
|
2014-10-07 17:08:08 +04:00
|
|
|
self.post(self.password_reset_url, data=payload, status_code=200)
|
2014-10-01 16:13:21 +04:00
|
|
|
self.assertEqual(len(mail.outbox), mail_count + 1)
|
2014-05-06 02:53:06 +04:00
|
|
|
|
2014-10-02 13:40:07 +04:00
|
|
|
url_kwargs = self._generate_uid_and_token(user)
|
2015-01-27 18:52:54 +03:00
|
|
|
url = reverse('rest_password_reset_confirm')
|
|
|
|
|
|
|
|
# wrong token
|
|
|
|
data = {
|
|
|
|
'new_password1': self.NEW_PASS,
|
|
|
|
'new_password2': self.NEW_PASS,
|
2015-11-19 11:38:57 +03:00
|
|
|
'uid': force_text(url_kwargs['uid']),
|
2015-01-27 18:52:54 +03:00
|
|
|
'token': '-wrong-token-'
|
|
|
|
}
|
|
|
|
self.post(url, data=data, status_code=400)
|
|
|
|
|
|
|
|
# wrong uid
|
|
|
|
data = {
|
|
|
|
'new_password1': self.NEW_PASS,
|
|
|
|
'new_password2': self.NEW_PASS,
|
|
|
|
'uid': '-wrong-uid-',
|
|
|
|
'token': url_kwargs['token']
|
|
|
|
}
|
|
|
|
self.post(url, data=data, status_code=400)
|
|
|
|
|
|
|
|
# wrong token and uid
|
|
|
|
data = {
|
|
|
|
'new_password1': self.NEW_PASS,
|
|
|
|
'new_password2': self.NEW_PASS,
|
|
|
|
'uid': '-wrong-uid-',
|
|
|
|
'token': '-wrong-token-'
|
|
|
|
}
|
|
|
|
self.post(url, data=data, status_code=400)
|
|
|
|
|
|
|
|
# valid payload
|
2014-05-06 02:53:06 +04:00
|
|
|
data = {
|
|
|
|
'new_password1': self.NEW_PASS,
|
2014-10-07 17:08:08 +04:00
|
|
|
'new_password2': self.NEW_PASS,
|
2015-11-19 11:38:57 +03:00
|
|
|
'uid': force_text(url_kwargs['uid']),
|
2014-10-07 17:08:08 +04:00
|
|
|
'token': url_kwargs['token']
|
2014-05-06 02:53:06 +04:00
|
|
|
}
|
2014-10-07 17:08:08 +04:00
|
|
|
url = reverse('rest_password_reset_confirm')
|
2014-05-06 02:53:06 +04:00
|
|
|
self.post(url, data=data, status_code=200)
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
"username": self.USERNAME,
|
|
|
|
"password": self.NEW_PASS
|
|
|
|
}
|
|
|
|
self.post(self.login_url, data=payload, status_code=200)
|
|
|
|
|
2015-11-24 00:17:32 +03:00
|
|
|
def test_password_reset_with_invalid_email(self):
|
|
|
|
get_user_model().objects.create_user(self.USERNAME, self.EMAIL, self.PASS)
|
|
|
|
|
|
|
|
# call password reset
|
|
|
|
mail_count = len(mail.outbox)
|
|
|
|
payload = {'email': 'nonexisting@email.com'}
|
|
|
|
self.post(self.password_reset_url, data=payload, status_code=400)
|
|
|
|
self.assertEqual(len(mail.outbox), mail_count)
|
|
|
|
|
2014-07-08 13:36:59 +04:00
|
|
|
def test_user_details(self):
|
2015-04-13 16:19:54 +03:00
|
|
|
user = get_user_model().objects.create_user(self.USERNAME, self.EMAIL, self.PASS)
|
2014-07-08 13:36:59 +04:00
|
|
|
payload = {
|
|
|
|
"username": self.USERNAME,
|
|
|
|
"password": self.PASS
|
|
|
|
}
|
|
|
|
self.post(self.login_url, data=payload, status_code=200)
|
|
|
|
self.token = self.response.json['key']
|
|
|
|
self.get(self.user_url, status_code=200)
|
|
|
|
|
2014-10-02 13:18:23 +04:00
|
|
|
self.patch(self.user_url, data=self.BASIC_USER_DATA, status_code=200)
|
2015-04-13 16:19:54 +03:00
|
|
|
user = get_user_model().objects.get(pk=user.pk)
|
2014-10-02 13:40:07 +04:00
|
|
|
self.assertEqual(user.first_name, self.response.json['first_name'])
|
|
|
|
self.assertEqual(user.last_name, self.response.json['last_name'])
|
|
|
|
self.assertEqual(user.email, self.response.json['email'])
|
2014-05-06 02:53:06 +04:00
|
|
|
|
2014-10-01 16:13:21 +04:00
|
|
|
def test_registration(self):
|
2015-04-13 16:19:54 +03:00
|
|
|
user_count = get_user_model().objects.all().count()
|
2014-07-08 13:36:59 +04:00
|
|
|
|
2014-10-01 16:13:21 +04:00
|
|
|
# test empty payload
|
|
|
|
self.post(self.register_url, data={}, status_code=400)
|
2014-07-08 13:36:59 +04:00
|
|
|
|
2014-10-01 16:13:21 +04:00
|
|
|
self.post(self.register_url, data=self.REGISTRATION_DATA, status_code=201)
|
2015-04-13 16:19:54 +03:00
|
|
|
self.assertEqual(get_user_model().objects.all().count(), user_count + 1)
|
2014-10-01 16:13:21 +04:00
|
|
|
new_user = get_user_model().objects.latest('id')
|
|
|
|
self.assertEqual(new_user.username, self.REGISTRATION_DATA['username'])
|
2014-10-01 17:31:10 +04:00
|
|
|
|
2014-10-02 13:40:07 +04:00
|
|
|
self._login()
|
|
|
|
self._logout()
|
2014-10-01 17:31:10 +04:00
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
ACCOUNT_EMAIL_VERIFICATION='mandatory',
|
|
|
|
ACCOUNT_EMAIL_REQUIRED=True
|
|
|
|
)
|
|
|
|
def test_registration_with_email_verification(self):
|
2015-04-13 16:19:54 +03:00
|
|
|
user_count = get_user_model().objects.all().count()
|
2014-10-01 17:31:10 +04:00
|
|
|
mail_count = len(mail.outbox)
|
|
|
|
|
|
|
|
# test empty payload
|
2015-04-28 11:22:08 +03:00
|
|
|
self.post(
|
|
|
|
self.register_url,
|
|
|
|
data={},
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST
|
|
|
|
)
|
2014-10-01 17:31:10 +04:00
|
|
|
|
2015-04-28 11:22:08 +03:00
|
|
|
self.post(
|
|
|
|
self.register_url,
|
|
|
|
data=self.REGISTRATION_DATA_WITH_EMAIL,
|
|
|
|
status_code=status.HTTP_201_CREATED
|
|
|
|
)
|
2015-04-13 16:19:54 +03:00
|
|
|
self.assertEqual(get_user_model().objects.all().count(), user_count + 1)
|
2014-10-01 17:31:10 +04:00
|
|
|
self.assertEqual(len(mail.outbox), mail_count + 1)
|
|
|
|
new_user = get_user_model().objects.latest('id')
|
|
|
|
self.assertEqual(new_user.username, self.REGISTRATION_DATA['username'])
|
|
|
|
|
|
|
|
# email is not verified yet
|
|
|
|
payload = {
|
|
|
|
"username": self.USERNAME,
|
|
|
|
"password": self.PASS
|
|
|
|
}
|
2015-04-28 11:22:08 +03:00
|
|
|
self.post(
|
|
|
|
self.login_url,
|
|
|
|
data=payload,
|
|
|
|
status=status.HTTP_400_BAD_REQUEST
|
|
|
|
)
|
2014-10-01 18:34:51 +04:00
|
|
|
|
2015-04-28 11:22:08 +03:00
|
|
|
# verify email
|
2014-10-01 18:34:51 +04:00
|
|
|
email_confirmation = new_user.emailaddress_set.get(email=self.EMAIL)\
|
2014-10-02 13:23:52 +04:00
|
|
|
.emailconfirmation_set.order_by('-created')[0]
|
2015-04-28 11:22:08 +03:00
|
|
|
self.post(
|
|
|
|
self.veirfy_email_url,
|
|
|
|
data={"key": email_confirmation.key},
|
|
|
|
status_code=status.HTTP_200_OK
|
|
|
|
)
|
2014-10-01 18:34:51 +04:00
|
|
|
|
|
|
|
# try to login again
|
2014-10-02 13:40:07 +04:00
|
|
|
self._login()
|
|
|
|
self._logout()
|