Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reply_to is filled if the user is authenticated #290

Merged
merged 2 commits into from Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -31,3 +31,6 @@ Session.vim

# MacOS Stuff
.DS_Store

# Editor Stuff
.vscode
1 change: 1 addition & 0 deletions boxes/forms.py
Expand Up @@ -53,6 +53,7 @@ def clean(self):
class SubmitBoxForm(Form):
message = CharField(widget=Textarea, required=True)
file_name = CharField(required=False)
add_reply_to = BooleanField(required=False)

def clean_message(self):
# Quick check if the message really came encrypted
Expand Down
8 changes: 8 additions & 0 deletions boxes/static/javascripts/box_submit.js
Expand Up @@ -126,6 +126,7 @@ $(document).ready(function () {
* Only when javascript is enabled, the form is created.
*/
function createBox() {
var authenticated_user = $("#authenticated_user").length
var $formDiv = $(".form-div-js");

/* Hidden form fields */
Expand All @@ -146,6 +147,13 @@ $(document).ready(function () {
var $encryptedMessage = $("<input id='id_message' name='message' type='hidden'></input>");
$form.append($encryptedMessage);

if (authenticated_user) {
var $replyLabel = $("<label id='id_reply_label' for='id_add_reply_to'>Add own email to ReplyTo</label>");
var $replyInput = $("<input class='box_submit_input' id='id_add_reply_to' name='add_reply_to' type='checkbox'></input>");
var $replyGroup = $("<div class='box_submit_checkbox'></div>").append($replyLabel).append($replyInput);
$form.append($replyGroup);
}

$formDiv.append($form);

/* Input fields */
Expand Down
18 changes: 13 additions & 5 deletions boxes/tasks.py
Expand Up @@ -10,22 +10,30 @@


@shared_task
def process_email(message_id, form_data):
def process_email(message_id, form_data, sent_by=None):
message = Message.objects.get(id=message_id)
box = message.box
msg = form_data["message"]
file_name = form_data.get("file_name", "")
subject = _('New submission to your box: {}').format(box)
reply_to = [sent_by] if sent_by else []

if file_name:
body = _("The submitted file can be found in the attachments.")
email = EmailMultiAlternatives(
subject, body, settings.DEFAULT_FROM_EMAIL, [box.owner.email],
attachments=[(file_name, msg, "application/octet-stream")])
subject,
body,
settings.DEFAULT_FROM_EMAIL,
[box.owner.email],
reply_to=reply_to,
attachments=[(file_name, msg, "application/octet-stream")]
)
else:
email = EmailMultiAlternatives(subject, msg,
email = EmailMultiAlternatives(subject,
msg,
settings.DEFAULT_FROM_EMAIL,
[box.owner.email])
[box.owner.email],
reply_to=reply_to)

# Output e-mail message for debug purposes
# with open('email.mbox', 'w') as f:
Expand Down
3 changes: 3 additions & 0 deletions boxes/templates/boxes/box_submit.html
Expand Up @@ -54,6 +54,9 @@
<p class="xmt-small smalltext no-margin-bottom">{{object.description|linebreaks}}</p>
</div>
</div>
{% if request.user.is_authenticated %}
<input type="hidden" id="authenticated_user">
{% endif %}
</div>
</div>
</div>
Expand Down
34 changes: 31 additions & 3 deletions boxes/tests.py
@@ -1,6 +1,7 @@
from django.test import TestCase
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.core import mail
from humans.models import User
from datetime import timedelta
from .models import Box, Message
Expand Down Expand Up @@ -159,6 +160,17 @@ def test_encrypted_file(self):
"file_name": "test"})
self.assertEqual(form.is_valid(), True)

def test_add_reply_to_is_present(self):
form = SubmitBoxForm({"message": ENCRYPTED_MESSAGE,
"add_reply_to": "on"})
self.assertEqual(form.is_valid(), True)
self.assertTrue(form.cleaned_data.get("add_reply_to"))

def test_add_reply_to_is_not_present(self):
form = SubmitBoxForm({"message": ENCRYPTED_MESSAGE})
self.assertEqual(form.is_valid(), True)
self.assertFalse(form.cleaned_data.get("add_reply_to"))


class BoxListViewTests(TestCase):

Expand Down Expand Up @@ -247,8 +259,8 @@ class MailTaskTests(TestCase):

def test_email_sending(self):
"""
With a valid box_id an email is sent and the box status is changed
to sent
With a valid message_id an email is sent and the Message status
is changed to sent
"""
user = create_and_login_user(self.client)
create_boxes(user)
Expand All @@ -257,5 +269,21 @@ def test_email_sending(self):
process_email(message.id, {"message": ENCRYPTED_MESSAGE})
after_box = user.own_boxes.get(id=initial_box.id)
after_msg = after_box.messages.get(id=message.id)
#self.assertEqual(message.status, Message.SENT)
message.refresh_from_db()
self.assertEqual(message.status, Message.SENT)
self.assertEqual(after_msg.sent_at, after_box.last_sent_at)
self.assertEqual(len(mail.outbox[0].reply_to), 0)

def test_email_sending_with_reply_to(self):
mail.outbox = []
user = create_and_login_user(self.client)
create_boxes(user)
initial_box = user.own_boxes.all()[0]
message = initial_box.messages.create()
process_email(
message.id, {"message": ENCRYPTED_MESSAGE}, sent_by=user.email
)
message.refresh_from_db()
self.assertEqual(len(mail.outbox), 1)
self.assertIn(user.email, mail.outbox[0].reply_to)
self.assertEqual(message.status, Message.SENT)
11 changes: 10 additions & 1 deletion boxes/views.py
Expand Up @@ -166,15 +166,24 @@ def dispatch(self, request, *args, **kwargs):
def post(self, request, *args, **kwargs):
form = self.get_form(data={"data": request.POST})
if form.is_valid():
cleaned_data = form.cleaned_data
message = self.object.messages.create()

# Mark box as done
if self.object.messages.count() >= self.object.max_messages:
self.object.status = Box.DONE
self.object.save()

add_user_email = cleaned_data.get("add_reply_to", False)
if request.user.is_authenticated() and add_user_email:
user_email = request.user.email
else:
user_email = None

# Schedule e-mail
process_email.delay(message.id, form.cleaned_data)
process_email.delay(
message.id, form.cleaned_data, sent_by=user_email
)

return self.response_class(
request=self.request,
Expand Down