|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { |
| 5 | + createMockRequest, |
| 6 | + hybridAuthMockFns, |
| 7 | + inputValidationMock, |
| 8 | + inputValidationMockFns, |
| 9 | +} from '@sim/testing' |
| 10 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 11 | + |
| 12 | +vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock) |
| 13 | + |
| 14 | +import { buildDocxFromContent } from '@/lib/microsoft-word/document.server' |
| 15 | +import { POST } from '@/app/api/tools/microsoft_word/append/route' |
| 16 | + |
| 17 | +const { mockValidateUrlWithDNS, mockSecureFetchWithPinnedIP } = inputValidationMockFns |
| 18 | + |
| 19 | +const PINNED_IP = '93.184.216.34' |
| 20 | + |
| 21 | +const baseBody = { |
| 22 | + accessToken: 'token-123', |
| 23 | + documentId: 'doc-abc', |
| 24 | + content: 'Appended paragraph', |
| 25 | +} |
| 26 | + |
| 27 | +/** A Graph `driveItem` metadata response carrying a content tag. */ |
| 28 | +function itemResponse(cTag: string) { |
| 29 | + const body = { |
| 30 | + id: 'doc-abc', |
| 31 | + name: 'notes.docx', |
| 32 | + cTag, |
| 33 | + file: { mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }, |
| 34 | + } |
| 35 | + return { |
| 36 | + ok: true, |
| 37 | + status: 200, |
| 38 | + statusText: '', |
| 39 | + headers: new Headers(), |
| 40 | + body: null, |
| 41 | + text: async () => JSON.stringify(body), |
| 42 | + json: async () => body, |
| 43 | + arrayBuffer: async () => new ArrayBuffer(0), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** A Graph content response carrying a real `.docx` package. */ |
| 48 | +async function docxResponse() { |
| 49 | + const buffer = await buildDocxFromContent('Existing paragraph') |
| 50 | + return { |
| 51 | + ok: true, |
| 52 | + status: 200, |
| 53 | + statusText: '', |
| 54 | + headers: new Headers(), |
| 55 | + body: null, |
| 56 | + text: async () => '', |
| 57 | + json: async () => ({}), |
| 58 | + arrayBuffer: async () => |
| 59 | + buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength), |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function preconditionFailedResponse() { |
| 64 | + return { |
| 65 | + ok: false, |
| 66 | + status: 412, |
| 67 | + statusText: 'Precondition Failed', |
| 68 | + headers: new Headers(), |
| 69 | + body: null, |
| 70 | + text: async () => '', |
| 71 | + json: async () => ({}), |
| 72 | + arrayBuffer: async () => new ArrayBuffer(0), |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +beforeEach(() => { |
| 77 | + vi.clearAllMocks() |
| 78 | + hybridAuthMockFns.mockCheckInternalAuth.mockResolvedValue({ |
| 79 | + success: true, |
| 80 | + userId: 'user-1', |
| 81 | + authType: 'internal_jwt', |
| 82 | + }) |
| 83 | + mockValidateUrlWithDNS.mockResolvedValue({ |
| 84 | + isValid: true, |
| 85 | + resolvedIP: PINNED_IP, |
| 86 | + originalHostname: 'graph.microsoft.com', |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +describe('POST /api/tools/microsoft_word/append', () => { |
| 91 | + it('uploads the edit when the document has not changed, guarded by its content tag', async () => { |
| 92 | + mockSecureFetchWithPinnedIP |
| 93 | + .mockResolvedValueOnce(itemResponse('tag-1')) |
| 94 | + .mockResolvedValueOnce(await docxResponse()) |
| 95 | + .mockResolvedValueOnce(itemResponse('tag-1')) |
| 96 | + .mockResolvedValueOnce(itemResponse('tag-2')) |
| 97 | + |
| 98 | + const response = await POST(createMockRequest('POST', baseBody)) |
| 99 | + |
| 100 | + expect(response.status).toBe(200) |
| 101 | + const data = (await response.json()) as { |
| 102 | + success: boolean |
| 103 | + output: { updatedContent: boolean } |
| 104 | + } |
| 105 | + expect(data.success).toBe(true) |
| 106 | + expect(data.output.updatedContent).toBe(true) |
| 107 | + |
| 108 | + const uploadCall = mockSecureFetchWithPinnedIP.mock.calls.at(-1) |
| 109 | + expect(uploadCall?.[2]).toMatchObject({ method: 'PUT' }) |
| 110 | + expect(uploadCall?.[2].headers).toMatchObject({ 'if-match': 'tag-1' }) |
| 111 | + }) |
| 112 | + |
| 113 | + it('refuses to overwrite a document that changed while the edit was in flight', async () => { |
| 114 | + mockSecureFetchWithPinnedIP |
| 115 | + .mockResolvedValueOnce(itemResponse('tag-1')) |
| 116 | + .mockResolvedValueOnce(await docxResponse()) |
| 117 | + .mockResolvedValueOnce(itemResponse('tag-2')) |
| 118 | + |
| 119 | + const response = await POST(createMockRequest('POST', baseBody)) |
| 120 | + |
| 121 | + expect(response.status).toBe(409) |
| 122 | + const data = (await response.json()) as { success: boolean; error: string } |
| 123 | + expect(data.success).toBe(false) |
| 124 | + expect(data.error).toMatch(/no other change was overwritten/) |
| 125 | + |
| 126 | + // The conflict is detected before the PUT, so nothing was written. |
| 127 | + expect(mockSecureFetchWithPinnedIP.mock.calls.some((call) => call[2]?.method === 'PUT')).toBe( |
| 128 | + false |
| 129 | + ) |
| 130 | + }) |
| 131 | + |
| 132 | + it('surfaces a 412 from the upload precondition as the same conflict', async () => { |
| 133 | + mockSecureFetchWithPinnedIP |
| 134 | + .mockResolvedValueOnce(itemResponse('tag-1')) |
| 135 | + .mockResolvedValueOnce(await docxResponse()) |
| 136 | + .mockResolvedValueOnce(itemResponse('tag-1')) |
| 137 | + .mockResolvedValueOnce(preconditionFailedResponse()) |
| 138 | + |
| 139 | + const response = await POST(createMockRequest('POST', baseBody)) |
| 140 | + |
| 141 | + expect(response.status).toBe(409) |
| 142 | + const data = (await response.json()) as { error: string } |
| 143 | + expect(data.error).toMatch(/no other change was overwritten/) |
| 144 | + }) |
| 145 | +}) |
0 commit comments