Skip to content

fix: Reuse HMAC key as we have a limit of 5 #3037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 5, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions storage/cloud-client/hmac_samples_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os

from google.cloud import storage
import google.api_core.exceptions
import pytest

import storage_activate_hmac_key
Expand All @@ -36,11 +37,13 @@
STORAGE_CLIENT = storage.Client(project=PROJECT_ID)


@pytest.fixture
@pytest.fixture(scope="module")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this correct the fixture once for the entire test suite? If test_delete_key is ran first then it would be deleted for all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the way we have things configured, within the module these will run in order. Yes, if delete is run first, the key will be gone for our tests. So this change makes the order significant.

def new_hmac_key():
"""
Fixture to create a new HMAC key, and to guarantee all keys are deleted at
the end of each test.
the end of the module.

NOTE: Due to the module scope, test order in this file is significant
"""
hmac_key, secret = STORAGE_CLIENT.create_hmac_key(
service_account_email=SERVICE_ACCOUNT_EMAIL, project_id=PROJECT_ID
Expand Down Expand Up @@ -91,7 +94,7 @@ def test_activate_key(capsys, new_hmac_key):
assert hmac_key.state == "ACTIVE"


def test_deactivate_key(capsys, new_hmac_key):
def test_deactivate_key(capsys, new_hmac_key):
hmac_key = storage_deactivate_hmac_key.deactivate_key(
new_hmac_key.access_id, PROJECT_ID
)
Expand All @@ -100,7 +103,13 @@ def test_deactivate_key(capsys, new_hmac_key):


def test_delete_key(capsys, new_hmac_key):
new_hmac_key.state = "INACTIVE"
new_hmac_key.update()
# Due to reuse of the HMAC key for each test function, the previous
# test has deactivated the key already.
try:
new_hmac_key.state = "INACTIVE"
new_hmac_key.update()
except google.api_core.exceptions.BadRequest:
pass

storage_delete_hmac_key.delete_key(new_hmac_key.access_id, PROJECT_ID)
assert "The key is deleted" in capsys.readouterr().out