| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import type { INestApplication |
| 3 | import { and, eq |
| 4 | import request from 'supertest |
| 5 | import { afterAll, beforeAll |
| 6 | |
| 7 | import { DATABASE } |
| 8 | import { hasBackends, signUp |
| 9 | |
| 10 | const GH = ' |
| 11 | const DEVICE_GRANT = ' |
| 12 | |
| 13 | describe.skipIf(!hasBackends |
| 14 | let app: |
| 15 | let owner: |
| 16 | const username = |
| 17 | const api = |
| 18 | const requestCode = |
| 19 | const poll = |
| 20 | const form = |
| 21 | const decide = |
| 22 | await api().get |
| 23 | await api().post |
| 24 | }; |
| 25 | |
| 26 | beforeAll(async () |
| 27 | ({ app } |
| 28 | owner = await |
| 29 | }); |
| 30 | |
| 31 | afterAll(async () |
| 32 | await app?.close |
| 33 | }); |
| 34 | |
| 35 | it('hands gh a device code, form-encoded, with the web /device page to visit', |
| 36 | const response = |
| 37 | expect(response.headers |
| 38 | const code = |
| 39 | expect(code).toMatchObject |
| 40 | expect(code.user_code |
| 41 | expect(code.device_code |
| 42 | }); |
| 43 | |
| 44 | it('answers JSON when asked, and refuses an unknown client so gh does not loop', |
| 45 | const json = |
| 46 | expect(json.body |
| 47 | await api().post |
| 48 | }); |
| 49 | |
| 50 | it('answers authorization_pending before approval, and access_denied after a deny', |
| 51 | const pending = |
| 52 | expect(form((await |
| 53 | |
| 54 | const denied = |
| 55 | await decide(denied |
| 56 | expect(form((await |
| 57 | }); |
| 58 | |
| 59 | it('mints a ghost_pat_ key with the known scopes gh asked for, never the session', |
| 60 | const code = |
| 61 | await decide(code |
| 62 | expect(form((await |
| 63 | const token = |
| 64 | expect(token).toMatchObject |
| 65 | expect(token.access_token |
| 66 | const status = |
| 67 | expect(status.headers |
| 68 | expect(form((await |
| 69 | }); |
| 70 | |
| 71 | it('ignores a client_secret sent with the device grant', |
| 72 | const code = |
| 73 | await decide(code |
| 74 | const response = |
| 75 | expect(form(response |
| 76 | }); |
| 77 | |
| 78 | it('refuses a device code to a user app with device flow off, and runs the flow once it is on', |
| 79 | const db = |
| 80 | const clientId = |
| 81 | await db.insert |
| 82 | const refused = |
| 83 | expect(form(refused |
| 84 | |
| 85 | await db.update |
| 86 | const code = |
| 87 | await decide(code |
| 88 | const token = |
| 89 | expect(token.access_token |
| 90 | const [key] |
| 91 | expect(JSON.parse |
| 92 | }); |
| 93 | }); |