| 1 | import path from 'node:path'; |
| 2 | import { createDatabase, type |
| 3 | import { Test, type |
| 4 | import { eq } |
| 5 | import { migrate } |
| 6 | import { afterAll, beforeAll |
| 7 | |
| 8 | import { DATABASE } |
| 9 | import { |
| 10 | SIGNING_KEY_ID, |
| 11 | SIGNING_PUBLIC_KEY, |
| 12 | } from '../../lib/gpg/__fixtures__/signed-commit.js'; |
| 13 | import { UsersService } |
| 14 | import { |
| 15 | GpgKeyAlreadyExistsError, |
| 16 | GpgKeyEmailNotVerifiedError, |
| 17 | GpgKeyNotFoundError, |
| 18 | InvalidGpgKeyError, |
| 19 | } from './gpg-keys.errors.js'; |
| 20 | import { GpgKeysService } |
| 21 | |
| 22 | const CONNECTION = process |
| 23 | const MIGRATIONS = path |
| 24 | import.meta.dirname, |
| 25 | '../../../../../packages/db/drizzle', |
| 26 | ); |
| 27 | |
| 28 | const USER_ID = ' |
| 29 | const OTHER_ID = ' |
| 30 | const KEY_EMAIL = ' |
| 31 | |
| 32 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 33 | describe.skipIf(!CONNECTION |
| 34 | let db: |
| 35 | let pool: |
| 36 | let service: |
| 37 | |
| 38 | beforeAll(async () |
| 39 | ({ db, pool |
| 40 | await migrate(db |
| 41 | |
| 42 | const module: |
| 43 | providers: [ |
| 44 | GpgKeysService, |
| 45 | UsersService, |
| 46 | { provide: DATABASE, |
| 47 | ], |
| 48 | }).compile(); |
| 49 | service = module. |
| 50 | }); |
| 51 | |
| 52 | afterAll(async () |
| 53 | await db.delete |
| 54 | await db.delete |
| 55 | await pool.end |
| 56 | }); |
| 57 | |
| 58 | beforeEach(async () |
| 59 | await db.delete |
| 60 | await db.delete |
| 61 | await db.insert |
| 62 | { id: USER_ID, |
| 63 | { |
| 64 | id: OTHER_ID, |
| 65 | name: 'Other', |
| 66 | email: 'other@example.com', |
| 67 | emailVerified: true, |
| 68 | }, |
| 69 | ]); |
| 70 | }); |
| 71 | |
| 72 | it('adds a key that carries an address the account owns', |
| 73 | const added = |
| 74 | |
| 75 | expect(added.keyId |
| 76 | expect(added.emails |
| 77 | expect((await service |
| 78 | }); |
| 79 | |
| 80 | it('refuses a key whose addresses the account has not verified', |
| 81 | await expect(service |
| 82 | GpgKeyEmailNotVerifiedError, |
| 83 | ); |
| 84 | }); |
| 85 | |
| 86 | it('accepts a key matching a verified extra address', |
| 87 | await db |
| 88 | .update(schema.user |
| 89 | .set({ email: |
| 90 | .where(eq(schema |
| 91 | await db.insert |
| 92 | id: 'uem_gpg_spec', |
| 93 | userId: USER_ID, |
| 94 | email: KEY_EMAIL, |
| 95 | verified: true, |
| 96 | }); |
| 97 | |
| 98 | await expect( |
| 99 | service.add(USER_ID |
| 100 | ).resolves.toMatchObject( |
| 101 | }); |
| 102 | |
| 103 | it('refuses a key matching an address that is only claimed', |
| 104 | await db |
| 105 | .update(schema.user |
| 106 | .set({ email: |
| 107 | .where(eq(schema |
| 108 | await db.insert |
| 109 | id: 'uem_gpg_spec_2', |
| 110 | userId: USER_ID, |
| 111 | email: KEY_EMAIL, |
| 112 | verified: false, |
| 113 | }); |
| 114 | |
| 115 | await expect(service |
| 116 | GpgKeyEmailNotVerifiedError, |
| 117 | ); |
| 118 | }); |
| 119 | |
| 120 | it('refuses armor that is not a public key', |
| 121 | await expect(service |
| 122 | InvalidGpgKeyError, |
| 123 | ); |
| 124 | }); |
| 125 | |
| 126 | it('refuses a key that already belongs to an account', |
| 127 | await service.add |
| 128 | |
| 129 | await expect(service |
| 130 | GpgKeyAlreadyExistsError, |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | it('removes only the keys of the account that asks', |
| 135 | const added = |
| 136 | |
| 137 | await expect(service |
| 138 | GpgKeyNotFoundError, |
| 139 | ); |
| 140 | |
| 141 | await service.remove |
| 142 | expect((await service |
| 143 | }); |
| 144 | }); |