-
Notifications
You must be signed in to change notification settings - Fork 150
/
helpers.py
86 lines (72 loc) · 2.53 KB
/
helpers.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
from django.contrib.auth.models import User
try:
from unittest.mock import patch
except:
# Python 2.7 fallback
from mock import patch
# try getting reverse from django.urls
try:
# Django 1.10 +
from django.urls import reverse
except:
# Django 1.8 and 1.9
from django.core.urlresolvers import reverse
__all__ = [
"HelperMixin",
"patch"
]
class HelperMixin:
"""
Mixin which encapsulates methods for login, logout, request reset password and reset password confirm
"""
def setUpUrls(self):
""" set up urls by using djangos reverse function """
self.reset_password_request_url = reverse('password_reset:reset-password-request')
self.reset_password_confirm_url = reverse('password_reset:reset-password-confirm')
self.reset_password_validate_token_url = reverse('password_reset:reset-password-validate')
def django_check_login(self, username, password):
"""
Checks the django login by querying the user from the database and calling check_password()
:param username:
:param password:
:return:
"""
user = User.objects.filter(username=username).first()
return user.check_password(password)
def rest_do_request_reset_token(self, email, HTTP_USER_AGENT='', REMOTE_ADDR='127.0.0.1'):
""" REST API wrapper for requesting a password reset token """
data = {
'email': email
}
return self.client.post(
self.reset_password_request_url,
data,
format='json',
HTTP_USER_AGENT=HTTP_USER_AGENT,
REMOTE_ADDR=REMOTE_ADDR
)
def rest_do_reset_password_with_token(self, token, new_password, HTTP_USER_AGENT='', REMOTE_ADDR='127.0.0.1'):
""" REST API wrapper for requesting a password reset token """
data = {
'token': token,
'password': new_password
}
return self.client.post(
self.reset_password_confirm_url,
data,
format='json',
HTTP_USER_AGENT=HTTP_USER_AGENT,
REMOTE_ADDR=REMOTE_ADDR
)
def rest_do_validate_token(self, token, HTTP_USER_AGENT='', REMOTE_ADDR='127.0.0.1'):
""" REST API wrapper for validating a token """
data = {
'token': token
}
return self.client.post(
self.reset_password_validate_token_url,
data,
format='json',
HTTP_USER_AGENT=HTTP_USER_AGENT,
REMOTE_ADDR=REMOTE_ADDR
)