| 1 | // Real Docs app, controlled Google responses. This is not a live Google test. |
| 2 | import assert from 'node:assert/strict' |
| 3 | import { pathToFileURL } from 'node:url' |
| 4 | const playwright = process.env.DOCS_IMPORT_PLAYWRIGHT |
| 5 | ? await import(pathToFileURL(process.env.DOCS_IMPORT_PLAYWRIGHT + '/index.mjs')) |
| 6 | : await import('playwright') |
| 7 | const config = JSON.parse(process.env.DOCS_GOOGLE_IMPORT_TEST) |
| 8 | const fixture = Buffer.from(config.fixture, 'base64') |
| 9 | const gis = `window.google={accounts:{oauth2:{initTokenClient(c){return{requestAccessToken(){c.callback({access_token:'controlled-google-bearer',scope:'https://www.googleapis.com/auth/drive.file'})}}},hasGrantedAllScopes(r,s){return r.scope===s}}}};` |
| 10 | const gapi = `window.gapi={load(name,c){google.picker={Action:{PICKED:'picked',CANCEL:'cancel'},DocsView:class{setMimeTypes(){return this}},PickerBuilder:class{setDeveloperKey(){return this}setAppId(){return this}setOAuthToken(){return this}setOrigin(){return this}addView(){return this}setCallback(fn){this.cb=fn;return this}build(){const cb=this.cb;let el;return{setVisible(){el=document.createElement('button');el.textContent='Select import fixture';el.onclick=()=>cb({action:'picked',docs:[{id:'controlled_file',name:'Import fixture',mimeType:'application/vnd.google-apps.document'}]});document.body.append(el)},dispose(){el?.remove()}}}}};c.callback()}};` |
| 11 | const expected = ['Import fixture', 'linked words', 'bold text', 'plain text', 'Table cell'] |
| 12 | for (const engine of ['chromium', 'webkit']) { |
| 13 | const browser = await playwright[engine].launch() |
| 14 | try { |
| 15 | const context = await browser.newContext({ viewport: { width: 900, height: 850 } }) |
| 16 | const cookies = config.cookies.map(c => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' })) |
| 17 | await context.addCookies(cookies) |
| 18 | const page = await context.newPage() |
| 19 | const violations = [], failures = [] |
| 20 | let exports = 0, leaked = false, externalRelationship = false |
| 21 | page.on('pageerror', e => failures.push(e.message)) |
| 22 | await page.addInitScript(() => document.addEventListener('securitypolicyviolation', e => console.log('IMPORT-CSP:' + e.violatedDirective))) |
| 23 | page.on('console', message => { if (message.text().startsWith('IMPORT-CSP:')) violations.push(message.text()) }) |
| 24 | page.on('request', request => { |
| 25 | if (request.url().startsWith(config.origin) && ((request.postData() || '').includes('controlled-google-bearer') || JSON.stringify(request.headers()).includes('controlled-google-bearer'))) leaked = true |
| 26 | if (request.url().startsWith('https://fixture.invalid/')) externalRelationship = true |
| 27 | }) |
| 28 | await context.route('https://accounts.google.com/gsi/client', r => r.fulfill({ contentType: 'text/javascript', body: gis })) |
| 29 | await context.route('https://apis.google.com/js/api.js', r => r.fulfill({ contentType: 'text/javascript', body: gapi })) |
| 30 | await context.route('https://www.googleapis.com/drive/v3/files/**', async r => { |
| 31 | exports++ |
| 32 | assert.equal(r.request().headers().authorization, 'Bearer controlled-google-bearer') |
| 33 | await r.fulfill({ headers: { 'Access-Control-Allow-Origin': '*' }, contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', body: fixture }) |
| 34 | }) |
| 35 | await page.goto(config.origin + '/import') |
| 36 | await page.waitForFunction(() => !document.querySelector('[data-google-choose]')?.disabled) |
| 37 | await page.locator('[data-google-choose]').click() |
| 38 | await page.getByRole('button', { name: 'Select import fixture', exact: true }).click() |
| 39 | await page.waitForURL(url => url.pathname !== '/import') |
| 40 | const editorPath = new URL(page.url()).pathname |
| 41 | assert.match(editorPath, /^\/d\/[^/]+$/) |
| 42 | const reportURL = config.origin + editorPath + '/import-report' |
| 43 | assert.match(await page.locator('body').innerText(), /import/i) |
| 44 | assert.match(await page.locator('body').innerText(), /table/i) |
| 45 | assert.match(await page.locator('body').innerText(), /link/i) |
| 46 | assert.equal(await page.locator('[docs-import-report]').count(), 1) |
| 47 | await page.goto(config.origin + editorPath) |
| 48 | const editor = page.locator('[docs-text][contenteditable="true"]') |
| 49 | try { await editor.waitFor({ state: 'visible', timeout: 30000 }) } |
| 50 | catch (cause) { throw new Error(engine + ': editor unavailable; errors=' + JSON.stringify(failures) + '; page=' + (await page.locator('body').innerText()).slice(0, 1500), { cause }) } |
| 51 | for (const text of expected) assert((await editor.innerText()).includes(text), 'missing ' + text) |
| 52 | assert.match(await editor.locator('strong').innerText(), /bold text/) |
| 53 | assert(!(await editor.locator('strong').innerText()).includes('plain text')) |
| 54 | assert.equal(await editor.locator('a').count(), 0) |
| 55 | const saved = page.waitForResponse(r => r.request().method() === 'POST' && r.url().endsWith('/updates') && r.ok()) |
| 56 | await editor.click() |
| 57 | await page.keyboard.press('End') |
| 58 | await page.keyboard.insertText(' Edited after import.') |
| 59 | await saved |
| 60 | await page.reload() |
| 61 | await page.waitForFunction(() => document.querySelector('[docs-text]')?.textContent.includes('Edited after import.')) |
| 62 | await page.goto(reportURL) |
| 63 | assert.match(await page.locator('body').innerText(), /table/i) |
| 64 | assert.equal(exports, 1) |
| 65 | assert.equal(leaked, false) |
| 66 | assert.equal(externalRelationship, false) |
| 67 | assert.deepEqual(violations, []) |
| 68 | assert.deepEqual(failures, []) |
| 69 | |
| 70 | const plainContext = await browser.newContext({ javaScriptEnabled: false }) |
| 71 | await plainContext.addCookies(cookies) |
| 72 | const plain = await plainContext.newPage() |
| 73 | await plain.goto(config.origin + '/import') |
| 74 | assert.equal(await plain.locator('[data-google-choose]').isVisible(), false) |
| 75 | await plain.locator('input[type="file"]').setInputFiles({ name: 'scriptless.docx', mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', buffer: fixture }) |
| 76 | await plain.locator('form:has(input[type="file"]) button[type="submit"]').click() |
| 77 | await plain.waitForURL(url => url.pathname !== '/import') |
| 78 | assert.match(await plain.locator('body').innerText(), /table/i) |
| 79 | await plainContext.close() |
| 80 | await context.close() |
| 81 | console.log(engine + ': actual import route, nested text, marks, editable persistence, report reopening and scriptless upload passed') |
| 82 | } finally { await browser.close() } |
| 83 | } |
| 84 | |