generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
463 additions
and
12 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
BackEnd/administration/templates/administration/admin_message_template.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<!DOCTYPE html> | ||
<html lang="uk"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Нове повідомлення</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
color: black; | ||
} | ||
.email-container { | ||
padding: 20px; | ||
border: 1px solid #ddd; | ||
border-radius: 5px; | ||
background-color: #f9f9f9; | ||
} | ||
.email-footer { | ||
margin-top: 20px; | ||
font-size: 12px; | ||
color: gray; | ||
} | ||
img { | ||
max-width: 150px; | ||
} | ||
.email-body { | ||
margin-bottom: 20px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="email-container"> | ||
<img src="{{ logo_url }}" alt="CraftMerge Logo" /> | ||
<div class="email-body"> | ||
<p>Доброго дня, {{ user_name }}!</p> | ||
<p>Ви отримали нове повідомлення:</p> | ||
<p><b>Категорія:</b> {{ category }}</p> | ||
<p><b>Повідомлення:</b></p> | ||
<p>{{ message }}</p> | ||
</div> | ||
<div class="email-footer"> | ||
<p>З повагою,</p> | ||
<p>Команда CraftMerge</p> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from django.core import mail | ||
from django.conf import settings | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
from authentication.factories import UserFactory | ||
from utils.administration.send_email_notification import send_email_to_user | ||
|
||
|
||
class TestSendMessageView(APITestCase): | ||
def setUp(self): | ||
self.admin = UserFactory(is_staff=True, is_active=True) | ||
self.user = UserFactory(is_active=True) | ||
self.client.force_authenticate(self.admin) | ||
self.url = f"/api/admin/users/{self.user.id}/send_message/" | ||
|
||
def test_send_message_success(self): | ||
data = { | ||
"email": self.user.email, | ||
"category": "Інше", | ||
"message": "Valid message for testing.", | ||
} | ||
response = self.client.post(self.url, data) | ||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
|
||
def test_send_message_invalid_data(self): | ||
data = { | ||
"email": self.user.email, | ||
"category": "Iнше", | ||
"message": "Short", | ||
} | ||
response = self.client.post(self.url, data) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) | ||
|
||
def test_send_message_unauthorized(self): | ||
self.client.logout() | ||
data = { | ||
"email": self.user.email, | ||
"category": "Інше", | ||
"message": "Valid message for testing.", | ||
} | ||
response = self.client.post(self.url, data) | ||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) | ||
|
||
def test_send_message_user_not_found(self): | ||
url = "/api/admin/users/9999/send_message/" | ||
data = { | ||
"email": "nonexistent@test.com", | ||
"category": "Інше", | ||
"message": "Valid message for testing.", | ||
} | ||
response = self.client.post(url, data) | ||
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) | ||
|
||
|
||
class TestSendEmailFunctionality(APITestCase): | ||
def setUp(self): | ||
self.user = UserFactory( | ||
name="Test", surname="User", email="test_user@example.com" | ||
) | ||
|
||
def send_email(self, category, message_content, email=None): | ||
email = email if email else self.user.email | ||
return send_email_to_user( | ||
user=self.user, | ||
category=category, | ||
message_content=message_content, | ||
email=email, | ||
) | ||
|
||
def test_send_email_success(self): | ||
self.send_email( | ||
category="Інше", | ||
message_content="This is a test message.", | ||
email="test_user@example.com", | ||
) | ||
|
||
self.assertEqual(len(mail.outbox), 1) | ||
email = mail.outbox[0] | ||
self.assertEqual(email.subject, "Адміністратор CraftMerge - Інше") | ||
self.assertIn("This is a test message.", email.body) | ||
self.assertEqual(email.to, ["test_user@example.com"]) | ||
self.assertEqual(email.from_email, settings.EMAIL_HOST_USER) | ||
|
||
def test_send_email_empty_message(self): | ||
with self.assertRaises(ValueError) as e: | ||
self.send_email( | ||
category="Інше", | ||
message_content="", | ||
email="test_user@example.com", | ||
) | ||
self.assertEqual(str(e.exception), "Message content cannot be empty.") | ||
|
||
def test_send_email_invalid_email(self): | ||
with self.assertRaises(ValueError) as e: | ||
self.send_email( | ||
category="Інше", | ||
message_content="Test message", | ||
email="invalid_email", | ||
) | ||
self.assertEqual(str(e.exception), "Invalid email address.") | ||
|
||
def test_send_email_missing_category(self): | ||
with self.assertRaises(ValueError) as e: | ||
self.send_email( | ||
category="", | ||
message_content="Test message", | ||
email="test_user@example.com", | ||
) | ||
self.assertEqual(str(e.exception), "Category is required.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from django.core.mail import EmailMultiAlternatives | ||
from django.template.loader import render_to_string | ||
from django.conf import settings | ||
from django.core.validators import validate_email | ||
from django.core.exceptions import ValidationError | ||
|
||
EMAIL_CONTENT_SUBTYPE = "html" | ||
PROTOCOL = "http" | ||
|
||
|
||
def send_email_to_user( | ||
user, | ||
category, | ||
message_content, | ||
email=None, | ||
sender_name="Адміністратор CraftMerge", | ||
template_name="administration/admin_message_template.html", | ||
): | ||
""" | ||
Sends an email message to the user using the specified template. | ||
:param user: The user object (CustomUser) | ||
:param category: The email category | ||
:param message_content: The message content | ||
:param email: (Optional) The recipient's email | ||
:param sender_name: Name of the sender | ||
:param template_name: The path to the HTML template | ||
""" | ||
if not category: | ||
raise ValueError("Category is required.") | ||
if not message_content.strip(): | ||
raise ValueError("Message content cannot be empty.") | ||
try: | ||
validate_email(email or user.email) | ||
except ValidationError: | ||
raise ValueError("Invalid email address.") | ||
|
||
context = { | ||
"user_name": f"{user.name} {user.surname}", | ||
"message": message_content, | ||
"category": category, | ||
"sender_name": sender_name, | ||
"logo_url": f"{PROTOCOL}://178.212.110.52/craftMerge-logo.png", | ||
} | ||
|
||
email_body = render_to_string(template_name, context) | ||
recipient_email = email if email else user.email | ||
|
||
subject = f"{sender_name} - {category}" | ||
|
||
email = EmailMultiAlternatives( | ||
subject=subject, | ||
body=email_body, | ||
from_email=settings.EMAIL_HOST_USER, | ||
to=[recipient_email], | ||
) | ||
email.content_subtype = EMAIL_CONTENT_SUBTYPE | ||
email.send(fail_silently=False) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.