Skip to content

Commit

Permalink
Updates after merge of #144
Browse files Browse the repository at this point in the history
* Update imports, type hinting, and datetime calls in webhook modules
* Update mypy command in Makefile
* Update dependencies
* Update _test_env fixture and datetime calls in conftest module
* Update test_validate_missing_signature_returns_false with more explicit error check
  • Loading branch information
ehanson8 committed Aug 17, 2023
1 parent 52b7b8e commit 1cce502
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ black:
pipenv run black --check --diff .

mypy:
pipenv run mypy lambdas -v
pipenv run mypy .

ruff:
pipenv run ruff check .
Expand Down
101 changes: 49 additions & 52 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions lambdas/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import json
import logging
import os
from collections.abc import Callable
from datetime import UTC, datetime
from typing import Callable

from typing import Any

import boto3
import sentry_sdk
Expand Down Expand Up @@ -186,8 +186,10 @@ def handle_job_end_webhook(message_body: dict) -> dict:


def get_job_type(job_name: str) -> tuple[str, Callable]:
"""Given an expected job name from the Alma webhook POST request, return the job type
and corresponding function for generating the step function input.
"""Get job name from Alma webhook POST request.
Given an expected job name, return the job type and corresponding function for
generating the step function input.
"""
job_types = [
(
Expand All @@ -213,7 +215,7 @@ def get_job_type(job_name: str) -> tuple[str, Callable]:
for job_type, env_key, step_function_input_handler, log_msg in job_types:
job_name_prefix = os.getenv(env_key)
if not job_name_prefix:
logger.warning("expected env var not present: %s" % env_key)
logger.warning("Expected env var not present: %s", env_key)
if job_name_prefix and job_name.startswith(job_name_prefix):
logger.info(log_msg)
return job_type, step_function_input_handler
Expand All @@ -234,18 +236,18 @@ def count_exported_records(counter: list[dict]) -> int:
)


def generate_ppod_step_function_input(message_body) -> tuple[str, str]:
timestamp = datetime.now().strftime("%Y-%m-%dt%H-%M-%S")
def generate_ppod_step_function_input(message_body: dict[str, Any]) -> tuple[str, str]:
timestamp = datetime.now(tz=UTC).strftime("%Y-%m-%dt%H-%M-%S")
job_date = message_body["job_instance"]["end_time"][:10]
result = {
"filename-prefix": "exlibris/pod/POD_ALMA_EXPORT_" f"{job_date.replace('-', '')}"
"filename-prefix": f"exlibris/pod/POD_ALMA_EXPORT_{job_date.replace('-', '')}"
}
execution_name = f"ppod-upload-{timestamp}"
return json.dumps(result), execution_name


def generate_timdex_step_function_input(message_body) -> tuple[str, str]:
timestamp = datetime.now().strftime("%Y-%m-%dt%H-%M-%S")
def generate_timdex_step_function_input(message_body: dict[str, Any]) -> tuple[str, str]:
timestamp = datetime.now(tz=UTC).strftime("%Y-%m-%dt%H-%M-%S")
job_date = message_body["job_instance"]["end_time"][:10]
run_type = message_body["job_instance"]["name"].split()[-1].lower()
result = {
Expand All @@ -259,7 +261,7 @@ def generate_timdex_step_function_input(message_body) -> tuple[str, str]:
return json.dumps(result), execution_name


def generate_bursar_step_function_input(message_body) -> tuple[str, str]:
def generate_bursar_step_function_input(message_body: dict[str, Any]) -> tuple[str, str]:
result = {
"job_id": message_body["job_instance"]["id"],
"job_name": message_body["job_instance"]["name"],
Expand Down
54 changes: 27 additions & 27 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import os
import urllib
from datetime import UTC, datetime

import botocore.session
import pytest
Expand All @@ -12,27 +12,27 @@

@pytest.fixture(autouse=True)
def _test_env():
os.environ = {
"AWS_ACCESS_KEY_ID": "testing",
"AWS_DEFAULT_REGION": "us-east-1",
"AWS_SECRET_ACCESS_KEY": "testing",
"AWS_SECURITY_TOKEN": "testing",
"AWS_SESSION_TOKEN": "testing",
"ALMA_CHALLENGE_SECRET": "itsasecret",
"ALMA_POD_EXPORT_JOB_NAME": "PPOD Export",
"ALMA_TIMDEX_EXPORT_JOB_NAME_PREFIX": "TIMDEX Export",
"ALMA_BURSAR_EXPORT_JOB_NAME_PREFIX": "Export to bursar",
"LAMBDA_FUNCTION_URL": "http://example.com/lambda",
"PPOD_STATE_MACHINE_ARN": "arn:aws:states:us-east-1:account:stateMachine:"
"ppod-test",
"TIMDEX_STATE_MACHINE_ARN": "arn:aws:states:us-east-1:account:stateMachine:"
"timdex-test",
"BURSAR_STATE_MACHINE_ARN": "arn:aws:states:us-east-1:account:stateMachine:"
"bursar-test",
"VALID_POD_EXPORT_DATE": "2022-05-23",
"WORKSPACE": "test",
}
return
os.environ["AWS_ACCESS_KEY_ID"] = "testing"
os.environ["AWS_DEFAULT_REGION"] = "us-east-1"
os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
os.environ["AWS_SECURITY_TOKEN"] = "testing"
os.environ["AWS_SESSION_TOKEN"] = "testing"
os.environ["ALMA_CHALLENGE_SECRET"] = "itsasecret"
os.environ["ALMA_POD_EXPORT_JOB_NAME"] = "PPOD Export"
os.environ["ALMA_TIMDEX_EXPORT_JOB_NAME_PREFIX"] = "TIMDEX Export"
os.environ["ALMA_BURSAR_EXPORT_JOB_NAME_PREFIX"] = "Export to bursar"
os.environ["LAMBDA_FUNCTION_URL"] = "http://example.com/lambda"
os.environ[
"PPOD_STATE_MACHINE_ARN"
] = "arn:aws:states:us-east-1:account:stateMachine:ppod-test"
os.environ[
"TIMDEX_STATE_MACHINE_ARN"
] = "arn:aws:states:us-east-1:account:stateMachine:timdex-test"
os.environ[
"BURSAR_STATE_MACHINE_ARN"
] = "arn:aws:states:us-east-1:account:stateMachine:bursar-test"
os.environ["VALID_POD_EXPORT_DATE"] = "2022-05-23"
os.environ["WORKSPACE"] = "test"


@pytest.fixture
Expand Down Expand Up @@ -61,7 +61,7 @@ def post_request_valid_signature():
}


@pytest.fixture()
@pytest.fixture
def mocked_valid_signature(mocker):
return mocker.patch("lambdas.webhook.valid_signature", return_value=True)

Expand Down Expand Up @@ -102,7 +102,7 @@ def stubbed_ppod_sfn_client():
)
expected_response = {
"executionArn": "arn:aws:states:us-east-1:account:execution:ppod-test:12345",
"startDate": datetime(2022, 5, 1, tzinfo=UTC),
"startDate": datetime.datetime(2022, 5, 1, tzinfo=datetime.UTC),
}
expected_params = {
"stateMachineArn": "arn:aws:states:us-east-1:account:stateMachine:ppod-test",
Expand All @@ -121,7 +121,7 @@ def stubbed_timdex_sfn_client():
)
expected_response = {
"executionArn": "arn:aws:states:us-east-1:account:execution:timdex-test:12345",
"startDate": datetime(2022, 5, 1, tzinfo=UTC),
"startDate": datetime.datetime(2022, 5, 1, tzinfo=datetime.UTC),
}
expected_params = {
"stateMachineArn": "arn:aws:states:us-east-1:account:stateMachine:timdex-test",
Expand All @@ -134,14 +134,14 @@ def stubbed_timdex_sfn_client():
yield sfn


@pytest.fixture()
@pytest.fixture
def stubbed_bursar_sfn_client():
sfn = botocore.session.get_session().create_client(
"stepfunctions", region_name="us-east-1"
)
expected_response = {
"executionArn": "arn:aws:states:us-east-1:account:execution:-test:bursar12345",
"startDate": datetime.datetime(2022, 5, 1),
"startDate": datetime.datetime(2022, 5, 1, tzinfo=datetime.UTC),
}
expected_params = {
"stateMachineArn": "arn:aws:states:us-east-1:account:stateMachine:bursar-test",
Expand Down
Loading

0 comments on commit 1cce502

Please sign in to comment.