Skip to content

Commit

Permalink
Update/modernize testing a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ubernostrum committed Jun 12, 2023
1 parent fa1eb56 commit 0788474
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 125 deletions.
40 changes: 20 additions & 20 deletions tests/base.py
Expand Up @@ -139,7 +139,7 @@ def test_registration_open(self):
"""
resp = self.client.get(reverse("django_registration_register"))
self.assertEqual(200, resp.status_code)
assert resp.status_code == 200

@override_settings(REGISTRATION_OPEN=False)
def test_registration_closed(self):
Expand All @@ -162,9 +162,9 @@ def test_registration_get(self):
"""
resp = self.client.get(reverse("django_registration_register"))
self.assertEqual(200, resp.status_code)
assert resp.status_code == 200
self.assertTemplateUsed(resp, "django_registration/registration_form.html")
self.assertTrue(isinstance(resp.context["form"], RegistrationForm))
assert isinstance(resp.context["form"], RegistrationForm)

def test_registration(self):
"""
Expand All @@ -181,8 +181,8 @@ def test_registration(self):
user_model = get_user_model()
new_user = user_model.objects.get(**self.user_lookup_kwargs)

self.assertTrue(new_user.check_password(self.valid_data["password1"]))
self.assertEqual(new_user.email, self.valid_data["email"])
assert new_user.check_password(self.valid_data["password1"])
assert new_user.email == self.valid_data["email"]

def test_registration_failure(self):
"""
Expand All @@ -195,9 +195,9 @@ def test_registration_failure(self):
with self.assertSignalNotSent(signals.user_registered):
resp = self.client.post(reverse("django_registration_register"), data=data)

self.assertEqual(200, resp.status_code)
self.assertFalse(resp.context["form"].is_valid())
self.assertTrue(resp.context["form"].has_error("password2"))
assert resp.status_code == 200
assert not resp.context["form"].is_valid()
assert resp.context["form"].has_error("password2")

def test_registration_signal(self):
"""
Expand All @@ -206,15 +206,15 @@ def test_registration_signal(self):
"""
# pylint: disable=invalid-name
User = get_user_model()
with self.assertSignalSent(signals.user_registered) as cm:
with self.assertSignalSent(signals.user_registered) as signal_context:
self.client.post(
reverse("django_registration_register"), data=self.valid_data
)
self.assertEqual(
cm.received_kwargs["user"].get_username(),
self.valid_data[User.USERNAME_FIELD],
assert (
signal_context.received_kwargs["user"].get_username()
== self.valid_data[User.USERNAME_FIELD]
)
self.assertTrue(isinstance(cm.received_kwargs["request"], HttpRequest))
assert isinstance(signal_context.received_kwargs["request"], HttpRequest)


class ActivationTestCase(WorkflowTestCase):
Expand All @@ -239,11 +239,11 @@ def test_registration(self):
new_user = user_model.objects.get(**self.user_lookup_kwargs)

# New user must not be active.
self.assertFalse(new_user.is_active)
assert not new_user.is_active

# An activation email was sent.
self.assertEqual(len(mail.outbox), 1)
self.assertTrue("http" in mail.outbox[0].subject)
assert len(mail.outbox) == 1
assert "http" in mail.outbox[0].subject

def test_registration_failure(self):
"""
Expand All @@ -254,7 +254,7 @@ def test_registration_failure(self):
super().test_registration_failure()

# Activation email was not sent.
self.assertEqual(0, len(mail.outbox))
assert 0 == len(mail.outbox)

def test_registration_no_sites(self):
"""
Expand All @@ -269,10 +269,10 @@ def test_registration_no_sites(self):
reverse("django_registration_register"), data=self.valid_data
)

self.assertEqual(302, resp.status_code)
assert 302 == resp.status_code

user_model = get_user_model()
new_user = user_model.objects.get(**self.user_lookup_kwargs)

self.assertTrue(new_user.check_password(self.valid_data["password1"]))
self.assertEqual(new_user.email, self.valid_data["email"])
assert new_user.check_password(self.valid_data["password1"])
assert new_user.email == self.valid_data["email"]
76 changes: 31 additions & 45 deletions tests/test_activation_workflow.py
Expand Up @@ -94,16 +94,13 @@ def test_repeat_activation(self):
)

# Second activation fails.
self.assertEqual(200, resp.status_code)
assert 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,
},
)
assert resp.context["activation_error"] == {
"message": ActivationView.ALREADY_ACTIVATED_MESSAGE,
"code": "already_activated",
"params": None,
}

def test_bad_key(self):
"""
Expand All @@ -127,17 +124,14 @@ def test_bad_key(self):
)

# Second activation fails.
self.assertEqual(200, resp.status_code)
assert 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},
},
)
assert "activation_error" in resp.context
assert 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
Expand Down Expand Up @@ -192,17 +186,14 @@ def test_activation_expired(self):
)
)

self.assertEqual(200, resp.status_code)
assert 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,
},
)
assert "activation_error" in resp.context
assert resp.context["activation_error"] == {
"message": ActivationView.EXPIRED_MESSAGE,
"code": "expired",
"params": None,
}

def test_nonexistent_activation(self):
"""
Expand All @@ -221,17 +212,14 @@ def test_nonexistent_activation(self):
)
)

self.assertEqual(200, resp.status_code)
assert 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,
},
)
assert "activation_error" in resp.context
assert resp.context["activation_error"] == {
"message": ActivationView.BAD_USERNAME_MESSAGE,
"code": "bad_username",
"params": None,
}

def test_activation_signal(self):
"""
Expand All @@ -256,13 +244,11 @@ def test_activation_signal(self):
kwargs={"activation_key": activation_key},
)
)
self.assertEqual(
signal_context.received_kwargs["user"].get_username(),
self.valid_data[user_model.USERNAME_FIELD],
)
self.assertTrue(
isinstance(signal_context.received_kwargs["request"], HttpRequest)
assert (
signal_context.received_kwargs["user"].get_username()
== self.valid_data[user_model.USERNAME_FIELD]
)
assert isinstance(signal_context.received_kwargs["request"], HttpRequest)


@override_settings(AUTH_USER_MODEL="tests.CustomUser")
Expand Down

0 comments on commit 0788474

Please sign in to comment.