| 1 | import type { AddressInfo } from 'node:net'; |
| 2 | |
| 3 | import type { INestApplication |
| 4 | import { Test } |
| 5 | import { AuthService } |
| 6 | |
| 7 | import type { Auth |
| 8 | |
| 9 | export const DATABASE_URL = |
| 10 | export const S3_ENDPOINT = |
| 11 | |
| 12 | export const s3Credentials = |
| 13 | S3_ACCESS_KEY_ID: process.env |
| 14 | S3_SECRET_ACCESS_KEY: process.env |
| 15 | S3_BUCKET: process.env |
| 16 | }; |
| 17 | |
| 18 | /** Suites need a throwaway Postgres and an S3 endpoint (RustFS from compose.yaml works), and are skipped without them. */ |
| 19 | export const hasBackends = |
| 20 | |
| 21 | /** Serves the whole app on `port`, random unless given, against the database and bucket global-setup.ts prepared. `env` overrides the defaults below, such as turning the SSH transport on. `configure` runs before anything else is mounted, so middleware it adds sees every request. */ |
| 22 | export async function startApp |
| 23 | env: Record |
| 24 | port = 0, |
| 25 | httpsOptions?: { |
| 26 | configure?: (app |
| 27 | ) { |
| 28 | Object.assign(process |
| 29 | DATABASE_URL, |
| 30 | S3_ENDPOINT, |
| 31 | ...s3Credentials, |
| 32 | BETTER_AUTH_SECRET: 'e2e-secret-e2e-secret-e2e-secret', |
| 33 | BETTER_AUTH_URL: 'http://127.0.0.1', |
| 34 | EMAIL_VERIFICATION_ENABLED: 'false', |
| 35 | // A local .env may configure real delivery; tests must never send mail. |
| 36 | EMAIL_PROXY: '', |
| 37 | MAIL_HOST: '', |
| 38 | ZOEKT_URL: '', |
| 39 | GIT_SSH_HOST_KEY: '', |
| 40 | OTEL_EXPORTER_OTLP_ENDPOINT: '', |
| 41 | PYROSCOPE_SERVER_ADDRESS: '', |
| 42 | ...env, |
| 43 | }); |
| 44 | |
| 45 | // Imported late so the module reads the environment set above. |
| 46 | const { AppModule |
| 47 | const { configureApp |
| 48 | const moduleRef = |
| 49 | imports: [AppModule], |
| 50 | }).compile(); |
| 51 | const app = |
| 52 | bodyParser: false, |
| 53 | httpsOptions, |
| 54 | }); |
| 55 | configure?.(app); |
| 56 | configureApp(app); |
| 57 | await app.listen |
| 58 | const scheme = |
| 59 | return { |
| 60 | app, |
| 61 | origin: `${scheme} |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | /** A fresh account with a session cookie for the API and an API key for git over HTTP. */ |
| 66 | export async function signUp |
| 67 | const auth = |
| 68 | const email = |
| 69 | const password = |
| 70 | const { user |
| 71 | body: { email, |
| 72 | }); |
| 73 | const { headers |
| 74 | body: { email, |
| 75 | returnHeaders: true, |
| 76 | }); |
| 77 | return { |
| 78 | cookie: headers.get |
| 79 | key: await apiKey |
| 80 | userId: user.id |
| 81 | }; |
| 82 | } |
| 83 | |
| 84 | /** Another API key for the same account, each with its own rate limit. */ |
| 85 | export async function apiKey |
| 86 | const auth = |
| 87 | const { key |
| 88 | return key; |
| 89 | } |
| 90 | |
| 91 | /** An API key holding only `scopes`, as the device flow mints them. */ |
| 92 | export async function scopedKey |
| 93 | app: INestApplication |
| 94 | userId: string, |
| 95 | scopes: string[] |
| 96 | ) { |
| 97 | const auth = |
| 98 | const { key |
| 99 | body: { userId, |
| 100 | }); |
| 101 | return key; |
| 102 | } |