|
import { test, expect } from '@playwright/test' |
|
import { adminSessionFile } from './lib' |
|
import Chance from 'chance' |
|
const c = new Chance() |
|
|
|
test.describe.configure({ mode: 'parallel' }) |
|
test.use({ storageState: adminSessionFile }) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const query = ` |
|
query ListAPIKeys { |
|
gqlAPIKeys { |
|
id |
|
name |
|
} |
|
} |
|
|
|
mutation DeleteAPIKey($id: ID!) { |
|
deleteGQLAPIKey(id: $id) |
|
} |
|
|
|
query ServiceInfo($firstID: ID!) { |
|
service(id: $firstID) { |
|
id |
|
} |
|
} |
|
|
|
query ServiceInfo2($secondID: ID!) { |
|
service(id: $secondID) { |
|
id |
|
} |
|
} |
|
` |
|
|
|
test('GQL API keys', async ({ page, request, isMobile, baseURL }) => { |
|
|
|
if (isMobile) return |
|
|
|
if (!baseURL) throw new Error('baseURL is required') |
|
|
|
const baseName = |
|
'apikeytest ' + |
|
c.string({ length: 12, casing: 'lower', symbols: false, alpha: true }) |
|
|
|
|
|
const originalName = baseName + ' 3' |
|
const duplicateName = baseName + ' 4' |
|
|
|
const descrtiption = c.sentence({ words: 5 }) |
|
|
|
await page.goto('./') |
|
|
|
|
|
await page.click('text=Admin') |
|
|
|
await page.locator('nav').locator('text=API Keys').click() |
|
await page.click('text=Create API Key') |
|
|
|
await page.fill('[name="name"]', originalName) |
|
await page.fill('[name="description"]', descrtiption) |
|
await page.click('[aria-haspopup="listbox"]') |
|
|
|
await page.click('li:text("Admin")') |
|
|
|
const editor = page.locator('.cm-editor') |
|
await editor.click() |
|
await page.keyboard.insertText(query) |
|
|
|
await page.click('text=Submit') |
|
|
|
|
|
const originalToken = await page.textContent('[aria-label="Copy"]') |
|
|
|
await page.click('text=Okay') |
|
|
|
|
|
await expect(page.locator('p', { hasText: originalName })).toBeVisible() |
|
|
|
|
|
await page.locator('li', { hasText: originalName }).click() |
|
|
|
await page.click('text=Duplicate') |
|
await page.click('text=Submit') |
|
|
|
const duplicateToken = await page.textContent('[aria-label="Copy"]') |
|
|
|
await page.click('text=Okay') |
|
|
|
await expect(page.locator('li', { hasText: duplicateName })).toBeVisible() |
|
|
|
const gqlURL = baseURL.replace(/\/$/, '') + '/api/graphql' |
|
let resp = await request.post(gqlURL, { |
|
headers: { |
|
Authorization: `Bearer ${duplicateToken}`, |
|
}, |
|
data: { query, operationName: 'ListAPIKeys' }, |
|
}) |
|
|
|
expect(resp.status()).toBe(200) |
|
const data = await resp.json() |
|
expect(data).toHaveProperty('data') |
|
expect(data).not.toHaveProperty('errors') |
|
expect(data.data).toHaveProperty('gqlAPIKeys') |
|
|
|
|
|
resp = await request.post(gqlURL, { |
|
headers: { |
|
Authorization: `Bearer ${duplicateToken}`, |
|
}, |
|
data: { |
|
variables: { |
|
firstID: '00000000-0000-0000-0000-000000000000', |
|
}, |
|
operationName: 'ServiceInfo', |
|
}, |
|
}) |
|
expect(resp.status()).toBe(200) |
|
expect(await resp.json()).not.toHaveProperty('errors') |
|
|
|
resp = await request.post(gqlURL, { |
|
headers: { |
|
Authorization: `Bearer ${duplicateToken}`, |
|
}, |
|
data: { query: '{wrongQuery}' }, |
|
}) |
|
|
|
expect(resp.status()).toBe(200) |
|
const badResp = await resp.json() |
|
|
|
expect(badResp).toHaveProperty('errors') |
|
|
|
type Key = { |
|
id: string |
|
name: string |
|
} |
|
|
|
const originalID = data.data.gqlAPIKeys.find( |
|
(key: Key) => key.name === originalName, |
|
).id |
|
const duplicateID = data.data.gqlAPIKeys.find( |
|
(key: Key) => key.name === duplicateName, |
|
).id |
|
expect(originalID).toBeDefined() |
|
expect(duplicateID).toBeDefined() |
|
|
|
|
|
resp = await request.post(gqlURL, { |
|
headers: { |
|
Authorization: `Bearer ${duplicateToken}`, |
|
}, |
|
data: { |
|
|
|
operationName: 'DeleteAPIKey', |
|
variables: { |
|
id: originalID, |
|
}, |
|
}, |
|
}) |
|
|
|
expect(resp.status()).toBe(200) |
|
expect(await resp.json()).not.toHaveProperty('errors') |
|
|
|
await page.reload() |
|
|
|
await expect(page.locator('li', { hasText: originalName })).not.toBeVisible() |
|
|
|
|
|
resp = await request.post(gqlURL, { |
|
headers: { |
|
Authorization: `Bearer ${originalToken}`, |
|
}, |
|
data: { |
|
query, |
|
operationName: 'DeleteAPIKey', |
|
variables: { |
|
id: duplicateID, |
|
}, |
|
}, |
|
}) |
|
|
|
|
|
expect(resp.status()).toBe(401) |
|
|
|
|
|
|
|
await page |
|
.locator('li', { hasText: duplicateName }) |
|
.locator('[aria-label="Other Actions"]') |
|
.click() |
|
await page.locator('[role=menuitem]', { hasText: 'Delete' }).click() |
|
await page.click('text=Confirm') |
|
|
|
|
|
await expect(page.locator('li', { hasText: duplicateName })).not.toBeVisible() |
|
}) |
|
|