summaryrefslogtreecommitdiff
path: root/service/test/integration/test_drafts.py
blob: d0505d75d91531a2499a324b2c61e7da16d743da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
# Copyright (c) 2014 ThoughtWorks, Inc.
#
# Pixelated is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pixelated is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.

from test.support.integration import SoledadTestBase, MailBuilder
from mockito import unstub, when, any
from twisted.internet import defer


class DraftsTest(SoledadTestBase):

    def tearDown(self):
        unstub()

    @defer.inlineCallbacks
    def test_post_sends_mail_and_deletes_previous_draft_if_it_exists(self):
        # act as if sending the mail by SMTP succeeded
        sendmail_deferred = defer.Deferred()
        when(self.mail_sender).sendmail(any()).thenReturn(sendmail_deferred)

        # creates one draft
        first_draft = MailBuilder().with_subject('First draft').build_json()
        first_draft_ident = (yield self.put_mail(first_draft)[0])['ident']

        # sends an updated version of the draft
        second_draft = MailBuilder().with_subject('Second draft').with_ident(first_draft_ident).build_json()
        deferred_res = self.post_mail(second_draft)

        sendmail_deferred.callback(None)  # SMTP succeeded

        yield deferred_res

        sent_mails = yield self.get_mails_by_tag('sent')
        drafts = yield self.get_mails_by_tag('drafts')

        # make sure there is one email in the sent mailbox and it is the second draft
        self.assertEquals(1, len(sent_mails))
        self.assertEquals('Second draft', sent_mails[0].subject)

        # make sure that there are no drafts in the draft mailbox
        self.assertEquals(0, len(drafts))

    @defer.inlineCallbacks
    def test_post_sends_mail_even_when_draft_does_not_exist(self):
        # act as if sending the mail by SMTP succeeded
        sendmail_deferred = defer.Deferred()
        when(self.mail_sender).sendmail(any()).thenReturn(sendmail_deferred)

        first_draft = MailBuilder().with_subject('First draft').build_json()
        res = self.post_mail(first_draft)
        sendmail_deferred.callback(True)
        yield res

        sent_mails = yield self.get_mails_by_tag('sent')
        drafts = yield self.get_mails_by_tag('drafts')

        self.assertEquals(1, len(sent_mails))
        self.assertEquals('First draft', sent_mails[0].subject)
        self.assertEquals(0, len(drafts))

    def post_mail(self, data):
        deferred_res, req = self.post('/mails', data)
        return deferred_res

    @defer.inlineCallbacks
    def test_put_creates_a_draft_if_it_does_not_exist(self):
        mail = MailBuilder().with_subject('A new draft').build_json()
        yield self.put_mail(mail)[0]
        mails = yield self.get_mails_by_tag('drafts')

        self.assertEquals('A new draft', mails[0].subject)

    @defer.inlineCallbacks
    def test_put_updates_draft_if_it_already_exists(self):
        draft = MailBuilder().with_subject('First draft').build_json()
        draft_ident = (yield self.put_mail(draft)[0])['ident']

        updated_draft = MailBuilder().with_subject('First draft edited').with_ident(draft_ident).build_json()
        yield self.put_mail(updated_draft)[0]

        drafts = yield self.get_mails_by_tag('drafts')

        self.assertEquals(1, len(drafts))
        self.assertEquals('First draft edited', drafts[0].subject)

    @defer.inlineCallbacks
    def test_respond_unprocessable_entity_if_draft_to_remove_doesnt_exist(self):
        draft = MailBuilder().with_subject('First draft').build_json()
        yield self.put_mail(draft)[0]

        updated_draft = MailBuilder().with_subject('First draft edited').with_ident('NOTFOUND').build_json()
        response, request = self.put_mail(updated_draft)
        yield response

        self.assertEquals(422, request.code)