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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
"""
Tests for the signed-token activation registration workflow.
"""
import datetime
import time
from django.apps import apps
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core import signing
from django.http import HttpRequest
from django.test import modify_settings, override_settings
from django.urls import reverse
from django_registration import signals
from django_registration.backends.activation.views import (
REGISTRATION_SALT,
ActivationView,
)
from .base import ActivationTestCase
@modify_settings(INSTALLED_APPS={"remove": "django_registration"})
@override_settings(ROOT_URLCONF="django_registration.backends.activation.urls")
class ActivationBackendViewTests(ActivationTestCase):
"""
Tests for the signed-token activation registration workflow.
"""
def test_activation(self):
"""
Activation of an account functions properly.
"""
user_model = get_user_model()
resp = self.client.post(
reverse("django_registration_register"), data=self.valid_data
)
activation_key = signing.dumps(
obj=self.valid_data[user_model.USERNAME_FIELD], salt=REGISTRATION_SALT
)
with self.assertSignalSent(signals.user_activated):
resp = self.client.get(
reverse(
"django_registration_activate",
args=(),
kwargs={"activation_key": activation_key},
)
)
self.assertRedirects(resp, reverse("django_registration_activation_complete"))
def test_repeat_activation(self):
"""
Once activated, attempting to re-activate an account (even
with a valid key) does nothing.
"""
user_model = get_user_model()
resp = self.client.post(
reverse("django_registration_register"), data=self.valid_data
)
activation_key = signing.dumps(
obj=self.valid_data[user_model.USERNAME_FIELD], salt=REGISTRATION_SALT
)
with self.assertSignalSent(signals.user_activated):
resp = self.client.get(
reverse(
"django_registration_activate",
args=(),
kwargs={"activation_key": activation_key},
)
)
# First activation redirects to success.
self.assertRedirects(resp, reverse("django_registration_activation_complete"))
with self.assertSignalNotSent(signals.user_activated):
resp = self.client.get(
reverse(
"django_registration_activate",
args=(),
kwargs={"activation_key": activation_key},
)
)
# Second activation fails.
self.assertEqual(200, resp.status_code)
self.assertTemplateUsed(resp, "django_registration/activation_failed.html")
self.assertEqual(
resp.context["activation_error"],
{
"message": ActivationView.ALREADY_ACTIVATED_MESSAGE,
"code": "already_activated",
"params": None,
},
)
def test_bad_key(self):
"""
An invalid activation key fails to activate.
"""
user_model = get_user_model()
resp = self.client.post(
reverse("django_registration_register"), data=self.valid_data
)
activation_key = self.valid_data[user_model.USERNAME_FIELD]
with self.assertSignalNotSent(signals.user_activated):
resp = self.client.get(
reverse(
"django_registration_activate",
args=(),
kwargs={"activation_key": activation_key},
)
)
# Second activation fails.
self.assertEqual(200, resp.status_code)
self.assertTemplateUsed(resp, "django_registration/activation_failed.html")
self.assertTrue("activation_error" in resp.context)
self.assertEqual(
resp.context["activation_error"],
{
"message": ActivationView.INVALID_KEY_MESSAGE,
"code": "invalid_key",
"params": {"activation_key": activation_key},
},
)
# The timestamp calculation will error if USE_TZ=True, due to
# trying to subtract a naive from an aware datetime. Since time
# zones aren't relevant to the test, we just temporarily disable
# time-zone support rather than do the more complex dance of
# checking the setting and forcing everything to naive or aware.
@override_settings(USE_TZ=False)
def test_activation_expired(self):
"""
An expired account can't be activated.
"""
user_model = get_user_model()
self.client.post(reverse("django_registration_register"), data=self.valid_data)
# We need to create an activation key valid for the username,
# but with a timestamp > ACCOUNT_ACTIVATION_DAYS days in the
# past. This requires monkeypatching time.time() to return
# that timestamp, since TimestampSigner uses time.time().
#
# On Python 3.3+ this is much easier because of the
# timestamp() method of datetime objects, but since
# django-registration has to run on Python 2.7, we manually
# calculate it using a timedelta between the signup date and
# the UNIX epoch, and patch time.time() temporarily to return
# a date (ACCOUNT_ACTIVATION_DAYS + 1) days in the past.
user = user_model.objects.get(**self.user_lookup_kwargs)
joined_timestamp = (
user.date_joined - datetime.datetime.fromtimestamp(0)
).total_seconds()
expired_timestamp = (
joined_timestamp - (settings.ACCOUNT_ACTIVATION_DAYS + 1) * 86400
)
_old_time = time.time
try:
time.time = lambda: expired_timestamp
activation_key = signing.dumps(
obj=self.valid_data[user_model.USERNAME_FIELD],
salt=REGISTRATION_SALT,
)
finally:
time.time = _old_time
with self.assertSignalNotSent(signals.user_activated):
resp = self.client.get(
reverse(
"django_registration_activate",
args=(),
kwargs={"activation_key": activation_key},
)
)
self.assertEqual(200, resp.status_code)
self.assertTemplateUsed(resp, "django_registration/activation_failed.html")
self.assertTrue("activation_error" in resp.context)
self.assertEqual(
resp.context["activation_error"],
{
"message": ActivationView.EXPIRED_MESSAGE,
"code": "expired",
"params": None,
},
)
def test_nonexistent_activation(self):
"""
A nonexistent username in an activation key will fail to
activate.
"""
activation_key = signing.dumps(obj="parrot", salt=REGISTRATION_SALT)
with self.assertSignalNotSent(signals.user_activated):
resp = self.client.get(
reverse(
"django_registration_activate",
args=(),
kwargs={"activation_key": activation_key},
)
)
self.assertEqual(200, resp.status_code)
self.assertTemplateUsed(resp, "django_registration/activation_failed.html")
self.assertTrue("activation_error" in resp.context)
self.assertEqual(
resp.context["activation_error"],
{
"message": ActivationView.BAD_USERNAME_MESSAGE,
"code": "bad_username",
"params": None,
},
)
def test_activation_signal(self):
user_model = get_user_model()
self.client.post(reverse("django_registration_register"), data=self.valid_data)
activation_key = signing.dumps(
obj=self.valid_data[user_model.USERNAME_FIELD], salt=REGISTRATION_SALT
)
with self.assertSignalSent(
signals.user_activated, required_kwargs=["user", "request"]
) as cm:
self.client.get(
reverse(
"django_registration_activate",
args=(),
kwargs={"activation_key": activation_key},
)
)
self.assertEqual(
cm.received_kwargs["user"].get_username(),
self.valid_data[user_model.USERNAME_FIELD],
)
self.assertTrue(isinstance(cm.received_kwargs["request"], HttpRequest))
@override_settings(AUTH_USER_MODEL="tests.CustomUser")
@override_settings(ROOT_URLCONF="tests.urls.custom_user_activation")
class ActivationBackendCustomUserTests(ActivationBackendViewTests):
"""
Runs the activation workflow's test suite, but with a custom user model.
"""
def test_custom_user_configured(self):
"""
Asserts that the user model in use is the custom user model
defined in this test suite.
"""
user_model = get_user_model()
custom_user = apps.get_model("tests", "CustomUser")
assert user_model is custom_user
|