Skip to content

Commit

Permalink
Fix auth-queue unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
seeker25 committed May 1, 2024
1 parent b93c142 commit e8a0ec0
Show file tree
Hide file tree
Showing 19 changed files with 169 additions and 281 deletions.
6 changes: 0 additions & 6 deletions queue_services/account-mailer/.envrc

This file was deleted.

5 changes: 5 additions & 0 deletions queue_services/account-mailer/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
CacheControl==0.14.0
Flask==1.1.2
Jinja2==3.0.3
MarkupSafe==2.0.1
Werkzeug==1.0.1
attrs==23.2.0
blinker==1.8.1
certifi==2024.2.2
charset-normalizer==3.3.2
click==8.1.7
expiringdict==1.2.2
idna==3.7
importlib-resources==5.13.0
itsdangerous==2.0.1
jaeger-client==4.8.0
jsonschema==4.17.3
launchdarkly-server-sdk==8.2.1
msgpack==1.0.8
opentracing==2.4.0
pkgutil_resolve_name==1.3.10
protobuf==3.19.6
Expand All @@ -20,6 +24,7 @@ pycountry==23.12.11
pyrsistent==0.20.0
python-dotenv==1.0.1
pytz==2024.1
requests==2.31.0
semver==3.0.2
sentry-sdk==2.0.1
six==1.16.0
Expand Down
3 changes: 2 additions & 1 deletion queue_services/account-mailer/requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Jinja2==3.0.3
markupsafe==2.0.1
protobuf~=3.19.5
launchdarkly-server-sdk==8.2.1

cachecontrol
urllib3==1.26.18
3 changes: 1 addition & 2 deletions queue_services/account-mailer/src/account_mailer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import sentry_sdk
from auth_api import config
from auth_api.models import cache, db
from auth_api.models import db
from auth_api.resources.ops import bp as ops_bp
from auth_api.services.flags import flags
from auth_api.services.gcp_queue import queue
Expand Down Expand Up @@ -56,7 +56,6 @@ def create_app(run_mode=os.getenv('DEPLOYMENT_ENV', 'production')) -> Flask:
)

db.init_app(app)
cache.init_app(app)
flags.init_app(app)
queue.init_app(app)

Expand Down
57 changes: 0 additions & 57 deletions queue_services/account-mailer/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
from auth_api.services.rest_service import RestService
from flask import Flask
from flask_migrate import Migrate, upgrade
from nats.aio.client import Client as Nats
from sqlalchemy import event, text
from stan.aio.client import Client as Stan

from account_mailer.config import get_named_config

Expand Down Expand Up @@ -221,61 +219,6 @@ def keycloak_mock(monkeypatch):
lambda *args, **kwargs: None)


@pytest.fixture(scope='session')
def stan_server(docker_services):
"""Create the nats / stan services that the integration tests will use."""
if os.getenv('TEST_NATS_DOCKER'):
docker_services.start('nats')
time.sleep(2)
# TODO get the wait part working, as opposed to sleeping for 2s
# public_port = docker_services.wait_for_service("nats", 4222)
# dsn = "{docker_services.docker_ip}:{public_port}".format(**locals())
# return dsn


@pytest.fixture(scope='function')
@pytest.mark.asyncio
async def stan(event_loop, client_id):
"""Create a stan connection for each function, to be used in the tests."""
nc = Nats()
sc = Stan()
cluster_name = 'test-cluster'

await nc.connect(io_loop=event_loop, name='entity.filing.tester')

await sc.connect(cluster_name, client_id, nats=nc)

yield sc

await sc.close()
await nc.close()


@pytest.fixture(scope='function')
@pytest.mark.asyncio
async def events_stan(app, event_loop, client_id):
"""Create a stan connection for each function.
Uses environment variables for the cluster name.
"""
nc = Nats()
sc = Stan()

await nc.connect(io_loop=event_loop)

cluster_name = os.getenv('STAN_CLUSTER_NAME')

if not cluster_name:
raise ValueError('Missing env variable: STAN_CLUSTER_NAME-')

await sc.connect(cluster_name, client_id, nats=nc)

yield sc

await sc.close()
await nc.close()


@pytest.fixture(scope='function')
def future(event_loop):
"""Return a future that is used for managing function tests."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import pytest
from auth_api.services.rest_service import RestService
from entity_queue_common.service_utils import subscribe_to_queue
from auth_api.utils.enums import QueueMessageTypes

from account_mailer.enums import MessageType, SubjectType
from account_mailer.enums import SubjectType
from account_mailer.services import notification_service
from account_mailer.services.minio_service import MinioService
from account_mailer.utils import get_local_formatted_date
Expand All @@ -29,34 +29,17 @@
from .utils import helper_add_event_to_queue, helper_add_ref_req_to_queue


@pytest.mark.asyncio
async def test_account_mailer_queue(app, session, stan_server, event_loop, client_id, events_stan, future):
def test_account_mailer_queue(app, session, stan_server, event_loop, client_id, events_stan, future):
"""Assert that events can be retrieved and decoded from the Queue."""
# Call back for the subscription
from account_mailer.worker import cb_subscription_handler

# vars
org_id = '1'

events_subject = 'test_subject'
events_queue = 'test_queue'
events_durable_name = 'test_durable'

# Create a Credit Card Payment

# register the handler to test it
await subscribe_to_queue(events_stan,
events_subject,
events_queue,
events_durable_name,
cb_subscription_handler)

# add an event to queue
mail_details = {
'type': 'payment_completed'
}
await helper_add_event_to_queue(events_stan, events_subject, org_id=org_id, mail_details=mail_details)

assert True


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,49 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities used by the integration tests."""
"""Utilities used by the unit tests."""
import json
import uuid
from datetime import datetime, timezone

import stan


async def helper_add_event_to_queue(stan_client: stan.aio.client.Client,
subject: str,
def build_request_for_queue_push(message_type, payload):
"""Build request for queue message."""
queue_message_bytes = to_queue_message(SimpleCloudEvent(
id=str(uuid.uuid4()),
source='pay-queue',
subject=None,
time=datetime.now(tz=timezone.utc).isoformat(),
type=message_type,
data=payload
))

return {
'message': {
'data': base64.b64encode(queue_message_bytes).decode('utf-8')
},
'subscription': 'foobar'
}


def post_to_queue(client, request_payload):
"""Post request to worker using an http request on our wrapped flask instance."""
response = client.post('/', data=json.dumps(request_payload),
headers={'Content-Type': 'application/json'})
assert response.status_code == 200

def helper_add_event_to_queue(subject: str,
org_id: str = '1', msg_type='account.mailer', mail_details: dict = {}):
"""Add event to the Queue."""
queue_payload = {
'fileName': file_name,
'location': current_app.config['MINIO_BUCKET_NAME']
}
payload = build_request_for_queue_push(message_type, queue_payload)
post_to_queue(client, payload)


payload = {
'specversion': '1.x-wip',
'type': msg_type,
Expand All @@ -30,11 +63,12 @@ async def helper_add_event_to_queue(stan_client: stan.aio.client.Client,
'datacontenttype': 'application/json',
'data': mail_details
}
await stan_client.publish(subject=subject,
payload=json.dumps(payload).encode('utf-8'))
# TODO publish
#await stan_client.publish(subject=subject,
#payload=json.dumps(payload).encode('utf-8'))


async def helper_add_ref_req_to_queue(stan_client: stan.aio.client.Client,
def helper_add_ref_req_to_queue(
subject: str,
invoice_id: str = '1', mail_details: dict = {},
pay_method: str = 'direct_pay'):
Expand All @@ -48,5 +82,4 @@ async def helper_add_ref_req_to_queue(stan_client: stan.aio.client.Client,
'datacontenttype': 'application/json',
'data': mail_details
}
await stan_client.publish(subject=subject,
payload=json.dumps(payload).encode('utf-8'))
# TODO publish
7 changes: 2 additions & 5 deletions queue_services/auth-queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Application Name

BC Registries Business events listener
BC Registries Auth Queue

## Technology Stack Used
* Python, Flask
Expand All @@ -18,9 +18,6 @@ As of 2018-02-22 in **ALPHA**

## Documentation

The names-events-listener is used to create entities within sbc-auth and affiliate them to orgs (accounts).
It's a bridge between namerequest -> sbc-auth via queue, based on events.

## Security

Future - BCGov Keycloak
Expand Down Expand Up @@ -61,7 +58,7 @@ By participating in this project you agree to abide by its terms.

## License

Copyright 2018 Province of British Columbia
Copyright 2024 Province of British Columbia

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions queue_services/auth-queue/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@


import os
from names_event_listener import create_app
from auth_queue import create_app


app = create_app()
if __name__ == '__main__':
server_port = os.environ.get('PORT', '5007')
server_port = os.environ.get('PORT', '5005')
app.run(debug=False, port=server_port, host='0.0.0.0')
2 changes: 2 additions & 0 deletions queue_services/auth-queue/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CacheControl==0.14.0
Flask==1.1.2
Jinja2==3.0.3
MarkupSafe==2.0.1
Expand All @@ -22,6 +23,7 @@ itsdangerous==2.0.1
jaeger-client==4.8.0
jsonschema==4.17.3
launchdarkly-server-sdk==8.2.1
msgpack==1.0.8
opentracing==2.4.0
pkgutil_resolve_name==1.3.10
proto-plus==1.23.0
Expand Down
1 change: 1 addition & 0 deletions queue_services/auth-queue/requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ protobuf~=3.19.5
launchdarkly-server-sdk==8.2.1
google-auth==2.18.1
google-cloud-pubsub==2.17.0
cachecontrol
1 change: 1 addition & 0 deletions queue_services/auth-queue/src/auth_queue/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class TestConfig(_Config): # pylint: disable=too-few-public-methods
'DATABASE_TEST_URL',
default=f'postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{int(DB_PORT)}/{DB_NAME}'
)
PAY_API_URL = os.getenv('PAY_API_URL') + os.getenv('PAY_API_VERSION')


class ProdConfig(_Config): # pylint: disable=too-few-public-methods
Expand Down
Loading

0 comments on commit e8a0ec0

Please sign in to comment.