| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  | // Helpers.
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const { createStrapiInstance } = require('../../../test/helpers/strapi'); | 
					
						
							| 
									
										
										
										
											2020-03-20 22:24:14 +09:00
										 |  |  | const { createAuthRequest, createRequest } = require('../../../test/helpers/request'); | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | let strapi; | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  | let authReq; | 
					
						
							|  |  |  | const data = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('Test Graphql user service', () => { | 
					
						
							|  |  |  |   beforeAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-11-30 20:20:36 +01:00
										 |  |  |     strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     authReq = await createAuthRequest({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   afterAll(async () => { | 
					
						
							|  |  |  |     await strapi.destroy(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  |   describe('Check createUser authorizations', () => { | 
					
						
							|  |  |  |     test('createUser is forbidden to public', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       const rq = createRequest({ strapi }); | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         url: '/graphql', | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           query: /* GraphQL */ `
 | 
					
						
							|  |  |  |             mutation { | 
					
						
							| 
									
										
										
										
											2020-03-20 22:24:14 +09:00
										 |  |  |               createUser(input: { data: { username: "test", email: "test", password: "test" } }) { | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  |                 user { | 
					
						
							|  |  |  |                   id | 
					
						
							|  |  |  |                   username | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           `,
 | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           createUser: null, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         errors: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             message: 'Forbidden', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('createUser is authorized for admins', async () => { | 
					
						
							|  |  |  |       const res = await authReq({ | 
					
						
							|  |  |  |         url: '/graphql', | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           query: /* GraphQL */ `
 | 
					
						
							|  |  |  |             mutation { | 
					
						
							|  |  |  |               createUser( | 
					
						
							| 
									
										
										
										
											2020-05-18 20:39:39 +02:00
										 |  |  |                 input: { | 
					
						
							|  |  |  |                   data: { username: "test", email: "test-graphql@strapi.io", password: "test" } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  |               ) { | 
					
						
							|  |  |  |                 user { | 
					
						
							|  |  |  |                   id | 
					
						
							|  |  |  |                   username | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           `,
 | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 22:24:14 +09:00
										 |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           createUser: { | 
					
						
							|  |  |  |             user: { | 
					
						
							|  |  |  |               id: expect.anything(), | 
					
						
							|  |  |  |               username: 'test', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       data.user = res.body.data.createUser.user; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Check updateUser authorizations', () => { | 
					
						
							|  |  |  |     test('updateUser is forbidden to public', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       const rq = createRequest({ strapi }); | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         url: '/graphql', | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           query: /* GraphQL */ `
 | 
					
						
							|  |  |  |             mutation { | 
					
						
							|  |  |  |               updateUser( | 
					
						
							|  |  |  |                 input: { | 
					
						
							|  |  |  |                   where: { id: 1 } | 
					
						
							|  |  |  |                   data: { username: "test", email: "test", password: "test" } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               ) { | 
					
						
							|  |  |  |                 user { | 
					
						
							|  |  |  |                   id | 
					
						
							|  |  |  |                   username | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           `,
 | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           updateUser: null, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         errors: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             message: 'Forbidden', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('updateUser is authorized for admins', async () => { | 
					
						
							|  |  |  |       const res = await authReq({ | 
					
						
							|  |  |  |         url: '/graphql', | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           query: /* GraphQL */ `
 | 
					
						
							|  |  |  |             mutation updateUser($id: ID!) { | 
					
						
							| 
									
										
										
										
											2020-03-20 22:24:14 +09:00
										 |  |  |               updateUser(input: { where: { id: $id }, data: { username: "newUsername" } }) { | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  |                 user { | 
					
						
							|  |  |  |                   id | 
					
						
							|  |  |  |                   username | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           `,
 | 
					
						
							|  |  |  |           variables: { | 
					
						
							|  |  |  |             id: data.user.id, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           updateUser: { | 
					
						
							|  |  |  |             user: { | 
					
						
							|  |  |  |               id: expect.anything(), | 
					
						
							|  |  |  |               username: 'newUsername', | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       data.user = res.body.data.updateUser.user; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Check deleteUser authorizations', () => { | 
					
						
							|  |  |  |     test('deleteUser is forbidden to public', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       const rq = createRequest({ strapi }); | 
					
						
							| 
									
										
										
										
											2019-09-18 12:07:59 +02:00
										 |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         url: '/graphql', | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           query: /* GraphQL */ `
 | 
					
						
							|  |  |  |             mutation deleteUser($id: ID!) { | 
					
						
							|  |  |  |               deleteUser(input: { where: { id: $id } }) { | 
					
						
							|  |  |  |                 user { | 
					
						
							|  |  |  |                   id | 
					
						
							|  |  |  |                   username | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           `,
 | 
					
						
							|  |  |  |           variables: { | 
					
						
							|  |  |  |             id: data.user.id, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           deleteUser: null, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         errors: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             message: 'Forbidden', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('deleteUser is authorized for admins', async () => { | 
					
						
							|  |  |  |       const res = await authReq({ | 
					
						
							|  |  |  |         url: '/graphql', | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           query: /* GraphQL */ `
 | 
					
						
							|  |  |  |             mutation deleteUser($id: ID!) { | 
					
						
							|  |  |  |               deleteUser(input: { where: { id: $id } }) { | 
					
						
							|  |  |  |                 user { | 
					
						
							|  |  |  |                   id | 
					
						
							|  |  |  |                   username | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           `,
 | 
					
						
							|  |  |  |           variables: { | 
					
						
							|  |  |  |             id: data.user.id, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           deleteUser: { | 
					
						
							|  |  |  |             user: data.user, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |