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
7import unittest
8
9from compiled_file_system import CompiledFileSystem
10from content_providers import ContentProviders
11from extensions_paths import CHROME_EXTENSIONS
12from gcs_file_system_provider import CloudStorageFileSystemProvider
13from object_store_creator import ObjectStoreCreator
14from test_file_system import TestFileSystem
15from test_util import DisableLogging
16
17
18_CONTENT_PROVIDERS = {
19  'apples': {
20    'chromium': {
21      'dir': 'chrome/common/extensions/apples'
22    },
23    'serveFrom': 'apples-dir',
24  },
25  'bananas': {
26    'serveFrom': '',
27    'chromium': {
28      'dir': 'chrome/common/extensions'
29    },
30  },
31  'github-provider': {
32    'serveFrom': 'gh',
33    'github': {
34      'dir': 'chrome/common/extensions',
35      'owner': 'GoogleChrome',
36      'repo': 'hello-world',
37    },
38  },
39  'github-provider-with-dir': {
40    'serveFrom': 'gh2',
41    'github': {
42      'dir': 'chrome/common/extensions/tomatoes/are/a',
43      'owner': 'SomeOwner',
44      'repo': 'some-repo',
45    },
46  },
47  'tomatoes': {
48    'serveFrom': 'tomatoes-dir/are/a',
49    'chromium': {
50      'dir': 'chrome/common/extensions/tomatoes/are/a'
51    },
52  },
53}
54
55
56_FILE_SYSTEM_DATA = {
57  'docs': {
58    'templates': {
59      'json': {
60        'content_providers.json': json.dumps(_CONTENT_PROVIDERS),
61      },
62    },
63  },
64  'apples': {
65    'gala.txt': 'gala apples',
66    'green': {
67      'granny smith.txt': 'granny smith apples',
68    },
69  },
70  'tomatoes': {
71    'are': {
72      'a': {
73        'vegetable.txt': 'no they aren\'t',
74        'fruit': {
75          'cherry.txt': 'cherry tomatoes',
76        },
77      },
78    },
79  },
80}
81
82
83class _MockGithubFileSystemProvider(object):
84  '''A GithubFileSystemProvider imitation which records every call to Create
85  and returns them from GetAndReset.
86  '''
87
88  def __init__(self, file_system):
89    self._file_system = file_system
90    self._calls = []
91
92  def Create(self, owner, repo):
93    self._calls.append((owner, repo))
94    return self._file_system
95
96  def GetAndReset(self):
97    calls = self._calls
98    self._calls = []
99    return calls
100
101
102class ContentProvidersTest(unittest.TestCase):
103  def setUp(self):
104    object_store_creator = ObjectStoreCreator.ForTest()
105    test_file_system = TestFileSystem(_FILE_SYSTEM_DATA,
106                                      relative_to=CHROME_EXTENSIONS)
107    self._github_fs_provider = _MockGithubFileSystemProvider(test_file_system)
108    object_store_creator = ObjectStoreCreator.ForTest()
109    # TODO(mangini): create tests for GCS
110    self._gcs_fs_provider = CloudStorageFileSystemProvider(object_store_creator)
111    self._content_providers = ContentProviders(
112        object_store_creator,
113        CompiledFileSystem.Factory(object_store_creator),
114        test_file_system,
115        self._github_fs_provider,
116        self._gcs_fs_provider)
117
118  def testSimpleRootPath(self):
119    provider = self._content_providers.GetByName('apples')
120    self.assertEqual(
121        'gala apples',
122        provider.GetContentAndType('gala.txt').Get().content)
123    self.assertEqual(
124        'granny smith apples',
125        provider.GetContentAndType('green/granny smith.txt').Get().content)
126
127  def testComplexRootPath(self):
128    provider = self._content_providers.GetByName('tomatoes')
129    self.assertEqual(
130        'no they aren\'t',
131        provider.GetContentAndType('vegetable.txt').Get().content)
132    self.assertEqual(
133        'cherry tomatoes',
134        provider.GetContentAndType('fruit/cherry.txt').Get().content)
135
136  def testParentRootPath(self):
137    provider = self._content_providers.GetByName('bananas')
138    self.assertEqual(
139        'gala apples',
140        provider.GetContentAndType('apples/gala.txt').Get().content)
141
142  def testSimpleServlet(self):
143    provider, serve_from, path = self._content_providers.GetByServeFrom(
144        'apples-dir')
145    self.assertEqual('apples', provider.name)
146    self.assertEqual('apples-dir', serve_from)
147    self.assertEqual('', path)
148    provider, serve_from, path = self._content_providers.GetByServeFrom(
149        'apples-dir/')
150    self.assertEqual('apples', provider.name)
151    self.assertEqual('apples-dir', serve_from)
152    self.assertEqual('', path)
153    provider, serve_from, path = self._content_providers.GetByServeFrom(
154        'apples-dir/are/forever')
155    self.assertEqual('apples', provider.name)
156    self.assertEqual('apples-dir', serve_from)
157    self.assertEqual('are/forever', path)
158
159  def testComplexServlet(self):
160    provider, serve_from, path = self._content_providers.GetByServeFrom(
161        'tomatoes-dir/are/a')
162    self.assertEqual('tomatoes', provider.name)
163    self.assertEqual('tomatoes-dir/are/a', serve_from)
164    self.assertEqual('', path)
165    provider, serve_from, path = self._content_providers.GetByServeFrom(
166        'tomatoes-dir/are/a/fruit/they/are')
167    self.assertEqual('tomatoes', provider.name)
168    self.assertEqual('tomatoes-dir/are/a', serve_from)
169    self.assertEqual('fruit/they/are', path)
170
171  def testEmptyStringServlet(self):
172    provider, serve_from, path = self._content_providers.GetByServeFrom(
173        'tomatoes-dir/are')
174    self.assertEqual('bananas', provider.name)
175    self.assertEqual('', serve_from)
176    self.assertEqual('tomatoes-dir/are', path)
177    provider, serve_from, path = self._content_providers.GetByServeFrom('')
178    self.assertEqual('bananas', provider.name)
179    self.assertEqual('', serve_from)
180    self.assertEqual('', path)
181
182  @DisableLogging('error')
183  def testProviderNotFound(self):
184    self.assertEqual(None, self._content_providers.GetByName('cabbages'))
185
186  def testGithubContentProvider(self):
187    provider, serve_from, path = self._content_providers.GetByServeFrom(
188        'gh/apples/green/granny smith.txt')
189    self.assertEqual('github-provider', provider.name)
190    self.assertEqual('gh', serve_from)
191    self.assertEqual('apples/green/granny smith.txt', path)
192    self.assertEqual([('GoogleChrome', 'hello-world')],
193                     self._github_fs_provider.GetAndReset())
194    self.assertEqual(
195        'granny smith apples',
196        provider.GetContentAndType(path).Get().content)
197
198  def testGithubContentProviderWithDir(self):
199    provider, serve_from, path = self._content_providers.GetByServeFrom(
200        'gh2/fruit/cherry.txt')
201    self.assertEqual('github-provider-with-dir', provider.name)
202    self.assertEqual('gh2', serve_from)
203    self.assertEqual('fruit/cherry.txt', path)
204    self.assertEqual([('SomeOwner', 'some-repo')],
205                     self._github_fs_provider.GetAndReset())
206    self.assertEqual(
207        'cherry tomatoes',
208        provider.GetContentAndType(path).Get().content)
209
210if __name__ == '__main__':
211  unittest.main()
212