|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect, use } from 'chai'; |
| 19 | +import chaiAsPromised from 'chai-as-promised'; |
| 20 | +import sinonChai from 'sinon-chai'; |
| 21 | +import { PasswordPolicy } from '../../model/public_types'; |
| 22 | +import { PasswordPolicyImpl } from './password_policy_impl'; |
| 23 | +import { GetPasswordPolicyResponse } from '../../api/password_policy/get_password_policy'; |
| 24 | + |
| 25 | +use(sinonChai); |
| 26 | +use(chaiAsPromised); |
| 27 | + |
| 28 | +describe('core/auth/password_policy_impl', () => { |
| 29 | + const TEST_MIN_PASSWORD_LENGTH = 6; |
| 30 | + const TEST_MAX_PASSWORD_LENGTH = 30; |
| 31 | + const TEST_CONTAINS_LOWERCASE = true; |
| 32 | + const TEST_CONTAINS_UPPERCASE = true; |
| 33 | + const TEST_CONTAINS_NUMERIC = true; |
| 34 | + const TEST_CONTAINS_NON_ALPHANUMERIC = true; |
| 35 | + const TEST_ALLOWED_NON_ALPHANUMERIC_CHARS = ['!', '(', ')']; |
| 36 | + const TEST_SCHEMA_VERSION = 1; |
| 37 | + const PASSWORD_POLICY_RESPONSE_REQUIRE_ALL: GetPasswordPolicyResponse = { |
| 38 | + customStrengthOptions: { |
| 39 | + minPasswordLength: TEST_MIN_PASSWORD_LENGTH, |
| 40 | + maxPasswordLength: TEST_MAX_PASSWORD_LENGTH, |
| 41 | + containsLowercaseCharacter: TEST_CONTAINS_LOWERCASE, |
| 42 | + containsUppercaseCharacter: TEST_CONTAINS_UPPERCASE, |
| 43 | + containsNumericCharacter: TEST_CONTAINS_NUMERIC, |
| 44 | + containsNonAlphanumericCharacter: TEST_CONTAINS_NON_ALPHANUMERIC |
| 45 | + }, |
| 46 | + allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS, |
| 47 | + schemaVersion: TEST_SCHEMA_VERSION |
| 48 | + }; |
| 49 | + const PASSWORD_POLICY_RESPONSE_REQUIRE_LENGTH: GetPasswordPolicyResponse = { |
| 50 | + customStrengthOptions: { |
| 51 | + minPasswordLength: TEST_MIN_PASSWORD_LENGTH, |
| 52 | + maxPasswordLength: TEST_MAX_PASSWORD_LENGTH |
| 53 | + }, |
| 54 | + allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS, |
| 55 | + schemaVersion: TEST_SCHEMA_VERSION |
| 56 | + }; |
| 57 | + const PASSWORD_POLICY_REQUIRE_ALL: PasswordPolicy = { |
| 58 | + customStrengthOptions: { |
| 59 | + minPasswordLength: TEST_MIN_PASSWORD_LENGTH, |
| 60 | + maxPasswordLength: TEST_MAX_PASSWORD_LENGTH, |
| 61 | + containsLowercaseLetter: TEST_CONTAINS_LOWERCASE, |
| 62 | + containsUppercaseLetter: TEST_CONTAINS_UPPERCASE, |
| 63 | + containsNumericCharacter: TEST_CONTAINS_NUMERIC, |
| 64 | + containsNonAlphanumericCharacter: TEST_CONTAINS_UPPERCASE |
| 65 | + }, |
| 66 | + allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS |
| 67 | + }; |
| 68 | + const PASSWORD_POLICY_REQUIRE_LENGTH: PasswordPolicy = { |
| 69 | + customStrengthOptions: { |
| 70 | + minPasswordLength: TEST_MIN_PASSWORD_LENGTH, |
| 71 | + maxPasswordLength: TEST_MAX_PASSWORD_LENGTH |
| 72 | + }, |
| 73 | + allowedNonAlphanumericCharacters: TEST_ALLOWED_NON_ALPHANUMERIC_CHARS |
| 74 | + }; |
| 75 | + |
| 76 | + context('#PasswordPolicyImpl', () => { |
| 77 | + it('can construct the password policy from the backend response', () => { |
| 78 | + const policy: PasswordPolicy = new PasswordPolicyImpl( |
| 79 | + PASSWORD_POLICY_RESPONSE_REQUIRE_ALL |
| 80 | + ); |
| 81 | + // The password policy contains the schema version internally, but the public typing does not. |
| 82 | + // Only check the fields that are publicly exposed. |
| 83 | + expect(policy.customStrengthOptions).to.eql( |
| 84 | + PASSWORD_POLICY_REQUIRE_ALL.customStrengthOptions |
| 85 | + ); |
| 86 | + expect(policy.allowedNonAlphanumericCharacters).to.eql( |
| 87 | + PASSWORD_POLICY_REQUIRE_ALL.allowedNonAlphanumericCharacters |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('only includes requirements defined in the response', () => { |
| 92 | + const policy: PasswordPolicy = new PasswordPolicyImpl( |
| 93 | + PASSWORD_POLICY_RESPONSE_REQUIRE_LENGTH |
| 94 | + ); |
| 95 | + expect(policy.customStrengthOptions).to.eql( |
| 96 | + PASSWORD_POLICY_REQUIRE_LENGTH.customStrengthOptions |
| 97 | + ); |
| 98 | + expect(policy.allowedNonAlphanumericCharacters).to.eql( |
| 99 | + PASSWORD_POLICY_REQUIRE_LENGTH.allowedNonAlphanumericCharacters |
| 100 | + ); |
| 101 | + // Requirements that are not in the response should be undefined. |
| 102 | + expect(policy.customStrengthOptions.containsLowercaseLetter).to.be |
| 103 | + .undefined; |
| 104 | + expect(policy.customStrengthOptions.containsUppercaseLetter).to.be |
| 105 | + .undefined; |
| 106 | + expect(policy.customStrengthOptions.containsNumericCharacter).to.be |
| 107 | + .undefined; |
| 108 | + expect(policy.customStrengthOptions.containsNonAlphanumericCharacter).to |
| 109 | + .be.undefined; |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments