def test_client_cert_missing_one(self, request, tmp_path): """Test cert or key supplied, but not the other.""" ax_url = get_url(request) test_path = tmp_path / TEST_CLIENT_CERT_NAME test_path.write_text(TEST_CLIENT_CERT) with pytest.raises(HttpError): Http(url=ax_url, cert_client_cert=test_path, certwarn=False) with pytest.raises(HttpError): Http(url=ax_url, cert_client_key=test_path, certwarn=False)
def test_certwarn_true(self, request, httpbin_secure): """Test quiet_urllib=False shows warning from urllib3.""" url = httpbin_secure.url http = Http(url=url, certwarn=True, save_history=True) with pytest.warns(InsecureRequestWarning): http()
def test_verify_ca_bundle(self, request, httpbin_secure, httpbin_ca_bundle): """Test quiet_urllib=False no warning from urllib3 when using ca bundle.""" url = httpbin_secure.url http = Http(url=url, certwarn=False) response = http() assert response.status_code == 200
def get_auth(request): """Pass.""" http = Http(url=get_url(request), certwarn=False) obj = auth.ApiKey(http=http, **get_key_creds(request)) obj.login() return obj
def test_str_repr(self, request): """Test str/repr has URL.""" ax_url = get_url(request) http = Http(url=ax_url) assert ax_url in format(http) assert ax_url in repr(http)
def test_old_version(self, request, monkeypatch): """Test exc thrown when login() and login() already called.""" monkeypatch.setattr(ApiKey, "_validate_path", "api/badwolf") http = Http(url=get_url(request), certwarn=False) auth = ApiKey(http=http, **get_key_creds(request)) with pytest.raises(AuthError): auth.login()
def test_logout_not_logged_in(self, request): """Test exc thrown when logout() but login() not called.""" http = Http(url=get_url(request), certwarn=False) auth = ApiKey(http=http, **get_key_creds(request)) with pytest.raises(NotLoggedIn): auth.logout()
def test_save_last_true(self, request): """Test last req/resp with save_last=True.""" ax_url = get_url(request) http = Http(url=ax_url, save_last=True, certwarn=False) response = http() assert response == http.LAST_RESPONSE assert response.request == http.LAST_REQUEST
def test_save_history(self, request): """Test last resp added to history with save_history=True.""" ax_url = get_url(request) http = Http(url=ax_url, save_history=True, certwarn=False) response = http() assert response in http.HISTORY
def test_login_already_logged_in(self, request): """Test exc thrown when login() and login() already called.""" http = Http(url=get_url(request), certwarn=False) auth = ApiKey(http=http, **get_key_creds(request)) auth.login() with pytest.raises(AlreadyLoggedIn): auth.login()
def test_invalid_creds(self, request): """Test str/repr has URL.""" http = Http(url=get_url(request), certwarn=False) bad = "badwolf" auth = ApiKey(http=http, key=bad, secret=bad) with pytest.raises(InvalidCredentials): auth.login()
def test_client_cert_both(self, request, tmp_path): """Test cert or key supplied, but not the other.""" ax_url = get_url(request) both_path = tmp_path / TEST_CLIENT_CERT_NAME both_cert = "{}\n{}\n".format(TEST_CLIENT_CERT, TEST_CLIENT_KEY) both_path.write_text(both_cert) http = Http(url=ax_url, cert_client_both=both_path, certwarn=False) http()
def test_http_lock_fail(self, request): """Test using an http client from another authmethod throws exc.""" http = Http(url=get_url(request), certwarn=False) auth = ApiKey(http=http, **get_key_creds(request)) assert auth.http._auth_lock with pytest.raises(AuthError): auth = ApiKey(http=http, **get_key_creds(request))
def test_parsed_url(self, request): """Test url=UrlParser() works.""" ax_url = get_url(request) parsed_url = UrlParser(url=ax_url, default_scheme="https") http = Http(url=parsed_url) assert ax_url in format(http) assert ax_url in repr(http)
def test_save_last_false(self, request): """Test last req/resp with save_last=False.""" ax_url = get_url(request) http = Http(url=ax_url, save_last=False, certwarn=False) http() assert not http.LAST_RESPONSE assert not http.LAST_REQUEST
def test_valid_creds(self, request): """Test str/repr has URL.""" http = Http(url=get_url(request), certwarn=False) auth = ApiKey(http=http, **get_key_creds(request)) auth.login() assert auth.is_logged_in assert "url" in format(auth) assert "url" in repr(auth)
def test_logout(self, request): """Test no exc when logout() after login().""" http = Http(url=get_url(request), certwarn=False) auth = ApiKey(http=http, **get_key_creds(request)) auth.login() assert auth.is_logged_in auth.logout() assert not auth.is_logged_in
def test_log_req_body_false(self, request, caplog): """Test no logging of request body when log_request_body=False.""" caplog.set_level(logging.DEBUG) ax_url = get_url(request) http = Http(url=ax_url, log_request_body=False, certwarn=False, log_level="debug") http() assert not caplog.records
def test_log_response_attrs_none(self, request, caplog): """Test no logging of response attrs when log_response_attrs=None.""" caplog.set_level(logging.DEBUG) ax_url = get_url(request) http = Http(url=ax_url, log_response_attrs=None, certwarn=False, log_level="debug") http() assert not caplog.records
def test_client_cert_seperate(self, request, tmp_path): """Test cert or key supplied, but not the other.""" ax_url = get_url(request) cert_path = tmp_path / TEST_CLIENT_CERT_NAME cert_path.write_text(TEST_CLIENT_CERT) key_path = tmp_path / TEST_CLIENT_KEY_NAME key_path.write_text(TEST_CLIENT_KEY) http = Http( url=ax_url, cert_client_cert=cert_path, cert_client_key=key_path, certwarn=False, ) http()
def test_log_req_body_true(self, request, caplog): """Test logging of request body when log_request_body=True.""" caplog.set_level(logging.DEBUG) ax_url = get_url(request) http = Http(url=ax_url, log_request_body=True, certwarn=False, log_level="debug") http() assert len(caplog.records) == 1 entries = ["REQUEST BODY:.*"] log_check(caplog, entries)
def test_log_resp_attrs_true(self, request, caplog): """Test verbose logging of response attrs when log_response_attrs=True.""" caplog.set_level(logging.DEBUG) ax_url = get_url(request) http = Http( url=ax_url, log_response_attrs=["url", "headers"], certwarn=False, log_level="debug", ) http() assert len(caplog.records) == 1 entries = ["RESPONSE ATTRS:.*{}.*headers".format(http.url)] log_check(caplog, entries)
def test_certwarn_false(self, request, httpbin_secure): """Test quiet_urllib=False shows warning from urllib3.""" url = httpbin_secure.url http = Http(url=url, certwarn=False) http()
def test_user_agent(self, request): """Test user_agent has version in it.""" ax_url = get_url(request) http = Http(url=ax_url) assert __version__ in http.user_agent