2016-04-12 14:19:51 +01:00
|
|
|
import uuid
|
2017-01-06 17:49:20 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2019-03-25 10:25:05 +00:00
|
|
|
from unittest.mock import patch
|
2019-07-15 13:52:42 +01:00
|
|
|
from urllib.parse import parse_qs, urlparse
|
2019-03-25 10:25:05 +00:00
|
|
|
|
|
|
|
|
import pytest
|
2016-01-15 15:15:35 +00:00
|
|
|
from flask import url_for
|
2019-03-25 10:25:05 +00:00
|
|
|
from flask.testing import FlaskClient
|
2016-03-18 10:49:22 +00:00
|
|
|
from flask_login import login_user
|
2019-03-25 10:25:05 +00:00
|
|
|
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
from app.models.user import User
|
2016-01-15 15:15:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestClient(FlaskClient):
|
2016-03-30 10:53:15 +01:00
|
|
|
def login(self, user, mocker=None, service=None):
|
2016-01-15 15:15:35 +00:00
|
|
|
# Skipping authentication here and just log them in
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
model_user = User(user)
|
2016-01-15 15:15:35 +00:00
|
|
|
with self.session_transaction() as session:
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
session['current_session_id'] = model_user.current_session_id
|
|
|
|
|
session['user_id'] = model_user.id
|
2016-03-31 10:26:03 +01:00
|
|
|
if mocker:
|
2016-03-30 10:53:15 +01:00
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=user)
|
2016-03-31 10:26:03 +01:00
|
|
|
if mocker and service:
|
2017-01-30 13:59:43 +00:00
|
|
|
with self.session_transaction() as session:
|
|
|
|
|
session['service_id'] = service['id']
|
2016-03-30 10:53:15 +01:00
|
|
|
mocker.patch('app.service_api_client.get_service', return_value={'data': service})
|
2018-05-02 10:27:01 +01:00
|
|
|
|
|
|
|
|
with patch('app.events_api_client.create_event'):
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
login_user(model_user)
|
2016-03-18 16:20:37 +00:00
|
|
|
|
2016-01-15 15:15:35 +00:00
|
|
|
def logout(self, user):
|
2019-07-15 13:52:42 +01:00
|
|
|
self.get(url_for("main.sign_out"))
|
2016-01-15 15:15:35 +00:00
|
|
|
|
|
|
|
|
|
2016-04-12 14:19:51 +01:00
|
|
|
def sample_uuid():
|
|
|
|
|
return "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_uuid():
|
|
|
|
|
return uuid.uuid4()
|
|
|
|
|
|
|
|
|
|
|
2016-05-24 12:34:29 +01:00
|
|
|
def created_by_json(id_, name='', email_address=''):
|
|
|
|
|
return {'id': id_, 'name': name, 'email_address': email_address}
|
|
|
|
|
|
|
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
def user_json(
|
|
|
|
|
id_='1234',
|
|
|
|
|
name='Test User',
|
|
|
|
|
email_address='test@gov.uk',
|
|
|
|
|
mobile_number='+447700900986',
|
|
|
|
|
password_changed_at=None,
|
|
|
|
|
permissions={generate_uuid(): [
|
2018-02-28 15:37:49 +00:00
|
|
|
'view_activity',
|
2018-02-19 16:53:29 +00:00
|
|
|
'send_texts',
|
|
|
|
|
'send_emails',
|
|
|
|
|
'send_letters',
|
|
|
|
|
'manage_users',
|
|
|
|
|
'manage_templates',
|
|
|
|
|
'manage_settings',
|
|
|
|
|
'manage_api_keys']
|
|
|
|
|
},
|
|
|
|
|
auth_type='sms_auth',
|
|
|
|
|
failed_login_count=0,
|
2019-06-05 12:14:50 +01:00
|
|
|
logged_in_at=None,
|
2018-02-19 16:53:29 +00:00
|
|
|
state='active',
|
|
|
|
|
max_failed_login_count=3,
|
|
|
|
|
platform_admin=False,
|
|
|
|
|
current_session_id='1234',
|
|
|
|
|
organisations=[],
|
2018-03-13 08:23:29 +00:00
|
|
|
services=None
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
):
|
|
|
|
|
return {
|
|
|
|
|
'id': id_,
|
|
|
|
|
'name': name,
|
|
|
|
|
'email_address': email_address,
|
|
|
|
|
'mobile_number': mobile_number,
|
|
|
|
|
'password_changed_at': password_changed_at,
|
|
|
|
|
'permissions': permissions,
|
|
|
|
|
'auth_type': auth_type,
|
|
|
|
|
'failed_login_count': failed_login_count,
|
2019-06-05 12:14:50 +01:00
|
|
|
'logged_in_at': logged_in_at or datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f'),
|
2018-02-19 16:53:29 +00:00
|
|
|
'state': state,
|
|
|
|
|
'max_failed_login_count': max_failed_login_count,
|
|
|
|
|
'platform_admin': platform_admin,
|
|
|
|
|
'current_session_id': current_session_id,
|
2018-03-12 15:21:46 +00:00
|
|
|
'organisations': organisations,
|
2018-03-13 08:23:29 +00:00
|
|
|
'services': list(permissions.keys()) if services is None else services
|
2018-02-19 16:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def invited_user(
|
|
|
|
|
_id='1234',
|
|
|
|
|
service=None,
|
|
|
|
|
from_user='1234',
|
|
|
|
|
email_address='testinviteduser@gov.uk',
|
|
|
|
|
permissions=None,
|
|
|
|
|
status='pending',
|
|
|
|
|
created_at=datetime.utcnow(),
|
|
|
|
|
auth_type='sms_auth',
|
|
|
|
|
organisation=None
|
|
|
|
|
):
|
|
|
|
|
data = {
|
|
|
|
|
'id': _id,
|
|
|
|
|
'from_user': from_user,
|
|
|
|
|
'email_address': email_address,
|
|
|
|
|
'status': status,
|
|
|
|
|
'created_at': created_at,
|
|
|
|
|
'auth_type': auth_type,
|
|
|
|
|
}
|
|
|
|
|
if service:
|
|
|
|
|
data['service'] = service
|
|
|
|
|
if permissions:
|
|
|
|
|
data['permissions'] = permissions
|
|
|
|
|
if organisation:
|
|
|
|
|
data['organisation'] = organisation
|
|
|
|
|
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
return data
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
2016-06-01 16:07:43 +01:00
|
|
|
def service_json(
|
2016-10-26 18:37:26 +01:00
|
|
|
id_='1234',
|
|
|
|
|
name='Test Service',
|
|
|
|
|
users=None,
|
2016-08-11 12:32:38 +01:00
|
|
|
message_limit=1000,
|
2016-11-08 13:17:08 +00:00
|
|
|
active=True,
|
2016-08-11 12:32:38 +01:00
|
|
|
restricted=True,
|
|
|
|
|
email_from=None,
|
|
|
|
|
reply_to_email_address=None,
|
2017-05-23 13:40:18 +01:00
|
|
|
sms_sender='GOVUK',
|
2016-08-08 10:28:40 +01:00
|
|
|
research_mode=False,
|
2018-02-07 10:30:49 +00:00
|
|
|
email_branding=None,
|
2016-11-08 13:17:08 +00:00
|
|
|
branding='govuk',
|
2017-03-02 15:56:28 +00:00
|
|
|
created_at=None,
|
2017-06-02 16:25:24 +01:00
|
|
|
letter_contact_block=None,
|
2017-06-21 12:15:53 +01:00
|
|
|
inbound_api=None,
|
2017-12-04 15:07:11 +00:00
|
|
|
service_callback_api=None,
|
2017-11-01 15:36:27 +00:00
|
|
|
permissions=None,
|
2017-10-05 11:08:22 +01:00
|
|
|
organisation_type='central',
|
2017-11-07 11:08:26 +00:00
|
|
|
prefix_sms=True,
|
2018-06-05 10:37:41 +01:00
|
|
|
contact_link=None,
|
2019-06-12 12:09:26 +01:00
|
|
|
organisation_id=None,
|
2016-08-11 12:32:38 +01:00
|
|
|
):
|
2016-10-26 18:37:26 +01:00
|
|
|
if users is None:
|
|
|
|
|
users = []
|
2017-06-05 14:48:24 +01:00
|
|
|
if permissions is None:
|
2017-11-01 15:36:27 +00:00
|
|
|
permissions = ['email', 'sms']
|
2017-06-21 12:15:53 +01:00
|
|
|
if inbound_api is None:
|
|
|
|
|
inbound_api = []
|
2016-01-15 15:15:35 +00:00
|
|
|
return {
|
|
|
|
|
'id': id_,
|
|
|
|
|
'name': name,
|
|
|
|
|
'users': users,
|
2016-04-12 14:19:51 +01:00
|
|
|
'message_limit': message_limit,
|
2016-01-15 15:15:35 +00:00
|
|
|
'active': active,
|
2016-03-30 17:12:00 +01:00
|
|
|
'restricted': restricted,
|
2016-05-16 13:09:58 +01:00
|
|
|
'email_from': email_from,
|
2016-06-01 16:07:43 +01:00
|
|
|
'reply_to_email_address': reply_to_email_address,
|
2016-08-11 12:32:38 +01:00
|
|
|
'sms_sender': sms_sender,
|
2016-08-08 10:28:40 +01:00
|
|
|
'research_mode': research_mode,
|
2017-10-05 11:08:22 +01:00
|
|
|
'organisation_type': organisation_type,
|
2018-02-07 10:30:49 +00:00
|
|
|
'email_branding': email_branding,
|
2016-09-12 11:40:21 +01:00
|
|
|
'branding': branding,
|
2017-03-02 15:56:28 +00:00
|
|
|
'created_at': created_at or str(datetime.utcnow()),
|
2019-02-05 16:34:05 +00:00
|
|
|
'letter_branding': None,
|
2017-04-20 12:15:30 +01:00
|
|
|
'letter_contact_block': letter_contact_block,
|
2017-06-02 16:25:24 +01:00
|
|
|
'permissions': permissions,
|
2017-06-21 12:15:53 +01:00
|
|
|
'inbound_api': inbound_api,
|
2017-12-04 15:07:11 +00:00
|
|
|
'service_callback_api': service_callback_api,
|
2017-11-07 11:08:26 +00:00
|
|
|
'prefix_sms': prefix_sms,
|
2018-10-26 12:13:04 +01:00
|
|
|
'contact_link': None,
|
2019-02-15 11:03:19 +00:00
|
|
|
'volume_email': 111111,
|
|
|
|
|
'volume_sms': 222222,
|
|
|
|
|
'volume_letter': 333333,
|
|
|
|
|
'consent_to_research': True,
|
2019-03-25 14:46:42 +00:00
|
|
|
'count_as_live': True,
|
2019-06-12 12:09:26 +01:00
|
|
|
'organisation': organisation_id,
|
2016-01-15 15:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-01-18 17:35:28 +00:00
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
def organisation_json(
|
|
|
|
|
id_='1234',
|
2019-04-04 11:12:53 +01:00
|
|
|
name=False,
|
2018-02-19 16:53:29 +00:00
|
|
|
users=None,
|
|
|
|
|
active=True,
|
|
|
|
|
created_at=None,
|
2019-02-19 17:26:16 +00:00
|
|
|
services=None,
|
|
|
|
|
letter_branding_id=None,
|
|
|
|
|
email_branding_id=None,
|
|
|
|
|
domains=None,
|
2019-04-04 11:12:53 +01:00
|
|
|
crown=True,
|
|
|
|
|
agreement_signed=False,
|
2019-06-18 14:24:29 +01:00
|
|
|
agreement_signed_version=None,
|
|
|
|
|
agreement_signed_on_behalf_of_name=None,
|
|
|
|
|
agreement_signed_on_behalf_of_email_address=None,
|
2019-04-18 12:44:18 +01:00
|
|
|
organisation_type='',
|
2019-05-13 14:50:40 +01:00
|
|
|
request_to_go_live_notes=None,
|
2018-02-19 16:53:29 +00:00
|
|
|
):
|
|
|
|
|
if users is None:
|
|
|
|
|
users = []
|
|
|
|
|
if services is None:
|
|
|
|
|
services = []
|
|
|
|
|
return {
|
|
|
|
|
'id': id_,
|
2019-04-04 11:12:53 +01:00
|
|
|
'name': 'Test Organisation' if name is False else name,
|
2018-02-19 16:53:29 +00:00
|
|
|
'active': active,
|
|
|
|
|
'users': users,
|
2019-02-19 17:26:16 +00:00
|
|
|
'created_at': created_at or str(datetime.utcnow()),
|
|
|
|
|
'email_branding_id': email_branding_id,
|
|
|
|
|
'letter_branding_id': letter_branding_id,
|
2019-04-18 12:44:18 +01:00
|
|
|
'organisation_type': organisation_type,
|
2019-04-04 11:12:53 +01:00
|
|
|
'crown': crown,
|
|
|
|
|
'agreement_signed': agreement_signed,
|
2019-02-19 17:26:16 +00:00
|
|
|
'agreement_signed_at': None,
|
|
|
|
|
'agreement_signed_by': None,
|
2019-06-18 14:24:29 +01:00
|
|
|
'agreement_signed_version': agreement_signed_version,
|
|
|
|
|
'agreement_signed_on_behalf_of_name': agreement_signed_on_behalf_of_name,
|
|
|
|
|
'agreement_signed_on_behalf_of_email_address': agreement_signed_on_behalf_of_email_address,
|
2019-02-19 17:26:16 +00:00
|
|
|
'domains': domains or [],
|
2019-05-13 14:50:40 +01:00
|
|
|
'request_to_go_live_notes': request_to_go_live_notes,
|
2019-06-12 12:09:26 +01:00
|
|
|
'count_of_live_services': len(services),
|
2018-02-19 16:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-04-14 12:00:55 +01:00
|
|
|
def template_json(service_id,
|
|
|
|
|
id_,
|
|
|
|
|
name="sample template",
|
2017-07-13 13:05:41 +01:00
|
|
|
type_=None,
|
2017-06-24 17:29:28 +01:00
|
|
|
content=None,
|
2016-05-11 11:20:45 +01:00
|
|
|
subject=None,
|
2016-07-01 13:10:19 +01:00
|
|
|
version=1,
|
2017-01-18 15:11:34 +00:00
|
|
|
archived=False,
|
2017-06-28 15:26:09 +01:00
|
|
|
process_type='normal',
|
2017-06-24 17:29:28 +01:00
|
|
|
redact_personalisation=None,
|
2018-01-03 10:44:36 +00:00
|
|
|
service_letter_contact=None,
|
|
|
|
|
reply_to=None,
|
|
|
|
|
reply_to_text=None,
|
2018-03-02 17:06:51 +00:00
|
|
|
is_precompiled_letter=False,
|
2019-04-03 13:59:44 +01:00
|
|
|
postage=None,
|
|
|
|
|
folder=None
|
2017-06-28 15:26:09 +01:00
|
|
|
):
|
2016-04-14 12:00:55 +01:00
|
|
|
template = {
|
2016-01-19 15:54:12 +00:00
|
|
|
'id': id_,
|
|
|
|
|
'name': name,
|
2017-07-13 13:05:41 +01:00
|
|
|
'template_type': type_ or "sms",
|
2016-01-19 15:54:12 +00:00
|
|
|
'content': content,
|
2016-05-11 11:20:45 +01:00
|
|
|
'service': service_id,
|
2016-07-01 13:10:19 +01:00
|
|
|
'version': version,
|
|
|
|
|
'updated_at': datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f'),
|
2017-01-18 15:11:34 +00:00
|
|
|
'archived': archived,
|
2017-06-28 15:26:09 +01:00
|
|
|
'process_type': process_type,
|
2018-01-03 10:44:36 +00:00
|
|
|
'service_letter_contact': service_letter_contact,
|
|
|
|
|
'reply_to': reply_to,
|
|
|
|
|
'reply_to_text': reply_to_text,
|
2018-03-02 17:06:51 +00:00
|
|
|
'is_precompiled_letter': is_precompiled_letter,
|
2019-04-03 13:59:44 +01:00
|
|
|
'folder': folder,
|
2018-12-19 17:06:09 +00:00
|
|
|
'postage': postage
|
2016-01-19 15:54:12 +00:00
|
|
|
}
|
2017-06-24 17:29:28 +01:00
|
|
|
if content is None:
|
|
|
|
|
template['content'] = "template content"
|
2017-06-24 17:12:45 +01:00
|
|
|
if subject is None and type_ != 'sms':
|
|
|
|
|
template['subject'] = "template subject"
|
2016-04-14 12:00:55 +01:00
|
|
|
if subject is not None:
|
|
|
|
|
template['subject'] = subject
|
2017-06-28 15:26:09 +01:00
|
|
|
if redact_personalisation is not None:
|
|
|
|
|
template['redact_personalisation'] = redact_personalisation
|
2016-04-14 12:00:55 +01:00
|
|
|
return template
|
2016-01-19 15:54:12 +00:00
|
|
|
|
|
|
|
|
|
2016-05-11 11:20:45 +01:00
|
|
|
def template_version_json(service_id,
|
|
|
|
|
id_,
|
|
|
|
|
created_by,
|
|
|
|
|
version=1,
|
|
|
|
|
created_at=None,
|
|
|
|
|
**kwargs):
|
|
|
|
|
template = template_json(service_id, id_, **kwargs)
|
2016-05-24 12:34:29 +01:00
|
|
|
template['created_by'] = created_by_json(
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
created_by['id'],
|
|
|
|
|
created_by['name'],
|
|
|
|
|
created_by['email_address'],
|
2016-05-24 12:34:29 +01:00
|
|
|
)
|
2016-05-11 11:20:45 +01:00
|
|
|
if created_at is None:
|
|
|
|
|
created_at = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')
|
|
|
|
|
template['created_at'] = created_at
|
|
|
|
|
template['version'] = version
|
|
|
|
|
return template
|
|
|
|
|
|
|
|
|
|
|
2016-01-21 12:28:05 +00:00
|
|
|
def api_key_json(id_, name, expiry_date=None):
|
|
|
|
|
return {'id': id_,
|
|
|
|
|
'name': name,
|
|
|
|
|
'expiry_date': expiry_date
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-26 15:33:17 +00:00
|
|
|
|
2019-03-15 10:25:00 +00:00
|
|
|
def invite_json(id_,
|
|
|
|
|
from_user,
|
|
|
|
|
service_id,
|
|
|
|
|
email_address,
|
|
|
|
|
permissions,
|
|
|
|
|
created_at,
|
|
|
|
|
status,
|
|
|
|
|
auth_type,
|
|
|
|
|
folder_permissions):
|
2017-11-13 13:39:31 +00:00
|
|
|
return {
|
|
|
|
|
'id': id_,
|
|
|
|
|
'from_user': from_user,
|
|
|
|
|
'service': service_id,
|
|
|
|
|
'email_address': email_address,
|
|
|
|
|
'status': status,
|
|
|
|
|
'permissions': permissions,
|
|
|
|
|
'created_at': created_at,
|
2019-03-15 10:25:00 +00:00
|
|
|
'auth_type': auth_type,
|
|
|
|
|
'folder_permissions': folder_permissions,
|
2017-11-13 13:39:31 +00:00
|
|
|
}
|
2016-02-26 15:33:17 +00:00
|
|
|
|
|
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
def org_invite_json(id_, invited_by, org_id, email_address, created_at, status):
|
|
|
|
|
return {
|
|
|
|
|
'id': id_,
|
|
|
|
|
'invited_by': invited_by,
|
|
|
|
|
'organisation': org_id,
|
|
|
|
|
'email_address': email_address,
|
|
|
|
|
'status': status,
|
|
|
|
|
'created_at': created_at,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-01-18 16:01:04 +00:00
|
|
|
TEST_USER_EMAIL = 'test@user.gov.uk'
|
2016-01-15 15:15:35 +00:00
|
|
|
|
2016-01-18 17:35:28 +00:00
|
|
|
|
2016-02-29 14:57:07 +00:00
|
|
|
def create_test_api_user(state, permissions={}):
|
2016-01-19 22:47:42 +00:00
|
|
|
user_data = {'id': 1,
|
|
|
|
|
'name': 'Test User',
|
|
|
|
|
'password': 'somepassword',
|
|
|
|
|
'email_address': TEST_USER_EMAIL,
|
|
|
|
|
'mobile_number': '+441234123412',
|
2016-02-29 14:57:07 +00:00
|
|
|
'state': state,
|
|
|
|
|
'permissions': permissions
|
2016-01-19 22:47:42 +00:00
|
|
|
}
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
return user_data
|
2016-01-19 22:47:42 +00:00
|
|
|
|
|
|
|
|
|
2016-08-26 09:29:34 +01:00
|
|
|
def job_json(
|
|
|
|
|
service_id,
|
|
|
|
|
created_by,
|
|
|
|
|
job_id=None,
|
|
|
|
|
template_id=None,
|
|
|
|
|
template_version=1,
|
|
|
|
|
created_at=None,
|
|
|
|
|
bucket_name='',
|
|
|
|
|
original_file_name="thisisatest.csv",
|
|
|
|
|
notification_count=1,
|
|
|
|
|
notifications_sent=1,
|
|
|
|
|
notifications_requested=1,
|
2016-10-05 17:45:04 +01:00
|
|
|
job_status='finished',
|
2016-08-09 10:39:57 +01:00
|
|
|
scheduled_for=''
|
2016-08-26 09:29:34 +01:00
|
|
|
):
|
2016-05-24 12:34:29 +01:00
|
|
|
if job_id is None:
|
|
|
|
|
job_id = str(generate_uuid())
|
|
|
|
|
if template_id is None:
|
2017-06-24 17:12:45 +01:00
|
|
|
template_id = "5d729fbd-239c-44ab-b498-75a985f3198f"
|
2016-05-24 12:34:29 +01:00
|
|
|
if created_at is None:
|
2016-07-18 09:05:00 +01:00
|
|
|
created_at = str(datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%S.%f%z'))
|
2016-01-29 15:35:35 +00:00
|
|
|
data = {
|
2016-04-12 14:19:51 +01:00
|
|
|
'id': job_id,
|
2016-05-24 12:34:29 +01:00
|
|
|
'service': service_id,
|
|
|
|
|
'template': template_id,
|
|
|
|
|
'template_version': template_version,
|
|
|
|
|
'original_file_name': original_file_name,
|
2016-02-22 14:52:16 +00:00
|
|
|
'created_at': created_at,
|
2016-05-24 12:34:29 +01:00
|
|
|
'notification_count': notification_count,
|
|
|
|
|
'notifications_sent': notifications_sent,
|
2016-08-30 15:13:37 +01:00
|
|
|
'notifications_requested': notifications_requested,
|
2016-08-26 09:29:34 +01:00
|
|
|
'job_status': job_status,
|
2016-09-05 14:45:34 +01:00
|
|
|
'statistics': [],
|
2016-05-24 12:34:29 +01:00
|
|
|
'created_by': created_by_json(
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
created_by['id'],
|
|
|
|
|
created_by['name'],
|
|
|
|
|
created_by['email_address'],
|
2016-08-09 10:39:57 +01:00
|
|
|
),
|
|
|
|
|
'scheduled_for': scheduled_for
|
|
|
|
|
}
|
2016-01-29 15:35:35 +00:00
|
|
|
return data
|
2016-03-02 16:15:15 +00:00
|
|
|
|
|
|
|
|
|
2016-07-11 10:49:01 +01:00
|
|
|
def notification_json(
|
|
|
|
|
service_id,
|
|
|
|
|
job=None,
|
|
|
|
|
template=None,
|
2017-10-02 12:34:10 +01:00
|
|
|
to=None,
|
2016-07-11 10:49:01 +01:00
|
|
|
status=None,
|
|
|
|
|
sent_at=None,
|
|
|
|
|
job_row_number=None,
|
|
|
|
|
created_at=None,
|
|
|
|
|
updated_at=None,
|
|
|
|
|
with_links=False,
|
2017-06-24 17:12:45 +01:00
|
|
|
rows=5,
|
|
|
|
|
personalisation=None,
|
2017-07-13 13:05:41 +01:00
|
|
|
template_type=None,
|
2018-03-19 16:12:14 +00:00
|
|
|
reply_to_text=None,
|
|
|
|
|
client_reference=None,
|
2018-09-06 14:41:55 +01:00
|
|
|
created_by_name=None,
|
2018-09-24 11:53:40 +01:00
|
|
|
postage=None,
|
2016-07-11 10:49:01 +01:00
|
|
|
):
|
2016-03-16 16:57:10 +00:00
|
|
|
if template is None:
|
2017-07-13 13:05:41 +01:00
|
|
|
template = template_json(service_id, str(generate_uuid()), type_=template_type)
|
2017-10-02 12:34:10 +01:00
|
|
|
if to is None:
|
|
|
|
|
if template_type == 'letter':
|
|
|
|
|
to = '1 Example Street'
|
|
|
|
|
elif template_type == 'email':
|
|
|
|
|
to = 'example@gov.uk'
|
|
|
|
|
else:
|
|
|
|
|
to = '07123456789'
|
2016-03-16 16:57:10 +00:00
|
|
|
if sent_at is None:
|
2016-05-11 11:20:45 +01:00
|
|
|
sent_at = str(datetime.utcnow().time())
|
2016-03-16 16:57:10 +00:00
|
|
|
if created_at is None:
|
2016-09-21 10:13:25 +01:00
|
|
|
created_at = datetime.now(timezone.utc).isoformat()
|
2016-05-10 11:36:49 +01:00
|
|
|
if updated_at is None:
|
2016-05-11 11:20:45 +01:00
|
|
|
updated_at = str((datetime.utcnow() + timedelta(minutes=1)).time())
|
2016-07-11 10:49:01 +01:00
|
|
|
if status is None:
|
|
|
|
|
status = 'delivered'
|
2016-03-16 16:57:10 +00:00
|
|
|
links = {}
|
2018-09-24 11:53:40 +01:00
|
|
|
if template_type == 'letter':
|
2018-09-28 17:08:28 +01:00
|
|
|
postage = postage or 'second'
|
2017-01-13 11:37:14 +00:00
|
|
|
|
2016-03-16 16:57:10 +00:00
|
|
|
if with_links:
|
|
|
|
|
links = {
|
2017-01-13 11:37:14 +00:00
|
|
|
'prev': '/service/{}/notifications?page=0'.format(service_id),
|
|
|
|
|
'next': '/service/{}/notifications?page=1'.format(service_id),
|
|
|
|
|
'last': '/service/{}/notifications?page=2'.format(service_id)
|
2016-03-16 16:57:10 +00:00
|
|
|
}
|
2017-01-13 11:37:14 +00:00
|
|
|
|
2016-08-22 16:25:35 +01:00
|
|
|
job_payload = None
|
|
|
|
|
if job:
|
|
|
|
|
job_payload = {'id': job['id'], 'original_file_name': job['original_file_name']}
|
|
|
|
|
|
2016-03-02 16:15:15 +00:00
|
|
|
data = {
|
|
|
|
|
'notifications': [{
|
2019-05-09 17:33:22 +01:00
|
|
|
'id': sample_uuid(),
|
2016-03-16 16:57:10 +00:00
|
|
|
'to': to,
|
2017-06-16 14:57:48 +01:00
|
|
|
'template': template,
|
2016-08-22 16:25:35 +01:00
|
|
|
'job': job_payload,
|
2016-03-16 16:57:10 +00:00
|
|
|
'sent_at': sent_at,
|
|
|
|
|
'status': status,
|
2016-05-10 11:36:49 +01:00
|
|
|
'created_at': created_at,
|
2017-06-16 14:57:48 +01:00
|
|
|
'created_by': None,
|
2016-05-16 11:53:22 +01:00
|
|
|
'updated_at': updated_at,
|
2016-05-20 16:13:01 +01:00
|
|
|
'job_row_number': job_row_number,
|
2017-06-16 14:57:48 +01:00
|
|
|
'service': service_id,
|
2017-06-24 17:12:45 +01:00
|
|
|
'template_version': template['version'],
|
|
|
|
|
'personalisation': personalisation or {},
|
2018-09-24 11:53:40 +01:00
|
|
|
'postage': postage,
|
2017-10-02 12:34:10 +01:00
|
|
|
'notification_type': template_type,
|
2018-01-03 10:44:36 +00:00
|
|
|
'reply_to_text': reply_to_text,
|
2018-03-19 16:12:14 +00:00
|
|
|
'client_reference': client_reference,
|
2018-09-06 14:41:55 +01:00
|
|
|
'created_by_name': created_by_name,
|
2016-07-11 10:49:01 +01:00
|
|
|
} for i in range(rows)],
|
2016-08-22 16:25:35 +01:00
|
|
|
'total': rows,
|
2016-04-19 11:45:36 +01:00
|
|
|
'page_size': 50,
|
2016-03-16 16:57:10 +00:00
|
|
|
'links': links
|
2016-03-02 16:15:15 +00:00
|
|
|
}
|
|
|
|
|
return data
|
2016-03-09 12:10:50 +00:00
|
|
|
|
|
|
|
|
|
2016-08-22 16:25:35 +01:00
|
|
|
def single_notification_json(
|
|
|
|
|
service_id,
|
|
|
|
|
job=None,
|
|
|
|
|
template=None,
|
|
|
|
|
status=None,
|
|
|
|
|
sent_at=None,
|
|
|
|
|
created_at=None,
|
2017-09-20 16:02:15 +01:00
|
|
|
updated_at=None,
|
|
|
|
|
notification_type='sms'
|
2016-08-22 16:25:35 +01:00
|
|
|
):
|
|
|
|
|
if template is None:
|
|
|
|
|
template = template_json(service_id, str(generate_uuid()))
|
|
|
|
|
if sent_at is None:
|
2016-08-24 12:09:38 +01:00
|
|
|
sent_at = str(datetime.utcnow())
|
2016-08-22 16:25:35 +01:00
|
|
|
if created_at is None:
|
2016-08-24 12:09:38 +01:00
|
|
|
created_at = str(datetime.utcnow())
|
2016-08-22 16:25:35 +01:00
|
|
|
if updated_at is None:
|
2016-08-24 12:09:38 +01:00
|
|
|
updated_at = str(datetime.utcnow() + timedelta(minutes=1))
|
2016-08-22 16:25:35 +01:00
|
|
|
if status is None:
|
|
|
|
|
status = 'delivered'
|
|
|
|
|
job_payload = None
|
|
|
|
|
if job:
|
|
|
|
|
job_payload = {'id': job['id'], 'original_file_name': job['original_file_name']}
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
'sent_at': sent_at,
|
2017-01-13 11:37:14 +00:00
|
|
|
'to': '07123456789',
|
2016-08-22 16:25:35 +01:00
|
|
|
'billable_units': 1,
|
|
|
|
|
'status': status,
|
|
|
|
|
'created_at': created_at,
|
|
|
|
|
'reference': None,
|
|
|
|
|
'updated_at': updated_at,
|
|
|
|
|
'template_version': 5,
|
|
|
|
|
'service': service_id,
|
|
|
|
|
'id': '29441662-17ce-4ffe-9502-fcaed73b2826',
|
|
|
|
|
'template': template,
|
|
|
|
|
'job_row_number': 0,
|
2017-09-20 16:02:15 +01:00
|
|
|
'notification_type': notification_type,
|
2016-08-22 16:25:35 +01:00
|
|
|
'api_key': None,
|
|
|
|
|
'job': job_payload,
|
|
|
|
|
'sent_by': 'mmg'
|
|
|
|
|
}
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
2016-03-09 12:10:50 +00:00
|
|
|
def validate_route_permission(mocker,
|
|
|
|
|
app_,
|
|
|
|
|
method,
|
|
|
|
|
response_code,
|
|
|
|
|
route,
|
|
|
|
|
permissions,
|
|
|
|
|
usr,
|
|
|
|
|
service):
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
usr['permissions'][str(service['id'])] = permissions
|
|
|
|
|
usr['services'] = [service['id']]
|
2016-03-09 12:10:50 +00:00
|
|
|
mocker.patch(
|
|
|
|
|
'app.user_api_client.check_verify_code',
|
|
|
|
|
return_value=(True, ''))
|
|
|
|
|
mocker.patch(
|
2016-03-17 10:46:47 +00:00
|
|
|
'app.service_api_client.get_services',
|
2016-03-09 12:10:50 +00:00
|
|
|
return_value={'data': []})
|
2016-06-01 16:07:43 +01:00
|
|
|
mocker.patch('app.service_api_client.update_service', return_value=service)
|
|
|
|
|
mocker.patch('app.service_api_client.update_service_with_properties', return_value=service)
|
2016-03-09 12:10:50 +00:00
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=usr)
|
|
|
|
|
mocker.patch('app.user_api_client.get_user_by_email', return_value=usr)
|
2016-03-17 10:46:47 +00:00
|
|
|
mocker.patch('app.service_api_client.get_service', return_value={'data': service})
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
mocker.patch('app.models.user.Users.client', return_value=[usr])
|
2018-07-30 17:40:32 +01:00
|
|
|
mocker.patch('app.job_api_client.has_jobs', return_value=False)
|
2016-03-09 12:10:50 +00:00
|
|
|
with app_.test_request_context():
|
|
|
|
|
with app_.test_client() as client:
|
|
|
|
|
client.login(usr)
|
|
|
|
|
resp = None
|
|
|
|
|
if method == 'GET':
|
|
|
|
|
resp = client.get(route)
|
|
|
|
|
elif method == 'POST':
|
|
|
|
|
resp = client.post(route)
|
|
|
|
|
else:
|
|
|
|
|
pytest.fail("Invalid method call {}".format(method))
|
|
|
|
|
if resp.status_code != response_code:
|
|
|
|
|
pytest.fail("Invalid permissions set for endpoint {}".format(route))
|
2016-03-09 13:51:56 +00:00
|
|
|
return resp
|
2017-07-03 17:21:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def validate_route_permission_with_client(mocker,
|
|
|
|
|
client,
|
|
|
|
|
method,
|
|
|
|
|
response_code,
|
|
|
|
|
route,
|
|
|
|
|
permissions,
|
|
|
|
|
usr,
|
|
|
|
|
service):
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
usr['permissions'][str(service['id'])] = permissions
|
2017-07-03 17:21:44 +01:00
|
|
|
mocker.patch(
|
|
|
|
|
'app.user_api_client.check_verify_code',
|
|
|
|
|
return_value=(True, ''))
|
|
|
|
|
mocker.patch(
|
|
|
|
|
'app.service_api_client.get_services',
|
|
|
|
|
return_value={'data': []})
|
|
|
|
|
mocker.patch('app.service_api_client.update_service', return_value=service)
|
|
|
|
|
mocker.patch('app.service_api_client.update_service_with_properties', return_value=service)
|
|
|
|
|
mocker.patch('app.user_api_client.get_user', return_value=usr)
|
|
|
|
|
mocker.patch('app.user_api_client.get_user_by_email', return_value=usr)
|
|
|
|
|
mocker.patch('app.service_api_client.get_service', return_value={'data': service})
|
|
|
|
|
mocker.patch('app.user_api_client.get_users_for_service', return_value=[usr])
|
2018-07-30 17:40:32 +01:00
|
|
|
mocker.patch('app.job_api_client.has_jobs', return_value=False)
|
2017-07-03 17:21:44 +01:00
|
|
|
client.login(usr)
|
|
|
|
|
resp = None
|
|
|
|
|
if method == 'GET':
|
|
|
|
|
resp = client.get(route)
|
|
|
|
|
elif method == 'POST':
|
|
|
|
|
resp = client.post(route)
|
|
|
|
|
else:
|
|
|
|
|
pytest.fail("Invalid method call {}".format(method))
|
|
|
|
|
if resp.status_code != response_code:
|
|
|
|
|
pytest.fail("Invalid permissions set for endpoint {}".format(route))
|
|
|
|
|
return resp
|
2019-07-15 13:52:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_url_expected(actual, expected):
|
|
|
|
|
actual_parts = urlparse(actual)
|
|
|
|
|
expected_parts = urlparse(expected)
|
|
|
|
|
for attribute in actual_parts._fields:
|
|
|
|
|
if attribute == 'query':
|
|
|
|
|
# query string ordering can be non-deterministic
|
|
|
|
|
# so we need to parse it first, which gives us a
|
|
|
|
|
# dictionary of keys and values, not a
|
|
|
|
|
# serialized string
|
|
|
|
|
assert parse_qs(
|
|
|
|
|
expected_parts.query
|
|
|
|
|
) == parse_qs(
|
|
|
|
|
actual_parts.query
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
assert getattr(
|
|
|
|
|
actual_parts, attribute
|
|
|
|
|
) == getattr(
|
|
|
|
|
expected_parts, attribute
|
|
|
|
|
), (
|
|
|
|
|
'Expected redirect: {}\n'
|
|
|
|
|
'Actual redirect: {}'
|
|
|
|
|
).format(expected, actual)
|