overleaf/server-ce/test/helpers/login.ts
Jakob Ackermann 585d72f1a6 Merge pull request #18745 from overleaf/jpa-active-uses-correct-email
[server-pro] add test for rendering of user activate page

GitOrigin-RevId: 3f0b8f7fa39698b739c9c5259da29fdd88b9caf6
2024-06-12 08:05:02 +00:00

84 lines
2.2 KiB
TypeScript

import { runScript } from './hostAdminClient'
const DEFAULT_PASSWORD = 'Passw0rd!'
const createdUsers = new Set<string>()
export function resetCreatedUsersCache() {
createdUsers.clear()
}
export async function createMongoUser({
email,
isAdmin = false,
}: {
email: string
isAdmin?: boolean
}) {
const t0 = Math.floor(Date.now() / 1000)
const { stdout } = await runScript({
cwd: 'services/web',
script: 'modules/server-ce-scripts/scripts/create-user.js',
args: [`--email=${email}`, `--admin=${isAdmin}`],
})
const [url] = stdout.match(/\/user\/activate\?token=\S+/)!
const userId = new URL(url, location.origin).searchParams.get('user_id')!
const signupDate = parseInt(userId.slice(0, 8), 16)
if (signupDate < t0) {
return { url, exists: true }
}
return { url, exists: false }
}
export function ensureUserExists({
email,
password = DEFAULT_PASSWORD,
isAdmin = false,
}: {
email: string
password?: string
isAdmin?: boolean
}) {
let url: string
let exists: boolean
before(async function () {
exists = createdUsers.has(email)
if (exists) return
;({ url, exists } = await createMongoUser({ email, isAdmin }))
})
before(function () {
if (exists) return
activateUser(url, password)
cy.then(() => {
createdUsers.add(email)
})
})
}
export function login(username: string, password = DEFAULT_PASSWORD) {
const id = [username, password, new Date()]
function startOrResumeSession() {
cy.session(id, () => {
cy.visit('/login')
cy.get('input[name="email"]').type(username)
cy.get('input[name="password"]').type(password)
cy.findByRole('button', { name: 'Login' }).click()
cy.url().should('contain', '/project')
})
}
startOrResumeSession()
return startOrResumeSession
}
export function activateUser(url: string, password = DEFAULT_PASSWORD) {
cy.session(url, () => {
cy.visit(url)
cy.url().then(url => {
if (url.includes('/login')) return
cy.url().should('contain', '/user/activate')
cy.get('input[name="password"]').type(password)
cy.findByRole('button', { name: 'Activate' }).click()
cy.url().should('contain', '/project')
})
})
}