forked from saleor/saleor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_registration.py
133 lines (96 loc) · 4.11 KB
/
test_registration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import pytest
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser
from django.core.urlresolvers import reverse
from saleor.registration.forms import LoginForm, SignupForm
from saleor.registration.backends import BaseBackend
from .utils import get_redirect_location
User = get_user_model()
@pytest.fixture
def base_backend():
base_backend = BaseBackend()
base_backend.DB_NAME = 'Backend'
return base_backend
def test_login_form_valid(customer_user):
data = {'username': '[email protected]', 'password': 'password'}
form = LoginForm(data=data)
assert form.is_valid()
assert form.get_user() == customer_user
def test_login_form_not_valid(customer_user):
data = {'user': '[email protected]', 'password': 'wrongpassword'}
form = LoginForm(data=data)
assert not form.is_valid()
assert form.get_user_id() is None
def test_login_view_valid(client, customer_user):
url = reverse('account_login')
response = client.post(
url, {'username': '[email protected]', 'password': 'password'},
follow=True)
assert response.context['user'] == customer_user
def test_login_view_not_valid(client, customer_user):
url = reverse('account_login')
response = client.post(
url, {'username': '[email protected]', 'password': 'wrong'},
follow=True)
assert isinstance(response.context['user'], AnonymousUser)
def test_login_view_next(client, customer_user):
url = reverse('account_login') + '?next=/cart/'
response = client.post(
url, {'username': '[email protected]', 'password': 'password'})
redirect_location = get_redirect_location(response)
assert redirect_location == '/cart/'
def test_logout_view_no_user(client):
url = reverse('account_logout')
response = client.get(url)
redirect_location = get_redirect_location(response)
location = '/account/login/'
assert location in redirect_location
def test_logout_with_user(authorized_client):
url = reverse('account_logout')
response = authorized_client.get(url, follow=True)
assert isinstance(response.context['user'], AnonymousUser)
def test_signup_form_empty():
form = SignupForm({})
assert not form.is_valid()
def test_signup_form_not_valid():
data = {'email': 'admin@example', 'password': 'password'}
form = SignupForm(data)
assert not form.is_valid()
assert 'email' in form.errors
def test_signup_form_user_exists(customer_user):
data = {'email': customer_user.email, 'password': 'password'}
form = SignupForm(data)
assert not form.is_valid()
error_message = 'User with this Email already exists.'
assert form.errors['email'] == [error_message]
def test_signup_view_create_user(client, db):
url = reverse('account_signup')
data = {'email': '[email protected]', 'password': 'password'}
response = client.post(url, data)
assert User.objects.count() == 1
assert User.objects.filter(email='[email protected]').exists()
redirect_location = get_redirect_location(response)
assert redirect_location == '/'
def test_signup_view_fail(client, db, customer_user):
url = reverse('account_signup')
data = {'email': customer_user.email, 'password': 'password'}
client.post(url, data)
assert User.objects.count() == 1
def test_password_reset_view_post(client, db):
url = reverse('account_reset_password')
data = {'email': '[email protected]'}
response = client.post(url, data)
redirect_location = get_redirect_location(response)
assert redirect_location == reverse('account_reset_password_done')
def test_password_reset_view_get(client, db):
url = reverse('account_reset_password')
response = client.get(url)
assert response.status_code == 200
assert response.template_name == ['account/password_reset.html']
def test_base_backend(authorization_key, base_backend):
key, secret = base_backend.get_key_and_secret()
assert key == 'Key'
assert secret == 'Password'
def test_backend_no_settings(settings, authorization_key, base_backend):
settings.SITE_SETTINGS_ID = None
assert base_backend.get_key_and_secret() is None