def test_announce(self): """ Tests the announce method of a tracker """ t = tracker.TrackerManager(b"-bM0100-010293949201", self.torrent) resp = async_run(t.announce()) self.assertIsInstance(resp, tracker.TrackerResponse) self.assertFalse(resp.failed) async_run(t.http_client.close())
def test_completed(self): """ Tests the completed announce call to the tracker """ t = tracker.TrackerManager(b"fake", self.torrent) t.announce = create_async_mock() async_run(t.completed()) t.announce.assert_called_once_with(event=tracker.EVENT_COMPLETED) async_run(t.http_client.close())
def test_creation(self): """ Tests we can create tracker object from a torrent """ tt = tracker.TrackerManager(b"fake", self.torrent) self.assertIsInstance(tt, tracker.TrackerManager) self.assertEqual(tt.info_hash, self.torrent.info_hash) self.assertEqual(tt.announce_urls, self.torrent.announce_urls) self.assertEqual(tt.peer_id, b"fake")
def test_invalid_params(self): """ Tests that a TrackerError is thrown when we send the tracker invalid parameters """ track = tracker.TrackerManager(b"fake", self.torrent) track._get_url_params = mock.MagicMock(return_value={}) with self.subTest(msg="Empty params"): self.assertRaises(tracker.TrackerConnectionError, async_run, track.announce()) async_run(track.http_client.close())
def test_invalid_request(self): """ Tests that announce fails with an invalid request """ track = tracker.TrackerManager(b"fake", self.torrent) track._get_url_params = mock.MagicMock(return_value=[]) with self.subTest(msg="Malformed URL"): self.assertRaises(tracker.TrackerConnectionError, async_run, track.announce()) async_run(track.http_client.close()) track = tracker.TrackerManager(b"fake", self.torrent) with mock.patch("aiohttp.ClientSession.get", new_callable=create_async_mock( data=b"", status=404)) as mocked_get: with self.subTest(msg="Non 200 HTTP response"): self.assertRaises(tracker.TrackerConnectionError, async_run, track.announce()) mocked_get.assert_called_once() async_run(track.http_client.close())
def test_failed_response(self): """ Tests that a tracker response that failed (contains b"failure reason") log_and_raise a TrackerError and properly finds the failure reason """ data = b"d14:failure reason14:mock mock mocke" status = 200 track = tracker.TrackerManager(b"fake", self.torrent) with self.subTest(msg="Failure reason key"): with mock.patch("aiohttp.ClientSession.get", new_callable=create_async_mock( data=data, status=status)) as mocked_get: with self.assertRaises(tracker.TrackerConnectionError): async_run(track.announce()) mocked_get.assert_called_once() async_run(track.http_client.close())
def test_valid_request_bad_data(self): """ Tests that a request to a page that returns a non bencoded dictionary log_and_raise a TrackerError (from a DecodeError) """ data = b"Not bencoded." code = 200 track = tracker.TrackerManager(b"fake", self.torrent) with mock.patch("aiohttp.ClientSession.get", new_callable=create_async_mock( data=data, status=code)) as mocked_get: with self.subTest(msg="Valid 200 HTTP response, invalid data."): self.assertRaises(tracker.TrackerConnectionError, async_run, track.announce()) mocked_get.assert_called_once() async_run(track.http_client.close())
def test__make_params(self): """ tests we properly construct the parameters for the announce call """ expected_params = { "info_hash": self.torrent.info_hash, "peer_id": b"fake", "port": 6881, "uploaded": 0, "downloaded": 0, "left": 958398464, "compact": 1, "event": tracker.EVENT_STARTED } tt = tracker.TrackerManager(b"fake", self.torrent) self.assertEqual(expected_params, tt._get_url_params(tracker.EVENT_STARTED))