File size: 7,985 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { test, expect, Page } from '@playwright/test'
import {
  baseURLFromFlags,
  dropdownSelect,
  pageAction,
  userSessionFile,
} from './lib'
import Chance from 'chance'
import { createService } from './lib/service'
const c = new Chance()

test.describe.configure({ mode: 'parallel' })
test.use({
  storageState: userSessionFile,
  baseURL: baseURLFromFlags(['univ-keys']),
})

async function setup(page: Page, isMobile: boolean): Promise<string> {
  const intKeyName = 'uik-key ' + c.name()
  const serviceName = 'uik-service ' + c.name()
  const serviceDescription = c.sentence()
  const serviceURL = await createService(page, serviceName, serviceDescription)

  await page.getByRole('link', { name: 'Integration Keys' }).click()

  await pageAction(page, 'Create Integration Key')
  await page.fill('input[name=name]', intKeyName)
  await dropdownSelect(page, 'Type', 'Universal Integration Key')
  await page.click('button[type=submit]')
  await page.waitForURL(/\/services\/.+\/integration-keys\/.+$/)

  const bread = page.locator('header nav')
  await expect(bread.getByRole('link', { name: 'Services' })).toBeVisible()
  if (!isMobile) {
    // mobile collapses these
    await expect(bread.getByRole('link', { name: serviceName })).toBeVisible()
    await expect(
      bread.getByRole('link', { name: 'Integration Keys' }),
    ).toBeVisible()
  }
  await expect(bread.getByRole('link', { name: intKeyName })).toBeVisible()

  return serviceURL
}

test('create universal key, add rule with action', async ({
  page,
  isMobile,
}) => {
  await setup(page, isMobile)
  const ruleName = c.name()
  const ruleDesc = c.sentence({ words: 5 })
  const ruleNewDesc = c.sentence({ words: 3 })

  // create a rule
  await page.getByRole('button', { name: 'Create Rule' }).click()
  await page.fill('input[name=name]', ruleName)
  await page.fill('input[name=description]', ruleDesc)
  const editor = await page
    .getByTestId('code-conditionExpr')
    .locator('.cm-editor')
  await editor.click()
  await page.keyboard.insertText('true')
  await page.getByRole('button', { name: 'Submit' }).click()
  await expect(page.locator('[role=dialog]')).toBeHidden()

  // verify
  await expect(page.locator('body')).toContainText(ruleName)
  await expect(page.locator('body')).toContainText(ruleDesc)

  // start editing
  await page
    .locator('li', { hasText: ruleName })
    .getByRole('button', { name: 'Other Actions' })
    .click()
  await page.getByRole('menuitem', { name: 'Edit Rule' }).click()

  // update description, submit
  await page.fill('input[name=description]', ruleNewDesc)
  await page.getByRole('button', { name: 'Submit' }).click()
  await expect(page.locator('[role=dialog]')).toBeHidden()

  // verify name does not change, with new description
  await expect(page.locator('body')).toContainText(ruleName)
  await expect(page.locator('body')).toContainText(ruleNewDesc)

  // ensure `no-actions` message is displayed
  await expect(
    page.locator('li', { hasText: ruleName }).getByTestId('no-actions'),
  ).toBeVisible()

  // add an action
  await page
    .locator('li', { hasText: ruleName })
    .getByRole('button', { name: 'Other Actions' })
    .click()
  await page.getByRole('menuitem', { name: 'Add Action' }).click()

  // should default to alert
  await page.getByRole('button', { name: 'Submit' }).click()
  await expect(
    page.locator('li', { hasText: ruleName }).getByTestId('no-actions'),
  ).toBeHidden()

  // edit action
  await page.getByTestId('destination-chip').getByTestId('EditIcon').click()

  // check delete
  await page.getByLabel('Delete this action').click()
  await page.getByRole('button', { name: 'Submit' }).click()
  await expect(page.locator('[role=dialog]')).toBeHidden()

  // ensure `no-actions` message is displayed again
  await expect(
    page.locator('li', { hasText: ruleName }).getByTestId('no-actions'),
  ).toBeVisible()

  // delete the rule
  await page
    .locator('li', { hasText: ruleName })
    .getByRole('button', { name: 'Other Actions' })
    .click()
  await page.getByRole('menuitem', { name: 'Delete Rule' }).click()
  expect(
    page.locator('[data-cy=dialog-title]', { hasText: 'Are you sure?' }),
  ).toBeVisible()
  await page.getByRole('button', { name: 'Confirm' }).click()

  // verify
  await expect(page.locator('[data-cy=list-empty-message]')).toHaveText(
    'No rules exist for this integration key.',
  )
})

test('create primary auth token then promote a second auth token', async ({
  page,
  playwright,
  isMobile,
}) => {
  const serviceURL = await setup(page, isMobile)

  await expect(page.locator('[data-cy=details]')).toContainText(
    'Auth Token: N/A',
  )

  await page.getByRole('button', { name: 'Generate Auth Token' }).click()
  await page.getByRole('button', { name: 'Generate' }).click()

  const origPrimaryToken = await page
    .getByRole('button', { name: 'Copy' })
    .textContent()
  expect(origPrimaryToken).not.toBeNull()
  await page.getByRole('button', { name: 'Done' }).click()

  function tokenHint(token: string): string {
    return token.slice(0, 2) + '...' + token.slice(-4)
  }

  await expect(page.locator('[data-cy=details]')).toContainText(
    tokenHint(origPrimaryToken as string),
  )

  const req = await playwright.request.newContext({
    baseURL: baseURLFromFlags(['univ-keys']),
    extraHTTPHeaders: { 'Content-Type': 'application/json', Cookie: '' },
  })

  async function testKey(key: string, status: number): Promise<void> {
    const auth = { Authorization: key ? 'Bearer ' + key : '' }
    const resp = await req.post('/api/v2/uik', { headers: auth, data: {} })
    expect(resp.status()).toBe(status)
  }

  await testKey('', 401)
  await testKey(origPrimaryToken as string, 204)

  await page.getByRole('button', { name: 'Generate Secondary Token' }).click()
  await page.getByRole('button', { name: 'Generate' }).click()
  const firstSecondaryToken = await page
    .getByRole('button', { name: 'Copy' })
    .textContent()
  expect(origPrimaryToken).not.toBeNull()
  await page.getByRole('button', { name: 'Done' }).click()

  await expect(page.locator('[data-cy=details]')).toContainText(
    tokenHint(origPrimaryToken as string),
  )
  await expect(page.locator('[data-cy=details]')).toContainText(
    tokenHint(firstSecondaryToken as string),
  )

  // ensure both work
  await testKey(origPrimaryToken as string, 204)
  await testKey(firstSecondaryToken as string, 204)

  await page.getByRole('button', { name: 'Delete Secondary Token' }).click()
  await page.getByLabel('I acknowledge the impact of this action').click()
  await page.getByRole('button', { name: 'Submit' }).click()
  await expect(page.locator('[data-cy=details]')).not.toContainText(
    tokenHint(firstSecondaryToken as string),
  )
  await testKey(origPrimaryToken as string, 204)
  await testKey(firstSecondaryToken as string, 401)

  await page.getByRole('button', { name: 'Generate Secondary Token' }).click()
  await page.getByRole('button', { name: 'Generate' }).click()
  const secondSecondaryToken = await page
    .getByRole('button', { name: 'Copy' })
    .textContent()
  expect(secondSecondaryToken).not.toBeNull()
  await page.getByRole('button', { name: 'Done' }).click()

  await page.getByRole('button', { name: 'Promote Secondary Token' }).click()
  await page.getByLabel('I acknowledge the impact of this action').click()
  await page.getByRole('button', { name: 'Promote Key' }).click()

  await expect(page.locator('[data-cy=details]')).toContainText(
    tokenHint(secondSecondaryToken as string),
  )
  await expect(page.locator('[data-cy=details]')).not.toContainText(
    tokenHint(origPrimaryToken as string),
  )

  await testKey(origPrimaryToken as string, 401)
  await testKey(secondSecondaryToken as string, 204)

  await page.goto(serviceURL)
  await page.getByRole('button', { name: 'Delete' }).click()
  await page.getByRole('button', { name: 'Confirm' }).click()
  await page.waitForURL(/services$/)
})