| 1 | import { Given, Then, When } from "@badeball/cypress-cucumber-preprocessor"; |
| 2 | |
| 3 | import { loginAsAdmin, logout } from "../common/authentication"; |
| 4 | import { extractOTPSecret, generateOTP, getTokenPeriodInMs } from "./otp"; |
| 5 | |
| 6 | // Used for the When and Then step of otp, as we need ensure the same token is used for both of these steps |
| 7 | let otpToken: string; |
| 8 | |
| 9 | Given("User {string} has OTP enabled", (login: string) => { |
| 10 | loginAsAdmin(); |
| 11 | |
| 12 | cy.visit(Cypress.env("BASE_URL") + "/active-users/" + login); |
| 13 | |
| 14 | cy.dataCy("user-tab-settings-checkbox-ipauserauthtype-otp").check(); |
| 15 | cy.dataCy("user-tab-settings-checkbox-ipauserauthtype-otp").should( |
| 16 | "be.checked" |
| 17 | ); |
| 18 | |
| 19 | cy.dataCy("user-tab-settings-kebab").click(); |
| 20 | cy.dataCy("user-tab-settings-kebab-add-otp-token").click(); |
| 21 | cy.dataCy("add-otp-token-modal").should("exist"); |
| 22 | |
| 23 | cy.dataCy("modal-textbox-clock-interval").type("5"); |
| 24 | cy.dataCy("modal-textbox-clock-interval").should("have.value", "5"); |
| 25 | |
| 26 | cy.dataCy("modal-button-add").click(); |
| 27 | cy.dataCy("add-otp-token-modal").should("not.exist"); |
| 28 | |
| 29 | cy.dataCy("configure-your-token-modal").should("exist"); |
| 30 | cy.dataCy("qr-code-link").should("exist"); |
| 31 | |
| 32 | cy.dataCy("qr-code-link") |
| 33 | .invoke("attr", "href") |
| 34 | .then((qrCodeLink) => generateOTP(extractOTPSecret(qrCodeLink))); |
| 35 | |
| 36 | cy.dataCy("modal-button-ok").click(); |
| 37 | cy.dataCy("configure-your-token-modal").should("not.exist"); |
| 38 | |
| 39 | cy.dataCy("user-tab-settings-button-save").click(); |
| 40 | cy.dataCy("user-tab-settings-button-save").should("be.disabled"); |
| 41 | |
| 42 | logout(); |
| 43 | }); |
| 44 | |
| 45 | When("I type in the {string} textbox text otp token", (textbox: string) => { |
| 46 | cy.wait(getTokenPeriodInMs()); |
| 47 | |
| 48 | otpToken = generateOTP(); |
| 49 | |
| 50 | cy.dataCy(textbox).clear(); |
| 51 | cy.dataCy(textbox).should("have.value", ""); |
| 52 | cy.dataCy(textbox).type(otpToken); |
| 53 | }); |
| 54 | |
| 55 | Then("I should see otp token in the {string} textbox", (textbox: string) => { |
| 56 | cy.dataCy(textbox).should("have.value", otpToken); |
| 57 | }); |
| 58 | |
| 59 | When( |
| 60 | "I log in as {string} with password {string} and generated OTP", |
| 61 | (username: string, password: string) => { |
| 62 | cy.visit(Cypress.env("BASE_URL") + "/login"); |
| 63 | |
| 64 | cy.get("#pf-login-username-id").type(username); |
| 65 | cy.get("#pf-login-username-id").should("have.value", username); |
| 66 | |
| 67 | cy.wait(getTokenPeriodInMs()); |
| 68 | |
| 69 | otpToken = generateOTP(); |
| 70 | |
| 71 | cy.get("#pf-login-password-id").type(password + otpToken); |
| 72 | cy.get("#pf-login-password-id").should("have.value", password + otpToken); |
| 73 | |
| 74 | cy.get("button[type='submit']").click(); |
| 75 | } |
| 76 | ); |