|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import { isResetButtonDisabled, validateCurrentPassword } from '../password-reset-panel-utils'; |
| 17 | +import { HttpStart } from 'opensearch-dashboards/public'; |
| 18 | + |
| 19 | +describe('Test Password Reset Panel Utlis', () => { |
| 20 | + const mockUrl = 'http://localhost:5601/auth/login'; |
| 21 | + let http: HttpStart; |
| 22 | + beforeEach(() => { |
| 23 | + http = { |
| 24 | + basePath: { |
| 25 | + prepend: jest.fn(() => mockUrl), |
| 26 | + }, |
| 27 | + } as any; |
| 28 | + global.fetch = jest.fn(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('validate current password resolves when the server returns 2xx', async () => { |
| 32 | + (global.fetch as jest.Mock).mockResolvedValue({ |
| 33 | + ok: true, |
| 34 | + }); |
| 35 | + |
| 36 | + await expect(validateCurrentPassword(http, 'user', 'password')).resolves.toBeUndefined(); |
| 37 | + |
| 38 | + expect(global.fetch).toHaveBeenCalledWith( |
| 39 | + mockUrl, |
| 40 | + expect.objectContaining({ |
| 41 | + method: 'POST', |
| 42 | + credentials: 'include', |
| 43 | + headers: { |
| 44 | + 'Content-Type': 'application/json', |
| 45 | + 'osd-xsrf': 'true', |
| 46 | + }, |
| 47 | + body: JSON.stringify({ username: 'user', password: 'password' }), |
| 48 | + }) |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('validate current password rejects with the server message on 4xx', async () => { |
| 53 | + const serverBody = { message: 'Unauthorized' }; |
| 54 | + (global.fetch as jest.Mock).mockResolvedValue({ |
| 55 | + ok: false, |
| 56 | + json: jest.fn().mockResolvedValue(serverBody), |
| 57 | + status: 401, |
| 58 | + }); |
| 59 | + |
| 60 | + await expect(validateCurrentPassword(http, 'user', 'incorrectpassword')).rejects.toThrow( |
| 61 | + 'Unauthorized' |
| 62 | + ); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('Validates password reset button state based on input conditions', () => { |
| 67 | + const testScenarios = [ |
| 68 | + { |
| 69 | + description: 'should be disabled when currentPassword is empty', |
| 70 | + currentPassword: '', |
| 71 | + newPassword: 'newpass', |
| 72 | + isRepeatNewPasswordInvalid: false, |
| 73 | + expected: true, |
| 74 | + }, |
| 75 | + { |
| 76 | + description: 'should be disabled when newPassword is empty', |
| 77 | + currentPassword: 'current', |
| 78 | + newPassword: '', |
| 79 | + isRepeatNewPasswordInvalid: false, |
| 80 | + expected: true, |
| 81 | + }, |
| 82 | + { |
| 83 | + description: 'should be disabled when isRepeatNewPasswordInvalid is true', |
| 84 | + currentPassword: 'current', |
| 85 | + newPassword: 'newpass', |
| 86 | + isRepeatNewPasswordInvalid: true, |
| 87 | + expected: true, |
| 88 | + }, |
| 89 | + { |
| 90 | + description: 'should be enabled when all conditions are valid', |
| 91 | + currentPassword: 'current', |
| 92 | + newPassword: 'newpass', |
| 93 | + isRepeatNewPasswordInvalid: false, |
| 94 | + expected: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + description: 'should be disabled when all fields are invalid', |
| 98 | + currentPassword: '', |
| 99 | + newPassword: '', |
| 100 | + isRepeatNewPasswordInvalid: true, |
| 101 | + expected: true, |
| 102 | + }, |
| 103 | + ] as const; |
| 104 | + |
| 105 | + it.each(testScenarios)('$description', async function ({ |
| 106 | + currentPassword, |
| 107 | + newPassword, |
| 108 | + isRepeatNewPasswordInvalid, |
| 109 | + expected, |
| 110 | + }) { |
| 111 | + const result = isResetButtonDisabled(currentPassword, newPassword, isRepeatNewPasswordInvalid); |
| 112 | + expect(result).toBe(expected); |
| 113 | + }); |
| 114 | +}); |
0 commit comments