Skip to content

Commit

Permalink
[WIP] test(TestAPI): run all unmocked tests through VCRAPI
Browse files Browse the repository at this point in the history
- TODO: deal with deduplication/replay
- TODO: deal with mocked tests
- TODO: prove convergence with TestAPIProxy; plan for removal of
  TestShared
- TODO: WIPs
- TODO: document
  • Loading branch information
cfm committed Feb 1, 2024
1 parent 30609cd commit 33cdf0b
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions client/tests/sdk/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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]

Expand Down

0 comments on commit 33cdf0b

Please sign in to comment.