mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-01 22:51:10 -04:00
This naming was introduced in 2016 without explanation [1]. I find it confusing because: - It's reminiscent of "_app", which is a Python convention indicating the variable is internal, so maybe avoid using it. - It suggests there's some other "app" fixture I should be using (there isn't, though). The Python style guide describes using an underscore suffix to avoid clashes with inbuilt names [1], which is sort of applicable if we need to import the "app" module [2]. However, we can also avoid clashes by choosing a different name, without the strange underscore. [1]:3b1d521c10[2]:78824f54fd/tests/app/main/views/test_forgot_password.py (L5)
215 lines
6.0 KiB
Python
215 lines
6.0 KiB
Python
from unittest.mock import ANY, call
|
|
|
|
import pytest
|
|
|
|
from app import organisations_client
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
(
|
|
'client_method,'
|
|
'expected_cache_get_calls,'
|
|
'cache_value,'
|
|
'expected_api_calls,'
|
|
'expected_cache_set_calls,'
|
|
'expected_return_value,'
|
|
),
|
|
[
|
|
(
|
|
'get_domains',
|
|
[
|
|
call('domains'),
|
|
],
|
|
b"""
|
|
[
|
|
{"name": "org 1", "domains": ["a", "b", "c"]},
|
|
{"name": "org 2", "domains": ["c", "d", "e"]}
|
|
]
|
|
""",
|
|
[],
|
|
[],
|
|
['a', 'b', 'c', 'd', 'e'],
|
|
),
|
|
(
|
|
'get_domains',
|
|
[
|
|
call('domains'),
|
|
call('organisations'),
|
|
],
|
|
None,
|
|
[
|
|
call(url='/organisations')
|
|
],
|
|
[
|
|
call(
|
|
'organisations',
|
|
'[{"domains": ["x", "y", "z"]}]',
|
|
ex=604800,
|
|
),
|
|
call(
|
|
'domains',
|
|
'["x", "y", "z"]',
|
|
ex=604800
|
|
),
|
|
],
|
|
'from api',
|
|
),
|
|
(
|
|
'get_organisations',
|
|
[
|
|
call('organisations'),
|
|
],
|
|
b"""
|
|
[
|
|
{"name": "org 1", "domains": ["a", "b", "c"]},
|
|
{"name": "org 2", "domains": ["c", "d", "e"]}
|
|
]
|
|
""",
|
|
[],
|
|
[],
|
|
[
|
|
{"name": "org 1", "domains": ["a", "b", "c"]},
|
|
{"name": "org 2", "domains": ["c", "d", "e"]}
|
|
],
|
|
),
|
|
(
|
|
'get_organisations',
|
|
[
|
|
call('organisations'),
|
|
],
|
|
None,
|
|
[
|
|
call(url='/organisations')
|
|
],
|
|
[
|
|
call(
|
|
'organisations',
|
|
'[{"domains": ["x", "y", "z"]}]',
|
|
ex=604800,
|
|
),
|
|
],
|
|
'from api',
|
|
),
|
|
]
|
|
)
|
|
def test_returns_value_from_cache(
|
|
notify_admin,
|
|
mocker,
|
|
client_method,
|
|
expected_cache_get_calls,
|
|
cache_value,
|
|
expected_return_value,
|
|
expected_api_calls,
|
|
expected_cache_set_calls,
|
|
):
|
|
|
|
mock_redis_get = mocker.patch(
|
|
'app.extensions.RedisClient.get',
|
|
return_value=cache_value,
|
|
)
|
|
mock_api_get = mocker.patch(
|
|
'app.notify_client.NotifyAdminAPIClient.get',
|
|
return_value=[
|
|
{'domains': ['x', 'y', 'z']}
|
|
],
|
|
)
|
|
mock_redis_set = mocker.patch(
|
|
'app.extensions.RedisClient.set',
|
|
)
|
|
|
|
getattr(organisations_client, client_method)()
|
|
|
|
assert mock_redis_get.call_args_list == expected_cache_get_calls
|
|
assert mock_api_get.call_args_list == expected_api_calls
|
|
assert mock_redis_set.call_args_list == expected_cache_set_calls
|
|
|
|
|
|
def test_deletes_domain_cache(
|
|
notify_admin,
|
|
mock_get_user,
|
|
mocker,
|
|
fake_uuid,
|
|
):
|
|
mocker.patch('app.notify_client.current_user', id='1')
|
|
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
|
mock_request = mocker.patch('notifications_python_client.base.BaseAPIClient.request')
|
|
|
|
organisations_client.update_organisation(fake_uuid, foo='bar')
|
|
|
|
assert call('domains') in mock_redis_delete.call_args_list
|
|
assert len(mock_request.call_args_list) == 1
|
|
|
|
|
|
def test_update_organisation_when_not_updating_org_type(mocker, fake_uuid):
|
|
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
|
mock_post = mocker.patch('app.notify_client.organisations_api_client.OrganisationsClient.post')
|
|
|
|
organisations_client.update_organisation(fake_uuid, foo='bar')
|
|
|
|
mock_post.assert_called_with(
|
|
url='/organisations/{}'.format(fake_uuid),
|
|
data={'foo': 'bar'}
|
|
)
|
|
assert mock_redis_delete.call_args_list == [call('organisations'), call('domains')]
|
|
|
|
|
|
def test_update_organisation_when_updating_org_type_and_org_has_services(mocker, fake_uuid):
|
|
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
|
mock_post = mocker.patch('app.notify_client.organisations_api_client.OrganisationsClient.post')
|
|
|
|
organisations_client.update_organisation(
|
|
fake_uuid,
|
|
cached_service_ids=['a', 'b', 'c'],
|
|
organisation_type='central',
|
|
)
|
|
|
|
mock_post.assert_called_with(
|
|
url='/organisations/{}'.format(fake_uuid),
|
|
data={'organisation_type': 'central'}
|
|
)
|
|
assert mock_redis_delete.call_args_list == [
|
|
call('service-a', 'service-b', 'service-c'),
|
|
call('organisations'),
|
|
call('domains'),
|
|
]
|
|
|
|
|
|
def test_update_organisation_when_updating_org_type_but_org_has_no_services(mocker, fake_uuid):
|
|
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
|
mock_post = mocker.patch('app.notify_client.organisations_api_client.OrganisationsClient.post')
|
|
|
|
organisations_client.update_organisation(
|
|
fake_uuid,
|
|
cached_service_ids=[],
|
|
organisation_type='central',
|
|
)
|
|
|
|
mock_post.assert_called_with(
|
|
url='/organisations/{}'.format(fake_uuid),
|
|
data={'organisation_type': 'central'}
|
|
)
|
|
assert mock_redis_delete.call_args_list == [
|
|
call('organisations'),
|
|
call('domains'),
|
|
]
|
|
|
|
|
|
def test_update_service_organisation_deletes_cache(mocker, fake_uuid):
|
|
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
|
mock_post = mocker.patch('app.notify_client.organisations_api_client.OrganisationsClient.post')
|
|
|
|
organisations_client.update_service_organisation(
|
|
service_id=fake_uuid,
|
|
org_id=fake_uuid
|
|
)
|
|
|
|
assert sorted(mock_redis_delete.call_args_list) == [
|
|
call('live-service-and-organisation-counts'),
|
|
call('organisations'),
|
|
call('service-{}'.format(fake_uuid)),
|
|
]
|
|
mock_post.assert_called_with(
|
|
url='/organisations/{}/service'.format(fake_uuid),
|
|
data=ANY
|
|
)
|