Fakultas Ilmu Komputer UI

Commit e1324a2f authored by Dave Nathanael's avatar Dave Nathanael Committed by Irwanto
Browse files

Implement SendGrid email integration for Account activation

parent 1baf565c
import json
from faker import Faker
from django.urls import reverse
from django.core import mail
from rest_framework import status
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase, APIClient
......@@ -160,9 +161,12 @@ class AccountViewTest(APITestCase):
"is_active": True,
}
prev_outbox_count = len(mail.outbox)
self.client = APIClient(HTTP_AUTHORIZATION=HEADER_PREFIX + self.token_2.key)
response = self.client.put(path=url, data=data, format="json",)
current_outbox_count = len(mail.outbox)
expected_returned_data = data
expected_returned_data["username"] = self.officer.user.username
......@@ -178,6 +182,9 @@ class AccountViewTest(APITestCase):
self.assertIn(f'"object_id":"{self.officer.id}"', response_string)
self.assertIn(f'"action_type":"{ACTIVITY_TYPE_EDIT}"', response_string)
# Activation email sent
self.assertEqual(current_outbox_count, prev_outbox_count + 1)
def test_edit_account_fail_without_complete_fields(self):
url = self.BASE_URL + str(self.officer.id) + "/"
......
from django.core.mail import send_mail
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django_filters.rest_framework import DjangoFilterBackend
......@@ -26,6 +27,9 @@ from apps.constants import (
ACTIVITY_TYPE_CREATE,
ACTIVITY_TYPE_EDIT,
ACTIVITY_TYPE_DELETE,
EMAIL_ACCOUNT_ACTIVATION_SENDER,
EMAIL_ACCOUNT_ACTIVATION_CONTENT,
EMAIL_ACCOUNT_ACTIVATION_SUBJECT,
)
......@@ -58,6 +62,16 @@ class AccountViewSet(viewsets.ModelViewSet):
user = User.objects.create_user(username=username, password=password)
Account.objects.create(user=user, **serializer.validated_data)
def perform_update(self, serializer):
serializer.save()
if serializer.data["is_verified"] and serializer.data["is_active"]:
send_mail(EMAIL_ACCOUNT_ACTIVATION_SUBJECT,
EMAIL_ACCOUNT_ACTIVATION_CONTENT.format(name=serializer.data["name"]),
EMAIL_ACCOUNT_ACTIVATION_SENDER,
[serializer.data["email"]],
fail_silently=False,
)
def perform_destroy(self, instance):
instance.delete(author=self.request.user.account)
......
......@@ -20,3 +20,11 @@ MODEL_NAME_MONITORING_CASE = "Monitoring Case"
ACTIVITY_TYPE_CREATE = "Create"
ACTIVITY_TYPE_EDIT = "Edit"
ACTIVITY_TYPE_DELETE = "Delete"
EMAIL_ACCOUNT_ACTIVATION_SENDER = "nezappl@gmail.com"
EMAIL_ACCOUNT_ACTIVATION_SUBJECT = "TBCare - Aktivasi Akun Berhasil"
EMAIL_ACCOUNT_ACTIVATION_CONTENT = """Halo {name},
Akun TBCare Anda telah aktif dan dapat digunakan.
Silahkan buka aplikasi TBCare dan log in menggunakan email dan password yang Anda daftarkan.
"""
......@@ -118,9 +118,9 @@ AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", },
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", },
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", },
]
# Pagination
......@@ -155,3 +155,9 @@ USE_TZ = True
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATIC_URL = "/static/"
# Email via SendGrid
EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"
SENDGRID_API_KEY = os.environ.get("SENDGRID_API_KEY")
SENDGRID_SANDBOX_MODE_IN_DEBUG = False
appdirs==1.4.3
asgiref==3.2.3
astroid==2.3.3
attrs==19.3.0
autopep8==1.5
black==19.10b0
Click==7.0
coverage==5.0.3
dj-database-url==0.5.0
Django==3.0.1
django-cors-headers==3.2.0
django-filter==2.2.0
django-sendgrid-v5==0.8.1
djangorestframework==3.11.0
entrypoints==0.3
factory-boy==2.12.0
Faker==4.0.1
future==0.18.2
gunicorn==20.0.4
isort==4.3.21
lazy-object-proxy==1.4.3
mccabe==0.6.1
pathspec==0.7.0
psycopg2-binary==2.8.4
pycodestyle==2.5.0
pyflakes==2.1.1
pylint==2.4.4
python-dateutil==2.8.1
python-dotenv==0.11.0
python-http-client==3.2.7
pytz==2019.3
regex==2020.1.8
sendgrid==6.2.2
six==1.14.0
sqlparse==0.3.0
text-unidecode==1.3
toml==0.10.0
typed-ast==1.4.1
wrapt==1.11.2
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment