This commit is contained in:
Kenneth Kehl
2023-08-29 14:54:30 -07:00
parent 19dcd7a48b
commit 1ecb747c6d
588 changed files with 34100 additions and 23589 deletions

View File

@@ -8,36 +8,41 @@ from tests.app.db import (
)
def test_get_inbound_sms_returns_200(
client, sample_service
):
def test_get_inbound_sms_returns_200(client, sample_service):
all_inbound_sms = [
create_inbound_sms(service=sample_service, user_number='447700900111', content='Hi'),
create_inbound_sms(service=sample_service, user_number='447700900112'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='Bye'),
create_inbound_sms(service=sample_service, user_number='07700900113')
create_inbound_sms(
service=sample_service, user_number="447700900111", content="Hi"
),
create_inbound_sms(service=sample_service, user_number="447700900112"),
create_inbound_sms(
service=sample_service, user_number="447700900111", content="Bye"
),
create_inbound_sms(service=sample_service, user_number="07700900113"),
]
auth_header = create_service_authorization_header(service_id=sample_service.id)
response = client.get(
path='/v2/received-text-messages',
headers=[('Content-Type', 'application/json'), auth_header])
path="/v2/received-text-messages",
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 200
assert response.headers['Content-type'] == 'application/json'
assert response.headers["Content-type"] == "application/json"
json_response = json.loads(response.get_data(as_text=True))['received_text_messages']
json_response = json.loads(response.get_data(as_text=True))[
"received_text_messages"
]
reversed_all_inbound_sms = sorted(all_inbound_sms, key=lambda sms: sms.created_at, reverse=True)
reversed_all_inbound_sms = sorted(
all_inbound_sms, key=lambda sms: sms.created_at, reverse=True
)
expected_response = [i.serialize() for i in reversed_all_inbound_sms]
assert json_response == expected_response
def test_get_inbound_sms_returns_200_when_service_has_callbacks(
client, sample_service
):
def test_get_inbound_sms_returns_200_when_service_has_callbacks(client, sample_service):
create_service_inbound_api(
service=sample_service,
url="https://inbound.example.com",
@@ -49,8 +54,8 @@ def test_get_inbound_sms_returns_200_when_service_has_callbacks(
auth_header = create_service_authorization_header(service_id=sample_service.id)
response = client.get(
path='/v2/received-text-messages',
headers=[('Content-Type', 'application/json'), auth_header],
path="/v2/received-text-messages",
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 200
@@ -58,105 +63,139 @@ def test_get_inbound_sms_returns_200_when_service_has_callbacks(
def test_get_inbound_sms_generate_page_links(client, sample_service, mocker):
mocker.patch.dict(
"app.v2.inbound_sms.get_inbound_sms.current_app.config",
{"API_PAGE_SIZE": 2}
"app.v2.inbound_sms.get_inbound_sms.current_app.config", {"API_PAGE_SIZE": 2}
)
all_inbound_sms = [
create_inbound_sms(service=sample_service, user_number='447700900111', content='Hi'),
create_inbound_sms(service=sample_service, user_number='447700900111'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='End'),
create_inbound_sms(
service=sample_service, user_number="447700900111", content="Hi"
),
create_inbound_sms(service=sample_service, user_number="447700900111"),
create_inbound_sms(
service=sample_service, user_number="447700900111", content="End"
),
]
reversed_inbound_sms = sorted(all_inbound_sms, key=lambda sms: sms.created_at, reverse=True)
reversed_inbound_sms = sorted(
all_inbound_sms, key=lambda sms: sms.created_at, reverse=True
)
auth_header = create_service_authorization_header(service_id=sample_service.id)
response = client.get(
url_for('v2_inbound_sms.get_inbound_sms'),
headers=[('Content-Type', 'application/json'), auth_header])
url_for("v2_inbound_sms.get_inbound_sms"),
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 200
json_response = json.loads(response.get_data(as_text=True))
expected_inbound_sms_list = [i.serialize() for i in reversed_inbound_sms[:2]]
assert json_response['received_text_messages'] == expected_inbound_sms_list
assert url_for(
'v2_inbound_sms.get_inbound_sms',
_external=True) == json_response['links']['current']
assert url_for(
'v2_inbound_sms.get_inbound_sms',
older_than=reversed_inbound_sms[1].id,
_external=True) == json_response['links']['next']
assert json_response["received_text_messages"] == expected_inbound_sms_list
assert (
url_for("v2_inbound_sms.get_inbound_sms", _external=True)
== json_response["links"]["current"]
)
assert (
url_for(
"v2_inbound_sms.get_inbound_sms",
older_than=reversed_inbound_sms[1].id,
_external=True,
)
== json_response["links"]["next"]
)
def test_get_next_inbound_sms_will_get_correct_inbound_sms_list(client, sample_service, mocker):
def test_get_next_inbound_sms_will_get_correct_inbound_sms_list(
client, sample_service, mocker
):
mocker.patch.dict(
"app.v2.inbound_sms.get_inbound_sms.current_app.config",
{"API_PAGE_SIZE": 2}
"app.v2.inbound_sms.get_inbound_sms.current_app.config", {"API_PAGE_SIZE": 2}
)
all_inbound_sms = [
create_inbound_sms(service=sample_service, user_number='447700900111', content='1'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='2'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='3'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='4'),
create_inbound_sms(
service=sample_service, user_number="447700900111", content="1"
),
create_inbound_sms(
service=sample_service, user_number="447700900111", content="2"
),
create_inbound_sms(
service=sample_service, user_number="447700900111", content="3"
),
create_inbound_sms(
service=sample_service, user_number="447700900111", content="4"
),
]
reversed_inbound_sms = sorted(all_inbound_sms, key=lambda sms: sms.created_at, reverse=True)
reversed_inbound_sms = sorted(
all_inbound_sms, key=lambda sms: sms.created_at, reverse=True
)
auth_header = create_service_authorization_header(service_id=sample_service.id)
response = client.get(
path=url_for('v2_inbound_sms.get_inbound_sms', older_than=reversed_inbound_sms[1].id),
headers=[('Content-Type', 'application/json'), auth_header])
path=url_for(
"v2_inbound_sms.get_inbound_sms", older_than=reversed_inbound_sms[1].id
),
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 200
json_response = json.loads(response.get_data(as_text=True))
expected_inbound_sms_list = [i.serialize() for i in reversed_inbound_sms[2:]]
assert json_response['received_text_messages'] == expected_inbound_sms_list
assert url_for(
'v2_inbound_sms.get_inbound_sms',
_external=True) == json_response['links']['current']
assert url_for(
'v2_inbound_sms.get_inbound_sms',
older_than=reversed_inbound_sms[3].id,
_external=True) == json_response['links']['next']
assert json_response["received_text_messages"] == expected_inbound_sms_list
assert (
url_for("v2_inbound_sms.get_inbound_sms", _external=True)
== json_response["links"]["current"]
)
assert (
url_for(
"v2_inbound_sms.get_inbound_sms",
older_than=reversed_inbound_sms[3].id,
_external=True,
)
== json_response["links"]["next"]
)
def test_get_next_inbound_sms_at_end_will_return_empty_inbound_sms_list(client, sample_service, mocker):
def test_get_next_inbound_sms_at_end_will_return_empty_inbound_sms_list(
client, sample_service, mocker
):
inbound_sms = create_inbound_sms(service=sample_service)
mocker.patch.dict(
"app.v2.inbound_sms.get_inbound_sms.current_app.config",
{"API_PAGE_SIZE": 1}
"app.v2.inbound_sms.get_inbound_sms.current_app.config", {"API_PAGE_SIZE": 1}
)
auth_header = create_service_authorization_header(service_id=inbound_sms.service.id)
response = client.get(
path=url_for('v2_inbound_sms.get_inbound_sms', older_than=inbound_sms.id),
headers=[('Content-Type', 'application/json'), auth_header])
path=url_for("v2_inbound_sms.get_inbound_sms", older_than=inbound_sms.id),
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 200
json_response = json.loads(response.get_data(as_text=True))
expected_inbound_sms_list = []
assert json_response['received_text_messages'] == expected_inbound_sms_list
assert url_for(
'v2_inbound_sms.get_inbound_sms',
_external=True) == json_response['links']['current']
assert 'next' not in json_response['links'].keys()
assert json_response["received_text_messages"] == expected_inbound_sms_list
assert (
url_for("v2_inbound_sms.get_inbound_sms", _external=True)
== json_response["links"]["current"]
)
assert "next" not in json_response["links"].keys()
def test_get_inbound_sms_for_no_inbound_sms_returns_empty_list(
client, sample_service
):
def test_get_inbound_sms_for_no_inbound_sms_returns_empty_list(client, sample_service):
auth_header = create_service_authorization_header(service_id=sample_service.id)
response = client.get(
path='/v2/received-text-messages',
headers=[('Content-Type', 'application/json'), auth_header])
path="/v2/received-text-messages",
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 200
assert response.headers['Content-type'] == 'application/json'
assert response.headers["Content-type"] == "application/json"
json_response = json.loads(response.get_data(as_text=True))['received_text_messages']
json_response = json.loads(response.get_data(as_text=True))[
"received_text_messages"
]
expected_response = []
@@ -166,15 +205,18 @@ def test_get_inbound_sms_for_no_inbound_sms_returns_empty_list(
def test_get_inbound_sms_with_invalid_query_string_returns_400(client, sample_service):
auth_header = create_service_authorization_header(service_id=sample_service.id)
response = client.get(
path='/v2/received-text-messages?user_number=447700900000',
headers=[('Content-Type', 'application/json'), auth_header])
path="/v2/received-text-messages?user_number=447700900000",
headers=[("Content-Type", "application/json"), auth_header],
)
assert response.status_code == 400
assert response.headers['Content-type'] == 'application/json'
assert response.headers["Content-type"] == "application/json"
json_response = json.loads(response.get_data(as_text=True))
assert json_response['status_code'] == 400
assert json_response['errors'][0]['error'] == 'ValidationError'
assert json_response['errors'][0]['message'] == \
'Additional properties are not allowed (user_number was unexpected)'
assert json_response["status_code"] == 400
assert json_response["errors"][0]["error"] == "ValidationError"
assert (
json_response["errors"][0]["message"]
== "Additional properties are not allowed (user_number was unexpected)"
)

View File

@@ -17,15 +17,12 @@ valid_inbound_sms = {
"service_id": "a5149c32-f03b-4711-af49-ad6993797d45",
"id": "342786aa-23ce-4695-9aad-7f79e68ee29a",
"notify_number": "testing",
"content": "Hello"
"content": "Hello",
}
valid_inbound_sms_list = {
"received_text_messages": [valid_inbound_sms],
"links": {
"current": valid_inbound_sms["id"]
}
"links": {"current": valid_inbound_sms["id"]},
}
invalid_inbound_sms = {
@@ -33,36 +30,44 @@ invalid_inbound_sms = {
"created_at": "2017-11-02T15:07:57.197546",
"service_id": "a5149c32-f03b-4711-af49-ad6993797d45",
"id": "342786aa-23ce-4695-9aad-7f79e68ee29a",
"notify_number": "testing"
"notify_number": "testing",
}
invalid_inbound_sms_list = {
"received_text_messages": [invalid_inbound_sms]
}
invalid_inbound_sms_list = {"received_text_messages": [invalid_inbound_sms]}
def test_get_inbound_sms_contract(client, sample_service):
all_inbound_sms = [
create_inbound_sms(service=sample_service, user_number='447700900113'),
create_inbound_sms(service=sample_service, user_number='447700900112'),
create_inbound_sms(service=sample_service, user_number='447700900111'),
create_inbound_sms(service=sample_service, user_number="447700900113"),
create_inbound_sms(service=sample_service, user_number="447700900112"),
create_inbound_sms(service=sample_service, user_number="447700900111"),
]
reversed_inbound_sms = sorted(all_inbound_sms, key=lambda sms: sms.created_at, reverse=True)
reversed_inbound_sms = sorted(
all_inbound_sms, key=lambda sms: sms.created_at, reverse=True
)
auth_header = create_service_authorization_header(service_id=all_inbound_sms[0].service_id)
response = client.get('/v2/received-text-messages', headers=[auth_header])
auth_header = create_service_authorization_header(
service_id=all_inbound_sms[0].service_id
)
response = client.get("/v2/received-text-messages", headers=[auth_header])
response_json = json.loads(response.get_data(as_text=True))
validated_resp = validate(response_json, get_inbound_sms_response)
assert validated_resp['received_text_messages'] == [i.serialize() for i in reversed_inbound_sms]
assert validated_resp['links']['current'] == url_for(
'v2_inbound_sms.get_inbound_sms', _external=True)
assert validated_resp['links']['next'] == url_for(
'v2_inbound_sms.get_inbound_sms', older_than=all_inbound_sms[0].id, _external=True)
assert validated_resp["received_text_messages"] == [
i.serialize() for i in reversed_inbound_sms
]
assert validated_resp["links"]["current"] == url_for(
"v2_inbound_sms.get_inbound_sms", _external=True
)
assert validated_resp["links"]["next"] == url_for(
"v2_inbound_sms.get_inbound_sms",
older_than=all_inbound_sms[0].id,
_external=True,
)
@pytest.mark.parametrize('request_args', [
{'older_than': "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6"}, {}]
@pytest.mark.parametrize(
"request_args", [{"older_than": "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6"}, {}]
)
def test_valid_inbound_sms_request_json(client, request_args):
validate(request_args, get_inbound_sms_request)
@@ -70,11 +75,14 @@ def test_valid_inbound_sms_request_json(client, request_args):
def test_invalid_inbound_sms_request_json(client):
with pytest.raises(expected_exception=ValidationError):
validate({'user_number': '447700900111'}, get_inbound_sms_request)
validate({"user_number": "447700900111"}, get_inbound_sms_request)
def test_valid_inbound_sms_response_json():
assert validate(valid_inbound_sms, get_inbound_sms_single_response) == valid_inbound_sms
assert (
validate(valid_inbound_sms, get_inbound_sms_single_response)
== valid_inbound_sms
)
def test_valid_inbound_sms_list_response_json():