Skip to content

Commit

Permalink
Fix unit tests for account mailer.
Browse files Browse the repository at this point in the history
  • Loading branch information
seeker25 committed May 1, 2024
1 parent e8a0ec0 commit 3dc6cd1
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 432 deletions.
23 changes: 0 additions & 23 deletions auth-api/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
from auth_api.exceptions import BusinessException, Error
from auth_api.models import db as _db


def mock_token(config_id='', config_secret=''):
"""Mock token generator."""
return 'TOKEN....'


@pytest.fixture(scope='session')
def app():
"""Return a session-wide application configured in TEST mode."""
Expand Down Expand Up @@ -134,23 +128,6 @@ def restart_savepoint(sess2, trans): # pylint: disable=unused-variable
conn.close()


@pytest.fixture(scope='function')
def client_id():
"""Return a unique client_id that can be used in tests."""
_id = random.SystemRandom().getrandbits(0x58)
# _id = (base64.urlsafe_b64encode(uuid.uuid4().bytes)).replace('=', '')

return f'client-{_id}'


@pytest.fixture(scope='session')
def stan_server(docker_services):
"""Create the nats / stan services that the integration tests will use."""
if os.getenv('USE_DOCKER_MOCK'):
docker_services.start('nats')
time.sleep(2)


@pytest.fixture(scope='session', autouse=True)
def auto(docker_services, app):
"""Spin up a keycloak instance and initialize jwt."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ def worker():
handle_pad_account_create(message_type, email_msg)
handle_eft_available_notification(message_type, email_msg)
handle_nsf_lock_unlock_account(message_type, email_msg)
handle_account_confirmation_period_over(message_type, email_msg)
handle_team_actions(message_type, email_msg)
handle_pad_invoice_created(message_type, email_msg)
handle_online_banking(message_type, email_msg)
handle_pad_setup_failed(message_type, email_msg)
handle_payment_pending(message_type, email_msg)
handle_ejv_failed(message_type, email_msg)
handle_reset_passcode(message_type, email_msg)
handle_affiliation_invitation(message_type, email_msg)
Expand Down
41 changes: 14 additions & 27 deletions queue_services/account-mailer/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright © 2019 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 All @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Common setup and fixtures for the pytest suite used by this service."""
import asyncio
import logging
import os
import random
Expand All @@ -22,11 +21,10 @@
import pytest
from auth_api import db as _db
from auth_api.services.rest_service import RestService
from flask import Flask
from flask_migrate import Migrate, upgrade
from sqlalchemy import event, text

from account_mailer.config import get_named_config
from account_mailer import create_app


def setup_logging(conf):
Expand All @@ -53,12 +51,9 @@ def not_raises(exception):
@pytest.fixture(scope='session')
def app():
"""Return a session-wide application configured in TEST mode."""
# _app = create_app('testing')
_app = Flask(__name__)
_app.config.from_object(get_named_config('testing'))
_db.init_app(_app)
# Bypass caching.
_app = create_app('testing')

# Bypass caching.
def get_service_token():
pass
RestService.get_service_account_token = get_service_token
Expand Down Expand Up @@ -219,25 +214,17 @@ def keycloak_mock(monkeypatch):
lambda *args, **kwargs: None)


@pytest.fixture(scope='function')
def future(event_loop):
"""Return a future that is used for managing function tests."""
_future = asyncio.Future(loop=event_loop)
return _future


@pytest.fixture
def create_mock_coro(mocker, monkeypatch):
"""Return a mocked coroutine, and optionally patch-it in."""
@pytest.fixture(autouse=True)
def mock_queue_auth(mocker):
"""Mock queue authorization."""
mocker.patch('auth_api.services.gcp_queue.gcp_auth.verify_jwt', return_value='')

def _create_mock_patch_coro(to_patch=None):
mock = mocker.Mock()

async def _coro(*args, **kwargs):
return mock(*args, **kwargs)
@pytest.fixture(autouse=True)
def mock_pub_sub_call(monkeypatch):
"""Mock pub sub call."""

if to_patch: # <-- may not need/want to patch anything
monkeypatch.setattr(to_patch, _coro)
return mock, _coro
def publish(topic, message):
return True

return _create_mock_patch_coro
monkeypatch.setattr('auth_api.services.gcp_queue.queue.publish', publish)
Loading

0 comments on commit 3dc6cd1

Please sign in to comment.