def test_set_base_url_non_string(self) -> None: """ Tests of the method which let us communicate the base url for the case that a non-string is given. """ given = True requester = Requester() self.assertRaises(TypeError, lambda: requester.set_base_url(given))
def test_authorization_header_non_string(self) -> None: """ Tests that the method which let us communicate the token raises an exception if a non-string value is given. """ given = True requester = Requester() self.assertRaises(TypeError, lambda: requester.set_authorization(given))
def test_set_base_url(self) -> None: """ Tests of the method which let us communicate the base url. """ given_base_url = "https://example.org/api" expected = "https://example.org/api" requester = Requester() requester.set_base_url(given_base_url) actual = requester.base_url self.assertEqual(expected, actual)
def test_authorization_header(self) -> None: """ Tests if the authorization header is correctly set. """ given_token = secrets.token_urlsafe(16) expected = {"Authorization": f"token {given_token}"} requester = Requester() requester.set_authorization(given_token) actual = requester.session.headers self.assertDictContainsSubset(expected, actual)
def test_bind_endpoint_to_base_url_endpoint_not_string(self) -> None: """ Tests of the method which let us bind an endpoint with the previouly given base url for the case that the given endpoint is a non-string. """ given_base_url = "https://example.org/api" given_endpoint = True requester = Requester() requester.set_base_url(given_base_url) self.assertRaises( TypeError, lambda: requester.bind_endpoint_to_base_url(given_endpoint))
def test_set_base_url_ends_with_with_slash(self) -> None: """ Tests of the method which let us communicate the base url for the case that a slash is given at the end of the URL. """ given_base_url = "https://example.org/api/" expected = "https://example.org/api" requester = Requester() requester.set_base_url(given_base_url) actual = requester.base_url self.assertEqual(expected, actual)
def test_get_error_type_fake_already_checked(self) -> None: """ Tests of the method which let us get the actual error type for the case that we "already know" that it is an error but it was just fake. """ given = { "@type": "user", "@href": "/user/4549848944894848948949", "@representation": "standard", "@permissions": { "read": True, "sync": True }, "id": 4549848944894848948949, "login": "******", "name": "Foo Bar", "github_id": 1148942198798789784897949849484523106, "vcs_id": "1148942198798789784897949849484523106", "vcs_type": "GithubUser", "avatar_url": None, "education": False, "allow_migration": False, "email": "*****@*****.**", "is_syncing": False, "synced_at": "2020-10-14T14:53:08Z", "recently_signed_up": False, "secure_user_hash": None, } self.assertRaises( KeyError, lambda: Requester.get_error_type(given, already_checked=True))
def test_get_error_type_not_error(self) -> None: """ Tests of the method which let us get the actual error type for the case that no error is actually given. """ given = { "@type": "user", "@href": "/user/4549848944894848948949", "@representation": "standard", "@permissions": { "read": True, "sync": True }, "id": 4549848944894848948949, "login": "******", "name": "Foo Bar", "github_id": 1148942198798789784897949849484523106, "vcs_id": "1148942198798789784897949849484523106", "vcs_type": "GithubUser", "avatar_url": None, "education": False, "allow_migration": False, "email": "*****@*****.**", "is_syncing": False, "synced_at": "2020-10-14T14:53:08Z", "recently_signed_up": False, "secure_user_hash": None, } expected = None actual = Requester.get_error_type(given) self.assertEqual(expected, actual)
def test_bind_endpoint_to_base_url(self) -> None: """ Tests of the method which let us bind an endpoint with the previously given base url. """ given_base_url = "https://example.org/api" given_endpoint = "hello/world" expected = "https://example.org/api/hello/world" requester = Requester() requester.set_base_url(given_base_url) actual = requester.bind_endpoint_to_base_url(given_endpoint) self.assertEqual(expected, actual)
def test_request_factory(self, mock_session_get): """ Tests of the request factory. """ given_base_url = "https://example.org/api/" given_endpoint = "hello/world" response = { "@type": "user", "@href": "/user/4549848944894848948949", "@representation": "standard", "@permissions": { "read": True, "sync": True }, "id": 4549848944894848948949, "login": "******", "name": "Foo Bar", "github_id": 1148942198798789784897949849484523106, "vcs_id": "1148942198798789784897949849484523106", "vcs_type": "GithubUser", "avatar_url": None, "education": False, "allow_migration": False, "email": "*****@*****.**", "is_syncing": False, "synced_at": "2020-10-14T14:53:08Z", "recently_signed_up": False, "secure_user_hash": None, } requester = Requester() requester.set_base_url(given_base_url) mock_session_get.return_value.headers = dict() mock_session_get.return_value.status_code = 200 mock_session_get.return_value.url = "https://example.org/api/hello/world" mock_session_get.return_value.json.return_value = response expected = response actual = requester.get(given_endpoint) self.assertEqual(expected, actual)
def test_request_factory_empty_esponse(self, mock_session_get): """ Tests of the request factory for the case that an empty response is given (back). """ given_base_url = "https://example.org/api/" given_endpoint = "hello/world" requester = Requester() requester.set_base_url(given_base_url) mock_session_get.return_value.status_code = 200 mock_session_get.return_value.headers = dict() mock_session_get.return_value.url = "https://example.org/api/hello/world" mock_session_get.return_value.text = "" mock_session_get.return_value.json.side_effect = json.decoder.JSONDecodeError( "hello", "world", 33) self.assertRaises(TravisCIError, lambda: requester.get(given_endpoint))
def test_request_factory_no_json_response(self, mock_session_get): """ Tests of the request factory for the case that no JSON is given as response. """ given_base_url = "https://example.org/api/" given_endpoint = "hello/world" response = { "@type": "user", "@href": "/user/4549848944894848948949", "@representation": "standard", "@permissions": { "read": True, "sync": True }, "id": 4549848944894848948949, "login": "******", "name": "Foo Bar", "github_id": 1148942198798789784897949849484523106, "vcs_id": "1148942198798789784897949849484523106", "vcs_type": "GithubUser", "avatar_url": None, "education": False, "allow_migration": False, "email": "*****@*****.**", "is_syncing": False, "synced_at": "2020-10-14T14:53:08Z", "recently_signed_up": False, "secure_user_hash": None, } requester = Requester() requester.set_base_url(given_base_url) mock_session_get.return_value.headers = dict() mock_session_get.return_value.status_code = 200 mock_session_get.return_value.url = "https://example.org/api/hello/world" mock_session_get.return_value.text = json.dumps(response) mock_session_get.return_value.json.side_effect = json.decoder.JSONDecodeError( "hello", "world", 33) self.assertRaises(TravisCIError, lambda: requester.get(given_endpoint))
def test_get_error_type(self) -> None: """ Tests of the method which let us get the actual error type. """ given = { "@type": "error", "error_type": "not_found", "error_message": "repository not found (or insufficient access)", "resource_type": "repository", } expected = "not_found" actual = Requester.get_error_type(given) self.assertEqual(expected, actual)
def test_is_error(self) -> None: """ Tests of the method which checks if the API response is an error. """ expected = True given = { "@type": "error", "error_type": "not_found", "error_message": "repository not found (or insufficient access)", "resource_type": "repository", } actual = Requester.is_error(given) self.assertEqual(expected, actual)
def test_default_header(self) -> None: """ Tests if the expected header is set. """ expected_api_version = "3" expected_user_agent = f"PyTravisCI/{__version__}" expected = { "Travis-API-Version": expected_api_version, "User-Agent": expected_user_agent, } requester = Requester() actual = requester.session.headers self.assertDictContainsSubset(expected, actual)
def test_get_error_type_already_checked(self) -> None: """ Tests of the method which let us get the actual error type for the case that we already know that it is an error. """ given = { "@type": "error", "error_type": "not_found", "error_message": "repository not found (or insufficient access)", "resource_type": "repository", } expected = "not_found" actual = Requester.get_error_type(given, already_checked=True) self.assertEqual(expected, actual)
def test_raise_if_error(self) -> None: """ Tests of the method which actually raises the exception when the API response is an error. """ given = { "@type": "error", "error_type": "not_found", "error_message": "repository not found (or insufficient access)", "resource_type": "repository", } fake_response = requests.Response() fake_response.url = "https://example.org" self.assertRaises( TravisCIError, lambda: Requester.raise_if_error(fake_response, given))
def test_raise_if_error_not_error(self) -> None: """ Tests of the method which actually raises the exception when the API response is an error, but for the case that no error is given (in the api_response) """ given = { "@type": "user", "@href": "/user/4549848944894848948949", "@representation": "standard", "@permissions": { "read": True, "sync": True }, "id": 4549848944894848948949, "login": "******", "name": "Foo Bar", "github_id": 1148942198798789784897949849484523106, "vcs_id": "1148942198798789784897949849484523106", "vcs_type": "GithubUser", "avatar_url": None, "education": False, "allow_migration": False, "email": "*****@*****.**", "is_syncing": False, "synced_at": "2020-10-14T14:53:08Z", "recently_signed_up": False, "secure_user_hash": None, } expected = None fake_response = requests.Response() fake_response.url = "https://example.org" actual = Requester.raise_if_error(fake_response, given) self.assertEqual(expected, actual)