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
|
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import json
from operator import itemgetter
import unittest
from extensions_paths import CHROME_EXTENSIONS
from permissions_data_source import PermissionsDataSource
from server_instance import ServerInstance
from third_party.handlebar import Handlebar
from test_file_system import TestFileSystem
_PERMISSION_FEATURES = {
# This will appear for extensions with a description as defined in the
# permissions.json file.
'activeTab': {
'extension_types': ['extension'],
},
# This will appear for apps and extensions with an auto-generated description
# since the entry appears in _api_features.json.
'alarms': {
'extension_types': ['platform_app', 'extension'],
},
# This won't appear for anything since there's no entry in permissions.json
# and it's not an API.
'audioCapture': {
'extension_types': ['platform_app'],
},
# This won't appear for anything because it's private.
'commandLinePrivate': {
'extension_types': ['platform_app', 'extension']
},
# This will only appear for apps with an auto-generated description because
# it's an API.
'cookies': {
'extension_types': ['platform_app']
},
}
_PERMISSIONS_JSON = {
# This will appear for both apps and extensions with a custom description,
# anchor, etc.
'host-permissions': {
'anchor': 'custom-anchor',
'extension_types': ['platform_app', 'extension'],
'literal_name': True,
'name': 'match pattern',
'partial': 'permissions/host_permissions.html',
},
# A custom 'partial' here overrides the default partial.
'activeTab': {
'partial': 'permissions/active_tab.html'
},
}
_PERMISSIONS_PARTIALS = {
'active_tab.html': 'active tab',
'host_permissions.html': 'host permissions',
'generic_description.html': 'generic description',
}
_API_FEATURES = {
'alarms': {
'dependencies': ['permission:alarms']
},
'cookies': {
'dependencies': ['permission:cookies']
},
}
class PermissionsDataSourceTest(unittest.TestCase):
def testCreatePermissionsDataSource(self):
expected_extensions = [
{
'anchor': 'custom-anchor',
'description': 'host permissions',
'literal_name': True,
'name': 'match pattern',
'platforms': ['apps', 'extensions']
},
{
'anchor': 'activeTab',
'description': 'active tab',
'name': 'activeTab',
'platforms': ['extensions'],
},
{
'anchor': 'alarms',
'description': 'generic description',
'name': 'alarms',
'platforms': ['apps', 'extensions'],
},
]
expected_apps = [
{
'anchor': 'custom-anchor',
'description': 'host permissions',
'literal_name': True,
'name': 'match pattern',
'platforms': ['apps', 'extensions'],
},
{
'anchor': 'alarms',
'description': 'generic description',
'name': 'alarms',
'platforms': ['apps', 'extensions'],
},
{
'anchor': 'cookies',
'description': 'generic description',
'name': 'cookies',
'platforms': ['apps'],
},
]
test_file_system = TestFileSystem({
'api': {
'_api_features.json': json.dumps(_API_FEATURES),
'_manifest_features.json': '{}',
'_permission_features.json': json.dumps(_PERMISSION_FEATURES),
},
'docs': {
'templates': {
'json': {
'manifest.json': '{}',
'permissions.json': json.dumps(_PERMISSIONS_JSON),
},
'private': {
'permissions': _PERMISSIONS_PARTIALS
},
}
}
}, relative_to=CHROME_EXTENSIONS)
permissions_data_source = PermissionsDataSource(
ServerInstance.ForTest(test_file_system), None)
actual_extensions = permissions_data_source.get('declare_extensions')
actual_apps = permissions_data_source.get('declare_apps')
# Normalise all test data.
# - Sort keys. Since the tests don't use OrderedDicts we can't make
# assertions about the order, which is unfortunate. Oh well.
# - Render all of the Handlerbar instances so that we can use ==.
# Handlebars don't implement __eq__, but they probably should.
for lst in (actual_apps, actual_extensions,
expected_apps, expected_extensions):
lst.sort(key=itemgetter('name'))
for mapping in lst:
for key, value in mapping.iteritems():
if isinstance(value, Handlebar):
mapping[key] = value.Render().text
self.assertEqual(expected_extensions, actual_extensions)
self.assertEqual(expected_apps, actual_apps)
if __name__ == '__main__':
unittest.main()
|