Skip to content

Commit

Permalink
test(e2e): add more typings for cypress helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 19, 2021
1 parent 2625d16 commit 60bd63f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 15 deletions.
12 changes: 6 additions & 6 deletions test/e2e/cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ Cypress.Commands.add('browserReturnUrlOry', ({} = {}) => {
})
})

Cypress.Commands.add('loginOidc', ({expectSession = true}) => {
cy.visit(APP_URL + '/auth/login')
Cypress.Commands.add('loginOidc', ({expectSession = true, url=APP_URL + '/login'}) => {
cy.visit(url)
cy.get('button[value="hydra"]').click()
if (expectSession) {
cy.getSession()
Expand All @@ -297,15 +297,15 @@ Cypress.Commands.add('loginOidc', ({expectSession = true}) => {
}
})

Cypress.Commands.add('login', ({email, password, expectSession = true}) => {
Cypress.Commands.add('login', ({email, password, expectSession = true, cookieUrl = APP_URL}) => {
if (expectSession) {
console.log('Singing in user: ', {email, password})
} else {
console.log('Attempting user sign in: ', {email, password})
}

// see https://github.com/cypress-io/cypress/issues/408
cy.visit(APP_URL)
cy.visit(cookieUrl)
cy.clearCookies()

cy.longPrivilegedSessionTime()
Expand Down Expand Up @@ -369,7 +369,7 @@ Cypress.Commands.add(
expect: {email},
type: {email: temail, password: tpassword} = {email: undefined, password: undefined}
}) => {
cy.url().should('include', '/auth/login')
cy.url().should('include', '/login')
cy.get('input[name="password_identifier"]').should('have.value', email)
if (temail) {
cy.get('input[name="password_identifier"]').clear().type(temail)
Expand Down Expand Up @@ -406,7 +406,7 @@ Cypress.Commands.add('deleteMail', ({atLeast = 0} = {}) => {
})

Cypress.Commands.add(
'session',
'getSession',
({expectAal = 'aal1', expectMethods = []} = {}) =>
cy.request('GET', `${KRATOS_PUBLIC}/sessions/whoami`).then((response) => {
expect(response.body.id).to.not.be.empty
Expand Down
74 changes: 65 additions & 9 deletions test/e2e/cypress/support/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {Session} from "@ory/kratos-client";
import {gen} from "../helpers";

export interface MailMessage {
fromAddress: string
toAddresses: Array<string>
body:string
subject:string
body: string
subject: string
}

declare global {
Expand All @@ -26,17 +25,36 @@ declare global {
* @param opts
*/
getSession(opts?:
{
expectAal: 'aal2' |'aal1',
expectMethods: Array<'password' | 'webauthn'>,
}
{
expectAal: 'aal2' | 'aal1',
expectMethods: Array<'password' | 'webauthn'>,
}
): Chainable<Session>

/**
* Expect that the browser has no valid Ory Kratos Cookie Session.
*/
noSession(): Chainable<Response<any>>

/**
* Log a user in
*
* @param opts
*/
login(opts: { email: string, password: string, expectSession?: boolean, cookieUrl?: string }): Chainable<Response<Session | undefined>>

/**
* Sign up a user
*
* @param opts
*/
register(opts: {
email: string,
password: string,
query?: { [key: string]: string },
fields?: { [key: string]: any }
}): Chainable<Response<void>>

/**
* Set the "privileged session lifespan" to a large value.
*/
Expand All @@ -49,7 +67,7 @@ declare global {
*/
getMail(opts?: { removeMail: boolean }): Chainable<MailMessage>

performEmailVerification(opts?: {expect?: {email?: string, redirectTo?: string}}): Chainable<void>
performEmailVerification(opts?: { expect?: { email?: string, redirectTo?: string } }): Chainable<void>

/**
* Sets the Ory Kratos configuration profile.
Expand All @@ -65,7 +83,7 @@ declare global {
*
* @param opts
*/
registerApi(opts?: {email:string, password:string, fields: { [key: string]: string }}): Chainable<Session>
registerApi(opts?: { email: string, password: string, fields: { [key: string]: string } }): Chainable<Session>

/**
* Changes the config so that the login flow lifespan is very short.
Expand All @@ -80,6 +98,7 @@ declare global {
* Changes the config so that the login flow lifespan is very long.
*
* Useful when testing expiry of login flows.
*
* @see shortLoginLifespan()
*/
longLoginLifespan(): Chainable<void>
Expand All @@ -88,6 +107,43 @@ declare global {
* Change the config so that `https://www.ory.sh/` is a allowed return to URL.
*/
browserReturnUrlOry(): Chainable<void>

/**
* Changes the config so that the registration flow lifespan is very short.
*
* Useful when testing expiry of registration flows.
*
* @see longRegisterLifespan()
*/
shortRegisterLifespan(): Chainable<void>

/**
* Changes the config so that the registration flow lifespan is very long.
*
* Useful when testing expiry of registration flows.
*
* @see shortRegisterLifespan()
*/
longRegisterLifespan(): Chainable<void>

/**
* Changes the config so that the settings privileged lifespan is very long.
*
* Useful when testing privileged settings flows.
*
* @see longPrivilegedSessionTime()
*/
shortPrivilegedSessionTime(): Chainable<void>

/**
* Re-authenticates a user.
*
* @param opts
*/
reauth(opts: {
expect: { email },
type: { email?: string, password?: string }
}): Chainable<void>
}
}
}

0 comments on commit 60bd63f

Please sign in to comment.