mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Add V2 inbound_sms API
- had to update the serialization in the model so that the date time is appended with the UTC timezone - test has been added to ensure that the schema will validate the response correctly
This commit is contained in:
0
tests/app/v2/inbound_sms/__init__.py
Normal file
0
tests/app/v2/inbound_sms/__init__.py
Normal file
131
tests/app/v2/inbound_sms/test_get_inbound_sms.py
Normal file
131
tests/app/v2/inbound_sms/test_get_inbound_sms.py
Normal file
@@ -0,0 +1,131 @@
|
||||
import datetime
|
||||
import pytest
|
||||
from flask import json, url_for
|
||||
|
||||
from app import DATETIME_FORMAT
|
||||
from tests import create_authorization_header
|
||||
from tests.app.db import create_inbound_sms
|
||||
|
||||
|
||||
def test_get_all_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='447700900113')
|
||||
]
|
||||
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
response = client.get(
|
||||
path='/v2/inbound_sms',
|
||||
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))['inbound_sms_list']
|
||||
|
||||
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_all_inbound_sms_for_no_inbound_sms_returns_200(
|
||||
client, sample_service
|
||||
):
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
response = client.get(
|
||||
path='/v2/inbound_sms',
|
||||
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))['inbound_sms_list']
|
||||
|
||||
expected_response = []
|
||||
|
||||
assert json_response == expected_response
|
||||
|
||||
|
||||
def test_get_inbound_sms_by_number_returns_200(
|
||||
client, sample_service
|
||||
):
|
||||
sample_inbound_sms = create_inbound_sms(service=sample_service)
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
response = client.get(
|
||||
path='/v2/inbound_sms/{}'.format(sample_inbound_sms.user_number),
|
||||
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))['inbound_sms_list']
|
||||
|
||||
expected_response = [sample_inbound_sms.serialize()]
|
||||
|
||||
assert json_response == expected_response
|
||||
|
||||
|
||||
def test_get_inbound_sms_for_no_inbound_sms_returns_200(
|
||||
client, sample_service
|
||||
):
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
response = client.get(
|
||||
path='/v2/inbound_sms',
|
||||
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))['inbound_sms_list']
|
||||
|
||||
expected_response = []
|
||||
|
||||
assert json_response == expected_response
|
||||
|
||||
|
||||
def test_get_inbound_sms_by_nonexistent_number(client, sample_service):
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
response = client.get(
|
||||
path='/v2/inbound_sms/447700900000',
|
||||
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))['inbound_sms_list']
|
||||
expected_response = []
|
||||
|
||||
assert json_response == expected_response
|
||||
|
||||
|
||||
@pytest.mark.parametrize('invalid_number,expected_message', [
|
||||
('0077700', 'Not enough digits'),
|
||||
('123456789012', 'Not a UK mobile number'),
|
||||
('invalid_number', 'Must not contain letters or symbols')
|
||||
])
|
||||
def test_get_inbound_sms_by_invalid_number(
|
||||
client, sample_service, invalid_number, expected_message):
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
response = client.get(
|
||||
path='/v2/inbound_sms/{}'.format(invalid_number),
|
||||
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 == {
|
||||
"errors": [
|
||||
{
|
||||
"error": "BadRequestError",
|
||||
"message": expected_message
|
||||
}
|
||||
],
|
||||
"status_code": 400
|
||||
}
|
||||
75
tests/app/v2/inbound_sms/test_inbound_sms_schemas.py
Normal file
75
tests/app/v2/inbound_sms/test_inbound_sms_schemas.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from flask import json
|
||||
from jsonschema.exceptions import ValidationError
|
||||
|
||||
from app.dao.api_key_dao import save_model_api_key
|
||||
from app.models import ApiKey, KEY_TYPE_NORMAL, EMAIL_TYPE, SMS_TYPE, TEMPLATE_TYPES
|
||||
from app.v2.inbound_sms.inbound_sms_schemas import get_inbound_sms_response, get_inbound_sms_single_response
|
||||
from app.schema_validation import validate
|
||||
|
||||
from tests import create_authorization_header
|
||||
|
||||
|
||||
valid_inbound_sms = {
|
||||
"provider_date": "2017-11-02T15:07:57.199541Z",
|
||||
"provider_reference": "foo",
|
||||
"user_number": "447700900111",
|
||||
"created_at": "2017-11-02T15:07:57.197546Z",
|
||||
"service_id": "a5149c32-f03b-4711-af49-ad6993797d45",
|
||||
"id": "342786aa-23ce-4695-9aad-7f79e68ee29a",
|
||||
"notify_number": "testing",
|
||||
"content": "Hello"
|
||||
}
|
||||
|
||||
valid_inbound_sms_list = {
|
||||
"inbound_sms_list": [valid_inbound_sms]
|
||||
}
|
||||
|
||||
invalid_inbound_sms = {
|
||||
"provider_date": "2017-11-02T15:07:57.199541",
|
||||
"provider_reference": "foo",
|
||||
"user_number": "447700900111",
|
||||
"created_at": "2017-11-02T15:07:57.197546",
|
||||
"service_id": "a5149c32-f03b-4711-af49-ad6993797d45",
|
||||
"id": "342786aa-23ce-4695-9aad-7f79e68ee29a",
|
||||
"notify_number": "testing"
|
||||
}
|
||||
|
||||
invalid_inbound_sms_list = {
|
||||
"inbound_sms_list": [invalid_inbound_sms]
|
||||
}
|
||||
|
||||
|
||||
def _get_inbound_sms(client, inbound_sms, url):
|
||||
auth_header = create_authorization_header(service_id=inbound_sms.service_id)
|
||||
response = client.get(url, headers=[auth_header])
|
||||
return json.loads(response.get_data(as_text=True))
|
||||
|
||||
|
||||
def test_get_inbound_sms_contract(client, sample_inbound_sms):
|
||||
response_json = _get_inbound_sms(
|
||||
client,
|
||||
sample_inbound_sms,
|
||||
'/v2/inbound_sms/{}'.format(sample_inbound_sms.user_number)
|
||||
)
|
||||
res = validate(response_json, get_inbound_sms_response)
|
||||
|
||||
|
||||
def test_valid_inbound_sms_json():
|
||||
validate(valid_inbound_sms, get_inbound_sms_single_response)
|
||||
|
||||
|
||||
def test_valid_inbound_sms_list_json():
|
||||
validate(valid_inbound_sms_list, get_inbound_sms_response)
|
||||
|
||||
|
||||
def test_invalid_inbound_sms_json():
|
||||
with pytest.raises(expected_exception=ValidationError):
|
||||
validate(invalid_inbound_sms, get_inbound_sms_single_response)
|
||||
|
||||
|
||||
def test_invalid_inbound_sms_list_json():
|
||||
with pytest.raises(expected_exception=ValidationError):
|
||||
validate(invalid_inbound_sms_list, get_inbound_sms_response)
|
||||
Reference in New Issue
Block a user