1# Copyright 2019 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import os
7from chrome_ent_test.infra.core import environment, before_all, test
8from infra import ChromeEnterpriseTestCase
9
10
11@environment(file="../policy_test.asset.textpb")
12class ExtensionInstallAllowlistTest(ChromeEnterpriseTestCase):
13  """Test the ExtensionInstallBlocklist policy.
14    https://cloud.google.com/docs/chrome-enterprise/policies/?policy=ExtensionInstallAllowlist"""
15
16  @before_all
17  def setup(self):
18    self.InstallChrome(self.win_config['client'])
19    self.EnableUITest(self.win_config['client'])
20    self.InstallWebDriver(self.win_config['client'])
21
22  def installExtension(self, url):
23    args = ['--url', url]
24
25    dir = os.path.dirname(os.path.abspath(__file__))
26    logging.info('Opening page: %s' % url)
27    output = self.RunUITest(
28        self.win_config['client'],
29        os.path.join(dir, '../install_extension.py'),
30        args=args)
31    return output
32
33  @test
34  def test_ExtensionAllowlist_hangout(self):
35    extension = 'nckgahadagoaajjgafhacjanaoiihapd'
36    self.SetPolicy(self.win_config['dc'], r'ExtensionInstallBlocklist\1', '*',
37                   'String')
38    self.SetPolicy(self.win_config['dc'], r'ExtensionInstallAllowlist\1',
39                   extension, 'String')
40    self.RunCommand(self.win_config['client'], 'gpupdate /force')
41    logging.info('Allowlist extension install for ' + extension +
42                 ' while disabling others')
43
44    test_url = 'https://chrome.google.com/webstore/detail/google-hangouts/nckgahadagoaajjgafhacjanaoiihapd'
45    output = self.installExtension(test_url)
46    self.assertIn('Not blocked', output)
47
48    negative_test_url = 'https://chrome.google.com/webstore/detail/grammarly-for-chrome/kbfnbcaeplbcioakkpcpgfkobkghlhen'
49    output = self.installExtension(negative_test_url)
50    self.assertIn('blocked', output)
51