Skip to content

Commit

Permalink
Merge branch 'main' into feature/brochure-sponsors
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick91 committed Sep 29, 2024
2 parents b10e7b8 + c78a65a commit 86d53cd
Show file tree
Hide file tree
Showing 72 changed files with 619 additions and 8,414 deletions.
29 changes: 1 addition & 28 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,32 +272,9 @@ jobs:
cache-to: type=local,dest=/tmp/.buildx-cache
platforms: linux/arm64

build-emails:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./email-templates/
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- uses: pnpm/action-setup@v4
name: Install pnpm
with:
version: 9
run_install: false
- name: Install dependencies
run: pnpm install
- name: Build templates
run: pnpm run build
- uses: actions/upload-artifact@v4
with:
name: built-email-templates
path: email-templates/build_production

terraform:
runs-on: ubuntu-latest
needs: [build-emails, build-and-push-service, build-and-push-arm-service, build-pretix, create-db]
needs: [build-and-push-service, build-and-push-arm-service, build-pretix, create-db]
environment:
name: ${{ fromJSON('["pastaporto", "production"]')[github.ref == 'refs/heads/main'] }}
defaults:
Expand All @@ -308,10 +285,6 @@ jobs:
with:
ref: ${{ github.ref }}
fetch-depth: 0
- uses: actions/download-artifact@v4
with:
name: built-email-templates
path: email-templates/build_production
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: 1.2.4
Expand Down
24 changes: 12 additions & 12 deletions backend/api/users/mutations/request_reset_password.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from grants.tasks import get_name
from notifications.models import EmailTemplate, EmailTemplateIdentifier
import strawberry
from django.utils import timezone
from datetime import timedelta
from api.users.types import OperationSuccess
from users.models import User as UserModel
from notifications.templates import EmailTemplate
from notifications.emails import get_email_backend
from django.conf import settings
import jwt
import logging
from typing import Annotated, Union

logger = logging.getLogger(__file__)

RequestResetPasswordResult = Annotated[
Union[OperationSuccess], strawberry.union(name="RequestResetPasswordResult")
]
Expand All @@ -28,19 +29,18 @@ def request_reset_password(email: str) -> RequestResetPasswordResult:

token = _create_reset_password_token(user=user)

backend = get_email_backend(
settings.PYTHONIT_EMAIL_BACKEND, environment=settings.ENVIRONMENT
email_template = EmailTemplate.objects.system_templates().get_by_identifier(
EmailTemplateIdentifier.reset_password
)
backend.send_email(
template=EmailTemplate.RESET_PASSWORD,
from_=settings.DEFAULT_FROM_EMAIL,
to=user.email,
subject="Reset your password",
variables={
"firstname": user.name,
"resetpasswordlink": f"https://pycon.it/reset-password/{token}",

email_template.send_email(
recipient=user,
placeholders={
"user_name": get_name(user, "there"),
"reset_password_link": f"https://pycon.it/reset-password/{token}",
},
)

logger.info("Sent reset password token to user_id=%s", user.id)
return OperationSuccess(ok=True)

Expand Down
55 changes: 26 additions & 29 deletions backend/api/users/tests/test_request_reset_password.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from notifications.templates import EmailTemplate
from unittest import mock
from unittest.mock import patch
import pytest

from users.tests.factories import UserFactory
Expand All @@ -7,33 +8,37 @@
pytestmark = pytest.mark.django_db


def test_request_reset_password(graphql_client, sent_emails):
user = UserFactory(email="reset@example.org")
def test_request_reset_password(graphql_client):
user = UserFactory(full_name="Sushi Op", email="reset@example.org")

body = graphql_client.query(
"""mutation($email: String!) {
requestResetPassword(email: $email) {
__typename
... on OperationSuccess {
ok
with patch(
"api.users.mutations.request_reset_password.EmailTemplate"
) as mock_email_template:
body = graphql_client.query(
"""mutation($email: String!) {
requestResetPassword(email: $email) {
__typename
... on OperationSuccess {
ok
}
}
}
}""",
variables={"email": user.email},
)
}""",
variables={"email": user.email},
)

assert body["data"]["requestResetPassword"]["__typename"] == "OperationSuccess"
assert body["data"]["requestResetPassword"]["ok"] is True

assert sent_emails[0]["template"] == EmailTemplate.RESET_PASSWORD
assert sent_emails[0]["subject"] == "Reset your password"
assert (
"https://pycon.it/reset-password/"
in sent_emails[0]["variables"]["resetpasswordlink"]
mock_email_template.objects.system_templates().get_by_identifier().send_email.assert_called_once_with(
recipient=user,
placeholders={
"user_name": "Sushi Op",
"reset_password_link": mock.ANY,
},
)


def test_request_reset_password_fails_with_not_active_user(graphql_client, sent_emails):
def test_request_reset_password_fails_with_not_active_user(graphql_client):
user = UserFactory(email="reset@example.org", is_active=False)

body = graphql_client.query(
Expand All @@ -51,12 +56,8 @@ def test_request_reset_password_fails_with_not_active_user(graphql_client, sent_
assert body["data"]["requestResetPassword"]["__typename"] == "OperationSuccess"
assert body["data"]["requestResetPassword"]["ok"] is False

assert len(sent_emails) == 0


def test_request_reset_password_fails_with_not_existing_user(
graphql_client, sent_emails
):
def test_request_reset_password_fails_with_not_existing_user(graphql_client):
UserFactory(email="reset@example.org", is_active=True)

body = graphql_client.query(
Expand All @@ -74,10 +75,8 @@ def test_request_reset_password_fails_with_not_existing_user(
assert body["data"]["requestResetPassword"]["__typename"] == "OperationSuccess"
assert body["data"]["requestResetPassword"]["ok"] is False

assert len(sent_emails) == 0


def test_request_reset_password_fails_with_empty_email(graphql_client, sent_emails):
def test_request_reset_password_fails_with_empty_email(graphql_client):
UserFactory(email="reset@example.org", is_active=True)

body = graphql_client.query(
Expand All @@ -94,5 +93,3 @@ def test_request_reset_password_fails_with_empty_email(graphql_client, sent_emai

assert body["data"]["requestResetPassword"]["__typename"] == "OperationSuccess"
assert body["data"]["requestResetPassword"]["ok"] is False

assert len(sent_emails) == 0
16 changes: 0 additions & 16 deletions backend/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,6 @@ def change_azure_account_to_test_name(settings):
settings.AZURE_STORAGE_ACCOUNT_NAME = "pytest-fakestorageaccount"


class TestEmailBackend:
ALL_EMAIL_BACKEND_CALLS = []

def __init__(self, *args, **kwargs) -> None:
pass

def send_email(self, **kwargs):
TestEmailBackend.ALL_EMAIL_BACKEND_CALLS.append(kwargs)


@pytest.fixture
def sent_emails():
TestEmailBackend.ALL_EMAIL_BACKEND_CALLS = []
yield TestEmailBackend.ALL_EMAIL_BACKEND_CALLS


@pytest.fixture
def image_file():
def wrapper(filename: str = "test.jpg"):
Expand Down
Loading

0 comments on commit 86d53cd

Please sign in to comment.