|
| 1 | +import { |
| 2 | + CognitoError, |
| 3 | + InvalidAuthorizationError, |
| 4 | + UserNotFoundError, |
| 5 | + PasswordResetRequiredError, |
| 6 | + VerificationCodeMismatchError, |
| 7 | + VerificationCodeExpiredError, |
| 8 | + InvalidPasswordError, |
| 9 | + PasswordCannotBeResetError, |
| 10 | + dispatchError, |
| 11 | +} from 'ember-cognito-identity/errors/cognito'; |
| 12 | +import { module, test } from 'qunit'; |
| 13 | + |
| 14 | +module('Unit | Utility | errors/cognito', function () { |
| 15 | + test('it works for a string', function (assert) { |
| 16 | + // @ts-ignore |
| 17 | + let result = dispatchError('test error'); |
| 18 | + assert.ok(result instanceof CognitoError); |
| 19 | + assert.equal(result.message, 'test error'); |
| 20 | + }); |
| 21 | + |
| 22 | + test('it works with null', function (assert) { |
| 23 | + // @ts-ignore |
| 24 | + let result = dispatchError(null); |
| 25 | + assert.ok(result instanceof CognitoError); |
| 26 | + assert.equal(result.message, 'An error has occurred.'); |
| 27 | + }); |
| 28 | + |
| 29 | + test('it works for PasswordResetRequiredException', function (assert) { |
| 30 | + let result = dispatchError({ |
| 31 | + code: 'PasswordResetRequiredException', |
| 32 | + name: 'PasswordResetRequiredException', |
| 33 | + message: 'test message', |
| 34 | + }); |
| 35 | + |
| 36 | + assert.ok(result instanceof PasswordResetRequiredError); |
| 37 | + assert.equal(result.message, 'Password reset required for the user.'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('it works for NotAuthorizedException', function (assert) { |
| 41 | + let result = dispatchError({ |
| 42 | + code: 'NotAuthorizedException', |
| 43 | + name: 'NotAuthorizedException', |
| 44 | + message: 'test message', |
| 45 | + }); |
| 46 | + |
| 47 | + assert.ok(result instanceof InvalidAuthorizationError); |
| 48 | + assert.equal(result.message, 'The password you provided is incorrect.'); |
| 49 | + }); |
| 50 | + |
| 51 | + test('it works for CodeMismatchException', function (assert) { |
| 52 | + let result = dispatchError({ |
| 53 | + code: 'CodeMismatchException', |
| 54 | + name: 'CodeMismatchException', |
| 55 | + message: 'test message', |
| 56 | + }); |
| 57 | + |
| 58 | + assert.ok(result instanceof VerificationCodeMismatchError); |
| 59 | + assert.equal( |
| 60 | + result.message, |
| 61 | + 'Invalid verification code provided, please try again.' |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + test('it works for InvalidPasswordException', function (assert) { |
| 66 | + let result = dispatchError({ |
| 67 | + code: 'InvalidPasswordException', |
| 68 | + name: 'InvalidPasswordException', |
| 69 | + message: 'test message', |
| 70 | + }); |
| 71 | + |
| 72 | + assert.ok(result instanceof InvalidPasswordError); |
| 73 | + assert.equal(result.message, 'test message'); |
| 74 | + }); |
| 75 | + |
| 76 | + test('it works for InvalidParameterException', function (assert) { |
| 77 | + let result = dispatchError({ |
| 78 | + code: 'InvalidParameterException', |
| 79 | + name: 'InvalidParameterException', |
| 80 | + message: 'test message', |
| 81 | + }); |
| 82 | + |
| 83 | + assert.ok(result instanceof InvalidPasswordError); |
| 84 | + assert.equal(result.message, 'test message'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('it works for UserNotFoundException', function (assert) { |
| 88 | + let result = dispatchError({ |
| 89 | + code: 'UserNotFoundException', |
| 90 | + name: 'UserNotFoundException', |
| 91 | + message: 'test message', |
| 92 | + }); |
| 93 | + |
| 94 | + assert.ok(result instanceof UserNotFoundError); |
| 95 | + assert.equal(result.message, 'This user does not exist.'); |
| 96 | + }); |
| 97 | + |
| 98 | + test('it works for ExpiredCodeException', function (assert) { |
| 99 | + let result = dispatchError({ |
| 100 | + code: 'ExpiredCodeException', |
| 101 | + name: 'ExpiredCodeException', |
| 102 | + message: 'test message', |
| 103 | + }); |
| 104 | + |
| 105 | + assert.ok(result instanceof VerificationCodeExpiredError); |
| 106 | + assert.equal( |
| 107 | + result.message, |
| 108 | + 'The verification code is expired, please request a new one.' |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + module('special cases', function () { |
| 113 | + test('it handles updating invalid attributes', function (assert) { |
| 114 | + let result = dispatchError({ |
| 115 | + name: 'NotAuthorizedException', |
| 116 | + code: 'NotAuthorizedException', |
| 117 | + message: 'A client attempted to write unauthorized attribute', |
| 118 | + }); |
| 119 | + |
| 120 | + assert.ok(result instanceof CognitoError); |
| 121 | + assert.equal( |
| 122 | + result.message, |
| 123 | + 'A client attempted to write unauthorized attribute' |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + test('it handles requesting a password reset for an inactive user', function (assert) { |
| 128 | + let result = dispatchError({ |
| 129 | + name: 'NotAuthorizedException', |
| 130 | + code: 'NotAuthorizedException', |
| 131 | + message: 'User password cannot be reset in the current state.', |
| 132 | + }); |
| 133 | + |
| 134 | + assert.ok(result instanceof PasswordCannotBeResetError); |
| 135 | + assert.equal( |
| 136 | + result.message, |
| 137 | + 'The user you are trying to reset the password for is not active.' |
| 138 | + ); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments