mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-11 09:27:56 -04:00
Flake8 Bugbear checks for some extra things that aren’t code style errors, but are likely to introduce bugs or unexpected behaviour. A good example is having mutable default function arguments, which get shared between every call to the function and therefore mutating a value in one place can unexpectedly cause it to change in another. This commit enables all the extra warnings provided by Flake8 Bugbear, except for: - the line length one (because we already lint for that separately) - B903 Data class should either be immutable or use `__slots__` because this seems to false-positive on some of our custom exceptions - B902 Invalid first argument 'cls' used for instance method because some SQLAlchemy decorators (eg `declared_attr`) make things that aren’t formally class methods take a class not an instance as their first argument It disables: - _B306: BaseException.message is removed in Python 3_ because I think our exceptions have a custom structure that means the `.message` attribute is still present Matches the work done in other repos: - https://github.com/alphagov/notifications-admin/pull/3172/files
120 lines
4.2 KiB
Python
120 lines
4.2 KiB
Python
import pytest
|
|
|
|
from flask import json
|
|
from itertools import product
|
|
|
|
from app.models import TEMPLATE_TYPES, EMAIL_TYPE
|
|
from tests import create_authorization_header
|
|
from tests.app.db import create_template
|
|
|
|
|
|
def test_get_all_templates_returns_200(client, sample_service):
|
|
templates = [
|
|
create_template(
|
|
sample_service,
|
|
template_type=tmp_type,
|
|
subject='subject_{}'.format(name) if tmp_type == EMAIL_TYPE else '',
|
|
template_name=name,
|
|
)
|
|
for name, tmp_type in product(('A', 'B', 'C'), TEMPLATE_TYPES)
|
|
]
|
|
|
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
|
|
|
response = client.get(path='/v2/templates',
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
assert response.status_code == 200
|
|
assert response.headers['Content-type'] == 'application/json'
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
assert len(json_response['templates']) == len(templates)
|
|
|
|
for index, template in enumerate(json_response['templates']):
|
|
assert template['id'] == str(templates[index].id)
|
|
assert template['body'] == templates[index].content
|
|
assert template['type'] == templates[index].template_type
|
|
if templates[index].template_type == EMAIL_TYPE:
|
|
assert template['subject'] == templates[index].subject
|
|
|
|
|
|
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
|
def test_get_all_templates_for_valid_type_returns_200(client, sample_service, tmp_type):
|
|
templates = [
|
|
create_template(
|
|
sample_service,
|
|
template_type=tmp_type,
|
|
template_name='Template {}'.format(i),
|
|
subject='subject_{}'.format(i) if tmp_type == EMAIL_TYPE else ''
|
|
)
|
|
for i in range(3)
|
|
]
|
|
|
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
|
|
|
response = client.get(path='/v2/templates?type={}'.format(tmp_type),
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
assert response.status_code == 200
|
|
assert response.headers['Content-type'] == 'application/json'
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
assert len(json_response['templates']) == len(templates)
|
|
|
|
for index, template in enumerate(json_response['templates']):
|
|
assert template['id'] == str(templates[index].id)
|
|
assert template['body'] == templates[index].content
|
|
assert template['type'] == tmp_type
|
|
if templates[index].template_type == EMAIL_TYPE:
|
|
assert template['subject'] == templates[index].subject
|
|
|
|
|
|
@pytest.mark.parametrize("tmp_type", TEMPLATE_TYPES)
|
|
def test_get_correct_num_templates_for_valid_type_returns_200(client, sample_service, tmp_type):
|
|
num_templates = 3
|
|
|
|
templates = []
|
|
for _ in range(num_templates):
|
|
templates.append(create_template(sample_service, template_type=tmp_type))
|
|
|
|
for other_type in TEMPLATE_TYPES:
|
|
if other_type != tmp_type:
|
|
templates.append(create_template(sample_service, template_type=other_type))
|
|
|
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
|
|
|
response = client.get(path='/v2/templates?type={}'.format(tmp_type),
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
assert response.status_code == 200
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
assert len(json_response['templates']) == num_templates
|
|
|
|
|
|
def test_get_all_templates_for_invalid_type_returns_400(client, sample_service):
|
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
|
|
|
invalid_type = 'coconut'
|
|
|
|
response = client.get(path='/v2/templates?type={}'.format(invalid_type),
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
assert response.status_code == 400
|
|
assert response.headers['Content-type'] == 'application/json'
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
assert json_response == {
|
|
'status_code': 400,
|
|
'errors': [
|
|
{
|
|
'message': 'type coconut is not one of [sms, email, letter, broadcast]',
|
|
'error': 'ValidationError'
|
|
}
|
|
]
|
|
}
|