File size: 8,095 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Integration Tests

End-to-end tests for the httpSMS API. The suite runs the API and its data
stores in Docker, keeps the existing Firebase/WireMock coverage, and adds a
standard-library-only HTTPS adapter emulator for URL-backed phone gateways.

## Architecture

```text
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚    API (Go)      β”‚
                              β”‚    Port 8000     β”‚
                              β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β”‚
                FCM HTTP              β”‚ HTTPS callbacks
           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
           β”‚ WireMock         β”‚β—€β”€β”€β”€β”€β”€β”€β”˜       β”‚ Adapter emulator     β”‚
           β”‚ Port 8080        β”‚               β”‚ HTTPS callback :9091β”‚
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚ HTTP control   :9092β”‚
                                              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                         β”‚ phone API calls
                                                         └──────────▢ API

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        HTTP        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Test runner      │───────────────────▢│ API, WireMock, and adapter β”‚
β”‚ Go test on host  β”‚                    β”‚ control endpoints          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Data stores: CockroachDB, Redis, and MongoDB.
```

### Components

| Component | Description |
| --- | --- |
| **API** | The httpSMS Go API server |
| **WireMock** | Existing fake Firebase and webhook endpoints |
| **Adapter emulator** | HTTPS URL-backed phone gateway with an HTTP-only host control API |
| **CockroachDB** | Relational database for API data |
| **Redis** | Cache and local event queue backend |
| **MongoDB** | Heartbeat and contact backend |
| **Seed** | One-shot container that inserts integration users and API keys |
| **Test runner** | Go tests running on the host |

### Gateway Flows

1. **Existing FCM flow:** the API sends Firebase-compatible requests to
   WireMock. Existing tests fetch outstanding messages and submit phone events
   without changing their transport.
2. **URL-backed outgoing flow:** the API posts an FCM-compatible envelope to
   `https://adapter-emulator:9091/notifications/{gatewayID}`. The adapter uses
   its registered phone API key to fetch the outstanding message and post
   `SENT` followed by `DELIVERED`.
3. **URL-backed incoming flow:** the test calls the adapter control API on
   host port `9092`; the adapter posts `/v1/messages/receive` as the registered
   phone.
4. **Heartbeat wake-up:** the test dispatches `phone.heartbeat.missed` through
   `/v1/events`. The API sends an HTTPS callback containing
   `KEY_HEARTBEAT_ID`, and the adapter posts `/v1/heartbeats`.

The HTTPS endpoint uses a two-day throwaway CA and server certificate with the
DNS SAN `adapter-emulator`. The API container trusts only that generated CA via
`SSL_CERT_FILE`; HTTPS verification is never bypassed. The notification sender
uses the standard OpenTelemetry-instrumented Go HTTP transport.

## Test Coverage

- [x] Existing encrypted send/receive phone scenarios through WireMock
- [x] Existing rate-limit, webhook, contacts, bulk, and thread scenarios
- [x] URL-backed outgoing message reaches `delivered`
- [x] URL-backed incoming message reaches `received`
- [x] URL-backed heartbeat callback stores a heartbeat
- [x] Adapter callback notification IDs are deduplicated in memory
- [x] HTTPS certificate trust is exercised

## Prerequisites

- [Docker](https://docs.docker.com/get-docker/) with Docker Compose
- [Go 1.25+](https://go.dev/dl/)
- [jq](https://jqlang.github.io/jq/download/)
- [OpenSSL](https://www.openssl.org/)

On Windows, the scripts can be run with Git Bash, for example
`C:\Program Files\Git\bin\bash.exe`.

## Running Locally

### 1. Generate throwaway credentials and certificates

Run both scripts before starting Docker:

```bash
cd tests
bash generate-firebase-credentials.sh firebase-credentials.json
bash generate-adapter-certificates.sh certs
export FIREBASE_CREDENTIALS=$(jq -c . firebase-credentials.json)
```

The generated Firebase credential and the complete `certs/` directory are
ignored by Git.

### 2. Start the stack and wait for seeding

```bash
docker compose up -d --build --wait
docker compose wait seed
sleep 2
```

### 3. Run the complete suite

```bash
go test -v -timeout 300s ./...
```

### 4. Tear down

```bash
docker compose down -v
```

### One-liner

```bash
cd tests && \
  bash generate-firebase-credentials.sh firebase-credentials.json && \
  bash generate-adapter-certificates.sh certs && \
  export FIREBASE_CREDENTIALS=$(jq -c . firebase-credentials.json) && \
  docker compose up -d --build --wait && \
  docker compose wait seed && \
  sleep 2 && \
  go test -v -timeout 300s ./... ; \
  docker compose down -v
```

## CI/CD

`.github/workflows/api.yml` generates both the fake Firebase credential and
the adapter CA/server certificate before building the Compose stack. The
workflow runs API handler integration tests and this complete host-side suite,
collects service logs on failure, and always tears the stack down.

## Test Data

| Entity | Value |
| --- | --- |
| User API key | `test-user-api-key` |
| System API key | `system-user-api-key` |
| User ID | `test-user-id` |
| System user ID | `system-user-id` |

Adapter tests create a unique gateway UUID, phone number, phone API key, and
callback path per test. See [`seed.sql`](./seed.sql) for shared seed data.

## Project Structure

```text
tests/
β”œβ”€β”€ adapter-emulator/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ go.mod
β”‚   β”œβ”€β”€ main.go
β”‚   β”œβ”€β”€ emulator.go
β”‚   β”œβ”€β”€ api_client.go
β”‚   β”œβ”€β”€ notification_handler.go
β”‚   β”œβ”€β”€ control_handler.go
β”‚   └── emulator_test.go
β”œβ”€β”€ wiremock/
β”‚   └── mappings/
β”œβ”€β”€ adapter_integration_test.go
β”œβ”€β”€ integration_test.go
β”œβ”€β”€ helpers_test.go
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ .env.test
β”œβ”€β”€ seed.sql
β”œβ”€β”€ generate-firebase-credentials.sh
β”œβ”€β”€ generate-adapter-certificates.sh
β”œβ”€β”€ go.mod
└── go.sum
```

## Troubleshooting

### API or adapter fails to start

```bash
docker compose logs --tail 200 api adapter-emulator
```

Confirm `tests/certs/ca.pem`, `server.pem`, and `server-key.pem` exist. TLS
errors should be fixed by regenerating certificates; do not disable HTTPS
verification.

### URL-backed outgoing message times out

```bash
docker compose logs --tail 200 api adapter-emulator
```

Adapter logs should show callback receipt, the outstanding-message fetch,
`SENT`, and `DELIVERED`. Confirm `SSL_CERT_FILE=/adapter-certs/ca.pem` is
present in the API container.

### URL-backed incoming message times out

Adapter logs should show the control request followed by a call to
`/v1/messages/receive`. The gateway registration contains the per-test phone
number and phone API key.

### Heartbeat callback times out

API logs should show `phone.heartbeat.missed`. Adapter logs should show
`KEY_HEARTBEAT_ID` followed by a successful heartbeat POST.

### Existing FCM scenario times out

```bash
docker compose logs --tail 200 api wiremock
```

Keep `FCM_ENDPOINT=http://wiremock:8080`; the adapter service does not replace
or weaken the WireMock phone tests.

### Seed container fails

```bash
docker compose logs seed
```

If a relation does not exist, inspect API migration/startup logs before
increasing health-check timing.