File size: 5,597 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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 })

// 1. Create a new API key (using query)
// 2. Verify the API key is listed in the table
// 3. Duplicate the API key
// 4. Verify the duplicate API key is listed in the table
// 5. Use the duplicate to delete the original
// 6. Verify the original is no longer listed in the table
// 7. Verify deleting the duplicate using the original fails (key deleted)
// 8. Delete the duplicate via the UI

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 }) => {
  // skip this test if we're running on mobile
  if (isMobile) return

  if (!baseURL) throw new Error('baseURL is required')

  const baseName =
    'apikeytest ' +
    c.string({ length: 12, casing: 'lower', symbols: false, alpha: true })

  // add 3 as a suffix so that the duplicate code will increment it to 4, and we can distinguish it from the original.
  const originalName = baseName + ' 3'
  const duplicateName = baseName + ' 4'

  const descrtiption = c.sentence({ words: 5 })

  await page.goto('./')

  // click on Admin, then API Keys
  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"]')
  // click the li with the text "Admin"
  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')

  // get the token from `[aria-label="Copy"]`
  const originalToken = await page.textContent('[aria-label="Copy"]')

  await page.click('text=Okay')

  // expect we have a <p> tag with the name as the text
  await expect(page.locator('p', { hasText: originalName })).toBeVisible()

  // click on it to open the drawer
  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')

  // Reproduce issue #3662
  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()

  // Delete the original using the duplicate via fetch call
  resp = await request.post(gqlURL, {
    headers: {
      Authorization: `Bearer ${duplicateToken}`,
    },
    data: {
      // Note: `query` is omitted to validate that it is not required for gql API keys.
      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()

  // Attempt to delete the duplicate using the original via fetch call
  resp = await request.post(gqlURL, {
    headers: {
      Authorization: `Bearer ${originalToken}`,
    },
    data: {
      query,
      operationName: 'DeleteAPIKey',
      variables: {
        id: duplicateID,
      },
    },
  })

  // expect the delete to fail, since the original was already deleted, and can no longer be used
  expect(resp.status()).toBe(401)

  // Delete the duplicate via the UI menu
  // find a div with a <p> tag with the text duplicateName, then click [aria-label="Other Actions"]
  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')

  // expect the duplicate to be gone
  await expect(page.locator('li', { hasText: duplicateName })).not.toBeVisible()
})