| 
									
										
										
										
											2022-08-03 22:43:03 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-05 10:32:20 +02:00
										 |  |  | const { createStrapiInstance } = require('api-tests/strapi'); | 
					
						
							|  |  |  | const { createRequest } = require('api-tests/request'); | 
					
						
							| 
									
										
										
										
											2022-08-03 22:43:03 +02:00
										 |  |  | const { createAuthenticatedUser } = require('../utils'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let strapi; | 
					
						
							|  |  |  | let rq; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const internals = { | 
					
						
							|  |  |  |   user: { | 
					
						
							|  |  |  |     username: 'test', | 
					
						
							|  |  |  |     email: 'test@strapi.io', | 
					
						
							|  |  |  |     password: 'Test1234', | 
					
						
							|  |  |  |     confirmed: true, | 
					
						
							|  |  |  |     provider: 'local', | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   newPassword: 'Test12345', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const data = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('Auth API', () => { | 
					
						
							|  |  |  |   beforeAll(async () => { | 
					
						
							|  |  |  |     strapi = await createStrapiInstance({ bypassAuth: false }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const { jwt, user } = await createAuthenticatedUser({ strapi, userInfo: internals.user }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data.user = user; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     rq = createRequest({ strapi }).setURLPrefix('/api/auth').setToken(jwt); | 
					
						
							| 
									
										
										
										
											2022-08-03 22:43:03 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							|  |  |  |     await strapi.destroy(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Change Password', () => { | 
					
						
							|  |  |  |     test('Fails on unauthenticated request', async () => { | 
					
						
							|  |  |  |       const nonAuthRequest = createRequest({ strapi }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const res = await nonAuthRequest({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/api/auth/change-password', | 
					
						
							|  |  |  |         body: {}, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 14:17:52 +02:00
										 |  |  |       expect(res.statusCode).toBe(403); | 
					
						
							|  |  |  |       expect(res.body.error.name).toBe('ForbiddenError'); | 
					
						
							|  |  |  |       expect(res.body.error.message).toBe('Forbidden'); | 
					
						
							| 
									
										
										
										
											2022-08-03 22:43:03 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-16 11:10:14 +00:00
										 |  |  |     test('Fails on invalid confirm password', async () => { | 
					
						
							| 
									
										
										
										
											2022-08-03 22:43:03 +02:00
										 |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/change-password', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           password: 'newPassword', | 
					
						
							|  |  |  |           passwordConfirmation: 'somethingElse', | 
					
						
							|  |  |  |           currentPassword: internals.user.password, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |       expect(res.body.error.name).toBe('ValidationError'); | 
					
						
							|  |  |  |       expect(res.body.error.message).toBe('Passwords do not match'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Fails on invalid current password', async () => { | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/change-password', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           password: 'newPassword', | 
					
						
							|  |  |  |           passwordConfirmation: 'newPassword', | 
					
						
							|  |  |  |           currentPassword: 'badPassword', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |       expect(res.body.error.name).toBe('ValidationError'); | 
					
						
							|  |  |  |       expect(res.body.error.message).toBe('The provided current password is invalid'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Fails when current and new password are the same', async () => { | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/change-password', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           password: internals.user.password, | 
					
						
							|  |  |  |           passwordConfirmation: internals.user.password, | 
					
						
							|  |  |  |           currentPassword: internals.user.password, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |       expect(res.body.error.name).toBe('ValidationError'); | 
					
						
							|  |  |  |       expect(res.body.error.message).toBe( | 
					
						
							|  |  |  |         'Your new password must be different than your current password' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Returns user info and jwt token on success', async () => { | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/change-password', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           password: internals.newPassword, | 
					
						
							|  |  |  |           passwordConfirmation: internals.newPassword, | 
					
						
							|  |  |  |           currentPassword: internals.user.password, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         jwt: expect.any(String), | 
					
						
							|  |  |  |         user: { | 
					
						
							|  |  |  |           id: data.user.id, | 
					
						
							|  |  |  |           email: data.user.email, | 
					
						
							|  |  |  |           username: data.user.username, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Can login with new password after success', async () => { | 
					
						
							|  |  |  |       const rq = createRequest({ strapi }).setURLPrefix('/api/auth'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/local', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |           identifier: internals.user.email, | 
					
						
							|  |  |  |           password: internals.newPassword, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |