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