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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#
# 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/>.
import unittest
import json
import sys
import pixelated.user_agent
from pixelated.adapter.tag import Tag
from pixelated.adapter.tag_service import TagService
from mockito import *
import crochet
import pixelated.reactor_manager as reactor_manager
import test.support.test_helper as test_helper
import pixelated.adapter.pixelated_mail
import os
class UserAgentTest(unittest.TestCase):
def setUp(self):
self.app = pixelated.user_agent.app.test_client()
self.mail_service = mock()
self.tag_service = mock()
self.draft_service = mock()
self.search_engine = mock()
pixelated.user_agent.DISABLED_FEATURES = []
pixelated.user_agent.mail_service = self.mail_service
pixelated.user_agent.tag_service = self.tag_service
pixelated.user_agent.draft_service = self.draft_service
pixelated.user_agent.search_engine = self.search_engine
self.input_mail = None
pixelated.adapter.pixelated_mail.input_mail_from_dict = lambda x: self.input_mail
def tearDown(self):
unstub()
def test_create_or_send_draft_should_create_draft_if_mail_has_no_ident(self):
self.input_mail = self.draft()
self.app.post('/mails', data='{}', content_type="application/json")
verify(self.draft_service).create_draft(self.input_mail)
def test_create_or_send_draft_should_send_draft_if_mail_has_ident(self):
self.input_mail = self.draft()
self.app.post('/mails', data='{"ident":1}', content_type="application/json")
verify(self.mail_service).send(1, self.input_mail)
def test_sending_mail_return_sent_mail_data_when_send_succeeds(self):
self.input_mail = self.draft()
when(self.mail_service).send(1, self.input_mail).thenReturn(self.input_mail)
self.input_mail.as_dict = lambda: {'header': {'from': 'a@a.a', 'to': 'b@b.b'},
'ident': 1,
'tags': [],
'status': [],
'security_casing': {},
'body': 'email body'}
result = self.app.post('/mails', data='{"ident":1}', content_type="application/json")
self.assertEqual(result.status_code, 200)
self.assertEqual(result.data, '{"status": [], "body": "email body", "ident": 1, "tags": [], "header": {"to": "b@b.b", "from": "a@a.a"}, "security_casing": {}}')
def test_sending_mail_return_error_message_when_send_fails(self):
self.input_mail = self.draft()
def send_that_throws_exception(id, mail):
raise Exception('email sending failed', 'more information of error')
self.mail_service.send = send_that_throws_exception
result = self.app.post('/mails', data='{"ident":1}', content_type="application/json")
self.assertEqual(result.status_code, 500)
self.assertEqual(result.data, '{"message": "email sending failed\\nmore information of error"}')
def test_update_draft(self):
self.input_mail = self.draft()
when(self.draft_service).update_draft(1, self.input_mail).thenReturn(self.input_mail)
self.app.put('/mails', data='{"ident":1}', content_type="application/json")
verify(self.draft_service).update_draft(1, self.input_mail)
def draft(self):
return test_helper.input_mail()
def test_that_default_config_file_is_home_dot_pixelated(self):
orig_config = pixelated.user_agent.app.config
try:
when(crochet).setup().thenReturn(None)
when(reactor_manager).start_reactor().thenReturn(None)
when(pixelated.user_agent).start_user_agent().thenReturn(None)
pixelated.user_agent.app.config = mock()
sys.argv = ['/tmp/does_not_exist']
pixelated.user_agent.setup()
verify(pixelated.user_agent.app.config).from_pyfile(os.path.join(os.environ['HOME'], '.pixelated'))
finally:
pixelated.user_agent.app.config = orig_config
def test_that_config_file_can_be_specified_on_command_line(self):
orig_config = pixelated.user_agent.app.config
try:
when(crochet).setup().thenReturn(None)
when(reactor_manager).start_reactor().thenReturn(None)
when(pixelated.user_agent).start_user_agent().thenReturn(None)
pixelated.user_agent.app.config = mock()
sys.argv = ['/tmp/does_not_exist', '--config', '/tmp/some/config/file']
pixelated.user_agent.setup()
verify(pixelated.user_agent.app.config).from_pyfile('/tmp/some/config/file')
finally:
pixelated.user_agent.app.config = orig_config
def test_that_tags_returns_all_tags(self):
when(self.tag_service).all_tags().thenReturn(TagService.SPECIAL_TAGS)
response = self.app.get('/tags')
self.assertEqual(200, response.status_code)
expected = json.dumps([tag.as_dict() for tag in TagService.SPECIAL_TAGS])
self.assertEqual(expected, response.data)
def test_that_tags_are_filtered_by_query(self):
when(self.tag_service).all_tags().thenReturn(TagService.SPECIAL_TAGS)
response = self.app.get('/tags?q=dr')
self.assertEqual(200, response.status_code)
expected = json.dumps([Tag('drafts', True).as_dict()])
self.assertEqual(expected, response.data)
def test_that_default_tags_are_ignorable(self):
when(self.tag_service).all_tags().thenReturn(TagService.SPECIAL_TAGS)
when(self.tag_service).all_custom_tags().thenReturn([Tag('test')])
response = self.app.get('/tags?skipDefaultTags=true')
self.assertEqual(200, response.status_code)
expected = json.dumps([Tag('test').as_dict()])
self.assertEqual(expected, response.data)
|