Skip to content

Commit

Permalink
Remove patch statement, minor fixes for consistenency
Browse files Browse the repository at this point in the history
  • Loading branch information
varun kumar committed Dec 16, 2023
1 parent 06d6c16 commit ba4dd0a
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 74 deletions.
4 changes: 2 additions & 2 deletions allauth/account/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ def get_client_ip(self, request):
return ip

def get_http_user_agent(self, request):
return request.META["HTTP_USER_AGENT"]
return request.META.get("HTTP_USER_AGENT", "Unspecified")

def generate_emailconfirmation_key(self, email):
key = get_random_string(64).lower()
Expand Down Expand Up @@ -767,7 +767,7 @@ def send_notification_mail(self, template_prefix, user, context):
if app_settings.EMAIL_NOTIFICATIONS:
context.update(
{
"current_site": get_current_site(self.request),
"site": get_current_site(self.request),
"timestamp": timezone.now(),
"ip": self.get_client_ip(self.request),
"user_agent": self.get_http_user_agent(self.request),
Expand Down
1 change: 0 additions & 1 deletion allauth/account/tests/test_change_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from unittest.mock import patch

from django.contrib.auth import get_user_model
from django.core import mail
from django.urls import reverse

import pytest
Expand Down
14 changes: 5 additions & 9 deletions allauth/account/tests/test_confirm_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,18 +382,14 @@ def test_confirm_logs_out_user(auth_client, settings, user, user_factory):
assert not auth_client.session.get(SESSION_KEY)


@patch("allauth.account.app_settings.EMAIL_NOTIFICATIONS", True)
def test_notification_on_email_remove(auth_client, user):
def test_notification_on_email_remove(auth_client, user, settings, mailoutbox):
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True
secondary = EmailAddress.objects.create(
email="[email protected]", user=user, verified=False, primary=False
)
resp = auth_client.post(
reverse("account_email"),
{"action_remove": "", "email": secondary.email},
**{
"HTTP_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
reverse("account_email"), {"action_remove": "", "email": secondary.email}
)
assert resp.status_code == 302
assert len(mail.outbox) == 1
assert "Following email has been removed" in mail.outbox[0].body
assert len(mailoutbox) == 1
assert "Following email has been removed" in mailoutbox[0].body
29 changes: 10 additions & 19 deletions allauth/account/tests/test_reset_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ def _create_user_and_login(self, usable_password=True):
return user


@patch("allauth.account.app_settings.EMAIL_NOTIFICATIONS", True)
def test_notification_on_password_change(user_factory, client):
def test_notification_on_password_change(user_factory, client, settings, mailoutbox):
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True
user = user_factory(
email="[email protected]",
password="password",
Expand All @@ -313,33 +313,24 @@ def test_notification_on_password_change(user_factory, client):
"password1": "change_password",
"password2": "change_password",
},
**{
"HTTP_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
)
assert len(mail.outbox) == 1
assert "Your password has been changed" in mail.outbox[0].body
assert len(mailoutbox) == 1
assert "Your password has been changed" in mailoutbox[0].body


@patch("allauth.account.app_settings.EMAIL_NOTIFICATIONS", True)
def test_notification_on_password_reset(user_factory, client, settings):
def test_notification_on_password_reset(user_factory, client, settings, mailoutbox):
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True
user = user_factory(
email="[email protected]",
password="password",
email_verified=True,
)

client.post(reverse("account_reset_password"), data={"email": user.email})
body = mail.outbox[0].body
body = mailoutbox[0].body
url = body[body.find("/password/reset/") :].split()[0]
resp = client.get(url)
resp = client.post(
resp.url,
{"password1": "newpass123", "password2": "newpass123"},
**{
"HTTP_USER_AGENT": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}
)
resp = client.post(resp.url, {"password1": "newpass123", "password2": "newpass123"})

assert len(mail.outbox) == 2
assert "Your password has been reset" in mail.outbox[1].body
assert len(mailoutbox) == 2
assert "Your password has been reset" in mailoutbox[1].body
2 changes: 1 addition & 1 deletion allauth/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def _action_remove(self, request, *args, **kwargs):
{"email": email_address.email},
)
adapter.send_notification_mail(
"account/email/email_removed",
"account/email/email_deleted",
request.user,
{"email": email_address.email},
)
Expand Down
2 changes: 1 addition & 1 deletion allauth/mfa/migrations/0002_authenticator_timestamps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by Django 3.2.22 on 2023-11-06 12:04

import django.utils.timezone
from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):
Expand Down
43 changes: 18 additions & 25 deletions allauth/mfa/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import django
from django.conf import settings
from django.core import mail
from django.urls import reverse

import pytest
from pytest_django.asserts import assertFormError

from allauth.account.authentication import AUTHENTICATION_METHODS_SESSION_KEY
from allauth.account.models import EmailAddress
from allauth.mfa import app_settings, signals
from allauth.mfa import app_settings
from allauth.mfa.adapter import get_adapter
from allauth.mfa.models import Authenticator

Expand Down Expand Up @@ -61,7 +60,6 @@ def test_activate_totp_with_unverified_email(
}


@patch("allauth.account.app_settings.EMAIL_NOTIFICATIONS", True)
def test_activate_totp_success(
auth_client, totp_validation_bypass, user, reauthentication_bypass
):
Expand All @@ -73,9 +71,7 @@ def test_activate_totp_success(
{
"code": "123",
},
**{"HTTP_USER_AGENT": "test"},
)
assert len(mail.outbox) == 1
assert resp["location"] == reverse("mfa_view_recovery_codes")
assert Authenticator.objects.filter(
user=user, type=Authenticator.Type.TOTP
Expand Down Expand Up @@ -286,10 +282,10 @@ def test_cannot_deactivate_totp(auth_client, user_with_totp, user_password):
}


@patch("allauth.account.app_settings.EMAIL_NOTIFICATIONS", True)
def test_notification_on_mfa_activate_totp(
auth_client, reauthentication_bypass, totp_validation_bypass
auth_client, reauthentication_bypass, totp_validation_bypass, settings, mailoutbox
):
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True
with reauthentication_bypass():
resp = auth_client.get(reverse("mfa_activate_totp"))
with totp_validation_bypass():
Expand All @@ -298,39 +294,36 @@ def test_notification_on_mfa_activate_totp(
{
"code": "123",
},
**{"HTTP_USER_AGENT": "test"},
)
assert len(mail.outbox) == 1
assert "Totp activated" in mail.outbox[0].subject
assert "Totp has been activated." in mail.outbox[0].body
assert len(mailoutbox) == 1
assert "Authenticator App Activated" in mailoutbox[0].subject
assert "Authenticator App has been activated." in mailoutbox[0].body


@patch("allauth.account.app_settings.EMAIL_NOTIFICATIONS", True)
def test_notification_on_mfa_deactivate_totp(
auth_client, user_with_totp, user_password
auth_client, user_with_totp, user_password, settings, mailoutbox
):
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True
resp = auth_client.get(reverse("mfa_deactivate_totp"))
assert resp.status_code == 302
assert resp["location"].startswith(reverse("account_reauthenticate"))
resp = auth_client.post(resp["location"], {"password": user_password})
assert resp.status_code == 302
resp = auth_client.post(
reverse("mfa_deactivate_totp"), **{"HTTP_USER_AGENT": "test"}
)
assert len(mail.outbox) == 1
assert "Totp deactivated" in mail.outbox[0].subject
assert "Totp has been deactivated." in mail.outbox[0].body
resp = auth_client.post(reverse("mfa_deactivate_totp"))
assert len(mailoutbox) == 1
assert "Authenticator App deactivated" in mailoutbox[0].subject
assert "Authenticator App has been deactivated." in mailoutbox[0].body


@patch("allauth.account.app_settings.EMAIL_NOTIFICATIONS", True)
def test_notification_on_authenticator_reset(
auth_client, user_with_recovery_codes, user_password
auth_client, user_with_recovery_codes, user_password, settings, mailoutbox
):
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True
resp = auth_client.get(reverse("mfa_generate_recovery_codes"))
assert resp["location"].startswith(reverse("account_reauthenticate"))
resp = auth_client.post(resp["location"], {"password": user_password})
assert resp.status_code == 302
resp = auth_client.post(resp["location"], **{"HTTP_USER_AGENT": "test"})
assert len(mail.outbox) == 1
assert "Totp reset" in mail.outbox[0].subject
assert "Totp has been reset." in mail.outbox[0].body
resp = auth_client.post(resp["location"])
assert len(mailoutbox) == 1
assert "Recovery codes generated" in mailoutbox[0].subject
assert "Recovery codes has been generated" in mailoutbox[0].body
4 changes: 3 additions & 1 deletion allauth/mfa/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ def form_valid(self, form):
signals.authenticator_reset.send(
sender=Authenticator, user=self.request.user, authenticator=rc_auth.instance
)
adapter.send_notification_mail("mfa/email/totp_reset", self.request.user, {})
adapter.send_notification_mail(
"mfa/email/recovery_codes_generated", self.request.user, {}
)
return super().form_valid(form)

def get_context_data(self, **kwargs):
Expand Down
4 changes: 1 addition & 3 deletions allauth/templates/account/email/base_notification.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
{% load account %}
{% load i18n %}

{% block content %}{% autoescape off %}{% user_display user as user_display %}{% blocktrans with site_name=current_site.name %}Greetings {{ user_display }},

You are receiving this mail because the following change was made to your account:{% endblocktrans %}
{% block content %}{% autoescape off %}{% user_display user as user_display %}{% blocktrans with site_name=current_site.name %}You are receiving this mail because the following change was made to your account:{% endblocktrans %}

{% block notification_message %}
{% endblock notification_message%}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends "account/email/base_notification.txt" %}
{% load i18n %}

{% block notification_message %}{% blocktrans %}Recovery codes has been generated.{% endblocktrans %}{% endblock notification_message %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Recovery codes generated{% endblocktrans %}
{% endautoescape %}
2 changes: 1 addition & 1 deletion allauth/templates/mfa/email/totp_activated_message.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "account/email/base_notification.txt" %}
{% load i18n %}

{% block notification_message %}{% blocktrans %}Totp has been activated.{% endblocktrans %}{% endblock notification_message %}
{% block notification_message %}{% blocktrans %}Authenticator App has been activated.{% endblocktrans %}{% endblock notification_message %}
2 changes: 1 addition & 1 deletion allauth/templates/mfa/email/totp_activated_subject.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Totp activated.{% endblocktrans %}
{% blocktrans %}Authenticator App Activated{% endblocktrans %}
{% endautoescape %}
2 changes: 1 addition & 1 deletion allauth/templates/mfa/email/totp_deactivated_message.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "account/email/base_notification.txt" %}
{% load i18n %}

{% block notification_message %}{% blocktrans %}Totp has been deactivated.{% endblocktrans %}{% endblock notification_message %}
{% block notification_message %}{% blocktrans %}Authenticator App has been deactivated.{% endblocktrans %}{% endblock notification_message %}
2 changes: 1 addition & 1 deletion allauth/templates/mfa/email/totp_deactivated_subject.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load i18n %}
{% autoescape off %}
{% blocktrans %}Totp deactivated{% endblocktrans %}
{% blocktrans %}Authenticator App deactivated{% endblocktrans %}
{% endautoescape %}
4 changes: 0 additions & 4 deletions allauth/templates/mfa/email/totp_reset_message.txt

This file was deleted.

4 changes: 0 additions & 4 deletions allauth/templates/mfa/email/totp_reset_subject.txt

This file was deleted.

0 comments on commit ba4dd0a

Please sign in to comment.