Dmitrii Filippov | 06117e8 | 2020-06-25 13:26:55 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright (C) 2016 The Android Open Source Project |
| 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 | */ |
Wyatt Allen | 31958fcb | 2016-06-17 10:21:59 -0700 | [diff] [blame] | 17 | |
Paladox none | 949dc06 | 2021-08-12 21:45:26 +0000 | [diff] [blame] | 18 | import '../../../test/common-test-setup-karma'; |
| 19 | import './gr-http-password'; |
| 20 | import {GrHttpPassword} from './gr-http-password'; |
| 21 | import {stubRestApi} from '../../../test/test-utils'; |
| 22 | import * as MockInteractions from '@polymer/iron-test-helpers/mock-interactions'; |
| 23 | import { |
| 24 | createAccountDetailWithId, |
| 25 | createServerInfo, |
| 26 | } from '../../../test/test-data-generators'; |
| 27 | import {AccountDetailInfo, ServerInfo} from '../../../types/common'; |
Paladox none | 0a920f3 | 2021-08-12 22:08:03 +0000 | [diff] [blame] | 28 | import {queryAndAssert} from '../../../test/test-utils'; |
| 29 | import {GrButton} from '../../shared/gr-button/gr-button'; |
Dmitrii Filippov | 06117e8 | 2020-06-25 13:26:55 +0200 | [diff] [blame] | 30 | |
| 31 | const basicFixture = fixtureFromElement('gr-http-password'); |
| 32 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 33 | suite('gr-http-password tests', () => { |
Paladox none | 949dc06 | 2021-08-12 21:45:26 +0000 | [diff] [blame] | 34 | let element: GrHttpPassword; |
| 35 | let account: AccountDetailInfo; |
| 36 | let config: ServerInfo; |
Wyatt Allen | 31958fcb | 2016-06-17 10:21:59 -0700 | [diff] [blame] | 37 | |
Chris Poucet | a2e173e | 2021-08-31 01:04:04 +0000 | [diff] [blame] | 38 | setup(async () => { |
Paladox none | 949dc06 | 2021-08-12 21:45:26 +0000 | [diff] [blame] | 39 | account = {...createAccountDetailWithId(), username: 'user name'}; |
| 40 | config = createServerInfo(); |
Wyatt Allen | 31958fcb | 2016-06-17 10:21:59 -0700 | [diff] [blame] | 41 | |
Ben Rohlfs | 3a6ff7e | 2021-01-18 14:08:39 +0100 | [diff] [blame] | 42 | stubRestApi('getAccount').returns(Promise.resolve(account)); |
| 43 | stubRestApi('getConfig').returns(Promise.resolve(config)); |
Wyatt Allen | 31958fcb | 2016-06-17 10:21:59 -0700 | [diff] [blame] | 44 | |
Dmitrii Filippov | 06117e8 | 2020-06-25 13:26:55 +0200 | [diff] [blame] | 45 | element = basicFixture.instantiate(); |
Chris Poucet | a2e173e | 2021-08-31 01:04:04 +0000 | [diff] [blame] | 46 | await element.loadData(); |
| 47 | await flush(); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 48 | }); |
Wyatt Allen | 31958fcb | 2016-06-17 10:21:59 -0700 | [diff] [blame] | 49 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 50 | test('generate password', () => { |
Paladox none | 0a920f3 | 2021-08-12 22:08:03 +0000 | [diff] [blame] | 51 | const button = queryAndAssert<GrButton>(element, '#generateButton'); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 52 | const nextPassword = 'the new password'; |
Paladox none | 949dc06 | 2021-08-12 21:45:26 +0000 | [diff] [blame] | 53 | let generateResolve: (value: string | PromiseLike<string>) => void; |
| 54 | const generateStub = stubRestApi('generateAccountHttpPassword').callsFake( |
| 55 | () => |
| 56 | new Promise(resolve => { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 57 | generateResolve = resolve; |
Paladox none | 949dc06 | 2021-08-12 21:45:26 +0000 | [diff] [blame] | 58 | }) |
| 59 | ); |
Wyatt Allen | 31958fcb | 2016-06-17 10:21:59 -0700 | [diff] [blame] | 60 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 61 | assert.isNotOk(element._generatedPassword); |
Wyatt Allen | 31958fcb | 2016-06-17 10:21:59 -0700 | [diff] [blame] | 62 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 63 | MockInteractions.tap(button); |
Han-Wen Nienhuys | 2d4b50c | 2017-01-23 16:15:37 +0100 | [diff] [blame] | 64 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 65 | assert.isTrue(generateStub.called); |
| 66 | assert.equal(element._generatedPassword, 'Generating...'); |
Han-Wen Nienhuys | 2d4b50c | 2017-01-23 16:15:37 +0100 | [diff] [blame] | 67 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 68 | generateStub.lastCall.returnValue.then(() => { |
Paladox none | 949dc06 | 2021-08-12 21:45:26 +0000 | [diff] [blame] | 69 | generateResolve(nextPassword); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 70 | assert.equal(element._generatedPassword, nextPassword); |
Wyatt Allen | 9f5f437 | 2017-03-15 12:46:52 -0700 | [diff] [blame] | 71 | }); |
Wyatt Allen | 31958fcb | 2016-06-17 10:21:59 -0700 | [diff] [blame] | 72 | }); |
| 73 | |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 74 | test('without http_password_url', () => { |
| 75 | assert.isNull(element._passwordUrl); |
| 76 | }); |
| 77 | |
Chris Poucet | a2e173e | 2021-08-31 01:04:04 +0000 | [diff] [blame] | 78 | test('with http_password_url', async () => { |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 79 | config.auth.http_password_url = 'http://example.com/'; |
Chris Poucet | a2e173e | 2021-08-31 01:04:04 +0000 | [diff] [blame] | 80 | await element.loadData(); |
| 81 | assert.isNotNull(element._passwordUrl); |
| 82 | assert.equal(element._passwordUrl, config.auth.http_password_url); |
Dmitrii Filippov | daf0ec9 | 2020-03-17 11:27:28 +0100 | [diff] [blame] | 83 | }); |
| 84 | }); |