# Real check: unsubscribe and bounces

The tests use a fake Gmail, so the one-click unsubscribe from Gmail and a real bounce have to be
checked once (HUB-171). Run it on staging.

## What you need

- `https://<API_URL host>/u/x` answers the "This link is not valid" page from the internet (see
  `deploy/README.md`, "Unsubscribe links").
- The business's postal address in the settings: `PATCH /api/v1/business/settings` with
  `addressLine1`, `city`, `state`, `postalCode`, `country`.
- A connected Gmail mailbox (see `docs/gmail-real-send-check.md`), and a second Gmail address you
  can read. Make the second address a contact of the business.
- Database access.

## Steps

1. **Send with an unsubscribe link.** `POST /api/v1/inbox/send`:

   ```json
   {
     "to": ["<second address>"],
     "subject": "Unsubscribe check <today>",
     "bodyHtml": "<p>Hello from the CRM.</p>",
     "unsubscribeLink": true,
     "idempotencyKey": "unsubscribe-check-0001"
   }
   ```

2. **The email as received.** Open it as the second address. The footer shows the business name,
   the postal address and "Unsubscribe". Show original: DKIM is `PASS`, and the headers are

   ```
   List-Unsubscribe: <https://<API_URL host>/u/...>
   List-Unsubscribe-Post: List-Unsubscribe=One-Click
   ```

3. **Gmail's one-click.** Gmail shows "Unsubscribe" next to the sender's name (it can take Gmail a
   while to show it for a new sender; if it isn't there, use the footer link for step 4 and note
   it in the result). Click it and confirm. Within a minute:

   ```sql
   SELECT reason, detail, created_at FROM email_suppressions
   WHERE business_id = '<business id>' AND email = '<second address>';
   ```

   One row: `unsubscribed`, `One-click unsubscribe from the mail app`. The contact
   (`GET /api/v1/contacts/<id>`) has `emailStatus: "unsubscribed"` and `unsubscribedAt`.

4. **The page.** Open the footer's "Unsubscribe" link in a browser: it says you are unsubscribed.
   Remove the address (`DELETE /api/v1/email-suppressions/<id>`), open the link again: the page
   asks first and nothing is stored until you press "Unsubscribe" (pick a reason). Then the row is
   back with that reason.

5. **A warning.** Send a normal email to the second address: it is sent, and the response has
   `warnings: [{ "email": "<second address>", "status": "unsubscribed" }]`.

6. **A hard bounce.** Make an address that does not exist a contact, e.g.
   `no-such-user-<today>@gmail.com`, and send it an email from the CRM. Gmail's "Address not
   found" notice arrives in the sender's inbox within minutes. After the next sync (up to a
   minute):

   ```sql
   SELECT id, bounced_at FROM email_messages WHERE id = '<sent message id>';
   SELECT bounce_of_message_id FROM email_messages WHERE subject LIKE 'Delivery Status%'
   ORDER BY created_at DESC LIMIT 1;
   SELECT reason, detail FROM email_suppressions WHERE email = '<that address>';
   ```

   `bounced_at` is set, the notice points at the sent email, and the address is `bounced`; the
   contact shows `emailStatus: "bounced"`.

   Only verified notices suppress (a forged one is linked to the email but changes nothing).
   In Gmail, "Show original" of the notice: the topmost `Authentication-Results: mx.google.com`
   header has `dkim=pass header.i=@googlemail.com` and `dmarc=pass header.from=googlemail.com`.
   If the address was not suppressed, look for `unverified` in the worker's
   `delivery failure notice` log line and attach the notice's headers to the ticket.

7. **A soft bounce.** A refusal by policy (e.g. `550 5.7.1` spam / blocked) or a full mailbox
   (`5.2.2`) does not suppress the address: the notice is linked to the email, `bounced_at`
   stays empty and the contact stays `ok`.

## Result

Fill this in and attach a screenshot of step 3 to the ticket (HUB-171).

|                                                         |                     |
| ------------------------------------------------------- | ------------------- |
| Date, environment                                       |                     |
| Who ran it                                              |                     |
| Footer with address, both headers, DKIM pass (2)?       |                     |
| Gmail's Unsubscribe shown and recorded in a minute (3)? |                     |
| Page asks first, then records the reason (4)?           |                     |
| Warning on a 1:1 email (5)?                             |                     |
| Hard bounce linked and suppressed (6)?                  |                     |
| Notice verified by Gmail (6)?                           |                     |
| Screenshot                                              | attached to HUB-171 |
