From 33cdf0b1a1502a58a2bf1beb4c5e45d76cd2e1eb Mon Sep 17 00:00:00 2001 From: Cory Francis Myers Date: Wed, 31 Jan 2024 18:17:50 -0800 Subject: [PATCH] [WIP] test(TestAPI): run all unmocked tests through VCRAPI - TODO: deal with deduplication/replay - TODO: deal with mocked tests - TODO: prove convergence with TestAPIProxy; plan for removal of TestShared - TODO: WIPs - TODO: document --- client/tests/sdk/test_api.py | 80 ++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/client/tests/sdk/test_api.py b/client/tests/sdk/test_api.py index 5424cc6649..396a63088d 100644 --- a/client/tests/sdk/test_api.py +++ b/client/tests/sdk/test_api.py @@ -8,6 +8,7 @@ import vcr from requests.exceptions import ConnectTimeout, ReadTimeout from test_shared import TestShared +from utils import VCRAPI from securedrop_client.sdk import API, RequestTimeoutError from securedrop_client.sdk.sdlocalobjects import AuthError, Reply, Submission @@ -21,6 +22,7 @@ class TestAPI(TestShared): API and API Proxy tests. """ + @VCRAPI.use_cassette def setup_method(self): self.totp = pyotp.TOTP("JHCOGO7VCER3EJ4L") self.username = "journalist" @@ -34,47 +36,47 @@ def setup_method(self): # It doesn't matter if these intermittent 403s are captured in the # cassette as we ignore them during playback. auth_result = None - with vcr.use_cassette("tests/sdk/data/test-setup.yml") as cassette: - for i in range(3): - totp = self.totp.now() - self.api = API(self.server, self.username, self.password, str(totp)) - try: - auth_result = self.api.authenticate() - except AuthError: - # Don't sleep on final retry attempt or during playback - if i < 2 and cassette.play_count == 0: - time.sleep(31) - continue - # No error, let's move on - break + for i in range(3): + totp = self.totp.now() + self.api = VCRAPI(self.server, self.username, self.password, str(totp)) + try: + auth_result = self.api.authenticate() + except AuthError: + # Don't sleep on final retry attempt or during playback + if i < 2 and cassette.play_count == 0: + time.sleep(31) + continue + # No error, let's move on + break if auth_result is None: raise AuthError("Could not obtain API token during test setup.") - @vcr.use_cassette("tests/sdk/data/test-baduser.yml") + @VCRAPI.use_cassette def test_auth_baduser(self): - self.api = API(self.server, "no", self.password, str(self.totp.now())) + self.api = VCRAPI(self.server, "no", self.password, str(self.totp.now())) with pytest.raises(AuthError): self.api.authenticate() - @vcr.use_cassette("tests/sdk/data/test-badpassword.yml") + @VCRAPI.use_cassette def test_auth_badpassword(self): - self.api = API(self.server, self.username, "no", str(self.totp.now())) + self.api = VCRAPI(self.server, self.username, "no", str(self.totp.now())) with pytest.raises(AuthError): self.api.authenticate() - @vcr.use_cassette("tests/sdk/data/test-badotp.yml") + @VCRAPI.use_cassette def test_auth_badotp(self): - self.api = API(self.server, self.username, self.password, "no") + self.api = VCRAPI(self.server, self.username, self.password, "no") with pytest.raises(AuthError): self.api.authenticate() + @VCRAPI.use_cassette def test_api_auth(self): super().api_auth() # This test is order-sensitive and must be run before the "seen" # state of files is altered. - @vcr.use_cassette("tests/sdk/data/test-download-submission.yml") + @VCRAPI.use_cassette def test_download_submission(self): submissions = self.api.get_all_submissions() unread_submission = None @@ -109,80 +111,80 @@ def test_download_submission(self): # Let us remove the temporary directory shutil.rmtree(tmpdir) - @vcr.use_cassette("tests/sdk/data/test-seen.yml") + @VCRAPI.use_cassette def test_seen(self): super().seen() - @vcr.use_cassette("tests/sdk/data/test-get-sources.yml") + @VCRAPI.use_cassette def test_get_sources(self): super().get_sources() - @vcr.use_cassette("tests/sdk/data/test-star-add-remove.yml") + @VCRAPI.use_cassette def test_star_add_remove(self): super().star_add_remove() - @vcr.use_cassette("tests/sdk/data/test-get-single-source.yml") + @VCRAPI.use_cassette def test_get_single_source(self): super().get_single_source() - @vcr.use_cassette("tests/sdk/data/test-get-single-source.yml") + @VCRAPI.use_cassette def test_get_single_source_from_string(self): super().get_single_source(from_string=True) - @vcr.use_cassette("tests/sdk/data/test-failed-single-source.yml") + @VCRAPI.use_cassette def test_failed_single_source(self): super().failed_single_source() - @vcr.use_cassette("tests/sdk/data/test-get-submissions.yml") + @VCRAPI.use_cassette def test_get_submissions(self): super().get_submissions() - @vcr.use_cassette("tests/sdk/data/test-get-submission.yml") + @VCRAPI.use_cassette def test_get_submission(self): super().get_submission() - @vcr.use_cassette("tests/sdk/data/test-get-submission.yml") + @VCRAPI.use_cassette def test_get_submission_from_string(self): super().get_submission(from_string=True) - @vcr.use_cassette("tests/sdk/data/test-get-wrong-submissions.yml") + @VCRAPI.use_cassette def test_get_wrong_submissions(self): super().get_wrong_submissions() - @vcr.use_cassette("tests/sdk/data/test-get-all-submissions.yml") + @VCRAPI.use_cassette def test_get_all_submissions(self): super().get_all_submissions() - @vcr.use_cassette("tests/sdk/data/test-flag-source.yml") + @VCRAPI.use_cassette def test_flag_source(self): super().flag_source() - @vcr.use_cassette("tests/sdk/data/test-get-current-user.yml") + @VCRAPI.use_cassette def test_get_current_user(self): super().get_current_user() - @vcr.use_cassette("tests/sdk/data/test-get-users.yml") + @VCRAPI.use_cassette def test_get_users(self): super().get_users() - @vcr.use_cassette("tests/sdk/data/test-error-unencrypted-reply.yml") + @VCRAPI.use_cassette def test_error_unencrypted_reply(self): super().error_unencrypted_reply() - @vcr.use_cassette("tests/sdk/data/test-get-replies-from-source.yml") + @VCRAPI.use_cassette def test_get_replies_from_source(self): super().get_replies_from_source() - @vcr.use_cassette("tests/sdk/data/test-get-reply-from-source.yml") + @VCRAPI.use_cassette def test_get_reply_from_source(self): super().get_reply_from_source() - @vcr.use_cassette("tests/sdk/data/test-get-all-replies.yml") + @VCRAPI.use_cassette def test_get_all_replies(self): super().get_all_replies() # This test is materially different in the API & API Proxy versions. - @vcr.use_cassette("tests/sdk/data/test-download-reply.yml") + @VCRAPI.use_cassette def test_download_reply(self): r = self.api.get_all_replies()[0]