Home | History | Annotate | Download | only in server2
      1 #!/usr/bin/env python
      2 # Copyright 2014 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 
      6 import unittest
      7 
      8 from owners_data_source import ParseOwnersFile, OwnersDataSource
      9 from server_instance import ServerInstance
     10 from servlet import Request
     11 from test_file_system import TestFileSystem
     12 
     13 
     14 _TEST_FS = {
     15   'chrome': {
     16     'browser': {
     17       'extensions': {
     18         'OWNERS': '\n'.join([
     19           '# Core owners.',
     20           'satsuki (at] revocs.tld'
     21         ]),
     22         'api': {
     23           'some_api': {
     24             'OWNERS': '\n'.join([
     25               'matoi (at] owner.tld'
     26             ]),
     27             'some_api.cc': ''
     28           },
     29           'another_api': {
     30             'another_api.cc': '',
     31             'another_api.h': ''
     32           },
     33           'moar_apis': {
     34             'OWNERS': '\n'.join([
     35               '# For editing moar_apis.',
     36               'satsuki (at] revocs.tld'
     37             ])
     38           }
     39         }
     40       }
     41     }
     42   },
     43   'extensions': {
     44     'browser': {
     45       'api': {
     46         'a_different_api': {
     47           'OWNERS': '\n'.join([
     48             '# Hallo!',
     49             'nonon (at] owner.tld',
     50             'matoi (at] owner.tld'
     51           ])
     52         }
     53       }
     54     }
     55   }
     56 }
     57 
     58 
     59 class OwnersDataSourceTest(unittest.TestCase):
     60   def setUp(self):
     61     server_instance = ServerInstance.ForTest(
     62         file_system=TestFileSystem(_TEST_FS))
     63     # Don't randomize the owners to avoid testing issues.
     64     self._owners_ds = OwnersDataSource(server_instance,
     65                                        Request.ForTest('/'),
     66                                        randomize=False)
     67 
     68   def testParseOwnersFile(self):
     69     owners_content = '\n'.join([
     70       'satsuki (at] revocs.tld',
     71       'mankanshoku (at] owner.tld',
     72       '',
     73       'matoi (at] owner.tld'
     74     ])
     75     owners, notes = ParseOwnersFile(owners_content, randomize=False)
     76     # The order of the owners list should reflect the order of the owners file.
     77     self.assertEqual(owners, [
     78       {
     79         'email': 'satsuki (at] revocs.tld',
     80         'username': 'satsuki'
     81       },
     82       {
     83         'email': 'mankanshoku (at] owner.tld',
     84         'username': 'mankanshoku'
     85       },
     86       {
     87         'email': 'matoi (at] owner.tld',
     88         'username': 'matoi',
     89         'last': True
     90       }
     91     ])
     92     self.assertEqual(notes, '')
     93 
     94     owners_content_with_comments = '\n'.join([
     95       '# This is a comment concerning this file',
     96       '# that should not be ignored.',
     97       'matoi (at] owner.tld',
     98       'mankanshoku (at] owner.tld',
     99       '',
    100       '# Only bug satsuki if matoi or mankanshoku are unavailable.',
    101       'satsuki (at] revocs.tld'
    102     ])
    103     owners, notes = ParseOwnersFile(owners_content_with_comments,
    104                                     randomize=False)
    105     self.assertEqual(owners, [
    106       {
    107         'email': 'matoi (at] owner.tld',
    108         'username': 'matoi'
    109       },
    110       {
    111         'email': 'mankanshoku (at] owner.tld',
    112         'username': 'mankanshoku'
    113       },
    114       {
    115         'email': 'satsuki (at] revocs.tld',
    116         'username': 'satsuki',
    117         'last': True
    118       }
    119     ])
    120     self.assertEqual(notes, '\n'.join([
    121       'This is a comment concerning this file',
    122       'that should not be ignored.',
    123       'Only bug satsuki if matoi or mankanshoku are unavailable.'
    124     ]))
    125 
    126 
    127   def testCollectOwners(self):
    128     # NOTE: Order matters. The list should be sorted by 'apiName'.
    129     self.assertEqual(self._owners_ds.get('apis'), [{
    130       'apiName': 'Core Extensions/Apps Owners',
    131       'owners': [
    132         {
    133           'email': 'satsuki (at] revocs.tld',
    134           'username': 'satsuki',
    135           'last': True
    136         }
    137       ],
    138       'notes': 'Core owners.',
    139       'id': 'core'
    140     },
    141     {
    142       'apiName': 'a_different_api',
    143       'owners': [
    144         {
    145           'email': 'nonon (at] owner.tld',
    146           'username': 'nonon'
    147         },
    148         {
    149           'email': 'matoi (at] owner.tld',
    150           'username': 'matoi',
    151           'last': True
    152         }
    153       ],
    154       'notes': 'Hallo!',
    155       'id': 'a_different_api'
    156     },
    157     {
    158       'apiName': 'another_api',
    159       'owners': [],
    160       'notes': 'Use one of the Core Extensions/Apps Owners.',
    161       'id': 'another_api'
    162     },
    163     {
    164       'apiName': 'moar_apis',
    165       'owners': [
    166         {
    167           'email': 'satsuki (at] revocs.tld',
    168           'username': 'satsuki',
    169           'last': True
    170         }
    171       ],
    172       'notes': 'For editing moar_apis.',
    173       'id': 'moar_apis'
    174     },
    175     {
    176       'apiName': 'some_api',
    177       'owners': [
    178         {
    179           'email': 'matoi (at] owner.tld',
    180           'username': 'matoi',
    181           'last': True
    182         }
    183       ],
    184       'notes': '',
    185       'id': 'some_api'
    186     }])
    187 
    188 if __name__ == '__main__':
    189   unittest.main()
    190