def setUp(self): self._r = Request() self._c = Connection(port=self._r.port)
class TestConnection: """ Run simple tests against Connection's methods. """ _USERNAME = '******' _PASSWORD = '******' _FROM = '*****@*****.**' _TO = '*****@*****.**' _SUBJECT = 'Test' _BDATA = b('\x00\x01\x02') _SDATA = u('0123456789') def setUp(self): self._r = Request() self._c = Connection(port=self._r.port) def test_auth(self): with self._r: Connection(port=self._r.port, username=self._USERNAME, password=self._PASSWORD).version() eq_(self._r.headers['authorization'], 'Basic {}'.format( b64encode('{}:{}'.format(self._USERNAME, self._PASSWORD).encode()).decode(), )) def test_raw(self): with self._r: self._c.raw(self._FROM, [self._TO], self._SDATA) data = loads(self._r.data.decode()) eq_(self._r.command, 'POST') eq_(self._r.path, '/v1/raw') eq_(data['body'], self._SDATA) @raises(TypeError) def test_raw_bad_to(self): with self._r: self._c.raw(self._FROM, self._TO, self._SDATA) def test_send(self): with self._r: self._c.send(self._FROM, [self._TO], self._SUBJECT, self._SDATA) data = loads(self._r.data.decode()) eq_(self._r.command, 'POST') eq_(self._r.path, '/v1/send') eq_(data['text'], self._SDATA) @raises(TypeError) def test_send_bad_to(self): with self._r: self._c.send(self._FROM, self._TO, self._SUBJECT, self._SDATA) @raises(ValueError) def test_send_empty_content(self): with self._r: self._c.send(self._FROM, [self._TO], self._SUBJECT) def test_send_attachment_binary(self): with self._r: self._c.send(self._FROM, [self._TO], self._SUBJECT, self._SDATA, attachments=[{'content': self._BDATA}]) data = loads(self._r.data.decode()) eq_(data['attachments'][0]['encoded'], True) eq_(b64decode(data['attachments'][0]['content']), self._BDATA) def test_send_attachment_file(self): with NamedTemporaryFile() as f: f.write(self._BDATA) f.seek(0) for use_name in [True, False]: with self._r: self._c.send(self._FROM, [self._TO], self._SUBJECT, self._SDATA, attachments=[f.name if use_name else f]) data = loads(self._r.data.decode()) eq_(data['attachments'][0]['filename'], basename(f.name)) eq_(data['attachments'][0]['encoded'], True) eq_(b64decode(data['attachments'][0]['content']), self._BDATA) def test_status(self): with self._r: self._c.status() eq_(self._r.command, 'GET') eq_(self._r.path, '/v1/status') def test_version(self): with self._r: self._c.version() eq_(self._r.command, 'GET') eq_(self._r.path, '/v1/version')