-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (86 loc) · 2.59 KB
/
index.js
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
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
import { v4 as uuidv4 } from "uuid"
export const prng = () => uuidv4()
const isStatusOk = (res) =>
res.ok
? Promise.resolve(res)
: Promise.reject(
new Error(`Received unexpected status code ${res.statusCode}`),
)
export const findEndUserAuthorization = (subject) =>
fetch(
Cypress.env("admin_url") +
"/oauth2/auth/sessions/consent?subject=" +
subject,
)
.then(isStatusOk)
.then((res) => res.json())
export const revokeEndUserAuthorization = (subject) =>
fetch(
Cypress.env("admin_url") +
"/oauth2/auth/sessions/consent?subject=" +
subject,
{ method: "DELETE" },
).then(isStatusOk)
export const createClient = (client) =>
cy
.request("POST", Cypress.env("admin_url") + "/clients", client)
.then(({ body }) =>
getClient(body.client_id).then((actual) => {
if (actual.client_id !== body.client_id) {
return Promise.reject(
new Error(
`Expected client_id's to match: ${actual.client_id} !== ${body.client}`,
),
)
}
return body
}),
)
export const deleteClients = () =>
cy.request(Cypress.env("admin_url") + "/clients").then(({ body = [] }) => {
;(body || []).forEach(({ client_id }) => deleteClient(client_id))
})
const deleteClient = (client_id) =>
cy.request("DELETE", Cypress.env("admin_url") + "/clients/" + client_id)
const getClient = (id) =>
cy
.request(Cypress.env("admin_url") + "/clients/" + id)
.then(({ body }) => body)
export const createGrant = (grant) =>
cy
.request(
"POST",
Cypress.env("admin_url") + "/trust/grants/jwt-bearer/issuers",
JSON.stringify(grant),
)
.then((response) => {
const grantID = response.body.id
getGrant(grantID).then((actual) => {
if (actual.id !== grantID) {
return Promise.reject(
new Error(`Expected id's to match: ${actual.id} !== ${grantID}`),
)
}
return Promise.resolve(response)
})
})
export const getGrant = (grantID) =>
cy
.request(
"GET",
Cypress.env("admin_url") + "/trust/grants/jwt-bearer/issuers/" + grantID,
)
.then(({ body }) => body)
export const deleteGrants = () =>
cy
.request(Cypress.env("admin_url") + "/trust/grants/jwt-bearer/issuers")
.then(({ body = [] }) => {
;(body || []).forEach(({ id }) => deleteGrant(id))
})
const deleteGrant = (id) =>
cy.request(
"DELETE",
Cypress.env("admin_url") + "/trust/grants/jwt-bearer/issuers/" + id,
)