2024-05-30 03:12:56 -04:00
|
|
|
import { runScript } from './hostAdminClient'
|
|
|
|
|
|
|
|
const DEFAULT_PASSWORD = 'Passw0rd!'
|
|
|
|
|
|
|
|
const createdUsers = new Set<string>()
|
|
|
|
|
2024-06-11 07:01:21 -04:00
|
|
|
export function resetCreatedUsersCache() {
|
|
|
|
createdUsers.clear()
|
|
|
|
}
|
|
|
|
|
2024-06-11 08:02:25 -04:00
|
|
|
export async function createMongoUser({
|
2024-05-30 03:12:56 -04:00
|
|
|
email,
|
|
|
|
isAdmin = false,
|
|
|
|
}: {
|
|
|
|
email: string
|
|
|
|
isAdmin?: boolean
|
|
|
|
}) {
|
2024-06-11 07:01:21 -04:00
|
|
|
const t0 = Math.floor(Date.now() / 1000)
|
2024-05-30 03:12:56 -04:00
|
|
|
const { stdout } = await runScript({
|
|
|
|
cwd: 'services/web',
|
|
|
|
script: 'modules/server-ce-scripts/scripts/create-user.js',
|
|
|
|
args: [`--email=${email}`, `--admin=${isAdmin}`],
|
|
|
|
})
|
2024-08-02 08:10:06 -04:00
|
|
|
const [url] = stdout.match(/http:\/\/.+\/user\/activate\?token=\S+/)!
|
2024-05-30 03:12:56 -04:00
|
|
|
const userId = new URL(url, location.origin).searchParams.get('user_id')!
|
2024-06-11 07:01:21 -04:00
|
|
|
const signupDate = parseInt(userId.slice(0, 8), 16)
|
2024-05-30 03:12:56 -04:00
|
|
|
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) {
|
2024-08-06 05:10:03 -04:00
|
|
|
cy.session(
|
|
|
|
[username, password],
|
|
|
|
() => {
|
2024-05-30 03:13:40 -04:00
|
|
|
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')
|
2024-08-06 05:10:03 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
cacheAcrossSpecs: true,
|
|
|
|
async validate() {
|
|
|
|
cy.request({ url: '/project', followRedirect: false }).then(
|
|
|
|
response => {
|
|
|
|
expect(response.status).to.equal(200)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2023-11-23 05:40:13 -05:00
|
|
|
}
|
2024-05-30 03:12:56 -04:00
|
|
|
|
2024-08-02 08:10:06 -04:00
|
|
|
let activateRateLimitState = { count: 0, reset: 0 }
|
|
|
|
export function resetActivateUserRateLimit() {
|
|
|
|
activateRateLimitState = { count: 0, reset: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleActivateUserRateLimit() {
|
|
|
|
cy.then(() => {
|
|
|
|
activateRateLimitState.count++
|
|
|
|
if (activateRateLimitState.reset < Date.now()) {
|
|
|
|
activateRateLimitState.reset = Date.now() + 65_000
|
|
|
|
activateRateLimitState.count = 1
|
|
|
|
} else if (activateRateLimitState.count >= 6) {
|
|
|
|
cy.wait(activateRateLimitState.reset - Date.now())
|
|
|
|
activateRateLimitState.count = 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
export function activateUser(url: string, password = DEFAULT_PASSWORD) {
|
2024-08-02 08:10:06 -04:00
|
|
|
handleActivateUserRateLimit()
|
|
|
|
|
2024-05-30 03:12:56 -04:00
|
|
|
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')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|