mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2024-11-25 18:43:44 +03:00
cleanup tests
This commit is contained in:
parent
34d3627c7c
commit
ff9fd1c3c1
|
@ -125,7 +125,6 @@ class APITestCase1(TestCase, BaseAPITestCase):
|
||||||
PASS = 'person'
|
PASS = 'person'
|
||||||
EMAIL = "person1@world.com"
|
EMAIL = "person1@world.com"
|
||||||
NEW_PASS = 'new-test-pass'
|
NEW_PASS = 'new-test-pass'
|
||||||
PROFILE_MODEL = 'rest_auth.UserProfile'
|
|
||||||
REGISTRATION_VIEW = 'rest_auth.runtests.RegistrationView'
|
REGISTRATION_VIEW = 'rest_auth.runtests.RegistrationView'
|
||||||
|
|
||||||
# data without user profile
|
# data without user profile
|
||||||
|
@ -149,21 +148,36 @@ class APITestCase1(TestCase, BaseAPITestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.init()
|
self.init()
|
||||||
self.login_url = reverse('rest_login')
|
self.login_url = reverse('rest_login')
|
||||||
|
self.logout_url = reverse('rest_logout')
|
||||||
self.password_change_url = reverse('rest_password_change')
|
self.password_change_url = reverse('rest_password_change')
|
||||||
self.register_url = reverse('rest_register')
|
self.register_url = reverse('rest_register')
|
||||||
self.password_reset_url = reverse('rest_password_reset')
|
self.password_reset_url = reverse('rest_password_reset')
|
||||||
self.user_url = reverse('rest_user_details')
|
self.user_url = reverse('rest_user_details')
|
||||||
self.veirfy_email_url = reverse('verify_email')
|
self.veirfy_email_url = reverse('verify_email')
|
||||||
|
|
||||||
setattr(settings, 'REST_PROFILE_MODULE', self.PROFILE_MODEL)
|
def _login(self):
|
||||||
self.user_profile_model = None
|
payload = {
|
||||||
if self.PROFILE_MODEL:
|
"username": self.USERNAME,
|
||||||
self.user_profile_model = _resolve_model(self.PROFILE_MODEL)
|
"password": self.PASS
|
||||||
|
}
|
||||||
|
self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK)
|
||||||
|
|
||||||
if self.REGISTRATION_VIEW:
|
def _logout(self):
|
||||||
setattr(settings, 'REST_REGISTRATION_BACKEND', self.REGISTRATION_VIEW)
|
self.post(self.logout_url, status=status.HTTP_200_OK)
|
||||||
elif hasattr(settings, 'REST_REGISTRATION_BACKEND'):
|
|
||||||
delattr(settings, 'REST_REGISTRATION_BACKEND')
|
def _generate_uid_and_token(self, user):
|
||||||
|
result = {}
|
||||||
|
from django.utils.encoding import force_bytes
|
||||||
|
from django.contrib.auth.tokens import default_token_generator
|
||||||
|
from django import VERSION
|
||||||
|
if VERSION[1] == 6:
|
||||||
|
from django.utils.http import urlsafe_base64_encode
|
||||||
|
result['uid'] = urlsafe_base64_encode(force_bytes(user.pk))
|
||||||
|
elif VERSION[1] == 5:
|
||||||
|
from django.utils.http import int_to_base36
|
||||||
|
result['uid'] = int_to_base36(user.pk)
|
||||||
|
result['token'] = default_token_generator.make_token(user)
|
||||||
|
return result
|
||||||
|
|
||||||
def test_login(self):
|
def test_login(self):
|
||||||
payload = {
|
payload = {
|
||||||
|
@ -242,7 +256,7 @@ class APITestCase1(TestCase, BaseAPITestCase):
|
||||||
self.post(self.password_reset_url, data=payload)
|
self.post(self.password_reset_url, data=payload)
|
||||||
self.assertEqual(len(mail.outbox), mail_count + 1)
|
self.assertEqual(len(mail.outbox), mail_count + 1)
|
||||||
|
|
||||||
url_kwargs = self.generate_uid_and_token(user)
|
url_kwargs = self._generate_uid_and_token(user)
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'new_password1': self.NEW_PASS,
|
'new_password1': self.NEW_PASS,
|
||||||
|
@ -259,8 +273,6 @@ class APITestCase1(TestCase, BaseAPITestCase):
|
||||||
|
|
||||||
def test_user_details(self):
|
def test_user_details(self):
|
||||||
user = User.objects.create_user(self.USERNAME, self.EMAIL, self.PASS)
|
user = User.objects.create_user(self.USERNAME, self.EMAIL, self.PASS)
|
||||||
if self.user_profile_model:
|
|
||||||
self.user_profile_model.objects.create(user=user)
|
|
||||||
payload = {
|
payload = {
|
||||||
"username": self.USERNAME,
|
"username": self.USERNAME,
|
||||||
"password": self.PASS
|
"password": self.PASS
|
||||||
|
@ -271,33 +283,10 @@ class APITestCase1(TestCase, BaseAPITestCase):
|
||||||
|
|
||||||
self.patch(self.user_url, data=self.BASIC_USER_DATA, status_code=200)
|
self.patch(self.user_url, data=self.BASIC_USER_DATA, status_code=200)
|
||||||
user = User.objects.get(pk=user.pk)
|
user = User.objects.get(pk=user.pk)
|
||||||
|
|
||||||
if self.user_profile_model:
|
|
||||||
self.post(self.user_url, data=self.USER_DATA, status_code=200)
|
|
||||||
user = User.objects.get(pk=user.pk)
|
|
||||||
self.assertEqual(user.first_name, self.response.json['user']['first_name'])
|
|
||||||
self.assertEqual(user.last_name, self.response.json['user']['last_name'])
|
|
||||||
self.assertEqual(user.email, self.response.json['user']['email'])
|
|
||||||
self.assertIn('newsletter_subscribe', self.response.json)
|
|
||||||
else:
|
|
||||||
self.assertEqual(user.first_name, self.response.json['first_name'])
|
self.assertEqual(user.first_name, self.response.json['first_name'])
|
||||||
self.assertEqual(user.last_name, self.response.json['last_name'])
|
self.assertEqual(user.last_name, self.response.json['last_name'])
|
||||||
self.assertEqual(user.email, self.response.json['email'])
|
self.assertEqual(user.email, self.response.json['email'])
|
||||||
|
|
||||||
def generate_uid_and_token(self, user):
|
|
||||||
result = {}
|
|
||||||
from django.utils.encoding import force_bytes
|
|
||||||
from django.contrib.auth.tokens import default_token_generator
|
|
||||||
from django import VERSION
|
|
||||||
if VERSION[1] == 6:
|
|
||||||
from django.utils.http import urlsafe_base64_encode
|
|
||||||
result['uid'] = urlsafe_base64_encode(force_bytes(user.pk))
|
|
||||||
elif VERSION[1] == 5:
|
|
||||||
from django.utils.http import int_to_base36
|
|
||||||
result['uid'] = int_to_base36(user.pk)
|
|
||||||
result['token'] = default_token_generator.make_token(user)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def test_registration(self):
|
def test_registration(self):
|
||||||
user_count = User.objects.all().count()
|
user_count = User.objects.all().count()
|
||||||
|
|
||||||
|
@ -309,11 +298,8 @@ class APITestCase1(TestCase, BaseAPITestCase):
|
||||||
new_user = get_user_model().objects.latest('id')
|
new_user = get_user_model().objects.latest('id')
|
||||||
self.assertEqual(new_user.username, self.REGISTRATION_DATA['username'])
|
self.assertEqual(new_user.username, self.REGISTRATION_DATA['username'])
|
||||||
|
|
||||||
payload = {
|
self._login()
|
||||||
"username": self.USERNAME,
|
self._logout()
|
||||||
"password": self.PASS
|
|
||||||
}
|
|
||||||
self.post(self.login_url, data=payload, status_code=200)
|
|
||||||
|
|
||||||
@override_settings(
|
@override_settings(
|
||||||
ACCOUNT_EMAIL_VERIFICATION='mandatory',
|
ACCOUNT_EMAIL_VERIFICATION='mandatory',
|
||||||
|
@ -349,4 +335,5 @@ class APITestCase1(TestCase, BaseAPITestCase):
|
||||||
status_code=status.HTTP_200_OK)
|
status_code=status.HTTP_200_OK)
|
||||||
|
|
||||||
# try to login again
|
# try to login again
|
||||||
self.post(self.login_url, data=payload, status_code=status.HTTP_200_OK)
|
self._login()
|
||||||
|
self._logout()
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm
|
from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm
|
||||||
from django.contrib.auth import authenticate, login, logout, get_user_model
|
from django.contrib.auth import login, logout, get_user_model
|
||||||
from django.contrib.auth.tokens import default_token_generator
|
from django.contrib.auth.tokens import default_token_generator
|
||||||
try:
|
try:
|
||||||
from django.utils.http import urlsafe_base64_decode as uid_decoder
|
from django.utils.http import urlsafe_base64_decode as uid_decoder
|
||||||
|
@ -12,7 +12,6 @@ from rest_framework import status
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.generics import GenericAPIView
|
from rest_framework.generics import GenericAPIView
|
||||||
from rest_framework.serializers import _resolve_model
|
|
||||||
from rest_framework.permissions import IsAuthenticated, AllowAny
|
from rest_framework.permissions import IsAuthenticated, AllowAny
|
||||||
from rest_framework.authentication import SessionAuthentication, \
|
from rest_framework.authentication import SessionAuthentication, \
|
||||||
TokenAuthentication
|
TokenAuthentication
|
||||||
|
@ -23,14 +22,6 @@ from rest_auth.serializers import (TokenSerializer, UserDetailsSerializer,
|
||||||
LoginSerializer, SetPasswordSerializer, PasswordResetSerializer)
|
LoginSerializer, SetPasswordSerializer, PasswordResetSerializer)
|
||||||
|
|
||||||
|
|
||||||
def get_user_profile_model():
|
|
||||||
# Get the UserProfile model from the setting value
|
|
||||||
user_profile_path = getattr(settings, 'REST_PROFILE_MODULE', None)
|
|
||||||
if user_profile_path:
|
|
||||||
setattr(settings, 'AUTH_PROFILE_MODULE', user_profile_path)
|
|
||||||
return _resolve_model(user_profile_path)
|
|
||||||
|
|
||||||
|
|
||||||
class LoggedInRESTAPIView(APIView):
|
class LoggedInRESTAPIView(APIView):
|
||||||
authentication_classes = ((SessionAuthentication, TokenAuthentication))
|
authentication_classes = ((SessionAuthentication, TokenAuthentication))
|
||||||
permission_classes = ((IsAuthenticated,))
|
permission_classes = ((IsAuthenticated,))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user