2017-02-17 14:06:09 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
import pytest
|
2018-02-20 11:22:17 +00:00
|
|
|
from flask import url_for
|
2016-01-21 12:31:09 +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
|
2023-11-14 07:51:56 -08:00
|
|
|
from tests.conftest import SERVICE_ONE_ID, normalize_spaces
|
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
|
|
|
|
2015-11-27 09:47:29 +00:00
|
|
|
|
2024-07-11 09:38:32 -07:00
|
|
|
def test_render_sign_in_template_for_new_user(client_request, mocker):
|
|
|
|
|
|
|
|
|
|
mocker.patch("app.notify_client.user_api_client.UserApiClient.deactivate_user")
|
2019-07-16 17:01:54 +01:00
|
|
|
client_request.logout()
|
2023-08-25 09:12:23 -07:00
|
|
|
page = client_request.get("main.sign_in")
|
|
|
|
|
assert normalize_spaces(page.select_one("h1").text) == "Sign in"
|
2024-04-15 15:50:42 -04:00
|
|
|
assert (
|
|
|
|
|
page.select("main p")[0].text
|
|
|
|
|
== "Access your Notify.gov account by signing in with Login.gov:"
|
|
|
|
|
)
|
2024-02-26 17:59:18 -05:00
|
|
|
# TODO: Fix this test to be less brittle! If the Login.gov link is enabled,
|
|
|
|
|
# then these indices need to be 1 instead of 0.
|
2024-02-26 18:07:26 -05:00
|
|
|
# Currently it's not enabled for the test or production environments.
|
2024-04-15 15:50:42 -04:00
|
|
|
assert page.select("main a")[0].text == "Sign in with Login.gov"
|
2024-03-19 15:53:38 -04:00
|
|
|
|
|
|
|
|
# TODO: We'll have to adjust this depending on whether Login.gov is
|
|
|
|
|
# enabled or not; fix this in the future.
|
2023-08-25 09:12:23 -07:00
|
|
|
assert "Sign in again" not in normalize_spaces(page.text)
|
2017-02-17 14:06:09 +00:00
|
|
|
|
|
|
|
|
|
2024-07-11 09:38:32 -07:00
|
|
|
def test_sign_in_explains_session_timeout(client_request, mocker):
|
|
|
|
|
|
|
|
|
|
mocker.patch("app.notify_client.user_api_client.UserApiClient.deactivate_user")
|
2022-01-04 15:40:42 +00:00
|
|
|
client_request.logout()
|
2023-08-25 09:12:23 -07:00
|
|
|
page = client_request.get("main.sign_in", next="/foo")
|
|
|
|
|
assert (
|
|
|
|
|
"We signed you out because you have not used Notify for a while." in page.text
|
|
|
|
|
)
|
2017-02-17 14:06:09 +00:00
|
|
|
|
|
|
|
|
|
2018-03-08 16:51:53 +00:00
|
|
|
def test_doesnt_redirect_to_sign_in_if_no_session_info(
|
2019-04-18 12:44:18 +01:00
|
|
|
client_request,
|
|
|
|
|
api_user_active,
|
2023-07-12 12:09:44 -04:00
|
|
|
mock_get_organization_by_domain,
|
2018-03-08 16:51:53 +00:00
|
|
|
):
|
2023-08-25 09:12:23 -07:00
|
|
|
api_user_active["current_session_id"] = str(uuid.UUID(int=1))
|
2018-03-08 16:51:53 +00:00
|
|
|
|
2019-03-26 12:35:32 +00:00
|
|
|
with client_request.session_transaction() as session:
|
2023-08-25 09:12:23 -07:00
|
|
|
session["current_session_id"] = None
|
|
|
|
|
|
2023-11-08 07:51:57 -08:00
|
|
|
with client_request.session_transaction() as session:
|
|
|
|
|
session["current_session_id"] = None
|
|
|
|
|
|
|
|
|
|
client_request.get("main.add_service")
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
def test_logged_in_user_redirects_to_account(client_request):
|
2018-11-15 15:38:43 +00:00
|
|
|
client_request.get(
|
2023-08-25 09:12:23 -07:00
|
|
|
"main.sign_in",
|
2018-11-15 15:38:43 +00:00
|
|
|
_expected_status=302,
|
2023-08-25 09:12:23 -07:00
|
|
|
_expected_redirect=url_for("main.show_accounts_or_dashboard"),
|
2018-11-15 15:38:43 +00:00
|
|
|
)
|
2016-01-22 17:24:14 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
def test_logged_in_user_redirects_to_next_url(client_request):
|
2021-07-14 23:10:49 +01:00
|
|
|
client_request.get(
|
2023-08-25 09:12:23 -07:00
|
|
|
"main.sign_in",
|
|
|
|
|
next="/user-profile",
|
2021-07-14 23:10:49 +01:00
|
|
|
_expected_status=302,
|
2023-08-25 09:12:23 -07:00
|
|
|
_expected_redirect=url_for("main.user_profile"),
|
2021-07-14 23:10:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
def test_logged_in_user_doesnt_do_evil_redirect(client_request):
|
2021-07-14 23:10:49 +01:00
|
|
|
client_request.get(
|
2023-08-25 09:12:23 -07:00
|
|
|
"main.sign_in",
|
|
|
|
|
next="http://www.evil.com",
|
2021-07-14 23:10:49 +01:00
|
|
|
_expected_status=302,
|
2023-08-25 09:12:23 -07:00
|
|
|
_expected_redirect=url_for("main.show_accounts_or_dashboard"),
|
2021-07-14 23:10:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-05-06 10:43:01 -07:00
|
|
|
@pytest.mark.skip("TODO is this still relevant post login.gov switch?")
|
2017-02-03 10:42:01 +00:00
|
|
|
def test_should_return_redirect_when_user_is_pending(
|
2022-01-04 15:40:42 +00:00
|
|
|
client_request,
|
2017-02-03 10:42:01 +00:00
|
|
|
mock_get_user_by_email_pending,
|
2021-06-10 19:07:35 +01:00
|
|
|
api_user_pending,
|
2017-02-03 10:42:01 +00:00
|
|
|
mock_verify_password,
|
2024-07-11 09:38:32 -07:00
|
|
|
mocker,
|
2017-02-03 10:42:01 +00:00
|
|
|
):
|
2024-07-11 09:38:32 -07:00
|
|
|
|
|
|
|
|
mocker.patch("app.notify_client.user_api_client.UserApiClient.deactivate_user")
|
2022-01-04 15:40:42 +00:00
|
|
|
client_request.logout()
|
|
|
|
|
client_request.post(
|
2023-08-25 09:12:23 -07:00
|
|
|
"main.sign_in",
|
2022-01-04 15:40:42 +00:00
|
|
|
_data={
|
2023-08-25 09:12:23 -07:00
|
|
|
"email_address": "pending_user@example.gsa.gov",
|
|
|
|
|
"password": "val1dPassw0rd!",
|
2022-01-04 15:40:42 +00:00
|
|
|
},
|
2023-08-25 09:12:23 -07:00
|
|
|
_expected_redirect=url_for("main.resend_email_verification"),
|
2021-06-10 19:07:35 +01:00
|
|
|
)
|
2022-01-04 15:40:42 +00:00
|
|
|
with client_request.session_transaction() as s:
|
2023-08-25 09:12:23 -07:00
|
|
|
assert s["user_details"] == {
|
|
|
|
|
"email": api_user_pending["email_address"],
|
|
|
|
|
"id": api_user_pending["id"],
|
2021-06-10 19:07:35 +01:00
|
|
|
}
|
2016-09-06 15:44:33 +01:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"redirect_url",
|
|
|
|
|
[
|
|
|
|
|
None,
|
|
|
|
|
f"/services/{SERVICE_ONE_ID}/templates",
|
|
|
|
|
],
|
|
|
|
|
)
|
2024-05-06 10:43:01 -07:00
|
|
|
@pytest.mark.skip("TODO is this still relevant post login.gov switch?")
|
2017-02-03 10:42:01 +00:00
|
|
|
def test_should_attempt_redirect_when_user_is_pending(
|
2024-07-11 09:38:32 -07:00
|
|
|
client_request,
|
|
|
|
|
mock_get_user_by_email_pending,
|
|
|
|
|
mock_verify_password,
|
|
|
|
|
redirect_url,
|
|
|
|
|
mocker,
|
2017-02-03 10:42:01 +00:00
|
|
|
):
|
2024-07-11 09:38:32 -07:00
|
|
|
|
|
|
|
|
mocker.patch("app.notify_client.user_api_client.UserApiClient.deactivate_user")
|
2022-01-04 15:40:42 +00:00
|
|
|
client_request.logout()
|
|
|
|
|
client_request.post(
|
2023-08-25 09:12:23 -07:00
|
|
|
"main.sign_in",
|
2022-01-04 15:40:42 +00:00
|
|
|
next=redirect_url,
|
|
|
|
|
_data={
|
2023-08-25 09:12:23 -07:00
|
|
|
"email_address": "pending_user@example.gsa.gov",
|
|
|
|
|
"password": "val1dPassw0rd!",
|
2022-01-04 15:40:42 +00:00
|
|
|
},
|
2023-08-25 09:12:23 -07:00
|
|
|
_expected_redirect=url_for("main.resend_email_verification", next=redirect_url),
|
2022-01-04 15:40:42 +00:00
|
|
|
)
|
2017-12-21 16:42:16 +00:00
|
|
|
|
|
|
|
|
|
2024-05-06 10:43:01 -07:00
|
|
|
@pytest.mark.skip("TODO move this to register and update with login.gov")
|
2021-07-02 18:11:53 +01:00
|
|
|
def test_when_signing_in_as_invited_user_you_cannot_accept_an_invite_for_another_email_address(
|
|
|
|
|
client_request,
|
|
|
|
|
mocker,
|
|
|
|
|
mock_verify_password,
|
|
|
|
|
api_user_active,
|
|
|
|
|
sample_invite,
|
|
|
|
|
mock_accept_invite,
|
|
|
|
|
mock_send_verify_code,
|
|
|
|
|
mock_get_invited_user_by_id,
|
|
|
|
|
):
|
2024-07-11 09:38:32 -07:00
|
|
|
|
|
|
|
|
mocker.patch("app.notify_client.user_api_client.UserApiClient.deactivate_user")
|
2023-08-25 09:12:23 -07:00
|
|
|
sample_invite["email_address"] = "some_other_user@user.gsa.gov"
|
2021-07-02 18:11:53 +01:00
|
|
|
|
|
|
|
|
mocker.patch(
|
2023-08-25 09:12:23 -07:00
|
|
|
"app.models.user.User.from_email_address_and_password_or_none",
|
2021-07-02 18:11:53 +01:00
|
|
|
return_value=User(api_user_active),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
client_request.logout()
|
|
|
|
|
|
|
|
|
|
with client_request.session_transaction() as session:
|
2023-08-25 09:12:23 -07:00
|
|
|
session["invited_user_id"] = sample_invite["id"]
|
2021-07-02 18:11:53 +01:00
|
|
|
|
|
|
|
|
page = client_request.post(
|
2023-08-25 09:12:23 -07:00
|
|
|
"main.sign_in",
|
|
|
|
|
_data={"email_address": "test@user.gsa.gov", "password": "val1dPassw0rd!"},
|
|
|
|
|
_expected_status=403,
|
2021-07-02 18:11:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert mock_accept_invite.called is False
|
|
|
|
|
assert mock_send_verify_code.called is False
|
2023-08-25 09:12:23 -07:00
|
|
|
assert (
|
|
|
|
|
page.select_one(".banner-dangerous").text.strip()
|
|
|
|
|
== "You cannot accept an invite for another person."
|
|
|
|
|
)
|