Skip to content

Commit

Permalink
Added password change views and basic tests. (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahboyce authored Mar 30, 2024
1 parent 225aaaf commit dfffba2
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 2 deletions.
76 changes: 76 additions & 0 deletions accounts/tests/test_auth_password_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from __future__ import annotations

from django.core import mail
from django.test import Client
from django.test import TestCase
from django.urls import reverse

from home.factories import UserFactory


class AuthViewTests(TestCase):
"""As we have only overridden the templates, these tests will check the views
render without error with our templates but no other functionality will be tested.
"""

@classmethod
def setUpTestData(cls):
cls.user = UserFactory.create()

def setUp(self):
self.client = Client()

def test_password_change_form(self):
self.client.force_login(self.user)
response = self.client.get(reverse("password_change"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Djangonaut Space")
self.assertContains(response, "Update password")

def test_password_change_done(self):
self.client.force_login(self.user)
response = self.client.get(reverse("password_change_done"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Djangonaut Space")
self.assertContains(response, "Your password has been updated successfully!")

def test_password_reset_form(self):
response = self.client.get(reverse("password_reset"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Djangonaut Space")
self.assertContains(response, "Forgotten your password?")

def test_password_reset_confirm(self):
response = self.client.post(
reverse("password_reset"), {"email": "[email protected]"}
)
self.assertEqual(response.status_code, 302)
self.assertEqual(len(mail.outbox), 1)
token = response.context[0]["token"]
uid = response.context[0]["uid"]
reset_confirm_url = reverse(
"password_reset_confirm", kwargs={"token": token, "uidb64": uid}
)
response = self.client.get(reset_confirm_url, follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Djangonaut Space")
self.assertContains(response, "Reset password")
self.assertContains(response, "Change password")

def test_password_reset_done(self):
response = self.client.get(reverse("password_reset_done"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Djangonaut Space")
self.assertContains(
response,
"emailed you instructions for setting your password, if an account exists "
"with the email you entered. You should receive them shortly.",
)

def test_password_reset_complete(self):
response = self.client.get(reverse("password_reset_complete"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Djangonaut Space")
self.assertContains(
response, "Your password has been set. You may go ahead and log in now."
)
2 changes: 1 addition & 1 deletion indymeet/templates/emails/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
<table role="presentation" border="0" cellpadding="0" cellspacing="0" style="border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: auto;">
<tbody>
<tr>
<td style="font-family: Helvetica, sans-serif; font-size: 16px; vertical-align: top; border-radius: 4px; text-align: center; background-color: #5c0287;" valign="top" align="center" bgcolor="#5c0287"> <a href="{{ cta_link }}" target="_blank" style="border: solid 2px #5c0287; border-radius: 4px; box-sizing: border-box; cursor: pointer; display: inline-block; font-size: 16px; font-weight: bold; margin: 0; padding: 12px 24px; text-decoration: none; text-transform: capitalize; background-color: #5c0287; border-color: #5c0287; color: #ffffff;">{% block cta_button_text %}{% endblock cta_button_text %}</a> </td>
<td style="font-family: Helvetica, sans-serif; font-size: 16px; vertical-align: top; border-radius: 4px; text-align: center; background-color: #5c0287;" valign="top" align="center" bgcolor="#5c0287">{% block cta_button %}<a href="{{ cta_link }}" target="_blank" style="border: solid 2px #5c0287; border-radius: 4px; box-sizing: border-box; cursor: pointer; display: inline-block; font-size: 16px; font-weight: bold; margin: 0; padding: 12px 24px; text-decoration: none; text-transform: capitalize; background-color: #5c0287; border-color: #5c0287; color: #ffffff;">{% block cta_button_text %}{% endblock cta_button_text %}</a>{% endblock cta_button %}</td>
</tr>
</tbody>
</table>
Expand Down
10 changes: 10 additions & 0 deletions indymeet/templates/includes/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
{% trans "Profile" %}
</a>
</li>
<li class="p-2">
<a href="{% url 'password_change' %}" class="block outline-link text-gray-500 transition hover:text-ds-purple">
{% trans "Update password" %}
</a>
</li>
<li class="p-2">
<a href="{% url 'logout' %}" class="block outline-link text-gray-500 transition hover:text-ds-purple">
{% trans "Logout" %}
Expand Down Expand Up @@ -113,6 +118,11 @@
{% trans "Profile" %}
</a>
</li>
<li class="p-2">
<a class="block outline-link text-gray-500 transition hover:text-ds-purple" href="{% url 'password_change' %}">
{% trans "Update password" %}
</a>
</li>
<li class="p-2">
<a class="block outline-link text-gray-500 transition hover:text-ds-purple" href="{% url 'logout' %}">
{% trans "Logout" %}
Expand Down
22 changes: 22 additions & 0 deletions indymeet/templates/registration/password_change_done.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "base.html" %}
{% load i18n wagtailcore_tags static %}

{% block title %}{% translate "Password update success | Djangonaut Space" %}{% endblock %}
{% block meta_title %}{% translate "Password update success | Djangonaut Space" %}{% endblock %}


{% block extra_css %}
<link rel="stylesheet" href="{% static 'css/registration.css' %}">
{% endblock extra_css %}

{% block content %}
<main class="section my-3 mx-5">
<div class="section-container">
<div class="row pt-4 pb-4">
<div class="col">
<p>{% translate "Your password has been updated successfully!" %}</p>
</div>
</div>
</div>
</main>
{% endblock content %}
28 changes: 28 additions & 0 deletions indymeet/templates/registration/password_change_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends "base.html" %}
{% load i18n wagtailcore_tags static %}

{% block title %}{% translate "Password update | Djangonaut Space" %}{% endblock %}
{% block meta_title %}{% translate "Password update | Djangonaut Space" %}{% endblock %}


{% block extra_css %}
<link rel="stylesheet" href="{% static 'css/registration.css' %}">
{% endblock extra_css %}

{% block content %}
<main class="section my-3 mx-5">
<div class="section-container">
<h1 class="text-5xl mb-6">{% translate "Update password" %}</h1>
<div class="row">
<div class="col">
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">{% translate "Update password" %}</button>
</form>
</div>
<div class="col"></div>
</div>
</div>
</main>
{% endblock content %}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div class="section-container">
<div class="row pt-4 pb-4">
<div class="col">
<p>{% translate "Your password has been set. You may go ahead and log in now." %}</p>
<p>{% translate "Your password has been set. You may go ahead and log in now." %}</p>
<p><a href="{{ login_url }}">{% translate 'Log in' %}</a></p>
</div>
</div>
Expand Down
11 changes: 11 additions & 0 deletions indymeet/templates/registration/password_reset_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends "emails/base.html" %}
{% block preheader %}Djangonaut Space - Password Reset{% endblock preheader %}
{% block before_cta %}
<p style="font-family: Helvetica, sans-serif; font-size: 16px; font-weight: normal; margin: 0; margin-bottom: 16px;">
You're receiving this email because you requested a password reset for your user account at {{ site_name }}.
</p>
<p style="font-family: Helvetica, sans-serif; font-size: 16px; font-weight: normal; margin: 0; margin-bottom: 16px;">
Your username, in case you’ve forgotten: {{ user.get_username }}
</p>
{% endblock before_cta %}
{% block cta_button %}<a href="{{ protocol }}://{{ domain }}/reset/{{ uid }}/{{ token }}/" target="_blank" style="border: solid 2px #5c0287; border-radius: 4px; box-sizing: border-box; cursor: pointer; display: inline-block; font-size: 16px; font-weight: bold; margin: 0; padding: 12px 24px; text-decoration: none; text-transform: capitalize; background-color: #5c0287; border-color: #5c0287; color: #ffffff;">Reset Password</a>{% endblock cta_button %}

0 comments on commit dfffba2

Please sign in to comment.