]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/tests/test_home.py
import 15.2.0 Octopus source
[ceph.git] / ceph / src / pybind / mgr / dashboard / tests / test_home.py
1 from __future__ import absolute_import
2
3 import logging
4 import os
5
6 try:
7 import mock
8 except ImportError:
9 import unittest.mock as mock
10
11 from . import ControllerTestCase, FakeFsMixin
12 from .. import mgr
13
14 from ..controllers.home import HomeController, LanguageMixin
15
16 logger = logging.getLogger()
17
18
19 class HomeTest(ControllerTestCase, FakeFsMixin):
20 @classmethod
21 def setup_server(cls):
22 frontend_path = mgr.get_frontend_path()
23 cls.fs.reset()
24 cls.fs.create_dir(frontend_path)
25 cls.fs.create_file(
26 os.path.join(frontend_path, '..', 'package.json'),
27 contents='{"config":{"locale": "en-US"}}')
28 with mock.patch(cls.builtins_open, new=cls.f_open),\
29 mock.patch('os.listdir', new=cls.f_os.listdir):
30 lang = LanguageMixin()
31 cls.fs.create_file(
32 os.path.join(lang.DEFAULT_LANGUAGE_PATH, 'index.html'),
33 contents='<!doctype html><html lang="en"><body></body></html>')
34 cls.setup_controllers([HomeController])
35
36 @mock.patch(FakeFsMixin.builtins_open, new=FakeFsMixin.f_open)
37 @mock.patch('os.stat', new=FakeFsMixin.f_os.stat)
38 @mock.patch('os.listdir', new=FakeFsMixin.f_os.listdir)
39 def test_home_default_lang(self):
40 self._get('/')
41 self.assertStatus(200)
42 logger.info(self.body)
43 self.assertIn('<html lang="en">', self.body.decode('utf-8'))
44
45 @mock.patch(FakeFsMixin.builtins_open, new=FakeFsMixin.f_open)
46 @mock.patch('os.stat', new=FakeFsMixin.f_os.stat)
47 @mock.patch('os.listdir', new=FakeFsMixin.f_os.listdir)
48 def test_home_uplevel_check(self):
49 self._get('/../../../../../../etc/shadow')
50 self.assertStatus(403)
51
52 @mock.patch(FakeFsMixin.builtins_open, new=FakeFsMixin.f_open)
53 @mock.patch('os.stat', new=FakeFsMixin.f_os.stat)
54 @mock.patch('os.listdir', new=FakeFsMixin.f_os.listdir)
55 def test_home_en_us(self):
56 self._get('/', headers=[('Accept-Language', 'en-US')])
57 self.assertStatus(200)
58 logger.info(self.body)
59 self.assertIn('<html lang="en">', self.body.decode('utf-8'))
60
61 @mock.patch(FakeFsMixin.builtins_open, new=FakeFsMixin.f_open)
62 @mock.patch('os.stat', new=FakeFsMixin.f_os.stat)
63 @mock.patch('os.listdir', new=FakeFsMixin.f_os.listdir)
64 def test_home_non_supported_lang(self):
65 self._get('/', headers=[('Accept-Language', 'NO-NO')])
66 self.assertStatus(200)
67 logger.info(self.body)
68 self.assertIn('<html lang="en">', self.body.decode('utf-8'))