docs-oauth-connector-integration.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 | createFakeGoogleDriveClient, |
| 9 | handleBeginDocsConnector, |
| 10 | handleDocsConnectorCallback, |
| 11 | handleImportDocsConnectorFiles, |
| 12 | handleListDocsConnectorFiles, |
| 13 | } from '../lib/docs/google-drive-connector.mjs'; |
| 14 | |
| 15 | const env = { |
| 16 | GOOGLE_DRIVE_OAUTH_CLIENT_ID: 'client', |
| 17 | GOOGLE_DRIVE_OAUTH_CLIENT_SECRET: 'credential', |
| 18 | KNOWTATION_DOCS_OAUTH_SECRET: 'v'.repeat(32), |
| 19 | DOCS_OAUTH_REDIRECT_URI: 'https://hub.example/docs/callback', |
| 20 | SCOOLING_RETURN_URL_ALLOWLIST: 'https://school.example/connect', |
| 21 | }; |
| 22 | |
| 23 | test('integration: Drive begin, callback, list, and import create proposals only', async () => { |
| 24 | const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'kn-docs-int-data-')); |
| 25 | const vaultPath = fs.mkdtempSync(path.join(os.tmpdir(), 'kn-docs-int-vault-')); |
| 26 | const client = createFakeGoogleDriveClient({ |
| 27 | files: [{ |
| 28 | id: 'drive_file_1', |
| 29 | name: 'Plan', |
| 30 | mimeType: 'text/markdown', |
| 31 | modifiedTime: '2026-08-17T00:00:00Z', |
| 32 | size: '7', |
| 33 | }], |
| 34 | contents: { drive_file_1: '# Plan\n' }, |
| 35 | }); |
| 36 | const begin = handleBeginDocsConnector({ |
| 37 | dataDir, |
| 38 | vaultId: 'vault-a', |
| 39 | body: { provider: 'google-drive', return_url: 'https://school.example/connect' }, |
| 40 | env, |
| 41 | authorizedOverride: true, |
| 42 | now: 1_000, |
| 43 | }); |
| 44 | assert.equal(begin.ok, true); |
| 45 | const authUrl = new URL(begin.payload.authorization_url); |
| 46 | assert.equal(authUrl.searchParams.get('code_challenge_method'), 'S256'); |
| 47 | const callback = await handleDocsConnectorCallback({ |
| 48 | dataDir, |
| 49 | query: { state: authUrl.searchParams.get('state'), code: 'authorization-code' }, |
| 50 | googleClient: client, |
| 51 | env, |
| 52 | authorizedOverride: true, |
| 53 | now: 2_000, |
| 54 | }); |
| 55 | assert.equal(callback.ok, true); |
| 56 | const list = await handleListDocsConnectorFiles({ |
| 57 | dataDir, |
| 58 | vaultId: 'vault-a', |
| 59 | connectorId: begin.payload.connector_id, |
| 60 | query: {}, |
| 61 | googleClient: client, |
| 62 | env, |
| 63 | authorizedOverride: true, |
| 64 | }); |
| 65 | assert.equal(list.payload.files[0].importable, true); |
| 66 | assert.equal(Object.hasOwn(list.payload.files[0], 'body'), false); |
| 67 | |
| 68 | const calls = []; |
| 69 | const imported = await handleImportDocsConnectorFiles({ |
| 70 | dataDir, |
| 71 | vaultPath, |
| 72 | vaultId: 'vault-a', |
| 73 | connectorId: begin.payload.connector_id, |
| 74 | body: { file_ids: ['drive_file_1'] }, |
| 75 | googleClient: client, |
| 76 | env, |
| 77 | authorizedOverride: true, |
| 78 | createProposalFn: (_dir, input) => { |
| 79 | calls.push(input); |
| 80 | return { ...input, proposal_id: 'proposal-1', status: 'proposed' }; |
| 81 | }, |
| 82 | loadProposalsFn: () => [], |
| 83 | listMarkdownFilesFn: () => [], |
| 84 | }); |
| 85 | assert.equal(imported.payload.proposed, 1); |
| 86 | assert.equal(calls[0].source, 'import'); |
| 87 | assert.equal(calls[0].review_queue, 'docs-sync'); |
| 88 | assert.equal(calls[0].path, 'imports/google-drive/drive_file_1.md'); |
| 89 | }); |
File History
1 commit
sha256:700fafdd1afa490919f9515d660ca6e75456bcd5bb67513abcd8757a634c01f6
docs: record AIP-b SD-21 land (KN #308)
Human
9 days ago