|
| 1 | +/** |
| 2 | + * Step definitions for internal-api.feature. |
| 3 | + * |
| 4 | + * All scenarios are pure HTTP — no browser needed. Steps call the |
| 5 | + * /_internal/* endpoints on pds-core directly using the shared secret. |
| 6 | + * |
| 7 | + * Note: Scenario "PAR login hint retrieval" is tagged @manual — it requires |
| 8 | + * a real PKCE PAR request. Implement when PAR submission steps exist |
| 9 | + * (needed for login-hint-resolution.feature too). |
| 10 | + */ |
| 11 | + |
| 12 | +import { Given, When, Then } from '@cucumber/cucumber' |
| 13 | +import type { EpdsWorld } from '../support/world.js' |
| 14 | +import { callInternalApi } from '../support/utils.js' |
| 15 | + |
| 16 | +// --------------------------------------------------------------------------- |
| 17 | +// Background |
| 18 | +// --------------------------------------------------------------------------- |
| 19 | + |
| 20 | +// The internalSecret guard also prevents account-creation Givens from running |
| 21 | +// in scenarios that require the secret — if the secret is absent the entire |
| 22 | +// scenario is marked pending before createAccountViaOAuth is called. |
| 23 | +Given('the internal API secret is configured', function (this: EpdsWorld) { |
| 24 | + return this.skipIfNoInternalSecret() |
| 25 | +}) |
| 26 | + |
| 27 | +// --------------------------------------------------------------------------- |
| 28 | +// Account-by-email |
| 29 | +// --------------------------------------------------------------------------- |
| 30 | + |
| 31 | +When( |
| 32 | + 'the internal API is queried for the account by email', |
| 33 | + async function (this: EpdsWorld) { |
| 34 | + if (!this.testEmail) |
| 35 | + throw new Error('No testEmail — account creation step must run first') |
| 36 | + const { status, body } = await callInternalApi( |
| 37 | + `/_internal/account-by-email?email=${encodeURIComponent(this.testEmail)}`, |
| 38 | + this.env.internalSecret, |
| 39 | + ) |
| 40 | + this.lastHttpStatus = status |
| 41 | + this.lastApiResponse = body |
| 42 | + }, |
| 43 | +) |
| 44 | + |
| 45 | +Then("the response contains the account's DID", function (this: EpdsWorld) { |
| 46 | + if (!this.userDid) |
| 47 | + throw new Error('No userDid — account creation step must run first') |
| 48 | + if (!this.lastApiResponse) |
| 49 | + throw new Error('No API response — query step must run first') |
| 50 | + const did = this.lastApiResponse.did |
| 51 | + if (did !== this.userDid) { |
| 52 | + throw new Error(`Expected DID "${this.userDid}" but got "${String(did)}"`) |
| 53 | + } |
| 54 | +}) |
| 55 | + |
| 56 | +When( |
| 57 | + 'the internal API is queried for account-by-email with an unknown address', |
| 58 | + async function (this: EpdsWorld) { |
| 59 | + const unknown = `unknown-${Date.now()}@example.com` |
| 60 | + const { status, body } = await callInternalApi( |
| 61 | + `/_internal/account-by-email?email=${encodeURIComponent(unknown)}`, |
| 62 | + this.env.internalSecret, |
| 63 | + ) |
| 64 | + this.lastHttpStatus = status |
| 65 | + this.lastApiResponse = body |
| 66 | + }, |
| 67 | +) |
| 68 | + |
| 69 | +Then('the response status is 200', function (this: EpdsWorld) { |
| 70 | + if (this.lastHttpStatus !== 200) { |
| 71 | + throw new Error( |
| 72 | + `Expected status 200 but got ${String(this.lastHttpStatus)}`, |
| 73 | + ) |
| 74 | + } |
| 75 | +}) |
| 76 | + |
| 77 | +Then('the response body has a null DID', function (this: EpdsWorld) { |
| 78 | + if (!this.lastApiResponse) |
| 79 | + throw new Error('No API response — query step must run first') |
| 80 | + if (this.lastApiResponse.did !== null) { |
| 81 | + throw new Error( |
| 82 | + `Expected did to be null but got "${String(this.lastApiResponse.did)}"`, |
| 83 | + ) |
| 84 | + } |
| 85 | +}) |
| 86 | + |
| 87 | +// --------------------------------------------------------------------------- |
| 88 | +// Account-by-handle |
| 89 | +// --------------------------------------------------------------------------- |
| 90 | + |
| 91 | +When( |
| 92 | + 'the internal API is queried for the account by handle', |
| 93 | + async function (this: EpdsWorld) { |
| 94 | + if (!this.userHandle) |
| 95 | + throw new Error('No userHandle — account creation step must run first') |
| 96 | + const { status, body } = await callInternalApi( |
| 97 | + `/_internal/account-by-handle?handle=${encodeURIComponent(this.userHandle)}`, |
| 98 | + this.env.internalSecret, |
| 99 | + ) |
| 100 | + this.lastHttpStatus = status |
| 101 | + this.lastApiResponse = body |
| 102 | + }, |
| 103 | +) |
| 104 | + |
| 105 | +Then("the response contains the account's email", function (this: EpdsWorld) { |
| 106 | + if (!this.testEmail) |
| 107 | + throw new Error('No testEmail — account creation step must run first') |
| 108 | + if (!this.lastApiResponse) |
| 109 | + throw new Error('No API response — query step must run first') |
| 110 | + const email = this.lastApiResponse.email |
| 111 | + if (email !== this.testEmail) { |
| 112 | + throw new Error( |
| 113 | + `Expected email "${this.testEmail}" but got "${String(email)}"`, |
| 114 | + ) |
| 115 | + } |
| 116 | +}) |
| 117 | + |
| 118 | +// --------------------------------------------------------------------------- |
| 119 | +// Check-handle |
| 120 | +// --------------------------------------------------------------------------- |
| 121 | + |
| 122 | +When( |
| 123 | + 'the internal API is queried to check if the handle exists', |
| 124 | + async function (this: EpdsWorld) { |
| 125 | + if (!this.userHandle) |
| 126 | + throw new Error('No userHandle — account creation step must run first') |
| 127 | + const { status, body } = await callInternalApi( |
| 128 | + `/_internal/check-handle?handle=${encodeURIComponent(this.userHandle)}`, |
| 129 | + this.env.internalSecret, |
| 130 | + ) |
| 131 | + this.lastHttpStatus = status |
| 132 | + this.lastApiResponse = body |
| 133 | + }, |
| 134 | +) |
| 135 | + |
| 136 | +Then('the response indicates the handle exists', function (this: EpdsWorld) { |
| 137 | + if (!this.lastApiResponse) |
| 138 | + throw new Error('No API response — query step must run first') |
| 139 | + if (this.lastApiResponse.exists !== true) { |
| 140 | + throw new Error( |
| 141 | + `Expected exists to be true but got "${String(this.lastApiResponse.exists)}"`, |
| 142 | + ) |
| 143 | + } |
| 144 | +}) |
| 145 | + |
| 146 | +// --------------------------------------------------------------------------- |
| 147 | +// Auth errors |
| 148 | +// --------------------------------------------------------------------------- |
| 149 | + |
| 150 | +When( |
| 151 | + 'the check-handle endpoint is called with an incorrect secret', |
| 152 | + async function (this: EpdsWorld) { |
| 153 | + const { status, body } = await callInternalApi( |
| 154 | + `/_internal/check-handle?handle=somehandle.pds.test`, |
| 155 | + 'definitely-wrong-secret', |
| 156 | + ) |
| 157 | + this.lastHttpStatus = status |
| 158 | + this.lastApiResponse = body |
| 159 | + }, |
| 160 | +) |
| 161 | + |
| 162 | +When( |
| 163 | + 'an internal endpoint is called without the secret header', |
| 164 | + async function (this: EpdsWorld) { |
| 165 | + const { status, body } = await callInternalApi( |
| 166 | + `/_internal/account-by-email?email=test@example.com`, |
| 167 | + null, |
| 168 | + ) |
| 169 | + this.lastHttpStatus = status |
| 170 | + this.lastApiResponse = body |
| 171 | + }, |
| 172 | +) |
| 173 | + |
| 174 | +When( |
| 175 | + 'an internal endpoint is called with an incorrect secret', |
| 176 | + async function (this: EpdsWorld) { |
| 177 | + const { status, body } = await callInternalApi( |
| 178 | + `/_internal/account-by-email?email=test@example.com`, |
| 179 | + 'definitely-wrong-secret', |
| 180 | + ) |
| 181 | + this.lastHttpStatus = status |
| 182 | + this.lastApiResponse = body |
| 183 | + }, |
| 184 | +) |
| 185 | + |
| 186 | +Then('the response status is 401', function (this: EpdsWorld) { |
| 187 | + if (this.lastHttpStatus !== 401) { |
| 188 | + throw new Error( |
| 189 | + `Expected status 401 but got ${String(this.lastHttpStatus)}`, |
| 190 | + ) |
| 191 | + } |
| 192 | +}) |
| 193 | + |
| 194 | +Then('the response body contains an auth error', function (this: EpdsWorld) { |
| 195 | + if (!this.lastApiResponse) |
| 196 | + throw new Error('No API response — query step must run first') |
| 197 | + const error = this.lastApiResponse.error |
| 198 | + if (typeof error !== 'string' || !error) { |
| 199 | + throw new Error( |
| 200 | + `Expected response body to contain an error string but got "${String(error)}"`, |
| 201 | + ) |
| 202 | + } |
| 203 | +}) |
| 204 | + |
| 205 | +// check-handle returns 403 (not 401) on bad secret — this is an intentional |
| 206 | +// server-side inconsistency documented in the route code |
| 207 | +Then('the response status is 403', function (this: EpdsWorld) { |
| 208 | + if (this.lastHttpStatus !== 403) { |
| 209 | + throw new Error( |
| 210 | + `Expected status 403 but got ${String(this.lastHttpStatus)}`, |
| 211 | + ) |
| 212 | + } |
| 213 | +}) |
0 commit comments