Skip to content
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

Add tests for API key Secret reconciliation #81

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion testsuite/openshift/objects/api_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import base64

from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject
from testsuite.openshift.objects import OpenShiftObject, modify


class APIKey(OpenShiftObject):
Expand Down Expand Up @@ -32,3 +32,8 @@ def create_instance(cls, openshift: OpenShiftClient, name, label, api_key):
}

return cls(model, context=openshift.context)

@modify
def update_api_key(self, api_key):
"""Updates API key Secret with new API key"""
self.model.data["api_key"] = base64.b64encode(api_key.encode("utf-8")).decode('ascii')
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Tests Secret reconciliation for API key identity verification & authentication"""
import pytest

from testsuite.httpx.auth import HeaderApiKeyAuth


@pytest.fixture(scope="function")
def api_key(create_api_key, module_label):
"""Creates API key Secret"""
api_key = "api_key_value"
return create_api_key("api-key", module_label, api_key)


@pytest.fixture(scope="function")
def auth(api_key):
"""Valid API Key Auth"""
return HeaderApiKeyAuth(api_key)


@pytest.fixture(scope="module")
def authorization(authorization, module_label):
"""Creates AuthConfig with API key identity"""
authorization.add_api_key_identity("api_key", match_label=module_label)
return authorization


def test_create_new_api_key(client, create_api_key, module_label):
"""Test reconciliation when API key Secret is freshly created with valid label"""
api_key = create_api_key("api-key", module_label, "new_api_key")
auth = HeaderApiKeyAuth(api_key)
response = client.get("/get", auth=auth)
assert response.status_code == 200


def test_delete_api_key(client, auth, api_key):
"""Test reconciliation when API key Secret is deleted"""
response = client.get("/get", auth=auth)
assert response.status_code == 200

api_key.delete()
response = client.get("/get", auth=auth)
assert response.status_code == 401


def test_update_api_key(client, auth, api_key):
"""Test reconciliation when API key Secret is updated"""
response = client.get("/get", auth=auth)
assert response.status_code == 200

api_key.update_api_key("update_api_key")
response = client.get("/get", auth=auth)
assert response.status_code == 401

auth = HeaderApiKeyAuth(api_key)
response = client.get("/get", auth=auth)
assert response.status_code == 200