def test_command_close_cancels_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=1) cmd.close('line', 'OK') yield from self.advance(3) yield from cmd.wait() self.assertEqual(Response('OK', ['line']), cmd.response)
def test_when_async_commands_timeout__they_should_be_removed_from_protocol_state( self): imap_client = yield from self.login_user('user', 'pass', select=True) yield from (imap_client.protocol.execute( Command('DELAY', imap_client.protocol.new_tag(), '3', loop=self.loop))) noop_task = asyncio.ensure_future( imap_client.protocol.execute( Command('NOOP', imap_client.protocol.new_tag(), '', loop=self.loop, timeout=2))) yield from self.advance(1) self.assertEqual(1, len(imap_client.protocol.pending_async_commands)) yield from self.advance(1.1) finished, pending = yield from asyncio.wait([noop_task], loop=self.loop) self.assertTrue(noop_task in finished) self.assertTrue(isinstance(noop_task.exception(), CommandTimeout)) self.assertEqual(0, len(imap_client.protocol.pending_async_commands))
def test_when_sync_commands_timeout__they_should_be_removed_from_protocol_state( self): imap_client = yield from self.login_user('user', 'pass') yield from (imap_client.protocol.execute( Command('DELAY', imap_client.protocol.new_tag(), '3', loop=self.loop))) delay_task = asyncio.ensure_future( imap_client.protocol.execute( Command('DELAY', imap_client.protocol.new_tag(), '0', loop=self.loop, timeout=2))) yield from self.advance(1) self.assertIsNotNone(imap_client.protocol.pending_sync_command) yield from self.advance(1.1) finished, pending = yield from asyncio.wait([delay_task], loop=self.loop) self.assertTrue(delay_task in finished) self.assertTrue(isinstance(delay_task.exception(), CommandTimeout)) self.assertIsNone(imap_client.protocol.pending_sync_command)
def test_command_repr(self): self.assertEqual('tag NAME', str(Command('NAME', 'tag'))) self.assertEqual('tag NAME arg1 arg2', str(Command('NAME', 'tag', 'arg1', 'arg2'))) self.assertEqual('tag UID NAME arg', str(Command('NAME', 'tag', 'arg', prefix='UID'))) self.assertEqual('tag UID NAME', str(Command('NAME', 'tag', prefix='UID')))
def test_command_timeout_while_receiving_data(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) yield from self.advance(1) cmd.begin_literal_data(12, b'literal') yield from self.advance(3) with self.assertRaises(AioImapException): yield from cmd.wait()
def test_command_timeout_while_receiving_data(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) yield from self.advance(1) cmd.begin_literal_data('literal', 12) yield from self.advance(3) with self.assertRaises(asyncio.TimeoutError): yield from cmd.wait()
def test_command_end_literal_data_resets_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) cmd.begin_literal_data(b'data', 4) yield from self.advance(1.9) cmd.end_literal_data() yield from self.advance(1.9) cmd.close('line', 'OK') yield from cmd.wait() self.assertEqual(Response('OK', [b'data', 'line']), cmd.response)
def test_unconplete_line_during_litteral_no_cmd_found(self): self.imap_protocol.data_received(b'* LIST () "/" {7}\r\nfoo/') self.imap_protocol.data_received(b'bar\r\nTAG OK LIST completed\r\n') self.imap_protocol._handle_line.assert_has_calls([call('* LIST () "/" {7}', None)]) self.imap_protocol._handle_line.assert_has_calls([call('* LIST () "/" {7}', None), call('', Command('NIL', 'unused')), call('TAG OK LIST completed', None)])
def test_line_with_litteral_no_cmd_found_no_AttributeError_thrown(self): self.imap_protocol.data_received( b'* 3 FETCH (UID 12 RFC822 {4}\r\nmail)\r\n' b'TAG OK FETCH completed.\r\n') self.imap_protocol._handle_line.assert_has_calls([ call('* 3 FETCH (UID 12 RFC822 {4}', None), call(')', Command('NIL', 'unused')), call('TAG OK FETCH completed.', None) ])
def test_line_with_litteral_no_cmd_found_no_AttributeError_thrown(self): self.line_handler.side_effect = [None, None, None] self.imap_protocol._handle_responses( b'* 3 FETCH (UID 12 RFC822 {4}\r\nmail)\r\n' b'TAG OK FETCH completed.\r\n', self.line_handler) self.line_handler.assert_has_calls([ call('* 3 FETCH (UID 12 RFC822 {4}', None), call(')', Command('NIL', 'unused')), call('TAG OK FETCH completed.', None) ])
def test_command_append_literal_data_resets_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) cmd.begin_literal_data(12, b'literal') yield from self.advance(1.9) cmd.append_literal_data(b' data') yield from self.advance(1.9) cmd.close('line', 'OK') yield from cmd.wait() self.assertEqual(Response('OK', [b'literal data', 'line']), cmd.response)
def test_split_responses_with_message_data(self): cmd = Command('FETCH', 'TAG') self.imap_protocol._handle_line = MagicMock(return_value=cmd) self.imap_protocol.data_received(b'* 1 FETCH (UID 1 RFC822 {26}\r\n...\r\n(mail content)\r\n...\r\n)\r\n' b'TAG OK FETCH completed.\r\n') self.imap_protocol._handle_line.assert_has_calls([call('* 1 FETCH (UID 1 RFC822 {26}', None)]) self.imap_protocol._handle_line.assert_has_calls([call(')', cmd)]) self.imap_protocol._handle_line.assert_has_calls([call('TAG OK FETCH completed.', None)]) self.assertEqual([b'...\r\n(mail content)\r\n...\r\n'], cmd.response.lines)
async def test_command_append_data_resets_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) cmd.begin_literal_data(4, b'da') await self.advance(1.9) cmd.append_literal_data(b'ta') await self.advance(1.9) cmd.close('line', 'OK') await cmd.wait() self.assertEqual(Response('OK', [b'data', 'line']), cmd.response)
def test_uncomplete_line_followed_by_uncomplete_literal(self): cmd = Command('FETCH', 'TAG') self.imap_protocol._handle_line = MagicMock(return_value=cmd) self.imap_protocol.data_received(b'* 2 FETCH (') self.imap_protocol.data_received(b'FLAGS () UID 160016 BODY[] {10}\r\non the ') self.imap_protocol.data_received(b'dot)\r\nTAG OK FETCH completed\r\n') self.imap_protocol._handle_line.assert_has_calls([call('* 2 FETCH (FLAGS () UID 160016 BODY[] {10}', None), call(')', cmd), call('TAG OK FETCH completed', None)]) self.assertEqual([b'on the dot'], cmd.response.lines)
def test_unconplete_line_with_litteral_fetch(self): cmd = Command('FETCH', 'TAG') self.imap_protocol._handle_line = MagicMock(return_value=cmd) self.imap_protocol.data_received(b'* 12 FETCH (BODY[HEADER] {4}\r\nyo\r\n)\r\n* 13 FETCH (BODY[') self.imap_protocol.data_received(b'HEADER] {5}\r\nyo2\r\n)\r\nTAG OK STORE completed.\r\n') self.imap_protocol._handle_line.assert_has_calls([call('* 12 FETCH (BODY[HEADER] {4}', None), call(')', cmd)]) self.imap_protocol._handle_line.assert_has_calls([call('* 13 FETCH (BODY[HEADER] {5}', None), call(')', cmd), call('TAG OK STORE completed.', None)]) self.assertEqual([b'yo\r\n', b'yo2\r\n'], cmd.response.lines)
def test_unconplete_lines_during_litteral(self): cmd = Command('LIST', 'TAG') self.imap_protocol._handle_line = MagicMock(return_value=cmd) self.imap_protocol.data_received(b'* LIST () "/" {11}\r\nfoo/') self.imap_protocol.data_received(b'bar/') self.imap_protocol.data_received(b'baz\r\n* LIST () "/" qux\r\nTAG OK LIST completed\r\n') self.imap_protocol._handle_line.assert_has_calls([call('* LIST () "/" {11}', None)]) self.imap_protocol._handle_line.assert_has_calls([call('* LIST () "/" qux', None), call('TAG OK LIST completed', None)]) self.assertEqual([b'foo/bar/baz'], cmd.response.lines)
def test_command_append_to_resp_resets_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) yield from self.advance(1.9) cmd.append_to_resp('line 1') yield from self.advance(1.9) cmd.close('line 2', 'OK') yield from cmd.wait() self.assertEqual(Response('OK', ['line 1', 'line 2']), cmd.response)
def test_split_responses_with_two_messages_data(self): cmd = Command('FETCH', 'TAG') self.imap_protocol._handle_line = MagicMock(return_value=cmd) self.imap_protocol.data_received(b'* 3 FETCH (UID 3 RFC822 {6}\r\nmail 1)\r\n' b'* 4 FETCH (UID 4 RFC822 {6}\r\nmail 2)\r\n' b'TAG OK FETCH completed.\r\n') self.imap_protocol._handle_line.assert_has_calls([call('* 3 FETCH (UID 3 RFC822 {6}', None), call(')', cmd), call('* 4 FETCH (UID 4 RFC822 {6}', None), call(')', cmd), call('TAG OK FETCH completed.', None)]) self.assertEqual([b'mail 1', b'mail 2'], cmd.response.lines)
def test_split_responses_with_message_data(self): cmd = Command('FETCH', 'TAG') self.line_handler.side_effect = [cmd, cmd, cmd] self.imap_protocol._handle_responses( b'* 1 FETCH (UID 1 RFC822 {26}\r\n...\r\n(mail content)\r\n...\r\n)\r\n' b'TAG OK FETCH completed.\r\n', self.line_handler) self.line_handler.assert_has_calls( [call('* 1 FETCH (UID 1 RFC822 {26}', None)]) self.line_handler.assert_has_calls([call(')', cmd)]) self.line_handler.assert_has_calls( [call('TAG OK FETCH completed.', None)]) self.assertEqual([b'...\r\n(mail content)\r\n...\r\n'], cmd.response.lines)
def test_line_with_attachment_litterals(self): cmd = Command('FETCH', 'TAG') self.imap_protocol._handle_line = MagicMock(return_value=cmd) self.imap_protocol.data_received(b'* 46 FETCH (UID 46 FLAGS () BODYSTRUCTURE (' b'("text" "calendar" ("charset" "UTF-8" "name" {16}\r\nG\xe9n\xe9ration 3.ics)' b' "<mqwssinzuqvhkzlnhlcq>" NIL "quoted-printable" 365 14 NIL ' b'("attachment" ("filename" {16}\r\nG\xe9n\xe9ration 3.ics)))\r\n') self.imap_protocol._handle_line.assert_has_calls([call('* 46 FETCH (UID 46 FLAGS () BODYSTRUCTURE (' '("text" "calendar" ("charset" "UTF-8" "name" {16}', None), call(') "<mqwssinzuqvhkzlnhlcq>" NIL "quoted-printable" 365 14 NIL ' '("attachment" ("filename" {16}', cmd), call(')))', cmd)]) self.assertEqual([b'G\xe9n\xe9ration 3.ics', b'G\xe9n\xe9ration 3.ics'], cmd.response.lines)
def test_split_responses_with_two_messages_data(self): cmd = Command('FETCH', 'TAG') self.line_handler.side_effect = [cmd, cmd, cmd, cmd, cmd] self.imap_protocol._handle_responses( b'* 3 FETCH (UID 3 RFC822 {6}\r\nmail 1)\r\n' b'* 4 FETCH (UID 4 RFC822 {6}\r\nmail 2)\r\n' b'TAG OK FETCH completed.\r\n', self.line_handler) self.line_handler.assert_has_calls([ call('* 3 FETCH (UID 3 RFC822 {6}', None), call(')', cmd), call('* 4 FETCH (UID 4 RFC822 {6}', None), call(')', cmd), call('TAG OK FETCH completed.', None) ]) self.assertEqual([b'mail 1', b'mail 2'], cmd.response.lines)
def test_append_too_short(self): imap_client = yield from self.login_user('user@mail', 'pass') self.assertEquals( 0, extract_exists((yield from imap_client.examine('INBOX')))) message_bytes = b'do you see me ?' * 2 imap_client.protocol.literal_data = message_bytes[:5] args = ['INBOX', '{%s}' % len(message_bytes)] response = yield from imap_client.protocol.execute( Command('APPEND', imap_client.protocol.new_tag(), *args, loop=self.loop)) self.assertEquals('BAD', response.result) self.assertTrue('expected 30 but was' in response.lines[0])
def test_unconplete_line_during_litteral_no_cmd_found(self): self.line_handler.side_effect = [None, None, None, None] with self.assertRaises(IncompleteLiteral) as expected: self.imap_protocol._handle_responses(b'* LIST () "/" {7}\r\nfoo/', self.line_handler) self.line_handler.assert_has_calls( [call('* LIST () "/" {7}', None)]) self.imap_protocol._handle_responses( expected.exception.partial + b'bar\r\nTAG OK LIST completed\r\n', self.line_handler, expected.exception.cmd) self.line_handler.assert_has_calls([ call('* LIST () "/" {7}', None), call('', Command('NIL', 'unused')), call('TAG OK LIST completed', None) ])
def test_unconplete_line_during_litteral(self): cmd = Command('LIST', 'TAG') self.line_handler.side_effect = [cmd, cmd, cmd, cmd] with self.assertRaises(IncompleteLiteral) as expected: self.imap_protocol._handle_responses(b'* LIST () "/" {7}\r\nfoo/', self.line_handler) self.line_handler.assert_has_calls( [call('* LIST () "/" {7}', None)]) self.imap_protocol._handle_responses( expected.exception.partial + b'bar\r\n* LIST () "/" baz\r\nTAG OK LIST completed\r\n', self.line_handler, expected.exception.cmd) self.line_handler.assert_has_calls([ call('* LIST () "/" baz', None), call('TAG OK LIST completed', None) ]) self.assertEqual([b'foo/bar'], cmd.response.lines)
def test_unconplete_line_with_litteral_fetch(self): cmd = Command('FETCH', 'TAG') self.line_handler.side_effect = [cmd, cmd, cmd, cmd, cmd] with self.assertRaises(asyncio.IncompleteReadError) as expected: self.imap_protocol._handle_responses( b'* 12 FETCH (BODY[HEADER] {4}\r\nyo\r\n)\r\n* 13 FETCH (BODY[', self.line_handler) self.line_handler.assert_has_calls( [call('* 12 FETCH (BODY[HEADER] {4}', None), call(')', cmd)]) self.line_handler.reset_mock() self.imap_protocol._handle_responses( expected.exception.partial + b'HEADER] {5}\r\nyo2\r\n)\r\nTAG OK STORE completed.\r\n', self.line_handler) self.line_handler.assert_has_calls([ call('* 13 FETCH (BODY[HEADER] {5}', None), call(')', cmd), call('TAG OK STORE completed.', None) ]) self.assertEqual([b'yo\r\n', b'yo2\r\n'], cmd.response.lines)
def test_command_timeout(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=1) yield from self.advance(2) with self.assertRaises(asyncio.TimeoutError): yield from cmd.wait()
def test_command_timeout(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=1) yield from self.advance(2) with self.assertRaises(AioImapException): yield from cmd.wait()