1# -*- coding: utf-8 -*-
2# Copyright 2013 Google Inc. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Integration tests for the webcfg command."""
16
17from __future__ import absolute_import
18
19import json
20import gslib.tests.testcase as testcase
21from gslib.tests.testcase.integration_testcase import SkipForS3
22from gslib.tests.util import ObjectToURI as suri
23
24WEBCFG_FULL = json.loads('{"notFoundPage": "404", "mainPageSuffix": "main"}\n')
25WEBCFG_MAIN = json.loads('{"mainPageSuffix": "main"}\n')
26WEBCFG_ERROR = json.loads('{"notFoundPage": "404"}\n')
27WEBCFG_EMPTY = 'has no website configuration'
28
29
30@SkipForS3('Web set not supported for S3, web get returns XML.')
31class TestWeb(testcase.GsUtilIntegrationTestCase):
32  """Integration tests for the web command."""
33
34  _set_web_cmd = ['web', 'set']
35  _get_web_cmd = ['web', 'get']
36
37  def test_full(self):
38    bucket_uri = self.CreateBucket()
39    self.RunGsUtil(
40        self._set_web_cmd + ['-m', 'main', '-e', '404', suri(bucket_uri)])
41    stdout = self.RunGsUtil(
42        self._get_web_cmd + [suri(bucket_uri)], return_stdout=True)
43    self.assertEquals(json.loads(stdout), WEBCFG_FULL)
44
45  def test_main(self):
46    bucket_uri = self.CreateBucket()
47    self.RunGsUtil(self._set_web_cmd + ['-m', 'main', suri(bucket_uri)])
48    stdout = self.RunGsUtil(
49        self._get_web_cmd + [suri(bucket_uri)], return_stdout=True)
50    self.assertEquals(json.loads(stdout), WEBCFG_MAIN)
51
52  def test_error(self):
53    bucket_uri = self.CreateBucket()
54    self.RunGsUtil(self._set_web_cmd + ['-e', '404', suri(bucket_uri)])
55    stdout = self.RunGsUtil(
56        self._get_web_cmd + [suri(bucket_uri)], return_stdout=True)
57    self.assertEquals(json.loads(stdout), WEBCFG_ERROR)
58
59  def test_empty(self):
60    bucket_uri = self.CreateBucket()
61    self.RunGsUtil(self._set_web_cmd + [suri(bucket_uri)])
62    stdout = self.RunGsUtil(
63        self._get_web_cmd + [suri(bucket_uri)], return_stdout=True)
64    self.assertIn(WEBCFG_EMPTY, stdout)
65
66  def testTooFewArgumentsFails(self):
67    """Ensures web commands fail with too few arguments."""
68    # No arguments for get, but valid subcommand.
69    stderr = self.RunGsUtil(self._get_web_cmd, return_stderr=True,
70                            expected_status=1)
71    self.assertIn('command requires at least', stderr)
72
73    # No arguments for set, but valid subcommand.
74    stderr = self.RunGsUtil(self._set_web_cmd, return_stderr=True,
75                            expected_status=1)
76    self.assertIn('command requires at least', stderr)
77
78    # Neither arguments nor subcommand.
79    stderr = self.RunGsUtil(['web'], return_stderr=True, expected_status=1)
80    self.assertIn('command requires at least', stderr)
81
82
83class TestWebOldAlias(TestWeb):
84  _set_web_cmd = ['setwebcfg']
85  _get_web_cmd = ['getwebcfg']
86