|
| 1 | +"""Agent contact / recovery email: the four-method management surface. |
| 2 | +
|
| 3 | +The server side is THECOLONYC-513..523 on the platform repo. The property |
| 4 | +that is easiest to regress, and the reason these tests are explicit about |
| 5 | +it: **the set/remove responses are deliberately uniform.** They say nothing |
| 6 | +about whether the address was available, because a response that differed |
| 7 | +would answer "is this address registered?" for any address a caller names. |
| 8 | +
|
| 9 | +So there is no success/failure signal to assert on for `set_email` beyond |
| 10 | +the shape — and a future contributor "helpfully" adding a |
| 11 | +`verification_sent: bool` would reintroduce exactly the enumeration leak |
| 12 | +THECOLONYC-518 closed. The mock's default state encodes the same care: an |
| 13 | +address that is attached but NOT yet verified, which is the state agents |
| 14 | +actually occupy between `set_email()` and clicking the link. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from unittest.mock import MagicMock, patch |
| 20 | + |
| 21 | +import pytest |
| 22 | +from test_api_methods import BASE, _authed_client, _last_body, _last_request, _mock_response |
| 23 | + |
| 24 | +from colony_sdk import AsyncColonyClient, ColonyClient |
| 25 | +from colony_sdk.testing import MockColonyClient |
| 26 | + |
| 27 | + |
| 28 | +class TestSyncEmailMethods: |
| 29 | + """The four management endpoints: path, verb, and body.""" |
| 30 | + |
| 31 | + @patch("colony_sdk.client.urlopen") |
| 32 | + def test_get_email_targets_the_right_endpoint(self, mock_urlopen: MagicMock) -> None: |
| 33 | + mock_urlopen.return_value = _mock_response({"email": "a@example.com", "email_verified": True}) |
| 34 | + result = _authed_client().get_email() |
| 35 | + |
| 36 | + req = _last_request(mock_urlopen) |
| 37 | + assert req.get_method() == "GET" |
| 38 | + assert req.full_url == f"{BASE}/auth/email" |
| 39 | + assert result["email_verified"] is True |
| 40 | + |
| 41 | + @patch("colony_sdk.client.urlopen") |
| 42 | + def test_set_email_posts_the_address(self, mock_urlopen: MagicMock) -> None: |
| 43 | + mock_urlopen.return_value = _mock_response( |
| 44 | + { |
| 45 | + "status": "verification_pending", |
| 46 | + "email": "a@example.com", |
| 47 | + "message": "If that address is available, ...", |
| 48 | + } |
| 49 | + ) |
| 50 | + result = _authed_client().set_email("a@example.com") |
| 51 | + |
| 52 | + req = _last_request(mock_urlopen) |
| 53 | + assert req.get_method() == "POST" |
| 54 | + assert req.full_url == f"{BASE}/auth/email" |
| 55 | + assert _last_body(mock_urlopen) == {"email": "a@example.com"} |
| 56 | + assert result["status"] == "verification_pending" |
| 57 | + |
| 58 | + @patch("colony_sdk.client.urlopen") |
| 59 | + def test_remove_email_uses_DELETE(self, mock_urlopen: MagicMock) -> None: |
| 60 | + mock_urlopen.return_value = _mock_response({"status": "removed", "message": "..."}) |
| 61 | + _authed_client().remove_email() |
| 62 | + |
| 63 | + req = _last_request(mock_urlopen) |
| 64 | + assert req.get_method() == "DELETE" |
| 65 | + assert req.full_url == f"{BASE}/auth/email" |
| 66 | + |
| 67 | + @patch("colony_sdk.client.urlopen") |
| 68 | + def test_verify_email_posts_the_token(self, mock_urlopen: MagicMock) -> None: |
| 69 | + mock_urlopen.return_value = _mock_response({"status": "verified", "email": "a@example.com"}) |
| 70 | + _authed_client().verify_email("tok-abc") |
| 71 | + |
| 72 | + req = _last_request(mock_urlopen) |
| 73 | + assert req.get_method() == "POST" |
| 74 | + assert req.full_url == f"{BASE}/auth/email/verify" |
| 75 | + assert _last_body(mock_urlopen) == {"token": "tok-abc"} |
| 76 | + |
| 77 | + @patch("colony_sdk.client.urlopen") |
| 78 | + def test_set_email_carries_no_availability_signal(self, mock_urlopen: MagicMock) -> None: |
| 79 | + """The enumeration property, asserted as a contract. |
| 80 | +
|
| 81 | + ``verification_sent`` was REMOVED in THECOLONYC-518 precisely |
| 82 | + because reporting whether mail went out answers "is this address |
| 83 | + taken?". If a future change reinstates any such field, this fails |
| 84 | + and the reviewer gets the reason rather than a merge conflict. |
| 85 | + """ |
| 86 | + mock_urlopen.return_value = _mock_response( |
| 87 | + { |
| 88 | + "status": "verification_pending", |
| 89 | + "email": "a@example.com", |
| 90 | + "message": "...", |
| 91 | + } |
| 92 | + ) |
| 93 | + result = _authed_client().set_email("a@example.com") |
| 94 | + |
| 95 | + for leaky in ("verification_sent", "available", "already_taken", "exists"): |
| 96 | + assert leaky not in result, ( |
| 97 | + f"{leaky!r} in the set_email response is an enumeration oracle: " |
| 98 | + "it tells a caller whether an address they do not own is " |
| 99 | + "registered. See THECOLONYC-518." |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.asyncio |
| 104 | +class TestAsyncEmailMethods: |
| 105 | + async def test_async_surface_matches_the_sync_one(self) -> None: |
| 106 | + """Both clients must speak the same four methods. |
| 107 | +
|
| 108 | + A method added to one and forgotten on the other is the recurring |
| 109 | + drift in this SDK, so pin it structurally rather than by writing |
| 110 | + four near-identical request tests. |
| 111 | + """ |
| 112 | + for name in ("get_email", "set_email", "remove_email", "verify_email"): |
| 113 | + assert hasattr(ColonyClient, name), f"sync client missing {name}" |
| 114 | + assert hasattr(AsyncColonyClient, name), f"async client missing {name}" |
| 115 | + |
| 116 | + |
| 117 | +class TestMockClient: |
| 118 | + def test_mock_exposes_all_four(self) -> None: |
| 119 | + mock = MockColonyClient() |
| 120 | + assert mock.get_email()["email_verified"] is False |
| 121 | + assert mock.set_email("x@example.com")["status"] == "verification_pending" |
| 122 | + assert mock.remove_email()["status"] == "removed" |
| 123 | + assert mock.verify_email("tok")["status"] == "verified" |
| 124 | + |
| 125 | + def test_mock_defaults_to_UNVERIFIED(self) -> None: |
| 126 | + """Deliberate: the window between set_email() and redeeming the link. |
| 127 | +
|
| 128 | + Defaulting to verified=True would let a caller ship code that never |
| 129 | + checks the flag and only discovers the gap against a real server. |
| 130 | + """ |
| 131 | + assert MockColonyClient().get_email()["email_verified"] is False |
| 132 | + |
| 133 | + def test_mock_records_the_call_arguments(self) -> None: |
| 134 | + mock = MockColonyClient() |
| 135 | + mock.set_email("recorded@example.com") |
| 136 | + mock.verify_email("recorded-token") |
| 137 | + calls = {c[0]: c[1] for c in mock.calls} |
| 138 | + assert calls["set_email"] == {"email": "recorded@example.com"} |
| 139 | + assert calls["verify_email"] == {"token": "recorded-token"} |
0 commit comments