|
1 | 1 | import { resetPasswordService } from '../../../../../src/identity-access-management/domain/services/reset-password.service.js';
|
2 | 2 | import { config } from '../../../../../src/shared/config.js';
|
| 3 | +import { tokenService } from '../../../../../src/shared/domain/services/token-service.js'; |
3 | 4 | import { createServer, databaseBuilder, expect } from '../../../../test-helper.js';
|
4 | 5 |
|
5 | 6 | describe('Acceptance | Identity Access Management | Application | Route | password', function () {
|
@@ -97,23 +98,120 @@ describe('Acceptance | Identity Access Management | Application | Route | passwo
|
97 | 98 | });
|
98 | 99 |
|
99 | 100 | describe('GET /api/password-reset-demands/{temporaryKey}', function () {
|
100 |
| - it('returns 200 http status code', async function () { |
101 |
| - // given |
102 |
| - const temporaryKey = await resetPasswordService.generateTemporaryKey(); |
103 |
| - const options = { |
104 |
| - method: 'GET', |
105 |
| - url: `/api/password-reset-demands/${temporaryKey}`, |
106 |
| - }; |
107 |
| - const userId = databaseBuilder.factory.buildUser({ email }).id; |
108 |
| - databaseBuilder.factory.buildAuthenticationMethod.withPixAsIdentityProviderAndHashedPassword({ userId }); |
109 |
| - databaseBuilder.factory.buildResetPasswordDemand({ temporaryKey, email }); |
110 |
| - await databaseBuilder.commit(); |
111 |
| - |
112 |
| - // when |
113 |
| - const response = await server.inject(options); |
114 |
| - |
115 |
| - // then |
116 |
| - expect(response.statusCode).to.equal(200); |
| 101 | + const options = { |
| 102 | + method: 'GET', |
| 103 | + url: null, |
| 104 | + }; |
| 105 | + context('when temporaryKey is not valid', function () { |
| 106 | + it('replies with 401 status code', async function () { |
| 107 | + // given |
| 108 | + options.url = '/api/password-reset-demands/invalid-temporary-key'; |
| 109 | + |
| 110 | + // when |
| 111 | + const response = await server.inject(options); |
| 112 | + |
| 113 | + // then |
| 114 | + expect(response.statusCode).to.equal(401); |
| 115 | + }); |
| 116 | + }); |
| 117 | + context('when temporaryKey is valid', function () { |
| 118 | + let temporaryKey; |
| 119 | + |
| 120 | + beforeEach(async function () { |
| 121 | + temporaryKey = await resetPasswordService.generateTemporaryKey(); |
| 122 | + options.url = `/api/password-reset-demands/${temporaryKey}`; |
| 123 | + }); |
| 124 | + |
| 125 | + context('when temporaryKey is not linked to a reset password demand', function () { |
| 126 | + it('replies with 404 status code', async function () { |
| 127 | + // when |
| 128 | + const response = await server.inject(options); |
| 129 | + |
| 130 | + // then |
| 131 | + expect(response.statusCode).to.equal(404); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + context('when temporaryKey is linked to a password reset demand', function () { |
| 136 | + beforeEach(async function () { |
| 137 | + databaseBuilder.factory.buildUser({ email }); |
| 138 | + databaseBuilder.factory.buildResetPasswordDemand({ email, temporaryKey }); |
| 139 | + |
| 140 | + await databaseBuilder.commit(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('replies with 200 status code', async function () { |
| 144 | + // when |
| 145 | + const response = await server.inject(options); |
| 146 | + |
| 147 | + // then |
| 148 | + expect(response.statusCode).to.equal(200); |
| 149 | + }); |
| 150 | + }); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + describe('POST /api/expired-password-updates', function () { |
| 155 | + context('Success cases', function () { |
| 156 | + it('returns 201 HTTP status code', async function () { |
| 157 | + // given |
| 158 | + const user = databaseBuilder.factory.buildUser.withRawPassword({ |
| 159 | + shouldChangePassword: true, |
| 160 | + }); |
| 161 | + await databaseBuilder.commit(); |
| 162 | + const passwordResetToken = tokenService.createPasswordResetToken(user.id); |
| 163 | + |
| 164 | + const options = { |
| 165 | + method: 'POST', |
| 166 | + url: '/api/expired-password-updates', |
| 167 | + payload: { |
| 168 | + data: { |
| 169 | + attributes: { |
| 170 | + 'password-reset-token': passwordResetToken, |
| 171 | + 'new-password': 'Password02', |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }; |
| 176 | + |
| 177 | + // when |
| 178 | + const response = await server.inject(options); |
| 179 | + |
| 180 | + // then |
| 181 | + expect(response.statusCode).to.equal(201); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + context('Error cases', function () { |
| 186 | + context('when shouldChangePassword is false', function () { |
| 187 | + it('responds 403 HTTP status code', async function () { |
| 188 | + // given |
| 189 | + const user = databaseBuilder.factory.buildUser.withRawPassword({ |
| 190 | + shouldChangePassword: false, |
| 191 | + }); |
| 192 | + await databaseBuilder.commit(); |
| 193 | + const passwordResetToken = tokenService.createPasswordResetToken(user.id); |
| 194 | + |
| 195 | + const options = { |
| 196 | + method: 'POST', |
| 197 | + url: '/api/expired-password-updates', |
| 198 | + payload: { |
| 199 | + data: { |
| 200 | + attributes: { |
| 201 | + 'password-reset-token': passwordResetToken, |
| 202 | + 'new-password': 'Password02', |
| 203 | + }, |
| 204 | + }, |
| 205 | + }, |
| 206 | + }; |
| 207 | + |
| 208 | + // when |
| 209 | + const response = await server.inject(options); |
| 210 | + |
| 211 | + // then |
| 212 | + expect(response.statusCode).to.equal(403); |
| 213 | + }); |
| 214 | + }); |
117 | 215 | });
|
118 | 216 | });
|
119 | 217 | });
|
0 commit comments