docs-oauth-connector-unit.test.mjs
sha256:700fafdd1afa490919f9515d660ca6e75456bcd5bb67513abcd8757a634c01f6
docs: record AIP-b SD-21 land (KN #308)
Human
9 days ago
| 1 | import assert from 'node:assert/strict'; |
| 2 | import fs from 'node:fs'; |
| 3 | import os from 'node:os'; |
| 4 | import path from 'node:path'; |
| 5 | import test from 'node:test'; |
| 6 | |
| 7 | import { |
| 8 | DOCS_OAUTH_GOOGLE_AUTHORIZED, |
| 9 | GOOGLE_DRIVE_OAUTH_SCOPES, |
| 10 | handleBeginDocsConnector, |
| 11 | isDocsGoogleOAuthEnabled, |
| 12 | } from '../lib/docs/google-drive-connector.mjs'; |
| 13 | import { |
| 14 | ALLOWED_DRIVE_MIMES, |
| 15 | buildDriveNameContainsQuery, |
| 16 | isImportableMime, |
| 17 | safeId, |
| 18 | } from '../lib/docs/google-drive-normalizer.mjs'; |
| 19 | import { |
| 20 | oauthTokenVaultPath, |
| 21 | oauthTokenVaultRoundTrip, |
| 22 | readOAuthTokenVault, |
| 23 | writeOAuthTokenVault, |
| 24 | } from '../lib/docs/oauth-token-vault.mjs'; |
| 25 | |
| 26 | test('unit: frozen gates, scopes, MIME and query validation', () => { |
| 27 | assert.equal(DOCS_OAUTH_GOOGLE_AUTHORIZED, true); |
| 28 | assert.equal(isDocsGoogleOAuthEnabled({ authorizedOverride: false }), false); |
| 29 | assert.deepEqual(GOOGLE_DRIVE_OAUTH_SCOPES, [ |
| 30 | 'openid', |
| 31 | 'https://www.googleapis.com/auth/drive.readonly', |
| 32 | ]); |
| 33 | for (const mime of ALLOWED_DRIVE_MIMES) assert.equal(isImportableMime(mime), true); |
| 34 | assert.equal(isImportableMime('application/vnd.google-apps.folder'), false); |
| 35 | assert.equal(buildDriveNameContainsQuery('Quarterly plan'), "name contains 'Quarterly plan'"); |
| 36 | assert.equal(buildDriveNameContainsQuery("x' or trashed = false"), null); |
| 37 | assert.equal(safeId('../bad:id'), 'badid'); |
| 38 | assert.equal(safeId('a'.repeat(100)).length, 64); |
| 39 | assert.deepEqual(handleBeginDocsConnector({ |
| 40 | dataDir: '/unreadable', |
| 41 | vaultId: 'v', |
| 42 | body: {}, |
| 43 | authorizedOverride: false, |
| 44 | }), { |
| 45 | ok: false, |
| 46 | status: 501, |
| 47 | code: 'NOT_AUTHORIZED', |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | test('unit: docs OAuth vault encrypts, authenticates, and isolates namespace', () => { |
| 52 | const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'kn-docs-unit-')); |
| 53 | const connectorId = 'conn_0123456789abcdef'; |
| 54 | const secret = 's'.repeat(32); |
| 55 | const payload = { |
| 56 | refresh_token: 'refresh-secret', |
| 57 | scope: GOOGLE_DRIVE_OAUTH_SCOPES.join(' '), |
| 58 | token_type: 'Bearer', |
| 59 | obtained_at: '2026-08-17T00:00:00.000Z', |
| 60 | account_sub: 'subject', |
| 61 | }; |
| 62 | assert.deepEqual(oauthTokenVaultRoundTrip(secret, payload), payload); |
| 63 | writeOAuthTokenVault(dir, connectorId, secret, payload); |
| 64 | assert.equal(oauthTokenVaultPath(dir, connectorId).includes(`${path.sep}docs_oauth${path.sep}`), true); |
| 65 | assert.deepEqual(readOAuthTokenVault(dir, connectorId, secret), payload); |
| 66 | assert.equal(fs.readFileSync(oauthTokenVaultPath(dir, connectorId), 'utf8').includes('refresh-secret'), false); |
| 67 | }); |
File History
1 commit
sha256:700fafdd1afa490919f9515d660ca6e75456bcd5bb67513abcd8757a634c01f6
docs: record AIP-b SD-21 land (KN #308)
Human
9 days ago