Skip to content

Commit

Permalink
Merge pull request #291 from djangonaut-space/develop
Browse files Browse the repository at this point in the history
Production deployment
  • Loading branch information
sarahboyce authored Feb 18, 2024
2 parents e8f99cf + 5fa8db3 commit ee7df36
Show file tree
Hide file tree
Showing 43 changed files with 825 additions and 305 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Djangonaut Space

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ Distributed under the MIT License. See `LICENSE.txt` for more information.
<!-- CONTACT -->
## Contact

- Dawn Wages - [@dawnwagessays](https://twitter.com/dawnwagessays) - [@fly00gemini8712@mastodon.online](https://mastodon.online/@fly00gemini8712)
- Dawn Wages - [@dawnwagessays](https://twitter.com/bajoranengineer) - [@bajoranengineer@mastodon.online](https://mastodon.online/@bajoranengineer)
- [Djangonaut Space Organizers](mailto:contact@djangonaut.space)


Expand Down
23 changes: 23 additions & 0 deletions accounts/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import factory
from django.db.models.signals import post_save
from accounts.models import CustomUser, UserProfile


@factory.django.mute_signals(post_save)
class ProfileFactory(factory.django.DjangoModelFactory):
class Meta:
model = UserProfile

user = factory.SubFactory("accounts.factories.UserFactory", profile=None)


@factory.django.mute_signals(post_save)
class UserFactory(factory.django.DjangoModelFactory):
class Meta:
model = CustomUser

username = factory.Sequence(lambda n: "user_%d" % n)
first_name = "Jane"
last_name = "Doe"
email = "example@example.com"
profile = factory.RelatedFactory(ProfileFactory, factory_related_name="user")
16 changes: 12 additions & 4 deletions accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ class CustomUserCreationForm(UserCreationForm):
receive_newsletter = forms.BooleanField(
required=False,
help_text="Optional: Please check this to opt-in for receiving "
"general updates about community and events. You can "
"opt-out on your profile page at anytime.",
"a newsletter containing general updates about Djangonaut Space. "
"This newsletter does not yet exist. You can opt-out on your profile "
"page at anytime.",
)
receive_event_updates = forms.BooleanField(
required=False,
help_text="Optional: Please check this to opt-in for receiving "
"emails to events you register to. You can opt-out on "
"emails about upcoming community events. You can opt-out on "
"your profile page at anytime.",
)
receive_program_updates = forms.BooleanField(
required=False,
help_text="Optional: Please check this to opt-in for receiving "
"emails about upcoming program sessions. You can opt-out on "
"your profile page at anytime.",
)
accepted_coc = forms.BooleanField(
Expand All @@ -48,8 +55,9 @@ class Meta:
"password2",
"email_consent",
"accepted_coc",
"receive_newsletter",
"receive_program_updates",
"receive_event_updates",
"receive_newsletter",
)

def __init__(self, *args, **kwargs):
Expand Down
18 changes: 18 additions & 0 deletions accounts/migrations/0008_userprofile_receiving_program_updates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.13 on 2024-02-15 19:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("accounts", "0007_alter_userprofile_user"),
]

operations = [
migrations.AddField(
model_name="userprofile",
name="receiving_program_updates",
field=models.BooleanField(default=False),
),
]
1 change: 1 addition & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class UserProfile(models.Model):
email_confirmed = models.BooleanField(default=False)
receiving_newsletter = models.BooleanField(default=False)
receiving_event_updates = models.BooleanField(default=False)
receiving_program_updates = models.BooleanField(default=False)

def __str__(self):
return self.user.username
Expand Down
57 changes: 57 additions & 0 deletions accounts/tests/test_activate_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from django.test import Client, TestCase
from django.urls import reverse
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode

from accounts.factories import UserFactory
from accounts.tokens import account_activation_token


class ActivateViewTests(TestCase):
def setUp(self):
self.client = Client()

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

def test_user_does_not_exist(self):
activate_url = reverse(
"activate_account",
kwargs={
"uidb64": urlsafe_base64_encode(force_bytes("500")),
"token": account_activation_token.make_token(self.user),
},
)
response = self.client.get(activate_url, follow=True)
self.assertRedirects(response, reverse("signup"))
self.assertContains(response, "Your confirmation link is invalid.")
self.user.profile.refresh_from_db()
self.assertFalse(self.user.profile.email_confirmed)

def test_invalid_token(self):
activate_url = reverse(
"activate_account",
kwargs={
"uidb64": urlsafe_base64_encode(force_bytes(self.user.pk)),
"token": "INVALID_TOKEN",
},
)
response = self.client.get(activate_url, follow=True)
self.assertRedirects(response, reverse("signup"))
self.assertContains(response, "Your confirmation link is invalid.")
self.user.profile.refresh_from_db()
self.assertFalse(self.user.profile.email_confirmed)

def test_activate_email(self):
activate_url = reverse(
"activate_account",
kwargs={
"uidb64": urlsafe_base64_encode(force_bytes(self.user.pk)),
"token": account_activation_token.make_token(self.user),
},
)
response = self.client.get(activate_url)
self.assertRedirects(response, reverse("profile"))
self.user.profile.refresh_from_db()
self.assertTrue(self.user.profile.email_confirmed)
28 changes: 28 additions & 0 deletions accounts/tests/test_profile_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.test import Client, TestCase
from django.urls import reverse

from accounts.factories import ProfileFactory


class ProfileViewTests(TestCase):
def setUp(self):
self.client = Client()

@classmethod
def setUpTestData(cls):
profile = ProfileFactory.create(user__username="test")
cls.user = profile.user
cls.profile_url = reverse("profile")

def test_redirect_when_unauthenticated(self):
response = self.client.get(self.profile_url, follow=True)
self.assertRedirects(response, f"{reverse('login')}?next={self.profile_url}")

def test_profile(self):
self.client.force_login(self.user)
response = self.client.get(self.profile_url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Welcome, Jane")
self.assertContains(response, "Profile Info")
self.assertContains(response, "test")
self.assertContains(response, "Jane Doe")
59 changes: 59 additions & 0 deletions accounts/tests/test_signup_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from unittest.mock import patch

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

from accounts.models import CustomUser


class SignUpViewTests(TestCase):
def setUp(self):
self.client = Client()
self.url = reverse("signup")

def test_signup_template_renders(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Registration")

@patch("captcha.fields.ReCaptchaField.validate", return_value=True)
def test_signup_template_post_success(self, mock_captcha):
response = self.client.post(
self.url,
data={
"username": "janedoe",
"email": "jane@whoareyou.com",
"first_name": "Jane",
"last_name": "Doe",
"password1": "secretpassword123",
"password2": "secretpassword123",
"email_consent": True,
"accepted_coc": True,
"receive_newsletter": True,
"receive_program_updates": True,
"receive_event_updates": True,
"g-recaptcha-response": "dummy-response",
},
follow=True,
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Registration")
self.assertContains(
response,
"Your registration was successful. Please check your email provided for a confirmation link.",
)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(
mail.outbox[0].subject, "Djangonaut Space Registration Confirmation"
)
self.assertIn(
"Thank you for signing up to Djangonaut Space! Click the link to verify your email:",
mail.outbox[0].body,
)
created_user = CustomUser.objects.get(username="janedoe")
self.assertTrue(created_user.is_active)
self.assertTrue(created_user.profile.accepted_coc)
self.assertTrue(created_user.profile.receiving_newsletter)
self.assertTrue(created_user.profile.receiving_program_updates)
self.assertTrue(created_user.profile.receiving_event_updates)
43 changes: 43 additions & 0 deletions accounts/tests/test_unsubscribe_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from django.test import Client, TestCase
from django.urls import reverse

from accounts.factories import ProfileFactory


class UnsubscribeViewTests(TestCase):
def setUp(self):
self.client = Client()

@classmethod
def setUpTestData(cls):
profile = ProfileFactory.create(
receiving_newsletter=True,
receiving_program_updates=True,
receiving_event_updates=True,
)
cls.user = profile.user
cls.unsubscribe_url = reverse(
"unsubscribe", kwargs={"user_id": cls.user.id, "token": "dummytoken"}
)

def test_user_does_not_exist(self):
response = self.client.get(
reverse("unsubscribe", kwargs={"user_id": 500, "token": "dummytoken"})
)
self.assertEqual(response.status_code, 404)

def test_redirect_when_unauthenticated(self):
response = self.client.get(self.unsubscribe_url)
self.assertRedirects(
response, f"{reverse('login')}?next={self.unsubscribe_url}"
)

def test_unsubscribe(self):
self.client.force_login(self.user)
response = self.client.get(self.unsubscribe_url)
self.assertEqual(response.status_code, 200)
profile = self.user.profile
profile.refresh_from_db()
self.assertFalse(profile.receiving_newsletter)
self.assertFalse(profile.receiving_program_updates)
self.assertFalse(profile.receiving_event_updates)
31 changes: 17 additions & 14 deletions accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
from django.shortcuts import render
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.encoding import force_bytes
from django.utils.encoding import force_str
Expand Down Expand Up @@ -66,13 +67,17 @@ def form_valid(self, form):
user = self.object
user.profile.accepted_coc = form.cleaned_data["accepted_coc"]
user.profile.receiving_newsletter = form.cleaned_data["receive_newsletter"]
user.profile.receiving_program_updates = form.cleaned_data[
"receive_program_updates"
]
user.profile.receiving_event_updates = form.cleaned_data[
"receive_event_updates"
]
user.profile.save(
update_fields=[
"accepted_coc",
"receiving_newsletter",
"receiving_program_updates",
"receiving_event_updates",
]
)
Expand All @@ -83,15 +88,18 @@ def form_valid(self, form):
"token": account_activation_token.make_token(user),
},
)
message = (
"To confirm your email address on djangonaut.space please visit the link: "
+ self.request.build_absolute_uri(invite_link)
)
unsubscribe_link = user.profile.create_unsubscribe_link()
email_dict = {
"cta_link": self.request.build_absolute_uri(invite_link),
"name": user.get_full_name(),
"unsubscribe_link": unsubscribe_link,
}
send_mail(
"Djangonaut Space Registration Confirmation",
message,
render_to_string("emails/email_confirmation.txt", email_dict),
settings.DEFAULT_FROM_EMAIL,
[user.email],
html_message=render_to_string("emails/email_confirmation.html", email_dict),
fail_silently=False,
)
return super().form_valid(form)
Expand All @@ -115,17 +123,12 @@ def unsubscribe(request, user_id, token):
) or user.profile.check_token(token):
# unsubscribe them
profile = user.profile
if request.GET.get("events", None):
email_type = "events"
profile.receiving_event_updates = False
else:
email_type = "newsletters"
profile.receiving_newsletter = False
profile.receiving_event_updates = False
profile.receiving_program_updates = False
profile.receiving_newsletter = False
profile.save()

return render(
request, "registration/unsubscribed.html", {"email_type": email_type}
)
return render(request, "registration/unsubscribed.html")

# Otherwise redirect to login page
next_url = reverse(
Expand Down
Loading

0 comments on commit ee7df36

Please sign in to comment.