"""Conftest for integration tests.""" import json from os import environ from time import sleep from jwtauth.testing import SecretLookupInfo import pytest import requests from tests.integration.consts import api from tests.integration import config from tests.integration import constants pytest_plugins = ["jwtauth.testing.pytest_plugin"] @pytest.fixture def get_test_rejection(): """Get test rejection.""" return { "note": "bad bad bad", } @pytest.fixture def fixture_product_id(): """Get the fixture product ID.""" return 3577604 @pytest.fixture def fixture_review_queue_id(): """Get the fixture product ID.""" return 21611 @pytest.fixture def get_headers(): """Get headers.""" return { "Orchard-Identity-Id": "13d94141-77e2-44c3-aa38-eabc83c4775c", "Orchard-Profile-Type": "ContentProfile", "Orchard-Profile-Id": "25995588", "Content-Type": "application/json", } @pytest.fixture def get_authorized_headers(authorized_user_jwt): """Get authorized headers.""" return { "Authorization": f"Bearer {authorized_user_jwt}", "Orchard-Identity-Id": "13d94141-77e2-44c3-aa38-eabc83c4775c", "Orchard-Profile-Type": "ContentProfile", "Orchard-Profile-Id": "25995588", "Content-Type": "application/json" } def wait_for_test_product_to_complete(product_id): """Wait for the test product updates to finish applying.""" counter = 0 max_count = int(environ.get("INTEGRATION_TEST_MAX_ATTEMPT", 10)) wait_interval = int(environ.get("INTEGRATION_TEST_WAIT_INTERVAL", 3)) while counter < max_count: response = requests.get(api.STATUS.format(product_id)) if response.status_code != 200: raise Exception("Failed getting status for product", response.text) if response.json().get("release_approval_status") != "checked_out": return counter += 1 sleep(wait_interval) raise Exception("Timed out waiting for product to finish applying") @pytest.fixture def reset_test_review_fixture(fixture_product_id): """Reset test review fixture.""" wait_for_test_product_to_complete(fixture_product_id) response = requests.put( api.RESET_TESTS.format(fixture_product_id), {}, headers={"Orchard-Identity-Id": "ows-product-review-integration-tests"}, ) assert ( response.status_code == 200 ), f"reset tests failed with {response.status_code}" return response.json()["review_queue_id"] @pytest.fixture def test_rejection_fixture( fixture_product_id, reset_test_review_fixture, get_test_rejection, get_authorized_headers ): """Test rejection fixture.""" requests.post( api.REJECT.format(reset_test_review_fixture), data=json.dumps(get_test_rejection), headers=get_authorized_headers, ) wait_for_test_product_to_complete(fixture_product_id) return reset_test_review_fixture @pytest.fixture def test_approval_fixture(fixture_product_id, reset_test_review_fixture, get_authorized_headers): """Test approval fixture.""" requests.post( api.APPROVE.format(reset_test_review_fixture), headers=get_authorized_headers, ) wait_for_test_product_to_complete(fixture_product_id) return reset_test_review_fixture @pytest.fixture(scope="session") def authorized_user_jwt( generate_bearer_token, jwtauth_secrets_manager ) -> str: """Return a JWT for the ows-product-review integration test user.""" return generate_bearer_token( get_user_creds_args=SecretLookupInfo( environment='qa', service_name=config.SERVICE_NAME, secret_name=constants.OWS_PRODUCT_REVIEW_TEST_USER_CREDENTIALS, ), get_auth0_creds_args=SecretLookupInfo( environment='qa', service_name=config.SERVICE_NAME, secret_name=constants.OWS_PRODUCT_REVIEW_TEST_AUTH0_CREDENTIALS, ), secrets_manager=jwtauth_secrets_manager, )