blob: eab8d2e667fc2d851c391090ffdcf45317122014 [file] [log] [blame]
Dmitrii Filippov06117e82020-06-25 13:26:55 +02001/**
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 Allen31958fcb2016-06-17 10:21:59 -070017
Paladox none949dc062021-08-12 21:45:26 +000018import '../../../test/common-test-setup-karma';
19import './gr-http-password';
20import {GrHttpPassword} from './gr-http-password';
21import {stubRestApi} from '../../../test/test-utils';
22import * as MockInteractions from '@polymer/iron-test-helpers/mock-interactions';
23import {
24 createAccountDetailWithId,
25 createServerInfo,
26} from '../../../test/test-data-generators';
27import {AccountDetailInfo, ServerInfo} from '../../../types/common';
Paladox none0a920f32021-08-12 22:08:03 +000028import {queryAndAssert} from '../../../test/test-utils';
29import {GrButton} from '../../shared/gr-button/gr-button';
Dmitrii Filippov06117e82020-06-25 13:26:55 +020030
31const basicFixture = fixtureFromElement('gr-http-password');
32
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010033suite('gr-http-password tests', () => {
Paladox none949dc062021-08-12 21:45:26 +000034 let element: GrHttpPassword;
35 let account: AccountDetailInfo;
36 let config: ServerInfo;
Wyatt Allen31958fcb2016-06-17 10:21:59 -070037
Chris Pouceta2e173e2021-08-31 01:04:04 +000038 setup(async () => {
Paladox none949dc062021-08-12 21:45:26 +000039 account = {...createAccountDetailWithId(), username: 'user name'};
40 config = createServerInfo();
Wyatt Allen31958fcb2016-06-17 10:21:59 -070041
Ben Rohlfs3a6ff7e2021-01-18 14:08:39 +010042 stubRestApi('getAccount').returns(Promise.resolve(account));
43 stubRestApi('getConfig').returns(Promise.resolve(config));
Wyatt Allen31958fcb2016-06-17 10:21:59 -070044
Dmitrii Filippov06117e82020-06-25 13:26:55 +020045 element = basicFixture.instantiate();
Chris Pouceta2e173e2021-08-31 01:04:04 +000046 await element.loadData();
47 await flush();
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010048 });
Wyatt Allen31958fcb2016-06-17 10:21:59 -070049
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010050 test('generate password', () => {
Paladox none0a920f32021-08-12 22:08:03 +000051 const button = queryAndAssert<GrButton>(element, '#generateButton');
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010052 const nextPassword = 'the new password';
Paladox none949dc062021-08-12 21:45:26 +000053 let generateResolve: (value: string | PromiseLike<string>) => void;
54 const generateStub = stubRestApi('generateAccountHttpPassword').callsFake(
55 () =>
56 new Promise(resolve => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010057 generateResolve = resolve;
Paladox none949dc062021-08-12 21:45:26 +000058 })
59 );
Wyatt Allen31958fcb2016-06-17 10:21:59 -070060
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010061 assert.isNotOk(element._generatedPassword);
Wyatt Allen31958fcb2016-06-17 10:21:59 -070062
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010063 MockInteractions.tap(button);
Han-Wen Nienhuys2d4b50c2017-01-23 16:15:37 +010064
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010065 assert.isTrue(generateStub.called);
66 assert.equal(element._generatedPassword, 'Generating...');
Han-Wen Nienhuys2d4b50c2017-01-23 16:15:37 +010067
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010068 generateStub.lastCall.returnValue.then(() => {
Paladox none949dc062021-08-12 21:45:26 +000069 generateResolve(nextPassword);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010070 assert.equal(element._generatedPassword, nextPassword);
Wyatt Allen9f5f4372017-03-15 12:46:52 -070071 });
Wyatt Allen31958fcb2016-06-17 10:21:59 -070072 });
73
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010074 test('without http_password_url', () => {
75 assert.isNull(element._passwordUrl);
76 });
77
Chris Pouceta2e173e2021-08-31 01:04:04 +000078 test('with http_password_url', async () => {
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010079 config.auth.http_password_url = 'http://example.com/';
Chris Pouceta2e173e2021-08-31 01:04:04 +000080 await element.loadData();
81 assert.isNotNull(element._passwordUrl);
82 assert.equal(element._passwordUrl, config.auth.http_password_url);
Dmitrii Filippovdaf0ec92020-03-17 11:27:28 +010083 });
84});