Skip to content
Snippets Groups Projects
Unverified Commit cde8e027 authored by Todd Dembrey's avatar Todd Dembrey Committed by GitHub
Browse files

Merge pull request #427 from OpenTechFund/bugfix/91-activation-email-error

Fix the issue with bytes in urls
parents b300bffb 5a2c5f41
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ Dear {{ name|default:username }}, ...@@ -4,7 +4,7 @@ Dear {{ name|default:username }},
An account on Open Technology Fund has been created. Activate your account by clicking this link or copying and pasting it to your browser: An account on Open Technology Fund has been created. Activate your account by clicking this link or copying and pasting it to your browser:
{% if site %}{{ site.root_url }}{% else %}{{ base_url }}{% endif %}{% url 'users:activate' uidb64=uid token=token %} {% if site %}{{ site.root_url }}{% else %}{{ base_url }}{% endif %}{{ activation_path }}
This link can be used once to log in and will lead you to a page where you can set your password. This link can be used once to log in and will lead you to a page where you can set your password.
......
Account details for {{ username }} at Open Technology Fund
from django.core import mail
from django.test import TestCase
from opentech.apply.users.tests.factories import UserFactory
from ..utils import send_activation_email
class TestActivationEmail(TestCase):
def test_activation_email_includes_link(self):
send_activation_email(UserFactory())
self.assertEqual(len(mail.outbox), 1)
...@@ -3,6 +3,7 @@ from django.contrib.auth.tokens import PasswordResetTokenGenerator ...@@ -3,6 +3,7 @@ from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode from django.utils.http import urlsafe_base64_encode
from django.urls import reverse
def can_use_oauth_check(user): def can_use_oauth_check(user):
...@@ -20,23 +21,29 @@ def can_use_oauth_check(user): ...@@ -20,23 +21,29 @@ def can_use_oauth_check(user):
return False return False
def send_activation_email(user, site): def send_activation_email(user, site=None):
""" """
Send the activation email. The activation key is the username, Send the activation email. The activation key is the username,
signed using TimestampSigner. signed using TimestampSigner.
""" """
token_generator = PasswordResetTokenGenerator() token_generator = PasswordResetTokenGenerator()
token = token_generator.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk)).decode()
activation_path = reverse('users:activate', kwargs={'uidb64': uid, 'token': token})
context = { context = {
'user': user, 'user': user,
'name': user.get_full_name(), 'name': user.get_full_name(),
'username': user.get_username(), 'username': user.get_username(),
'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'activation_path': activation_path,
'token': token_generator.make_token(user),
} }
if site: if site:
context.update(site=site) context.update(site=site)
subject = render_to_string('users/activation/email_subject.txt', context) subject = 'Account details for {username} at Open Technology Fund'.format(**context)
# Force subject to a single line to avoid header-injection issues. # Force subject to a single line to avoid header-injection issues.
subject = ''.join(subject.splitlines()) subject = ''.join(subject.splitlines())
message = render_to_string('users/activation/email.txt', context) message = render_to_string('users/activation/email.txt', context)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment