Skip to content

Commit e70e1c4

Browse files
committed
Fix plugin access
1 parent 1d6db06 commit e70e1c4

File tree

2 files changed

+34
-270
lines changed

2 files changed

+34
-270
lines changed

cypress/e2e/new-roles.cy.js

Lines changed: 34 additions & 270 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { STRAPI_ADMIN_ROLES } from './utils'
2-
3-
const ADMIN_CREDENTIALS = {
4-
5-
password: 'password',
6-
}
1+
const {
2+
env,
3+
[env]: { adminUrl },
4+
} = Cypress.env()
75

86
const USER_WITH_ACCESS_CREDENTIALS = {
97
@@ -16,116 +14,17 @@ const USER_WITHOUT_ACCESS_CREDENTIALS = {
1614
}
1715

1816
describe('wip test refactor', () => {
19-
// JWT token for admin panel operations (creating users, roles, permissions)
20-
let adminToken
21-
22-
// Long-lived API token for content management and plugin endpoints
23-
let apiToken
24-
25-
// Generate unique identifiers for this test run
26-
const timestamp = Date.now()
27-
const uniqueEmail = `test.user.${timestamp}@example.com`
28-
const uniqueRoleName = `Content Manager ${timestamp}`
29-
30-
// TODO: refactor as Cypress command
31-
const loginAsAdmin = (email, password) => {
32-
return cy.request({
33-
method: 'POST',
34-
url: 'http://localhost:1337/admin/login',
35-
body: {
36-
email,
37-
password,
38-
},
39-
})
40-
}
41-
42-
// TODO: refactor as Cypress command
43-
const createUser = ({ firstname, password, email, roleIds }) => {
44-
return cy
45-
.request({
46-
method: 'POST',
47-
url: 'http://localhost:1337/admin/users',
48-
headers: {
49-
Authorization: `Bearer ${adminToken}`,
50-
},
51-
body: {
52-
firstname: firstname,
53-
email: email,
54-
roles: roleIds,
55-
},
56-
})
57-
.then(createdUser => {
58-
expect(createdUser.status).to.eq(201)
59-
60-
return cy
61-
.request({
62-
method: 'PUT',
63-
url: `http://localhost:1337/admin/users/${createdUser.body.data.id}`,
64-
headers: { Authorization: `Bearer ${adminToken}` },
65-
body: {
66-
isActive: true,
67-
password: password,
68-
},
69-
})
70-
.then(updatedUser => {
71-
expect(updatedUser.status).to.eq(200)
72-
expect(updatedUser.body.data.isActive).to.be.true
73-
return updatedUser.body.data
74-
})
75-
})
76-
}
77-
7817
// TODO: refactor as Cypress command
7918
const loginUser = ({ email, password }) => {
80-
cy.visit('http://localhost:1337/admin')
19+
cy.visit(`${adminUrl}`)
8120
cy.get('form').should('be.visible')
8221
cy.get('input[name="email"]').type(email)
8322
cy.get('input[name="password"]').type(password)
8423
cy.get('button[role="checkbox"]').click()
8524
cy.get('button[type="submit"]').click()
8625
}
8726

88-
// before(() => {
89-
// // Login as admin to get JWT token for admin panel operations
90-
// loginAsAdmin(ADMIN_CREDENTIALS.email, ADMIN_CREDENTIALS.password)
91-
// .then(response => {
92-
// expect(response.status).to.eq(200)
93-
// adminToken = response.body.data.token
94-
95-
// // Get the API token created during bootstrap for content/plugin operations
96-
// return cy.request({
97-
// method: 'GET',
98-
// url: 'http://localhost:1337/admin/api-tokens',
99-
// headers: {
100-
// Authorization: `Bearer ${adminToken}`,
101-
// },
102-
// })
103-
// })
104-
// .then(response => {
105-
// expect(response.status).to.eq(200)
106-
// // console.log(
107-
// // 'API Tokens response:',
108-
// // JSON.stringify(response.body, null, 2),
109-
// // )
110-
// // const cypressToken = response.body.data.find(
111-
// // token => token.name === 'cypress-test-token',
112-
// // )
113-
// // expect(cypressToken).to.exist
114-
// // apiToken = cypressToken.accessKey
115-
// // console.log('API Token set to:', apiToken)
116-
// })
117-
// })
118-
11927
describe('admin user without plugin access', () => {
120-
// before(() => {
121-
// createUser({
122-
// firstname: 'Admin No Access',
123-
// email: userCredentials.email,
124-
// password: userCredentials.password,
125-
// roleIds: [STRAPI_ADMIN_ROLES.EDITOR],
126-
// })
127-
// })
128-
12928
beforeEach(() => {
13029
cy.session(
13130
USER_WITHOUT_ACCESS_CREDENTIALS.email,
@@ -134,193 +33,58 @@ describe('wip test refactor', () => {
13433
email: USER_WITHOUT_ACCESS_CREDENTIALS.email,
13534
password: USER_WITHOUT_ACCESS_CREDENTIALS.password,
13635
})
137-
138-
// TODO: assert `strapi_admin_refresh` cookie exists
139-
// cy.wait('@adminLogin')
140-
// .its('response.headers.set-cookie')
141-
// .should(
142-
// 'satisfy',
143-
// cookies =>
144-
// Array.isArray(cookies) &&
145-
// cookies.some(c => c.startsWith('strapi_admin_refresh=')),
146-
// )
14736
},
14837
{
14938
validate() {
150-
// TODO: uses `strapi_admin_refresh` cookie to validate login
151-
// cy.getCookie('strapi_admin_refresh').should('exist')
152-
15339
cy.wait(1000)
15440
cy.contains('Hello User without access').should('be.visible')
15541
},
15642
},
15743
)
15844
})
15945

160-
// it('works', () => {
161-
// expect(true).to.be.true
162-
163-
// // tests were green, so I added this to trigger hot reloading
164-
// expect(false).to.be.false
165-
// })
166-
16746
it('should not see the plugin in sidepanel', () => {
168-
cy.visit('http://localhost:1337/admin')
47+
cy.visit(`${adminUrl}`)
16948
cy.get('nav').should('not.contain', 'a[aria-label="Meilisearch"]')
17049
})
17150

17251
it('cannot access the plugin page', () => {
173-
cy.visit('http://localhost:1337/admin/plugins/meilisearch')
52+
cy.visit(`${adminUrl}/plugins/meilisearch`)
17453
cy.contains(
17554
"You don't have the permissions to access that content",
17655
).should('be.visible')
17756
})
17857
})
17958

18059
describe('admin user with plugin access', () => {
181-
const userCredentials = {
182-
email: `with-access-${timestamp}@example.com`,
183-
password: 'strapiPassword1234',
184-
username: `with-access-${timestamp}`,
185-
}
186-
187-
before(() => {
188-
createUser({
189-
firstname: 'Admin With Access',
190-
email: userCredentials.email,
191-
password: userCredentials.password,
192-
roleIds: [STRAPI_ADMIN_ROLES.EDITOR],
193-
})
60+
beforeEach(() => {
61+
cy.session(
62+
USER_WITH_ACCESS_CREDENTIALS.email,
63+
() => {
64+
loginUser({
65+
email: USER_WITH_ACCESS_CREDENTIALS.email,
66+
password: USER_WITH_ACCESS_CREDENTIALS.password,
67+
})
68+
},
69+
{
70+
validate() {
71+
cy.wait(1000)
72+
cy.contains('Hello User with access').should('be.visible')
73+
},
74+
},
75+
)
19476
})
195-
})
196-
197-
// it('should be able to create a new admin user with admin token', () => {
198-
// // Create a new admin user using the admin JWT token
199-
// cy.request({
200-
// method: 'POST',
201-
// url: 'http://localhost:1337/admin/users',
202-
// headers: {
203-
// Authorization: `Bearer ${adminToken}`,
204-
// },
205-
// body: {
206-
// firstname: 'Test',
207-
// lastname: 'User',
208-
// email: uniqueEmail,
209-
// roles: [2], // Editor role
210-
// },
211-
// }).then(response => {
212-
// expect(response.status).to.eq(201)
213-
// expect(response.body.data).to.have.property('id')
214-
// expect(response.body.data.email).to.eq(uniqueEmail)
215-
// expect(response.body.data.firstname).to.eq('Test')
216-
// expect(response.body.data.lastname).to.eq('User')
217-
// expect(response.body.data.roles).to.have.length(1)
218-
// expect(response.body.data.roles[0].code).to.eq('strapi-editor')
219-
220-
// // Store the created user ID for cleanup
221-
// Cypress.env('createdUserId', response.body.data.id)
222-
// })
223-
// })
224-
225-
// it('should be able to create a custom admin role with admin token', () => {
226-
// // Create a new custom admin role using the admin JWT token
227-
// cy.request({
228-
// method: 'POST',
229-
// url: 'http://localhost:1337/admin/roles',
230-
// headers: {
231-
// Authorization: `Bearer ${adminToken}`,
232-
// },
233-
// body: {
234-
// name: uniqueRoleName,
235-
// description: 'Can manage content but not system settings',
236-
// },
237-
// }).then(response => {
238-
// expect(response.status).to.eq(201)
239-
// expect(response.body.data).to.have.property('id')
240-
// expect(response.body.data.name).to.eq(uniqueRoleName)
241-
// expect(response.body.data.description).to.eq(
242-
// 'Can manage content but not system settings',
243-
// )
244-
245-
// // Store the created role ID for cleanup
246-
// Cypress.env('createdRoleId', response.body.data.id)
247-
// })
248-
// })
24977

250-
// it('should be able to list all admin users', () => {
251-
// // List all admin users using the admin JWT token
252-
// cy.request({
253-
// method: 'GET',
254-
// url: 'http://localhost:1337/admin/users',
255-
// headers: {
256-
// Authorization: `Bearer ${adminToken}`,
257-
// },
258-
// }).then(response => {
259-
// expect(response.status).to.eq(200)
260-
// expect(response.body.data).to.have.property('results')
261-
// expect(response.body.data.results).to.be.an('array')
262-
// expect(response.body.data.results.length).to.be.greaterThan(0)
78+
it.only('can access the plugin page', () => {
79+
cy.visit(`${adminUrl}`)
80+
cy.get('nav')
81+
.get('a[aria-label="Meilisearch"]', { timeout: 10000 })
82+
.should('be.visible')
83+
cy.get('nav')
84+
.get('a[aria-label="Meilisearch"]', { timeout: 10000 })
85+
.click()
26386

264-
// // Should include our original admin user
265-
// const adminUser = response.body.data.results.find(
266-
// user => user.email === '[email protected]',
267-
// )
268-
// expect(adminUser).to.exist
269-
// expect(adminUser.roles[0].code).to.eq('strapi-super-admin')
270-
// })
271-
// })
272-
273-
// it('should be able to list all admin roles', () => {
274-
// // List all admin roles using the admin JWT token
275-
// cy.request({
276-
// method: 'GET',
277-
// url: 'http://localhost:1337/admin/roles',
278-
// headers: {
279-
// Authorization: `Bearer ${adminToken}`,
280-
// },
281-
// }).then(response => {
282-
// expect(response.status).to.eq(200)
283-
// expect(response.body.data).to.be.an('array')
284-
// expect(response.body.data.length).to.be.greaterThan(2) // At least Super Admin, Editor, Author
285-
286-
// // Should include the default roles
287-
// const roleNames = response.body.data.map(role => role.name)
288-
// expect(roleNames).to.include('Super Admin')
289-
// expect(roleNames).to.include('Editor')
290-
// expect(roleNames).to.include('Author')
291-
// })
292-
// })
293-
294-
// Cleanup tests - run after the main tests
295-
// after(() => {
296-
// // Clean up created user if it exists
297-
// cy.window().then(win => {
298-
// // Check if the alias exists before trying to get it
299-
// if (Cypress.env('createdUserId')) {
300-
// cy.request({
301-
// method: 'DELETE',
302-
// url: `http://localhost:1337/admin/users/${Cypress.env('createdUserId')}`,
303-
// headers: {
304-
// Authorization: `Bearer ${adminToken}`,
305-
// },
306-
// failOnStatusCode: false, // Don't fail if user doesn't exist
307-
// })
308-
// }
309-
// })
310-
311-
// // Clean up created role if it exists
312-
// cy.window().then(win => {
313-
// // Check if the alias exists before trying to get it
314-
// if (Cypress.env('createdRoleId')) {
315-
// cy.request({
316-
// method: 'DELETE',
317-
// url: `http://localhost:1337/admin/roles/${Cypress.env('createdRoleId')}`,
318-
// headers: {
319-
// Authorization: `Bearer ${adminToken}`,
320-
// },
321-
// failOnStatusCode: false, // Don't fail if role doesn't exist
322-
// })
323-
// }
324-
// })
325-
// })
87+
cy.url().should('eq', `${adminUrl}/plugins/meilisearch`)
88+
})
89+
})
32690
})

playground/pre-seeded-database.db

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)