Some checks failed
PR Preview / teardown-preview (pull_request) Has been skipped
Test / unit-test (push) Successful in 5s
Test / build-check (push) Successful in 4s
PR Preview / test (pull_request) Successful in 5s
PR Preview / deploy-preview (pull_request) Successful in 12s
Test / e2e-test (push) Failing after 2m14s
根本原因: 所有测试硬编码了只在生产环境有效的admin token, CI创建新数据库时token不同导致全部认证失败。 修复: - CI: 设置已知ADMIN_TOKEN环境变量传给后端和Cypress - cypress/support/e2e.js: 新增cy.getAdminToken()动态获取token - 24个spec文件: 硬编码token改为cy.getAdminToken() - UI选择器: 适配管理页面从tab移到UserMenu、编辑器DOM变化 - API: create_recipe→share_recipe、ingredients格式、权限变化 - 超时: 300s→420s适应32个spec Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
245 lines
7.3 KiB
JavaScript
245 lines
7.3 KiB
JavaScript
describe('User Management Flow', () => {
|
|
let adminToken
|
|
let authHeaders
|
|
const TEST_USERNAME = 'cypress_test_user_e2e'
|
|
const TEST_DISPLAY_NAME = 'Cypress E2E Test User'
|
|
let testUserId = null
|
|
|
|
before(() => {
|
|
cy.getAdminToken().then(token => {
|
|
adminToken = token
|
|
authHeaders = { Authorization: `Bearer ${token}` }
|
|
})
|
|
})
|
|
|
|
describe('API: user lifecycle', () => {
|
|
// Cleanup any leftover test user first
|
|
before(() => {
|
|
cy.getAdminToken().then(token => {
|
|
const headers = { Authorization: `Bearer ${token}` }
|
|
cy.request({ url: '/api/users', headers }).then(res => {
|
|
const leftover = res.body.find(u => u.username === TEST_USERNAME)
|
|
if (leftover) {
|
|
cy.request({
|
|
method: 'DELETE',
|
|
url: `/api/users/${leftover.id || leftover._id}`,
|
|
headers,
|
|
failOnStatusCode: false
|
|
})
|
|
}
|
|
})
|
|
})
|
|
})
|
|
|
|
it('creates a new test user via API', () => {
|
|
cy.request({
|
|
method: 'POST',
|
|
url: '/api/users',
|
|
headers: authHeaders,
|
|
body: {
|
|
username: TEST_USERNAME,
|
|
display_name: TEST_DISPLAY_NAME,
|
|
role: 'viewer'
|
|
}
|
|
}).then(res => {
|
|
expect(res.status).to.be.oneOf([200, 201])
|
|
testUserId = res.body.id || res.body._id
|
|
// Should return a token for the new user
|
|
if (res.body.token) {
|
|
expect(res.body.token).to.be.a('string')
|
|
}
|
|
})
|
|
})
|
|
|
|
it('verifies the user appears in the user list', () => {
|
|
cy.request({
|
|
url: '/api/users',
|
|
headers: authHeaders
|
|
}).then(res => {
|
|
expect(res.body).to.be.an('array')
|
|
const found = res.body.find(u => u.username === TEST_USERNAME)
|
|
expect(found).to.exist
|
|
expect(found.display_name).to.eq(TEST_DISPLAY_NAME)
|
|
expect(found.role).to.eq('viewer')
|
|
testUserId = found.id || found._id
|
|
})
|
|
})
|
|
|
|
it('updates user role to editor', () => {
|
|
cy.request({
|
|
url: '/api/users',
|
|
headers: authHeaders
|
|
}).then(res => {
|
|
const found = res.body.find(u => u.username === TEST_USERNAME)
|
|
testUserId = found.id || found._id
|
|
cy.request({
|
|
method: 'PUT',
|
|
url: `/api/users/${testUserId}`,
|
|
headers: authHeaders,
|
|
body: { role: 'editor' }
|
|
}).then(res => {
|
|
expect(res.status).to.eq(200)
|
|
})
|
|
})
|
|
})
|
|
|
|
it('verifies role was updated', () => {
|
|
cy.request({
|
|
url: '/api/users',
|
|
headers: authHeaders
|
|
}).then(res => {
|
|
const found = res.body.find(u => u.username === TEST_USERNAME)
|
|
expect(found.role).to.eq('editor')
|
|
})
|
|
})
|
|
|
|
it('deletes the test user', () => {
|
|
cy.request({
|
|
url: '/api/users',
|
|
headers: authHeaders
|
|
}).then(res => {
|
|
const found = res.body.find(u => u.username === TEST_USERNAME)
|
|
if (found) {
|
|
testUserId = found.id || found._id
|
|
cy.request({
|
|
method: 'DELETE',
|
|
url: `/api/users/${testUserId}`,
|
|
headers: authHeaders
|
|
}).then(res => {
|
|
expect(res.status).to.eq(200)
|
|
})
|
|
}
|
|
})
|
|
})
|
|
|
|
it('verifies the user is deleted', () => {
|
|
cy.request({
|
|
url: '/api/users',
|
|
headers: authHeaders
|
|
}).then(res => {
|
|
const found = res.body.find(u => u.username === TEST_USERNAME)
|
|
expect(found).to.not.exist
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('UI: users page renders', () => {
|
|
it('visits /users and verifies page structure', () => {
|
|
cy.visit('/users', {
|
|
onBeforeLoad(win) {
|
|
win.localStorage.setItem('oil_auth_token', adminToken)
|
|
}
|
|
})
|
|
cy.get('.user-management', { timeout: 10000 }).should('exist')
|
|
cy.contains('用户管理').should('be.visible')
|
|
})
|
|
|
|
it('shows search input and role filter buttons', () => {
|
|
cy.visit('/users', {
|
|
onBeforeLoad(win) {
|
|
win.localStorage.setItem('oil_auth_token', adminToken)
|
|
}
|
|
})
|
|
cy.get('.user-management', { timeout: 10000 }).should('exist')
|
|
// Search box
|
|
cy.get('.search-input').should('exist')
|
|
// Role filter buttons
|
|
cy.get('.filter-btn').should('have.length.gte', 1)
|
|
cy.get('.filter-btn').contains('编辑').should('exist')
|
|
})
|
|
|
|
it('displays user list with user cards', () => {
|
|
cy.visit('/users', {
|
|
onBeforeLoad(win) {
|
|
win.localStorage.setItem('oil_auth_token', adminToken)
|
|
}
|
|
})
|
|
cy.get('.user-management', { timeout: 10000 }).should('exist')
|
|
cy.get('.user-card', { timeout: 5000 }).should('have.length.gte', 1)
|
|
// Each card shows name and role
|
|
cy.get('.user-card').first().within(() => {
|
|
cy.get('.user-name').should('not.be.empty')
|
|
cy.get('.user-role-badge').should('exist')
|
|
})
|
|
})
|
|
|
|
it('search filters users', () => {
|
|
cy.visit('/users', {
|
|
onBeforeLoad(win) {
|
|
win.localStorage.setItem('oil_auth_token', adminToken)
|
|
}
|
|
})
|
|
cy.get('.user-management', { timeout: 10000 }).should('exist')
|
|
cy.get('.user-card').then($cards => {
|
|
const total = $cards.length
|
|
// Search for something specific
|
|
cy.get('.search-input').type('admin')
|
|
cy.wait(300)
|
|
cy.get('.user-card').should('have.length.lte', total)
|
|
})
|
|
})
|
|
|
|
it('role filter narrows user list', () => {
|
|
cy.visit('/users', {
|
|
onBeforeLoad(win) {
|
|
win.localStorage.setItem('oil_auth_token', adminToken)
|
|
}
|
|
})
|
|
cy.get('.user-management', { timeout: 10000 }).should('exist')
|
|
cy.get('.user-card').then($cards => {
|
|
const total = $cards.length
|
|
// Click a role filter
|
|
cy.get('.filter-btn').contains('查看者').click()
|
|
cy.wait(300)
|
|
cy.get('.user-card').should('have.length.lte', total)
|
|
// Clicking again deactivates the filter
|
|
cy.get('.filter-btn').contains('查看者').click()
|
|
cy.wait(300)
|
|
cy.get('.user-card').should('have.length', total)
|
|
})
|
|
})
|
|
|
|
it('shows user count', () => {
|
|
cy.visit('/users', {
|
|
onBeforeLoad(win) {
|
|
win.localStorage.setItem('oil_auth_token', adminToken)
|
|
}
|
|
})
|
|
cy.get('.user-management', { timeout: 10000 }).should('exist')
|
|
cy.get('.user-count').should('contain', '个用户')
|
|
})
|
|
|
|
it('page has user management container', () => {
|
|
cy.visit('/users', {
|
|
onBeforeLoad(win) {
|
|
win.localStorage.setItem('oil_auth_token', adminToken)
|
|
}
|
|
})
|
|
cy.get('.user-management', { timeout: 10000 }).should('exist')
|
|
// Verify the page loaded with user data
|
|
cy.get('.user-card').should('have.length.gte', 1)
|
|
})
|
|
})
|
|
|
|
// Safety cleanup
|
|
after(() => {
|
|
cy.request({
|
|
url: '/api/users',
|
|
headers: authHeaders,
|
|
failOnStatusCode: false
|
|
}).then(res => {
|
|
if (res.status === 200 && Array.isArray(res.body)) {
|
|
const testUsers = res.body.filter(u => u.username === TEST_USERNAME)
|
|
testUsers.forEach(user => {
|
|
cy.request({
|
|
method: 'DELETE',
|
|
url: `/api/users/${user.id || user._id}`,
|
|
headers: authHeaders,
|
|
failOnStatusCode: false
|
|
})
|
|
})
|
|
}
|
|
})
|
|
})
|
|
})
|