Exemplo n.º 1
0
 async def test_multiple_torrents(self):
     self.srvapi.torrent.response = [
         Response(success=True,
                  msgs=['Added Some Torrent'],
                  torrent=MockTorrent(id=1, name='Some Torrent')),
         Response(success=False,
                  errors=('Something went wrong',),
                  torrent=None),
     ]
     process = await self.execute(AddTorrentsCmd, 'some.torrent', 'another.torrent')
     self.srvapi.torrent.assert_called(2, 'add',
                                    ('some.torrent',), {'stopped': False, 'path': None},
                                    ('another.torrent',), {'stopped': False, 'path': None})
     self.assertEqual(process.success, False)
     self.assert_stdout('add: Added Some Torrent')
     self.assert_stderr('add: Something went wrong')
Exemplo n.º 2
0
 async def test_failure(self):
     self.api.torrent.response = Response(
         success=False, msgs=[ClientError('Bogus torrent')], torrent=None)
     process = AddTorrentsCmd(['some.torrent'], loop=self.loop)
     with self.assertLogs(level='ERROR') as logged:
         await self.finish(process)
     self.api.torrent.assert_called(1, 'add', ('some.torrent', ),
                                    {'stopped': False})
     self.assertEqual(process.success, False)
     self.assert_logged(logged, ('ERROR', '^Bogus torrent$'))
Exemplo n.º 3
0
 async def torrents(self, torrents=None, keys='ALL', autoconnect=True):
     if self.delay:
         await asyncio.sleep(self.delay, loop=asyncio.get_event_loop())
     self.calls += 1
     self.arg_torrents = torrents
     self.arg_keys = keys
     if self.exc is None:
         return Response(torrents=self.tlist)
     else:
         raise self.exc
Exemplo n.º 4
0
 async def test_failure(self):
     self.srvapi.torrent.response = Response(
         success=False,
         errors=('Bogus torrent',),
         torrent=None)
     process = await self.execute(AddTorrentsCmd, 'some.torrent')
     self.srvapi.torrent.assert_called(1, 'add', ('some.torrent',), {'stopped': False, 'path': None})
     self.assertEqual(process.success, False)
     self.assert_stdout()
     self.assert_stderr('add: Bogus torrent')
Exemplo n.º 5
0
    async def do(self, args, tlist, success_exp, msgs=(), errors=()):
        self.srvapi.torrent.response = Response(success=success_exp, torrents=tlist, errors=errors)

        process = await self.execute(TorrentDetailsCmd, *args)
        self.assertEqual(process.success, success_exp)

        self.assert_stdout()
        self.assert_stderr(*tuple('^%s: %s$' % (TorrentDetailsCmd.name, err) for err in errors))

        self.srvapi.torrent.assert_called(1, 'torrents', (process.mock_tfilter,), {'keys': ('id', 'name')})
Exemplo n.º 6
0
 async def test_option_stopped(self):
     self.srvapi.torrent.response = Response(
         success=True,
         msgs=('Added Some Torrent',),
         torrent=MockTorrent(id=1, name='Some Torrent'))
     process = await self.execute(AddTorrentsCmd, 'some.torrent', '--stopped')
     self.srvapi.torrent.assert_called(1, 'add', ('some.torrent',), {'stopped': True, 'path': None})
     self.assertEqual(process.success, True)
     self.assert_stdout('add: Added Some Torrent')
     self.assert_stderr()
Exemplo n.º 7
0
 async def torrents(self, torrents=None, keys='ALL'):
     if self.delay:
         await asyncio.sleep(self.delay)
     self.calls += 1
     self.arg_torrents = torrents
     self.arg_keys = keys
     if self.exc is None:
         return Response(success=False, torrents=self.tlist)
     else:
         raise self.exc
Exemplo n.º 8
0
 async def test_multiple_torrents(self):
     self.api.torrent.response = [
         Response(success=True,
                  msgs=['Added Some Torrent'],
                  torrent=MockTorrent(id=1, name='Some Torrent')),
         Response(success=False,
                  msgs=[ClientError('Something went wrong')],
                  torrent=None),
     ]
     process = AddTorrentsCmd(['some.torrent', 'another.torrent'],
                              loop=self.loop)
     with self.assertLogs(level='INFO') as logged:
         await self.finish(process)
     self.api.torrent.assert_called(2, 'add', ('some.torrent', ),
                                    {'stopped': False},
                                    ('another.torrent', ),
                                    {'stopped': False})
     self.assertEqual(process.success, False)
     self.assert_logged(logged, ('INFO', '^Added Some Torrent$'),
                        ('ERROR', '^Something went wrong$'))
Exemplo n.º 9
0
    async def do(self, args, success_exp, msgs=(), errors=()):
        self.srvapi.torrent.response = Response(success=success_exp, msgs=msgs, errors=errors)

        process = await self.execute(VerifyTorrentsCmd, *args)
        self.assertEqual(process.success, success_exp)

        msgs_exp = tuple('^%s: %s$' % (VerifyTorrentsCmd.name, msg) for msg in msgs)
        errors_exp = tuple('^%s: %s$' % (VerifyTorrentsCmd.name, err) for err in errors)
        self.assert_stdout(*msgs_exp)
        self.assert_stderr(*errors_exp)

        self.srvapi.torrent.assert_called(1, 'verify', (process.mock_tfilter,), {})
Exemplo n.º 10
0
 async def test_option_stopped(self):
     self.api.torrent.response = Response(success=True,
                                          msgs=['Added Some Torrent'],
                                          torrent=MockTorrent(
                                              id=1, name='Some Torrent'))
     process = AddTorrentsCmd(['some.torrent', '--stopped'], loop=self.loop)
     with self.assertLogs(level='INFO') as logged:
         await self.finish(process)
     self.api.torrent.assert_called(1, 'add', ('some.torrent', ),
                                    {'stopped': True})
     self.assertEqual(process.success, True)
     self.assert_logged(logged, ('INFO', '^Added Some Torrent$'))
Exemplo n.º 11
0
 async def do(self, args, msgs=()):
     success_exp = all(isinstance(msg, str) for msg in msgs)
     self.api.torrent.response = Response(success=success_exp, msgs=msgs)
     process = VerifyTorrentsCmd(args, loop=self.loop)
     with self.assertLogs(level='INFO') as logged:
         await self.finish(process)
     self.api.torrent.assert_called(1, 'verify', (process.mock_tfilter, ),
                                    {})
     self.assertEqual(process.success, success_exp)
     exp_msgs = tuple(
         (('INFO' if isinstance(msg, str) else 'ERROR'), str(msg))
         for msg in msgs)
     self.assert_logged(logged, *exp_msgs)
Exemplo n.º 12
0
    async def do(self, args, tlist, success_exp, msgs=(), errors=(), delete=False, remove_called=True):
        self.srvapi.torrent.response = Response(success=success_exp, torrents=tlist, msgs=msgs, errors=errors)

        process = await self.execute(RemoveTorrentsCmd, *args)
        self.assertEqual(process.success, success_exp)

        msgs_exp = tuple('^%s: %s$' % (RemoveTorrentsCmd.name, msg) for msg in msgs)
        errors_exp = tuple('^%s: %s$' % (RemoveTorrentsCmd.name, err) for err in errors)
        self.assert_stdout(*msgs_exp)
        self.assert_stderr(*errors_exp)

        if remove_called:
            self.srvapi.torrent.assert_called(1, 'remove', (process.mock_tfilter,), {'delete': delete})
        else:
            self.srvapi.torrent.assert_called(0, 'remove')
Exemplo n.º 13
0
    async def do(self, args, success_exp, msgs=(), errors=(), force=False, toggle=False):
        self.srvapi.torrent.response = Response(success=success_exp, msgs=msgs, errors=errors)

        process = await self.execute(StartTorrentsCmd, *args)
        self.assertEqual(process.success, success_exp)

        msgs_exp = tuple('^%s: %s$' % (StartTorrentsCmd.name, msg) for msg in msgs)
        errors_exp = tuple('^%s: %s$' % (StartTorrentsCmd.name, err) for err in errors)
        self.assert_stdout(*msgs_exp)
        self.assert_stderr(*errors_exp)

        if toggle:
            self.srvapi.torrent.assert_called(1, 'toggle_stopped', (process.mock_tfilter,), {'force': force})
        else:
            self.srvapi.torrent.assert_called(1, 'start', (process.mock_tfilter,), {'force': force})
Exemplo n.º 14
0
    async def test_specifying_file(self):
        from stig.commands.cli import RenameCmd
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1234, name='Some Torrent'),))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['id=1234/mock/path/to/file', 'file2'], info_handler=info_cb, error_handler=err_cb, loop=self.loop)
        await process.wait_async()
        self.assertEqual(process.success, True)
        info_cb.assert_not_called()
        err_cb.assert_not_called()

        self.mock_get_relative_path_from_focused.assert_not_called()
        self.mock_select_torrents.assert_called_once_with('id=1234', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.mock_srvapi.torrent.rename.assert_called_once_with(1234, path='mock/path/to/file', new_name='file2')
Exemplo n.º 15
0
 async def do(self, args, msgs=(), force=False, toggle=False):
     success_exp = all(isinstance(msg, str) for msg in msgs)
     self.api.torrent.response = Response(success=success_exp, msgs=msgs)
     process = StartTorrentsCmd(args, loop=self.loop)
     with self.assertLogs(level='INFO') as logged:
         await self.finish(process)
     if toggle:
         self.api.torrent.assert_called(1, 'toggle_stopped',
                                        (process.mock_tfilter, ),
                                        {'force': force})
     else:
         self.api.torrent.assert_called(1, 'start',
                                        (process.mock_tfilter, ),
                                        {'force': force})
     self.assertEqual(process.success, success_exp)
     exp_msgs = tuple(
         (('INFO' if isinstance(msg, str) else 'ERROR'), str(msg))
         for msg in msgs)
     self.assert_logged(logged, *exp_msgs)
Exemplo n.º 16
0
    async def test_renaming_multiple_torrents_fails(self):
        from stig.commands.cli import RenameCmd
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1234, name='Some Torrent'),
                      MockTorrent(id=1235, name='Some Torrent')))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['Some Torrent', 'Renamed Torrent'], info_handler=info_cb, error_handler=err_cb, loop=self.loop)
        await process.wait_async()
        self.assertEqual(process.success, False)
        info_cb.assert_not_called()
        err_cb.assert_called_once_with('rename: mock filter matches more than one torrent')

        self.mock_get_relative_path_from_focused.assert_not_called()
        self.mock_select_torrents.assert_called_once_with('Some Torrent', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.mock_srvapi.torrent.rename.assert_not_called()
Exemplo n.º 17
0
    async def test_CLI_renaming_files_of_multiple_torrents_disabled_succeeds(self):
        from stig.commands.cli import RenameCmd
        self.mock_select_torrents.return_value = 'mock filter'
        self.mock_srvapi.torrent.torrents.return_value = Response(
            success=True,
            torrents=(MockTorrent(id=1235, name='Some Torrent'),))

        info_cb, err_cb = MagicMock(), MagicMock()
        process = RenameCmd(['--unique', 'id=1235/path/to/file', 'file2'],
                            info_handler=info_cb, error_handler=err_cb, loop=self.loop)
        await process.wait_async()
        self.assertEqual(process.success, True)
        info_cb.assert_not_called()
        err_cb.assert_not_called()

        self.mock_get_relative_path_from_focused.assert_not_called()
        self.mock_select_torrents.assert_called_once_with('id=1235', allow_no_filter=False, discover_torrent=True)
        self.mock_srvapi.torrent.torrents.assert_called_once_with('mock filter', keys=('id',))
        self.assertEqual(self.mock_srvapi.torrent.rename.call_args_list,
                         [call(1235, path='path/to/file', new_name='file2')])
Exemplo n.º 18
0
    async def do(self, args, errors):
        tlist = (
            MockTorrent(id=1, name='Some Torrent'),
            MockTorrent(id=2, name='Another Torrent')
        )
        self.srvapi.torrent.response = Response(success=bool(errors), errors=(), msgs=(), torrents=tlist)

        process = await self.execute(ListTorrentsCmd, *args)
        expected_success = not errors
        self.assertEqual(process.success, expected_success)
        if errors:
            self.assert_stdout()
            self.assert_stderr(*errors)
        else:
            self.assert_stdout('Some Torrent',
                               'Another Torrent')
            self.assert_stderr()
            keys_exp = set(process.mock_tsorter.needed_keys +
                           process.mock_tfilter.needed_keys +
                           ('name',))  # columns
            self.srvapi.torrent.assert_called(1, 'torrents', (process.mock_tfilter,),
                                           {'keys': keys_exp})
            self.assertEqual(process.mock_tsorter.applied, tlist)
Exemplo n.º 19
0
 async def do(self, args, errors):
     tlist = (MockTorrent(id=1, name='Some Torrent'),
              MockTorrent(id=2, name='Another Torrent'))
     self.api.torrent.response = Response(errors=(),
                                          msgs=[],
                                          torrents=tlist)
     with self.assertLogs(level='INFO') as logged:
         process = ListTorrentsCmd(args, loop=self.loop)
         await self.finish(process)
     expected_success = not errors
     self.assertEqual(process.success, expected_success)
     if errors:
         expected_msgs = tuple(('ERROR', regex) for regex in errors)
         self.assert_logged(logged, *expected_msgs)
     else:
         self.assert_logged(logged, ('INFO', 'Some Torrent'),
                            ('INFO', 'Another Torrent'))
         keys_exp = set(process.mock_tsorter.needed_keys + \
                        process.mock_tfilter.needed_keys + \
                        ('name',))  # columns
         self.api.torrent.assert_called(1, 'torrents',
                                        (process.mock_tfilter, ),
                                        {'keys': keys_exp})
         self.assertEqual(process.mock_tsorter.applied, tlist)