summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/behaviors/safe-types-behavior/safe-types-behavior_test.html
blob: bc16b39e09a7b628c0f7b01b3d9648ddb2759b43 (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
<!DOCTYPE html>
<!--
@license
Copyright (C) 2018 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.
-->

<title>safe-types-behavior</title>

<script src="../../bower_components/webcomponentsjs/webcomponents.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="safe-types-behavior.html">

<script>void(0);</script>

<test-fixture id="basic">
  <template>
    <safe-types-element></safe-types-element>
  </template>
</test-fixture>

<script>
  suite('gr-tooltip-behavior tests', () => {
    let element;
    let sandbox;

    suiteSetup(() => {
      Polymer({
        is: 'safe-types-element',
        behaviors: [Gerrit.SafeTypes],
      });
    });

    setup(() => {
      sandbox = sinon.sandbox.create();
      element = fixture('basic');
    });

    teardown(() => {
      sandbox.restore();
    });

    test('SafeUrl accepts valid urls', () => {
      function accepts(url) {
        const safeUrl = new element.SafeUrl(url);
        assert.isOk(safeUrl);
        assert.equal(url, safeUrl.asString());
      }
      accepts('http://www.google.com/');
      accepts('https://www.google.com/');
      accepts('HtTpS://www.google.com/');
      accepts('//www.google.com/');
      accepts('/c/1234/file/path.html@45');
      accepts('#hash-url');
      accepts('mailto:name@example.com');
    });

    test('SafeUrl rejects invalid urls', () => {
      function rejects(url) {
        assert.throws(() => { new element.SafeUrl(url); });
      }
      rejects('javascript://alert("evil");');
      rejects('ftp:example.com');
      rejects('data:text/html,scary business');
    });

    suite('safeTypesBridge', () => {
      function acceptsString(value, type) {
        assert.equal(Gerrit.SafeTypes.safeTypesBridge(value, type),
            value);
      }

      function rejects(value, type) {
        assert.throws(() => { Gerrit.SafeTypes.safeTypesBridge(value, type); });
      }

      test('accepts valid URL strings', () => {
        acceptsString('/foo/bar', 'URL');
        acceptsString('#baz', 'URL');
      });

      test('rejects invalid URL strings', () => {
        rejects('javascript://void();', 'URL');
      });

      test('accepts SafeUrl values', () => {
        const url = '/abc/123';
        const safeUrl = new element.SafeUrl(url);
        assert.equal(Gerrit.SafeTypes.safeTypesBridge(safeUrl, 'URL'), url);
      });

      test('rejects non-string or non-SafeUrl types', () => {
        rejects(3.1415926, 'URL');
      });

      test('accepts any binding to STRING or CONSTANT', () => {
        acceptsString('foo/bar/baz', 'STRING');
        acceptsString('lorem ipsum dolor', 'CONSTANT');
      });

      test('rejects all other types', () => {
        rejects('foo', 'JAVASCRIPT');
        rejects('foo', 'HTML');
        rejects('foo', 'RESOURCE_URL');
        rejects('foo', 'STYLE');
      });
    });
  });
</script>