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

IN-318 caching tests on routes #12

Merged
merged 6 commits into from
Aug 13, 2020
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: 5 additions & 2 deletions integration_tests/integration_tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ To set up for the integration tests you should check a few things first:

### Gotchas

Make sure you are able to assume digideps dev role with your credentials or tests will not work.
You may need to update some Sirius data. This is temporary, we're going to get them to create some stable
* Make sure you are able to assume digideps dev role with your credentials or tests will not work.

* You may need to update some Sirius data. This is temporary, we're going to get them to create some stable
test data for us, but for now, you need to log into Cloud9 and run:
`UPDATE cases SET onlinelpaid = 'A33718377316' WHERE uid = 700000000013;`

* If that doesn't work, check there are some cases to update using `SELECT uid as case_number, onlinelpaid, casetype, casesubtype, donor_id FROM cases WHERE casetype = 'LPA';`
17 changes: 11 additions & 6 deletions integration_tests/v1/test_healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
@pytest.mark.parametrize("test_config", configs_to_test)
def test_healthcheck_route(test_config):

status, response = send_a_request(
test_config=test_config,
url=f"{test_config['healthcheck_endpoint']['url']}",
method=test_config["healthcheck_endpoint"]["method"],
)
if test_config["name"] == "original collections api on aws dev":
print("Healthcheck does not exist on original collections api on aws dev")
pass
else:

assert status == 200
status, response = send_a_request(
test_config=test_config,
url=f"{test_config['healthcheck_endpoint']['url']}",
method=test_config["healthcheck_endpoint"]["method"],
)

assert status == 200
4 changes: 2 additions & 2 deletions integration_tests/v1/test_use_an_lpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def test_use_an_lpa_route(test_config):
assert status == 200
response_dict = json.loads(response)
# TODO we are getting a list from sirius?
assert is_valid_schema(response_dict[0], "use_an_lpa_schema.json")
assert response_dict[0]["uId"].replace("-", "") == valid_id
assert is_valid_schema(response_dict, "use_an_lpa_schema.json")
assert response_dict["uId"].replace("-", "") == valid_id


@pytest.mark.smoke_test
Expand Down
5 changes: 3 additions & 2 deletions lambda_functions/v1/functions/lpa/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


from .api.resources import api as api_blueprint
from .api.sirius_service import SiriusService
from .sirius_service.sirius_handler import SiriusService
from .config import Config


Expand All @@ -15,8 +15,9 @@ def create_app(flask=Flask, config=Config):
app.register_blueprint(api_blueprint)

if config.REQUEST_CACHING == "enabled":

app.redis = redis.StrictRedis.from_url(
url=config.REDIS_URL, charset="utf-8", decode_responses=True
url=f"redis://{config.REDIS_URL}", charset="utf-8", decode_responses=True
)

redis_cache = app.redis
Expand Down
18 changes: 8 additions & 10 deletions lambda_functions/v1/functions/lpa/app/api/handlers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import json
import os

import urllib3

# from flask import current_app
from flask import current_app
from werkzeug.exceptions import abort
Expand All @@ -11,7 +6,6 @@

# from .sirius import build_sirius_url, send_request_to_sirius
# from . import sirius
from .sirius_service import SiriusService

logger = custom_logger()

Expand All @@ -27,7 +21,7 @@ def get_by_online_tool_id(lpa_online_tool_id):
if sirius_status_code in [200]:
if len(sirius_response) > 0:
try:
return format_response(sirius_response=sirius_response), 200
return format_online_tool_response(sirius_response=sirius_response), 200
except Exception as e:
logger.error(f"Error formatting sirius response: {e}")
abort(404)
Expand All @@ -43,12 +37,12 @@ def get_by_sirius_uid(sirius_uid):
sirius_url = generate_sirius_url(sirius_uid=sirius_uid)

sirius_status_code, sirius_response = current_app.sirius.send_request_to_sirius(
key=sirius_url, url=sirius_url, method="GET"
key=sirius_uid, url=sirius_url, method="GET"
)
if sirius_status_code in [200]:
if len(sirius_response) > 0:
try:
return sirius_response, 200
return format_uid_response(sirius_response=sirius_response), 200
except Exception as e:
logger.error(f"Error formatting sirius response: {e}")
abort(404)
Expand Down Expand Up @@ -77,7 +71,11 @@ def generate_sirius_url(lpa_online_tool_id=None, sirius_uid=None):
return url


def format_response(sirius_response):
def format_uid_response(sirius_response):
return sirius_response[0]


def format_online_tool_response(sirius_response):

logger.info(f"type(sirius_response): {type(sirius_response)}")
lpa_data = sirius_response[0]
Expand Down
7 changes: 0 additions & 7 deletions lambda_functions/v1/functions/lpa/app/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ def handle504(error=None):
def handle_healthcheck_route():
response_message = "OK"

from flask import current_app

logger.info(f"current_app: {current_app}")
logger.info(f"current_app.config: {current_app.config}")

return jsonify(response_message), 200


Expand All @@ -75,8 +70,6 @@ def handle_lpa_online_tool(lpa_online_tool_id):
logger.info(f"lpa_online_tool_id: {lpa_online_tool_id}")

response, status = get_by_online_tool_id(lpa_online_tool_id=lpa_online_tool_id)
print(f"response: {response}")
print(f"status: {status}")

return jsonify(response), status

Expand Down
4 changes: 0 additions & 4 deletions lambda_functions/v1/functions/lpa/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ class Config(object):
REQUEST_CACHING = os.environ.get("REQUEST_CACHING", default="disabled")


class ProductionConfig(Config):
pass


class LocalMockConfig(Config):
# override prod values
DEBUG = True
Expand Down
4 changes: 2 additions & 2 deletions lambda_functions/v1/functions/lpa/app/lpa.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .flask_lambda import FlaskLambda
from . import create_app
from .config import ProductionConfig
from .config import Config

lambda_handler = create_app(flask=FlaskLambda, config=ProductionConfig)
lambda_handler = create_app(flask=FlaskLambda, config=Config)
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import requests
from botocore.exceptions import ClientError

from .helpers import custom_logger
from ..api.helpers import custom_logger

logger = custom_logger("sirius_service")

Expand Down Expand Up @@ -159,8 +159,6 @@ def send_request_to_sirius(self, key, url, method, content_type=None, data=None)

cache_enabled = True if self.request_caching == "enabled" else False

redis_service = self.cache

if self._check_sirius_available():
sirius_status_code, sirius_data = self._get_data_from_sirius(
url, method, content_type, data
Expand All @@ -170,16 +168,14 @@ def send_request_to_sirius(self, key, url, method, content_type=None, data=None)
logger.info(f"method: {method}")
if cache_enabled and method == "GET" and sirius_status_code == 200:
logger.info(f"Putting data in cache with key: {key}")
self._put_sirius_data_in_cache(
redis_conn=redis_service, key=key, data=sirius_data
)
self._put_sirius_data_in_cache(key=key, data=sirius_data)

return sirius_status_code, sirius_data
else:
if cache_enabled and method == "GET":
logger.info(f"Getting data from cache with key: {key}")
sirius_status_code, sirius_data = self._get_sirius_data_from_cache(
redis_conn=redis_service, key=key
key=key
)

return sirius_status_code, sirius_data
Expand Down Expand Up @@ -216,30 +212,27 @@ def _get_data_from_sirius(self, url, method, content_type=None, data=None):
error_message=f"Unable to send request to Sirius", error_details=e
)

def _put_sirius_data_in_cache(self, redis_conn, key, data):
def _put_sirius_data_in_cache(self, key, data):
logger.info(f"_put_sirius_data_in_cache")
cache_name = self.request_caching_name

cache_ttl_in_seconds = self.request_caching_ttl * 60 * 60

data = json.dumps(data)

redis_conn.set(name=f"{cache_name}-{key}", value=data, ex=cache_ttl_in_seconds)
self.cache.set(name=f"{cache_name}-{key}", value=data, ex=cache_ttl_in_seconds)

logger.info(f"setting redis: {cache_name}-{key}")

def _get_sirius_data_from_cache(self, redis_conn, key):
def _get_sirius_data_from_cache(self, key):

cache_name = self.request_caching_name

logger.info(f"getting redis: {cache_name}-{key}")
logger.info(
f'redis_conn.exists(f"{cache_name}-{key}"): {redis_conn.exists(f"{cache_name}-{key}")}'
)

if redis_conn.exists(f"{cache_name}-{key}"):
if self.cache.exists(f"{cache_name}-{key}"):
status_code = 200
result = redis_conn.get(f"{cache_name}-{key}")
result = self.cache.get(f"{cache_name}-{key}")
result = json.loads(result)
else:
status_code = 500
Expand Down
5 changes: 0 additions & 5 deletions lambda_functions/v1/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,3 @@ def aws_credentials(monkeypatch):
monkeypatch.setenv("AWS_SESSION_TOKEN", "testing")
monkeypatch.setenv("AWS_DEFAULT_REGION", "eu-west-1")
monkeypatch.setenv("AWS_XRAY_CONTEXT_MISSING", "LOG_ERROR")
monkeypatch.setenv("SIRIUS_BASE_URL", "http://not-really-sirius.com")
monkeypatch.setenv("SIRIUS_API_VERSION", "v1")
monkeypatch.setenv("SESSION_DATA", "publicapi@opgtest.com")
monkeypatch.setenv("ENVIRONMENT", "not-a-real-environment")
monkeypatch.setenv("JWT_SECRET", "THIS_IS_MY_SECRET_KEY")
59 changes: 54 additions & 5 deletions lambda_functions/v1/tests/routes/conftest.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
from textwrap import wrap

import fakeredis
import pytest
from flask import Flask

from lambda_functions.v1.functions.lpa.app import api
from lambda_functions.v1.functions.lpa.app import create_app
from lambda_functions.v1.functions.lpa.app.config import LocalTestingConfig

from lambda_functions.v1.functions.lpa.app.sirius_service import sirius_handler
from lambda_functions.v1.tests.helpers import load_data

mock_redis_server = fakeredis.FakeServer()


class NoCache(LocalTestingConfig):
REQUEST_CACHING = "disabled"


@pytest.fixture(scope="session")
def app(*args, **kwargs):
app = create_app(config=LocalTestingConfig)

app = create_app(Flask, config=LocalTestingConfig)

routes = [str(p) for p in app.url_map.iter_rules()]
print(f"routes: {routes}")

app.sirius.cache = fakeredis.FakeStrictRedis(
charset="utf-8", decode_responses=True, server=mock_redis_server
)

yield app


Expand All @@ -25,21 +38,45 @@ def test_server(app):
return app.test_client()


@pytest.fixture(scope="session")
def app_no_cache(*args, **kwargs):
app = create_app(Flask, config=NoCache)

routes = [str(p) for p in app.url_map.iter_rules()]
print(f"routes: {routes}")

app.sirius.cache = fakeredis.FakeStrictRedis(
charset="utf-8", decode_responses=True, server=mock_redis_server
)

yield app


@pytest.fixture(scope="function")
def test_server_no_cache(app_no_cache):
return app_no_cache.test_client()


@pytest.fixture(autouse=True)
def patched_get_secret(monkeypatch):
def mock_secret(*args, **kwargs):
print("patched_get_secret returning mock_secret")
return "this_is_a_secret_string"

monkeypatch.setattr(api.sirius_service.SiriusService, "_get_secret", mock_secret)
monkeypatch.setattr(sirius_handler.SiriusService, "_get_secret", mock_secret)


@pytest.fixture()
def patched_send_request_to_sirius(monkeypatch):
def mock_get(*args, **kwargs):
print("Using fake_send_request_to_sirius ")
print(f"args: {args}")
print(f"kwargs: {kwargs}")

url = args[1]

test_id = kwargs["url"].split("=")[1]
# test_id = kwargs["url"].split("=")[1]
test_id = url.split("=")[1]
print(f"test_id: {test_id}")

if test_id[0] == "7":
Expand Down Expand Up @@ -75,6 +112,18 @@ def mock_get(*args, **kwargs):
print("test_id is not a valid sirius_uid or lpa-online-tool id")
return 404, ""

monkeypatch.setattr(sirius_handler.SiriusService, "_get_data_from_sirius", mock_get)


@pytest.fixture
def patched_send_request_to_sirius_available(monkeypatch):
monkeypatch.setattr(
sirius_handler.SiriusService, "_check_sirius_available", lambda x: True
)


@pytest.fixture
def patched_send_request_to_sirius_unavailable(monkeypatch):
monkeypatch.setattr(
api.sirius_service.SiriusService, "send_request_to_sirius", mock_get
sirius_handler.SiriusService, "_check_sirius_available", lambda x: False
)
30 changes: 29 additions & 1 deletion lambda_functions/v1/tests/routes/test_healthcheck.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
def test_healthcheck_route(test_server):
from lambda_functions.v1.functions.lpa.app.sirius_service import sirius_handler
import pytest


@pytest.mark.parametrize("sirius_available", [(True), (False)])
def test_healthcheck_route_with_cache(test_server, monkeypatch, sirius_available):

monkeypatch.setattr(
sirius_handler.SiriusService,
"_check_sirius_available",
lambda x: sirius_available,
)

response = test_server.get(f"/v1/healthcheck")

assert response.status_code == 200
assert response.get_json() == "OK"


@pytest.mark.parametrize("sirius_available", [(True), (False)])
def test_healthcheck_route_no_cache(
test_server_no_cache, monkeypatch, sirius_available
):

monkeypatch.setattr(
sirius_handler.SiriusService,
"_check_sirius_available",
lambda x: sirius_available,
)

response = test_server_no_cache.get(f"/v1/healthcheck")

assert response.status_code == 200
assert response.get_json() == "OK"
Loading