summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/admin/gr-admin-view/gr-admin-view_test.html
blob: 117940a3b8bc71806a8c72bb9a1eeb887e807739 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!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'}),
          '//' + window.location.host + '/test');
      assert.equal(
          element._computeLinkURL({url: '/test', target: '_blank'}),
          '/test');
    });

    test('current page gets selected and is displayed', () => {
      element._filteredLinks = [{
        name: 'Projects',
        url: '/admin/projects',
        view: 'gr-project-list',
        children: [],
      }];

      element.params = {
        adminView: 'gr-project-list',
      };

      flushAsynchronousOperations();
      assert.equal(Polymer.dom(element.root).querySelectorAll(
          '.selected').length, 1);
      assert.ok(element.$$('gr-project-list'));
      assert.isNotOk(element.$$('gr-admin-create-project'));
    });

    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);

        // Projects
        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);

        // Projects
        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);

        // Projects
        assert.equal(element._filteredLinks[0].children.length, 0);
        assert.isNotOk(element._filteredLinks[0].subsection);
        done();
      });
    });

    test('Project shows up in nav', done => {
      element._projectName = 'Test Project';
      sandbox.stub(element.$.restAPI, 'getAccountCapabilities', () => {
        return Promise.resolve({
          createGroup: true,
          createProject: true,
          viewPlugins: true,
        });
      });
      element._loadAccountCapabilities().then(() => {
        assert.equal(element._filteredLinks.length, 3);

        // Projects
        assert.equal(element._filteredLinks[0].children.length, 0);
        assert.equal(element._filteredLinks[0].subsection.name, 'Test Project');

        // Groups
        assert.equal(element._filteredLinks[1].children.length, 0);

        // Plugins
        assert.equal(element._filteredLinks[2].children.length, 0);
        done();
      });
    });

    test('Nav is reloaded when project 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 = {project: 'Test Project', adminView: 'gr-project'};
      assert.equal(element.reload.callCount, 1);
      element.params = {project: 'Test Project 2',
        adminView: 'gr-project'};
      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, adminView: 'gr-group'};
      element._groupName = 'oldName';
      flushAsynchronousOperations();
      element.$$('gr-group').fire('name-changed', {name: newName});
    });
  });
</script>