permissions_data_source_test.py revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1#!/usr/bin/env python
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import json
7from operator import itemgetter
8import unittest
9
10from extensions_paths import CHROME_EXTENSIONS
11from permissions_data_source import PermissionsDataSource
12from server_instance import ServerInstance
13from third_party.handlebar import Handlebar
14from test_file_system import TestFileSystem
15
16
17_PERMISSION_FEATURES = {
18  # This will appear for extensions with a description as defined in the
19  # permissions.json file.
20  'activeTab': {
21    'extension_types': ['extension'],
22  },
23  # This will appear for apps and extensions with an auto-generated description
24  # since the entry appears in _api_features.json.
25  'alarms': {
26    'extension_types': ['platform_app', 'extension'],
27  },
28  # This won't appear for anything since there's no entry in permissions.json
29  # and it's not an API.
30  'audioCapture': {
31    'extension_types': ['platform_app'],
32  },
33  # This won't appear for anything because it's private.
34  'commandLinePrivate': {
35    'extension_types': ['platform_app', 'extension']
36  },
37  # This will only appear for apps with an auto-generated description because
38  # it's an API.
39  'cookies': {
40    'extension_types': ['platform_app']
41  },
42}
43
44
45_PERMISSIONS_JSON = {
46  # This will appear for both apps and extensions with a custom description,
47  # anchor, etc.
48  'host-permissions': {
49    'anchor': 'custom-anchor',
50    'extension_types': ['platform_app', 'extension'],
51    'literal_name': True,
52    'name': 'match pattern',
53    'partial': 'permissions/host_permissions.html',
54  },
55  # A custom 'partial' here overrides the default partial.
56  'activeTab': {
57    'partial': 'permissions/active_tab.html'
58  },
59}
60
61
62_PERMISSIONS_PARTIALS = {
63  'active_tab.html': 'active tab',
64  'host_permissions.html': 'host permissions',
65  'generic_description.html': 'generic description',
66}
67
68
69_API_FEATURES = {
70  'alarms': {
71    'dependencies': ['permission:alarms']
72  },
73  'cookies': {
74    'dependencies': ['permission:cookies']
75  },
76}
77
78
79class PermissionsDataSourceTest(unittest.TestCase):
80  def testCreatePermissionsDataSource(self):
81    expected_extensions = [
82      {
83        'anchor': 'custom-anchor',
84        'description': 'host permissions',
85        'literal_name': True,
86        'name': 'match pattern',
87        'platforms': ['apps', 'extensions']
88      },
89      {
90        'anchor': 'activeTab',
91        'description': 'active tab',
92        'name': 'activeTab',
93        'platforms': ['extensions'],
94      },
95      {
96        'anchor': 'alarms',
97        'description': 'generic description',
98        'name': 'alarms',
99        'platforms': ['apps', 'extensions'],
100      },
101    ]
102
103    expected_apps = [
104      {
105        'anchor': 'custom-anchor',
106        'description': 'host permissions',
107        'literal_name': True,
108        'name': 'match pattern',
109        'platforms': ['apps', 'extensions'],
110      },
111      {
112        'anchor': 'alarms',
113        'description': 'generic description',
114        'name': 'alarms',
115        'platforms': ['apps', 'extensions'],
116      },
117      {
118        'anchor': 'cookies',
119        'description': 'generic description',
120        'name': 'cookies',
121        'platforms': ['apps'],
122      },
123    ]
124
125    test_file_system = TestFileSystem({
126      'api': {
127        '_api_features.json': json.dumps(_API_FEATURES),
128        '_manifest_features.json': '{}',
129        '_permission_features.json': json.dumps(_PERMISSION_FEATURES),
130      },
131      'docs': {
132        'templates': {
133          'json': {
134            'manifest.json': '{}',
135            'permissions.json': json.dumps(_PERMISSIONS_JSON),
136          },
137          'private': {
138            'permissions': _PERMISSIONS_PARTIALS
139          },
140        }
141      }
142    }, relative_to=CHROME_EXTENSIONS)
143
144    permissions_data_source = PermissionsDataSource(
145        ServerInstance.ForTest(test_file_system), None)
146
147    actual_extensions = permissions_data_source.get('declare_extensions')
148    actual_apps = permissions_data_source.get('declare_apps')
149
150    # Normalise all test data.
151    #   - Sort keys. Since the tests don't use OrderedDicts we can't make
152    #     assertions about the order, which is unfortunate. Oh well.
153    #   - Render all of the Handlerbar instances so that we can use ==.
154    #     Handlebars don't implement __eq__, but they probably should.
155    for lst in (actual_apps, actual_extensions,
156                expected_apps, expected_extensions):
157      lst.sort(key=itemgetter('name'))
158      for mapping in lst:
159        for key, value in mapping.iteritems():
160          if isinstance(value, Handlebar):
161            mapping[key] = value.Render().text
162
163    self.assertEqual(expected_extensions, actual_extensions)
164    self.assertEqual(expected_apps, actual_apps)
165
166
167if __name__ == '__main__':
168  unittest.main()
169