async def test_add_torrent_url(core): request = await create_mock_request( core=core, url='http://localhost/test.torrent') data = Path(torrent_dir, 'random_one_file.torrent').read_bytes() class AsyncContextManager: async def __aenter__(self): return self async def __aexit__(self, exc_type, exc, tb): return async def read(self): return data class ClientSession: async def __aenter__(self): return self async def __aexit__(self, exc_type, exc_val, exc_tb): return def get(self, url): return AsyncContextManager() with patch('aiohttp.ClientSession', new=ClientSession): body, response = await json_response(torrent.post_torrent(request)) assert response.status == 201
async def test_add_torrent_url(): request = create_mock_request(url='http://localhost/test.torrent') data = open( os.path.join(torrent_dir, 'random_one_file.torrent'), 'rb').read() class AsyncContextManager: async def __aenter__(self): return self async def __aexit__(self, exc_type, exc, tb): return async def read(self): print('read') return data class ClientSession: def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): return def get(self, url): return AsyncContextManager() with patch('aiohttp.ClientSession', new=ClientSession): body, response = await json_response(torrent.post_torrent(request)) assert response.status == 201
async def test_add_torrent_bad_number_args(): request = create_mock_request( url='http://testing/test.torrent', info_hash='a0'*20) with assert_raises(aiohttp.errors.HttpProcessingError) as e: await json_response(torrent.post_torrent(request)) assert e.exception.code == 400
async def test_post_torrent_info_hash(): request = create_mock_request( info_hash='44a040be6d74d8d290cd20128788864cbf770719') body, response = await json_response(torrent.post_torrent(request)) assert 'info_hash' in body info_hash = body['info_hash'] assert info_hash == '44a040be6d74d8d290cd20128788864cbf770719'
async def test_add_torrent_bad_number_args(core): request = await create_mock_request( core=core, url='http://testing/test.torrent', info_hash='a0'*20) with pytest.raises(aiohttp.web.HTTPBadRequest): _, response = await json_response(torrent.post_torrent(request)) assert response.status == 400
async def test_add_torrent_lt_runtime_error(): request = create_mock_request(filename='random_one_file.torrent') add_torrent = MagicMock() add_torrent.side_effect = RuntimeError() with patch('spritzle.core.core.session.add_torrent', add_torrent): with assert_raises(aiohttp.errors.HttpProcessingError) as e: await json_response(torrent.post_torrent(request)) assert e.exception.code == 500
async def test_add_torrent_bad_args(core): request = await create_mock_request( core=core, filename='random_one_file.torrent', args={'bad_key': True}, ) with pytest.raises(aiohttp.web.HTTPBadRequest): _, response = await json_response(torrent.post_torrent(request)) assert response.status == 400
async def test_add_torrent_lt_runtime_error(core): request = await create_mock_request(core=core, filename='random_one_file.torrent') add_torrent = MagicMock() add_torrent.side_effect = RuntimeError() request.app['spritzle.core'].session.add_torrent = add_torrent with pytest.raises(aiohttp.web.HTTPInternalServerError): _, response = await json_response(torrent.post_torrent(request)) assert response.status == 500
async def test_post_torrent_bad_body(): request = create_mock_request(filename='random_one_file.torrent') async def json(): return b'\xc3\x28'.decode("utf8") request.json = json body, response = await json_response(torrent.post_torrent(request)) assert 'info_hash' in body info_hash = body['info_hash'] assert info_hash == '44a040be6d74d8d290cd20128788864cbf770719'
async def test_post_torrent(): request = create_mock_request(filename='random_one_file.torrent') body, response = await json_response(torrent.post_torrent(request)) assert 'info_hash' in body info_hash = body['info_hash'] assert response.headers['LOCATION'] == \ 'http://localhost:8080/torrent/{}'.format(info_hash) assert response.status == 201 assert info_hash == '44a040be6d74d8d290cd20128788864cbf770719' request = MagicMock() request.match_info = {} tlist, response = await json_response(torrent.get_torrent(request)) assert tlist == ['44a040be6d74d8d290cd20128788864cbf770719']
async def test_post_torrent(core): request = await create_mock_request(core=core, filename='random_one_file.torrent', tags=['foo']) body, response = await json_response(torrent.post_torrent(request)) assert 'info_hash' in body info_hash = body['info_hash'] assert response.headers['LOCATION'] == \ f'http://localhost:8080/torrent/{info_hash}' assert response.status == 201 assert info_hash == '44a040be6d74d8d290cd20128788864cbf770719' request.match_info = {} tlist, response = await json_response(torrent.get_torrent(request)) assert tlist == ['44a040be6d74d8d290cd20128788864cbf770719'] return request
async def test_add_torrent_bad_file(): request = create_mock_request('empty.torrent') with assert_raises(aiohttp.errors.HttpProcessingError) as e: await json_response(torrent.post_torrent(request)) assert e.exception.code == 400
async def test_add_torrent_bad_file(core): request = await create_mock_request(core=core, filename='empty.torrent') with pytest.raises(aiohttp.web.HTTPBadRequest): _, response = await json_response(torrent.post_torrent(request)) assert response.status == 400