|
| 1 | +const ADMIN_CREDENTIALS = { |
| 2 | + |
| 3 | + password: 'password', |
| 4 | +} |
| 5 | + |
| 6 | +const _USER_WITHOUT_ACCESS = { |
| 7 | + username: 'user_without_access', |
| 8 | + |
| 9 | + password: 'strapiPassword', |
| 10 | +} |
| 11 | + |
| 12 | +describe('wip test refactor', () => { |
| 13 | + // JWT token for admin panel operations (creating users, roles, permissions) |
| 14 | + let adminToken |
| 15 | + |
| 16 | + // Long-lived API token for content management and plugin endpoints |
| 17 | + let apiToken |
| 18 | + |
| 19 | + // Generate unique identifiers for this test run |
| 20 | + const timestamp = Date.now() |
| 21 | + const uniqueEmail = `test.user.${timestamp}@example.com` |
| 22 | + const uniqueRoleName = `Content Manager ${timestamp}` |
| 23 | + |
| 24 | + before(() => { |
| 25 | + // Login as admin to get JWT token for admin panel operations |
| 26 | + cy.request({ |
| 27 | + method: 'POST', |
| 28 | + url: 'http://localhost:1337/admin/login', |
| 29 | + body: { |
| 30 | + email: ADMIN_CREDENTIALS.email, |
| 31 | + password: ADMIN_CREDENTIALS.password, |
| 32 | + }, |
| 33 | + }) |
| 34 | + .then(response => { |
| 35 | + expect(response.status).to.eq(200) |
| 36 | + adminToken = response.body.data.token |
| 37 | + |
| 38 | + // Get the API token created during bootstrap for content/plugin operations |
| 39 | + return cy.request({ |
| 40 | + method: 'GET', |
| 41 | + url: 'http://localhost:1337/admin/api-tokens', |
| 42 | + headers: { |
| 43 | + Authorization: `Bearer ${adminToken}`, |
| 44 | + }, |
| 45 | + }) |
| 46 | + }) |
| 47 | + .then(response => { |
| 48 | + expect(response.status).to.eq(200) |
| 49 | + console.log( |
| 50 | + 'API Tokens response:', |
| 51 | + JSON.stringify(response.body, null, 2), |
| 52 | + ) |
| 53 | + const cypressToken = response.body.data.find( |
| 54 | + token => token.name === 'cypress-test-token', |
| 55 | + ) |
| 56 | + expect(cypressToken).to.exist |
| 57 | + apiToken = cypressToken.accessKey |
| 58 | + console.log('API Token set to:', apiToken) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('should be able to create a new admin user with admin token', () => { |
| 63 | + // Create a new admin user using the admin JWT token |
| 64 | + cy.request({ |
| 65 | + method: 'POST', |
| 66 | + url: 'http://localhost:1337/admin/users', |
| 67 | + headers: { |
| 68 | + Authorization: `Bearer ${adminToken}`, |
| 69 | + }, |
| 70 | + body: { |
| 71 | + firstname: 'Test', |
| 72 | + lastname: 'User', |
| 73 | + email: uniqueEmail, |
| 74 | + roles: [2], // Editor role |
| 75 | + }, |
| 76 | + }).then(response => { |
| 77 | + expect(response.status).to.eq(201) |
| 78 | + expect(response.body.data).to.have.property('id') |
| 79 | + expect(response.body.data.email).to.eq(uniqueEmail) |
| 80 | + expect(response.body.data.firstname).to.eq('Test') |
| 81 | + expect(response.body.data.lastname).to.eq('User') |
| 82 | + expect(response.body.data.roles).to.have.length(1) |
| 83 | + expect(response.body.data.roles[0].code).to.eq('strapi-editor') |
| 84 | + |
| 85 | + // Store the created user ID for cleanup |
| 86 | + Cypress.env('createdUserId', response.body.data.id) |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should be able to create a custom admin role with admin token', () => { |
| 91 | + // Create a new custom admin role using the admin JWT token |
| 92 | + cy.request({ |
| 93 | + method: 'POST', |
| 94 | + url: 'http://localhost:1337/admin/roles', |
| 95 | + headers: { |
| 96 | + Authorization: `Bearer ${adminToken}`, |
| 97 | + }, |
| 98 | + body: { |
| 99 | + name: uniqueRoleName, |
| 100 | + description: 'Can manage content but not system settings', |
| 101 | + }, |
| 102 | + }).then(response => { |
| 103 | + expect(response.status).to.eq(201) |
| 104 | + expect(response.body.data).to.have.property('id') |
| 105 | + expect(response.body.data.name).to.eq(uniqueRoleName) |
| 106 | + expect(response.body.data.description).to.eq( |
| 107 | + 'Can manage content but not system settings', |
| 108 | + ) |
| 109 | + |
| 110 | + // Store the created role ID for cleanup |
| 111 | + Cypress.env('createdRoleId', response.body.data.id) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should be able to list all admin users', () => { |
| 116 | + // List all admin users using the admin JWT token |
| 117 | + cy.request({ |
| 118 | + method: 'GET', |
| 119 | + url: 'http://localhost:1337/admin/users', |
| 120 | + headers: { |
| 121 | + Authorization: `Bearer ${adminToken}`, |
| 122 | + }, |
| 123 | + }).then(response => { |
| 124 | + expect(response.status).to.eq(200) |
| 125 | + expect(response.body.data).to.have.property('results') |
| 126 | + expect(response.body.data.results).to.be.an('array') |
| 127 | + expect(response.body.data.results.length).to.be.greaterThan(0) |
| 128 | + |
| 129 | + // Should include our original admin user |
| 130 | + const adminUser = response.body.data.results.find( |
| 131 | + user => user.email === '[email protected]', |
| 132 | + ) |
| 133 | + expect(adminUser).to.exist |
| 134 | + expect(adminUser.roles[0].code).to.eq('strapi-super-admin') |
| 135 | + }) |
| 136 | + }) |
| 137 | + |
| 138 | + it('should be able to list all admin roles', () => { |
| 139 | + // List all admin roles using the admin JWT token |
| 140 | + cy.request({ |
| 141 | + method: 'GET', |
| 142 | + url: 'http://localhost:1337/admin/roles', |
| 143 | + headers: { |
| 144 | + Authorization: `Bearer ${adminToken}`, |
| 145 | + }, |
| 146 | + }).then(response => { |
| 147 | + expect(response.status).to.eq(200) |
| 148 | + expect(response.body.data).to.be.an('array') |
| 149 | + expect(response.body.data.length).to.be.greaterThan(2) // At least Super Admin, Editor, Author |
| 150 | + |
| 151 | + // Should include the default roles |
| 152 | + const roleNames = response.body.data.map(role => role.name) |
| 153 | + expect(roleNames).to.include('Super Admin') |
| 154 | + expect(roleNames).to.include('Editor') |
| 155 | + expect(roleNames).to.include('Author') |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + // Cleanup tests - run after the main tests |
| 160 | + after(() => { |
| 161 | + // Clean up created user if it exists |
| 162 | + cy.window().then(win => { |
| 163 | + // Check if the alias exists before trying to get it |
| 164 | + if (Cypress.env('createdUserId')) { |
| 165 | + cy.request({ |
| 166 | + method: 'DELETE', |
| 167 | + url: `http://localhost:1337/admin/users/${Cypress.env('createdUserId')}`, |
| 168 | + headers: { |
| 169 | + Authorization: `Bearer ${adminToken}`, |
| 170 | + }, |
| 171 | + failOnStatusCode: false, // Don't fail if user doesn't exist |
| 172 | + }) |
| 173 | + } |
| 174 | + }) |
| 175 | + |
| 176 | + // Clean up created role if it exists |
| 177 | + cy.window().then(win => { |
| 178 | + // Check if the alias exists before trying to get it |
| 179 | + if (Cypress.env('createdRoleId')) { |
| 180 | + cy.request({ |
| 181 | + method: 'DELETE', |
| 182 | + url: `http://localhost:1337/admin/roles/${Cypress.env('createdRoleId')}`, |
| 183 | + headers: { |
| 184 | + Authorization: `Bearer ${adminToken}`, |
| 185 | + }, |
| 186 | + failOnStatusCode: false, // Don't fail if role doesn't exist |
| 187 | + }) |
| 188 | + } |
| 189 | + }) |
| 190 | + }) |
| 191 | +}) |
0 commit comments