async def test_get_apikey_finds_apikey(mocker, tmp_path): get_mock = mocker.patch('upsies.utils.http.get', AsyncMock()) post_mock = mocker.patch('upsies.utils.http.post', AsyncMock(return_value=Result( text=( '<html>' '<body>' '<form>' '<input id="api_key" name="api_key" value="d00d1e" />' '</form>' '</body>' '</html>' ), bytes=b'irrelevant', ))) imghost = ptpimg.PtpimgImageHost(options={}, cache_directory=tmp_path) apikey = await imghost.get_apikey('foo@localhost', 'hunter2') assert apikey == 'd00d1e' assert post_mock.call_args_list == [call( url=f'{imghost.options["base_url"]}/login.php', cache=False, data={ 'email': 'foo@localhost', 'pass': '******', 'login': '', }, )] assert get_mock.call_args_list == [call(f'{imghost.options["base_url"]}/logout.php')]
async def test_upload_image_succeeds(mocker, tmp_path): post_mock = mocker.patch( 'upsies.utils.http.post', AsyncMock(return_value=Result( text= '{"image":{"image":{"url":"https://localhost/path/to/image.png"}}}', bytes=b'irrelevant', ))) imghost = freeimage.FreeimageImageHost(cache_directory=tmp_path) url = await imghost._upload_image('some/image.png') assert url == 'https://localhost/path/to/image.png' assert post_mock.call_args_list == [ call( url=imghost.options['base_url'] + '/api/1/upload', cache=False, data={ 'key': imghost.options['apikey'], 'action': 'upload', 'format': 'json', }, files={ 'source': 'some/image.png', }, ) ]
async def test_upload_image_gets_unexpected_json(json_response, mocker, tmp_path): mocker.patch('upsies.utils.http.post', AsyncMock(return_value=Result( text=json.dumps(json_response), bytes=b'irrelevant', ))) imghost = ptpimg.PtpimgImageHost(options={'apikey': 'f00'}, cache_directory=tmp_path) with pytest.raises(RuntimeError, match=rf'^Unexpected response: {re.escape(str(json_response))}$'): await imghost._upload_image('some/path.jpg')
async def test_upload_image_gets_unexpected_response(response, mocker, tmp_path): mocker.patch('upsies.utils.http.post', AsyncMock(return_value=Result( text=response, bytes=b'irrelevant', ))) imghost = imgbb.ImgbbImageHost(cache_directory=tmp_path) with pytest.raises(RuntimeError, match=rf'^Unexpected response: {re.escape(response)}$'): await imghost._upload_image('some/image.png')
async def test_upload_succeeds(mocker): tracker = BhdTracker(options={ 'upload_url': 'http://bhd.local/upload', 'apikey': '1337' }) tracker_jobs_mock = Mock( post_data={ 'foo': 'asdf', 'bar': '', 'baz': 0, 'quux': '0', 'quuz': None, }, torrent_filepath='path/to/content.torrent', mediainfo_filehandle=io.StringIO('mediainfo mock'), ) http_mock = mocker.patch( 'upsies.utils.http', Mock(post=AsyncMock(return_value=Result( text=''' { "status_code": 2, "success": true, "status_message": "http://bhd.local/torrent/download/release_name.123456.d34db33f" } ''', bytes=b'irrelevant', )), )) torrent_page_url = await tracker.upload(tracker_jobs_mock) assert torrent_page_url == 'http://bhd.local/torrents/release_name.123456' assert http_mock.post.call_args_list == [ call( url='http://bhd.local/upload/1337', cache=False, user_agent=True, data={ 'foo': 'asdf', 'baz': '0', 'quux': '0', }, files={ 'file': { 'file': tracker_jobs_mock.torrent_filepath, 'mimetype': 'application/octet-stream', }, 'mediainfo': { 'file': tracker_jobs_mock.mediainfo_filehandle, 'filename': 'mediainfo', 'mimetype': 'application/octet-stream', }, }, ) ]
async def test_upload_draft_succeeds(mocker): tracker = BhdTracker(options={ 'upload_url': 'http://bhd.local/upload', 'apikey': '1337' }) warning_cb = Mock() tracker.signal.register('warning', warning_cb) tracker_jobs_mock = Mock( post_data={'foo': 'bar'}, torrent_filepath='path/to/content.torrent', mediainfo_filehandle=io.StringIO('mediainfo mock'), ) http_mock = mocker.patch( 'upsies.utils.http', Mock(post=AsyncMock(return_value=Result( text=''' { "status_code": 1, "success": true, "status_message": "Draft uploaded" } ''', bytes=b'irrelevant', )), )) torrent_filepath = await tracker.upload(tracker_jobs_mock) assert warning_cb.call_args_list == [ call('Draft uploaded'), call('You have to activate your upload manually ' 'on the website when you are ready to seed.') ] assert torrent_filepath == tracker_jobs_mock.torrent_filepath assert http_mock.post.call_args_list == [ call( url='http://bhd.local/upload/1337', cache=False, user_agent=True, data=tracker_jobs_mock.post_data, files={ 'file': { 'file': tracker_jobs_mock.torrent_filepath, 'mimetype': 'application/octet-stream', }, 'mediainfo': { 'file': tracker_jobs_mock.mediainfo_filehandle, 'filename': 'mediainfo', 'mimetype': 'application/octet-stream', }, }, ) ]
async def test_upload_finds_error_error_message(page, exp_message, headers, get_html_page, mocker): response = Result( text=get_html_page('bb', page), bytes=b'not relevant', headers=headers, ) mocker.patch('upsies.utils.http.post', AsyncMock(return_value=response)) tracker = BbTracker( options={ 'base_url': 'http://bb.local', }, ) tracker_jobs_mock = Mock() with pytest.raises(errors.RequestError, match=rf'^Upload failed: {exp_message}$'): await tracker.upload(tracker_jobs_mock)
async def test_upload_fails_to_find_error_or_warning_message(mocker): response = Result( text=''' <html> <body> </body> </html> ''', bytes=b'not relevant', ) html_dump_mock = mocker.patch('upsies.utils.html.dump') mocker.patch('upsies.utils.http.post', AsyncMock(return_value=response)) tracker = BbTracker(options={'base_url': 'http://bb.local'}) tracker_jobs_mock = Mock() with pytest.raises(RuntimeError, match=r'^Failed to find error message\. See upload\.html\.$'): await tracker.upload(tracker_jobs_mock) assert html_dump_mock.call_args_list == [call(response, 'upload.html')]
async def test_upload_handles_incomplete_warning_message(html_string, mocker): response = Result( text=html_string, bytes=b'not relevant', ) mocker.patch('upsies.utils.http.post', AsyncMock(return_value=response)) tracker = BbTracker( options={ 'base_url': 'http://bb.local', }, ) tracker_jobs_mock = Mock() html_dump_mock = mocker.patch('upsies.utils.html.dump') mocker.patch.object(tracker, 'warn') with pytest.raises(RuntimeError, match=r'^Failed to find error message\. See upload\.html\.$'): await tracker.upload(tracker_jobs_mock) assert html_dump_mock.call_args_list == [call(response, 'upload.html')] assert tracker.warn.call_args_list == []
async def test_upload_makes_expected_request(mocker): response = Result(text='', bytes=b'', headers={'Location': 'torrents.php?id=123'}) post_mock = mocker.patch('upsies.utils.http.post', AsyncMock(return_value=response)) tracker = BbTracker(options={'base_url': 'http://bb.local'}) tracker_jobs_mock = Mock() torrent_page_url = await tracker.upload(tracker_jobs_mock) assert torrent_page_url == 'http://bb.local/torrents.php?id=123' assert post_mock.call_args_list == [call( url='http://bb.local' + BbTracker._url_path['upload'], cache=False, user_agent=True, follow_redirects=False, files={'file_input': { 'file': tracker_jobs_mock.torrent_filepath, 'mimetype': 'application/octet-stream', }}, data=tracker_jobs_mock.post_data, )]
async def test_get_apikey_with_wrong_login(mocker, tmp_path): get_mock = mocker.patch('upsies.utils.http.get', AsyncMock()) post_mock = mocker.patch('upsies.utils.http.post', AsyncMock(return_value=Result( text=( '<html>' ' <body>' ' <div class="container">' ' <div class="page-header">' ' <div class="panel panel-danger">' ' <div class="panel-heading">' ' <i class="glyphicon glyphicon-remove">' ' </i>' ' <span>' ' Error!' ' </span>' ' </div>' ' <div class="panel-body">' ' <ul>' ' <li>' ' Credentials are incorrect. Please try again.' ' </li>' ' </ul>' ' </div>' ' </div>' ' </div>' ' </div>' ' </body>' '</html>' ), bytes=b'irrelevant', ))) imghost = ptpimg.PtpimgImageHost(options={}, cache_directory=tmp_path) with pytest.raises(errors.RequestError, match=r'^Credentials are incorrect\. Please try again\.$'): await imghost.get_apikey('foo@localhost', 'hunter2') assert post_mock.call_args_list == [call( url=f'{imghost.options["base_url"]}/login.php', cache=False, data={ 'email': 'foo@localhost', 'pass': '******', 'login': '', }, )] assert get_mock.call_args_list == [call(f'{imghost.options["base_url"]}/logout.php')]
async def test_upload_gets_error_status_code(mocker): tracker = BhdTracker(options={ 'upload_url': 'http://bhd.local/upload', 'apikey': '1337' }) tracker_jobs_mock = Mock( post_data={'foo': 'bar'}, torrent_filepath='path/to/content.torrent', mediainfo_filehandle=io.StringIO('mediainfo mock'), ) http_mock = mocker.patch( 'upsies.utils.http', Mock(post=AsyncMock(return_value=Result( text=''' { "status_code": 0, "success": false, "status_message": "This is the error message" } ''', bytes=b'irrelevant', )), )) with pytest.raises(errors.RequestError, match=r'^Upload failed: This is the error message$'): await tracker.upload(tracker_jobs_mock) assert http_mock.post.call_args_list == [ call( url='http://bhd.local/upload/1337', cache=False, user_agent=True, data=tracker_jobs_mock.post_data, files={ 'file': { 'file': tracker_jobs_mock.torrent_filepath, 'mimetype': 'application/octet-stream', }, 'mediainfo': { 'file': tracker_jobs_mock.mediainfo_filehandle, 'filename': 'mediainfo', 'mimetype': 'application/octet-stream', }, }, ) ]
async def test_get_apikey_fails_to_find_apikey(mocker, tmp_path): get_mock = mocker.patch('upsies.utils.http.get', AsyncMock()) post_mock = mocker.patch('upsies.utils.http.post', AsyncMock(return_value=Result( text='', bytes=b'irrelevant', ))) imghost = ptpimg.PtpimgImageHost(options={}, cache_directory=tmp_path) with pytest.raises(RuntimeError, match=r'^Failed to find API key$'): await imghost.get_apikey('foo@localhost', 'hunter2') assert post_mock.call_args_list == [call( url=f'{imghost.options["base_url"]}/login.php', cache=False, data={ 'email': 'foo@localhost', 'pass': '******', 'login': '', }, )] assert get_mock.call_args_list == [call(f'{imghost.options["base_url"]}/logout.php')]
async def test_upload_image_succeeds(mocker, tmp_path): post_mock = mocker.patch('upsies.utils.http.post', AsyncMock(return_value=Result( text='[{"code": "this_is_the_code", "ext": "png"}]', bytes=b'irrelevant', ))) imghost = ptpimg.PtpimgImageHost(options={'apikey': 'f00'}, cache_directory=tmp_path) url = await imghost._upload_image('some/path.jpg') assert url == imghost.options['base_url'] + '/this_is_the_code.png' assert post_mock.call_args_list == [call( url=imghost.options['base_url'] + '/upload.php', cache=False, headers={ 'referer': imghost.options['base_url'] + '/index.php', }, data={ 'api_key': 'f00', }, files={ 'file-upload[0]': 'some/path.jpg', }, )]
async def test_upload_finds_warning_message(page, exp_message, headers, get_html_page, mocker): response = Result( text=get_html_page('bb', page), bytes=b'not relevant', headers=headers, ) mocker.patch('upsies.utils.http.post', AsyncMock(return_value=response)) tracker = BbTracker( options={ 'base_url': 'http://bb.local', }, ) tracker_jobs_mock = Mock() mocker.patch.object(tracker, 'warn') torrent_page_url = await tracker.upload(tracker_jobs_mock) assert torrent_page_url is None assert tracker.warn.call_args_list == [ call('The torrent did not have the correct announce URL.'), call('The mediainfo was not encoded in ROT13.'), call('Your mom.'), ]
async def test_upload_gets_unexpected_status_code(mocker): tracker = BhdTracker(options={ 'upload_url': 'http://bhd.local/upload', 'apikey': '1337' }) tracker_jobs_mock = Mock( post_data={'foo': 'bar'}, torrent_filepath='path/to/content.torrent', mediainfo_filehandle=io.StringIO('mediainfo mock'), ) http_mock = mocker.patch( 'upsies.utils.http', Mock(post=AsyncMock(return_value=Result( text='{"status_code": 123.5}', bytes=b'irrelevant', )), )) with pytest.raises( RuntimeError, match=r"^Unexpected response: '\{\"status_code\": 123.5\}'$"): await tracker.upload(tracker_jobs_mock) assert http_mock.post.call_args_list == [ call( url='http://bhd.local/upload/1337', cache=False, user_agent=True, data=tracker_jobs_mock.post_data, files={ 'file': { 'file': tracker_jobs_mock.torrent_filepath, 'mimetype': 'application/octet-stream', }, 'mediainfo': { 'file': tracker_jobs_mock.mediainfo_filehandle, 'filename': 'mediainfo', 'mimetype': 'application/octet-stream', }, }, ) ]