-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathkc-fake-login.ts
69 lines (55 loc) · 1.69 KB
/
kc-fake-login.ts
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
import { createUUID, decodeToken } from "./utils";
Cypress.Commands.add("kcFakeLogin", (user: string, visitUrl = "") => {
Cypress.log({ name: "Fake Login" });
return cy.fixture(`users/${user}`).then((userData: UserData) => {
if (!userData.fakeLogin) {
throw new Error(
"To use kcFakeLogin command you should define fakeLogin data in fixture"
);
}
const authBaseUrl = Cypress.env("auth_base_url");
const realm = Cypress.env("auth_realm");
const {
account,
access_token,
refresh_token,
id_token
} = userData.fakeLogin;
const state = createUUID();
const { nonce } = decodeToken(access_token);
const token = {
access_token,
expires_in: 300,
refresh_expires_in: 1800,
refresh_token,
token_type: "bearer",
id_token,
"not-before-policy": 0,
session_state: createUUID(),
scope: "openid"
};
const localStorageObj = {
state,
nonce,
expires: Date() + 3600
};
const localStorageKey = `kc-callback-${state}`;
window.localStorage.setItem(
localStorageKey,
JSON.stringify(localStorageObj)
);
cy.server();
cy.route(
"post",
`${authBaseUrl}/realms/${realm}/protocol/openid-connect/token`,
token
);
cy.route(`${authBaseUrl}/realms/${realm}/account`, account);
// in case visitUrl is an url with a hash, a second hash should not be added to the url
const joiningCharacter = visitUrl.indexOf("#") === -1 ? "#" : "&";
const url = `${
Cypress.config().baseUrl
}/${visitUrl}${joiningCharacter}state=${state}&session_state=${createUUID()}&code=${createUUID()}`;
cy.visit(url);
});
});