blob: 21808fca6a9d23eb5bca786c3ba3ee5e148c7db0 [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-admin-view</title>
<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-admin-view.html">
<script>void(0);</script>
<test-fixture id="basic">
<template>
<gr-admin-view></gr-admin-view>
</template>
</test-fixture>
<script>
suite('gr-admin-view tests', () => {
let element;
let sandbox;
setup(() => {
sandbox = sinon.sandbox.create();
element = fixture('basic');
stub('gr-rest-api-interface', {
getProjectConfig() {
return Promise.resolve({});
},
});
});
teardown(() => {
sandbox.restore();
});
test('_computeURLHelper', () => {
const path = '/test';
const host = 'http://www.testsite.com';
const computedPath = element._computeURLHelper(host, path);
assert.equal(computedPath, '//http://www.testsite.com/test');
});
test('link URLs', () => {
assert.equal(
element._computeLinkURL({url: '/test', noBaseUrl: true}),
'//' + window.location.host + '/test');
sandbox.stub(element, 'getBaseUrl').returns('/foo');
assert.equal(
element._computeLinkURL({url: '/test', noBaseUrl: true}),
'//' + window.location.host + '/foo/test');
assert.equal(element._computeLinkURL({url: '/test'}), '/test');
assert.equal(
element._computeLinkURL({url: '/test', target: '_blank'}),
'/test');
});
test('current page gets selected and is displayed', () => {
element._filteredLinks = [{
name: 'Repositories',
url: '/admin/repos',
view: 'gr-repo-list',
children: [],
}];
element.params = {
view: 'admin',
adminView: 'gr-repo-list',
};
flushAsynchronousOperations();
assert.equal(Polymer.dom(element.root).querySelectorAll(
'.selected').length, 1);
assert.ok(element.$$('gr-repo-list'));
assert.isNotOk(element.$$('gr-admin-create-repo'));
});
test('_filteredLinks admin', done => {
sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
return Promise.resolve({
createGroup: true,
createProject: true,
viewPlugins: true,
});
});
element._loadAccountCapabilities().then(() => {
assert.equal(element._filteredLinks.length, 3);
// Repos
assert.equal(element._filteredLinks[0].children.length, 0);
assert.isNotOk(element._filteredLinks[0].subsection);
// Groups
assert.equal(element._filteredLinks[1].children.length, 0);
// Plugins
assert.equal(element._filteredLinks[2].children.length, 0);
done();
});
});
test('_filteredLinks non admin authenticated', done => {
sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
return Promise.resolve({});
});
element._loadAccountCapabilities().then(() => {
assert.equal(element._filteredLinks.length, 2);
// Repos
assert.equal(element._filteredLinks[0].children.length, 0);
assert.isNotOk(element._filteredLinks[0].subsection);
// Groups
assert.equal(element._filteredLinks[1].children.length, 0);
done();
});
});
test('_filteredLinks non admin unathenticated', done => {
element.reload().then(() => {
assert.equal(element._filteredLinks.length, 1);
// Repos
assert.equal(element._filteredLinks[0].children.length, 0);
assert.isNotOk(element._filteredLinks[0].subsection);
done();
});
});
test('Repo shows up in nav', done => {
element._repoName = 'Test Repo';
sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
return Promise.resolve({
createGroup: true,
createProject: true,
viewPlugins: true,
});
});
element._loadAccountCapabilities().then(() => {
assert.equal(element._filteredLinks.length, 3);
// Repos
assert.equal(element._filteredLinks[0].children.length, 0);
assert.equal(element._filteredLinks[0].subsection.name, 'Test Repo');
// Groups
assert.equal(element._filteredLinks[1].children.length, 0);
// Plugins
assert.equal(element._filteredLinks[2].children.length, 0);
done();
});
});
test('Nav is reloaded when repo changes', () => {
sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
return Promise.resolve({
createGroup: true,
createProject: true,
viewPlugins: true,
});
});
sandbox.stub(element.$.restAPI, 'getAccount', () => {
return Promise.resolve({_id: 1});
});
sandbox.stub(element, 'reload');
element.params = {repo: 'Test Repo', adminView: 'gr-repo'};
assert.equal(element.reload.callCount, 1);
element.params = {repo: 'Test Repo 2',
adminView: 'gr-repo'};
assert.equal(element.reload.callCount, 2);
});
test('Nav is reloaded when group changes', () => {
sandbox.stub(element, '_computeGroupName');
sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
return Promise.resolve({
createGroup: true,
createProject: true,
viewPlugins: true,
});
});
sandbox.stub(element.$.restAPI, 'getAccount', () => {
return Promise.resolve({_id: 1});
});
sandbox.stub(element, 'reload');
element.params = {groupId: '1', adminView: 'gr-group'};
assert.equal(element.reload.callCount, 1);
});
test('Nav is reloaded when group name changes', done => {
const newName = 'newName';
sandbox.stub(element, '_computeGroupName');
sandbox.stub(element, 'reload', () => {
assert.equal(element._groupName, newName);
assert.isTrue(element.reload.called);
done();
});
element.params = {group: 1, view: Gerrit.Nav.View.GROUP};
element._groupName = 'oldName';
flushAsynchronousOperations();
element.$$('gr-group').fire('name-changed', {name: newName});
});
suite('_computeSelectedClass', () => {
setup(() => {
sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
return Promise.resolve({
createGroup: true,
createProject: true,
viewPlugins: true,
});
});
sandbox.stub(element.$.restAPI, 'getAccount', () => {
return Promise.resolve({_id: 1});
});
return element.reload();
});
suite('repos', () => {
setup(() => {
stub('gr-repo-access', {
_repoChanged: () => {},
});
});
test('repo list', () => {
element.params = {
view: Gerrit.Nav.View.ADMIN,
adminView: 'gr-repo-list',
openCreateModal: false,
};
flushAsynchronousOperations();
const selected = element.$$('gr-page-nav .selected');
assert.isOk(selected);
assert.equal(selected.textContent.trim(), 'Repositories');
});
test('repo', () => {
element.params = {
view: Gerrit.Nav.View.REPO,
repoName: 'foo',
};
element._repoName = 'foo';
return element.reload().then(() => {
flushAsynchronousOperations();
const selected = element.$$('gr-page-nav .selected');
assert.isOk(selected);
assert.equal(selected.textContent.trim(), 'foo');
});
});
test('repo access', () => {
element.params = {
view: Gerrit.Nav.View.REPO,
detail: Gerrit.Nav.RepoDetailView.ACCESS,
repoName: 'foo',
};
element._repoName = 'foo';
return element.reload().then(() => {
flushAsynchronousOperations();
const selected = element.$$('gr-page-nav .selected');
assert.isOk(selected);
assert.equal(selected.textContent.trim(), 'Access');
});
});
test('repo dashboards', () => {
element.params = {
view: Gerrit.Nav.View.REPO,
detail: Gerrit.Nav.RepoDetailView.DASHBOARDS,
repoName: 'foo',
};
element._repoName = 'foo';
return element.reload().then(() => {
flushAsynchronousOperations();
const selected = element.$$('gr-page-nav .selected');
assert.isOk(selected);
assert.equal(selected.textContent.trim(), 'Dashboards');
});
});
});
suite('groups', () => {
setup(() => {
stub('gr-group', {
_loadGroup: () => Promise.resolve({}),
});
stub('gr-group-members', {
_loadGroupDetails: () => {},
});
sandbox.stub(element.$.restAPI, 'getGroupConfig')
.returns(Promise.resolve({
name: 'foo',
}));
sandbox.stub(element.$.restAPI, 'getIsGroupOwner')
.returns(Promise.resolve(true));
});
test('group list', () => {
element.params = {
view: Gerrit.Nav.View.ADMIN,
adminView: 'gr-admin-group-list',
openCreateModal: false,
};
flushAsynchronousOperations();
const selected = element.$$('gr-page-nav .selected');
assert.isOk(selected);
assert.equal(selected.textContent.trim(), 'Groups');
});
test('group', () => {
element.params = {
view: Gerrit.Nav.View.GROUP,
groupId: 1234,
};
element._groupName = 'foo';
return element.reload().then(() => {
flushAsynchronousOperations();
const selected = element.$$('gr-page-nav .selected');
assert.isOk(selected);
assert.equal(selected.textContent.trim(), 'foo');
});
});
test('group members', () => {
element.params = {
view: Gerrit.Nav.View.GROUP,
detail: Gerrit.Nav.GroupDetailView.MEMBERS,
groupId: 1234,
};
element._groupName = 'foo';
return element.reload().then(() => {
flushAsynchronousOperations();
const selected = element.$$('gr-page-nav .selected');
assert.isOk(selected);
assert.equal(selected.textContent.trim(), 'Members');
});
});
});
});
});
</script>