本文整理匯總了Python中django.contrib.auth.tokens.default_token_generator.make_token方法的典型用法代碼示例。如果您正苦於以下問題:Python default_token_generator.make_token方法的具體用法?Python default_token_generator.make_token怎麽用?Python default_token_generator.make_token使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.auth.tokens.default_token_generator
的用法示例。
在下文中一共展示了default_token_generator.make_token方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_reset_user_password
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_reset_user_password(self):
# set user.last_login, ensures we get same/valid token
# https://code.djangoproject.com/ticket/10265
self.user.last_login = now()
self.user.save()
token = default_token_generator.make_token(self.user)
new_password = "bobbob1"
data = {'token': token, 'new_password': new_password}
# missing uid, should fail
request = self.factory.post('/', data=data)
response = self.view(request)
self.assertEqual(response.status_code, 400)
data['uid'] = urlsafe_base64_encode(force_bytes(self.user.pk))
# with uid, should be successful
request = self.factory.post('/', data=data)
response = self.view(request)
self.assertEqual(response.status_code, 204)
user = User.objects.get(email=self.user.email)
self.assertTrue(user.check_password(new_password))
request = self.factory.post('/', data=data)
response = self.view(request)
self.assertEqual(response.status_code, 400)
示例2: test_request_reset_password_email_for_a_existing_user
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_request_reset_password_email_for_a_existing_user(user_factory, graphql_client):
user = user_factory()
response = _request_password_reset(graphql_client, user.email)
assert response["data"]["requestPasswordReset"]["__typename"] == "OperationResult"
assert response["data"]["requestPasswordReset"]["ok"] is True
assert len(mail.outbox) == 1
email = mail.outbox[0]
html_body = email.alternatives[0][0]
token = default_token_generator.make_token(user)
userid = urlsafe_b64encode(bytes(str(user.id), "utf-8")).decode("utf-8")
assert email.to == [user.email]
assert f"http://test.it/en/reset-password/{userid}/{token}" in email.body
assert f"http://test.it/en/reset-password/{userid}/{token}" in html_body
示例3: test_cannot_reuse_same_reset_password_token_multiple_times
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_cannot_reuse_same_reset_password_token_multiple_times(
user_factory, graphql_client
):
user = user_factory(password="old")
token = default_token_generator.make_token(user)
assert user.check_password("old")
response = _reset_password(graphql_client, token, user.id, "new")
assert response["data"]["resetPassword"]["__typename"] == "OperationResult"
assert response["data"]["resetPassword"]["ok"] is True
user.refresh_from_db()
assert user.check_password("new")
response = _reset_password(graphql_client, token, user.id, "another")
assert (
response["data"]["resetPassword"]["__typename"] == "ResetPasswordMutationErrors"
)
assert response["data"]["resetPassword"]["token"] == ["Invalid token"]
示例4: password_reset
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def password_reset(request):
"""REST API reset password view"""
serializer = UsernameSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
username = serializer.validated_data.get('username')
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
pass
else:
context = {
'email': username,
'domain': get_current_site(request).domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'user': user,
'token': default_token_generator.make_token(user),
'protocol': 'https' if request.is_secure() else 'http',
}
send_mail(
request,
'password_reset',
[username],
context=context,
send_copy_to_admin=False)
return Response(dict(), status=status.HTTP_201_CREATED)
示例5: test_post_readable_error_message_when_uid_is_broken
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_post_readable_error_message_when_uid_is_broken(self):
"""
Regression test for https://github.com/sunscrapers/djoser/issues/122
When uid was not correct unicode string, error message was a
standard Python error messsage. Now we provide human readable message.
"""
user = create_user()
data = {
"uid": b"\xd3\x10\xb4",
"token": default_token_generator.make_token(user),
"new_password": "new password",
}
response = self.client.post(self.base_url, data)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertIn("uid", response.data)
self.assertEqual(len(response.data["uid"]), 1)
self.assertEqual(
response.data["uid"][0],
default_settings.CONSTANTS.messages.INVALID_UID_ERROR,
)
示例6: test_post_not_set_new_password_if_password_mismatch
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_post_not_set_new_password_if_password_mismatch(self):
user = create_user()
data = {
"uid": djoser.utils.encode_uid(user.pk),
"token": default_token_generator.make_token(user),
"new_password": "new password",
"re_new_password": "wrong",
}
response = self.client.post(self.base_url, data)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data["non_field_errors"],
[default_settings.CONSTANTS.messages.PASSWORD_MISMATCH_ERROR],
)
示例7: test_post_readable_error_message_when_uid_is_broken
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_post_readable_error_message_when_uid_is_broken(self):
user = create_user()
data = {
"uid": b"\xd3\x10\xb4",
"token": default_token_generator.make_token(user),
"new_username": "new_username",
}
response = self.client.post(self.base_url, data)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertIn("uid", response.data)
self.assertEqual(len(response.data["uid"]), 1)
self.assertEqual(
response.data["uid"][0],
default_settings.CONSTANTS.messages.INVALID_UID_ERROR,
)
示例8: test_post_not_set_new_username_if_username_mismatch
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_post_not_set_new_username_if_username_mismatch(self):
user = create_user()
data = {
"uid": djoser.utils.encode_uid(user.pk),
"token": default_token_generator.make_token(user),
"new_username": "new_username",
"re_new_username": "wrong",
}
response = self.client.post(self.base_url, data)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data["non_field_errors"],
[
default_settings.CONSTANTS.messages.USERNAME_MISMATCH_ERROR.format(
User.USERNAME_FIELD
)
], # noqa
)
示例9: test_post_respond_with_bad_request_when_stale_token
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_post_respond_with_bad_request_when_stale_token(self):
user = create_user()
djoser.signals.user_activated.connect(self.signal_receiver)
data = {
"uid": djoser.utils.encode_uid(user.pk),
"token": default_token_generator.make_token(user),
}
response = self.client.post(self.base_url, data)
self.assert_status_equal(response, status.HTTP_403_FORBIDDEN)
self.assertEqual(list(response.data.keys()), ["detail"])
self.assertEqual(
response.data["detail"],
default_settings.CONSTANTS.messages.STALE_TOKEN_ERROR,
)
self.assertFalse(self.signal_sent)
示例10: test_post_sent_confirmation_email
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_post_sent_confirmation_email(self):
user = create_user()
user.is_active = False
user.save()
djoser.signals.user_activated.connect(self.signal_receiver)
data = {
"uid": djoser.utils.encode_uid(user.pk),
"token": default_token_generator.make_token(user),
}
response = self.client.post(self.base_url, data)
self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
self.assert_emails_in_mailbox(1)
self.assert_email_exists(to=[user.email])
self.assertTrue(self.signal_sent)
示例11: test_put
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_put(self):
old_password = '0ld_passworD'
new_password = 'n3w_Password'
user = UserFactory.create(password=old_password)
token = default_token_generator.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk))
request = self.create_request(
'put',
data={'new_password': new_password, 'new_password2': new_password},
auth=False,
)
view = self.view_class.as_view()
response = view(request, uidb64=uid, token=token)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Get the updated user from the db
user = User.objects.get(pk=user.pk)
self.assertTrue(user.check_password(new_password))
示例12: test_password_mismatch
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_password_mismatch(self):
old_password = '0ld_passworD'
new_password = 'n3w_Password'
invalid_password = 'different_new_password'
user = UserFactory.create(password=old_password)
token = default_token_generator.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk))
request = self.create_request(
'put',
data={
'new_password': new_password,
'new_password2': invalid_password,
},
auth=False,
)
view = self.view_class.as_view()
response = view(request, uidb64=uid, token=token)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
# Get the updated user from the db
user = User.objects.get(pk=user.pk)
self.assertTrue(user.check_password(old_password))
示例13: test_recupero_password_link_valido
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def test_recupero_password_link_valido(self):
presidente = crea_persona()
persona, sede, app = crea_persona_sede_appartenenza(presidente=presidente)
persona_in_sede = crea_persona()
utenza_persona_in_sede = crea_utenza(persona_in_sede)
appartenenza_persona_in_sede = crea_appartenenza(persona, sede)
uid = urlsafe_base64_encode(force_bytes(utenza_persona_in_sede.pk))
reset_pw_link = default_token_generator.make_token(utenza_persona_in_sede)
sessione = self.sessione_anonimo()
sessione.visit("%s%s" % (self.live_server_url, reverse('recupera_password_conferma', kwargs={ 'uidb64': uid, 'token': reset_pw_link})))
sessione.fill('new_password1', 'new_password')
sessione.fill('new_password2', 'new_password')
sessione.find_by_css('.btn.btn-block.btn-primary').first.click()
self.assertTrue(sessione.is_text_present('La tua nuova password è stata impostata'))
sessione.visit("%s%s" % (self.live_server_url, '/login/'))
sessione.fill('auth-username', utenza_persona_in_sede.email)
sessione.fill('auth-password', 'new_password')
sessione.find_by_css('.btn.btn-block.btn-primary').first.click()
testo_personalizzato = 'Ciao, {0}'.format(persona_in_sede.nome)
self.assertTrue(sessione.is_text_present(testo_personalizzato))
示例14: get_serializer_context
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def get_serializer_context(self):
user = self.request.user
if not user.pk:
return
uid = urlsafe_base64_encode(force_bytes(user.pk))
# until django 2.1 urlsafe_base64_encode returned a bytestring
if not isinstance(uid, str): # noqa
uid = uid.decode()
token = default_token_generator.make_token(user)
password_reset_urls = app_settings.PASSWORD_RESET_URLS
default_url = password_reset_urls.get('default')
password_reset_url = password_reset_urls.get(
str(self.organization.pk), default_url
)
password_reset_url = password_reset_url.format(
organization=self.organization.slug, uid=uid, token=token
)
context = {'request': self.request, 'password_reset_url': password_reset_url}
return context
示例15: save
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import make_token [as 別名]
def save(self):
user = User.objects.filter(email=self.cleaned_data["email"]).first()
if not user:
return True
token = default_token_generator.make_token(user)
return send_request_password_reset_mail(user, token) == 1