blob: 13130503aa9bd7649a8c6b819b8af6aed32646b4 [file] [log] [blame]
Julie Pichond1017642013-07-24 16:37:23 +01001# All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
Sean Dague8fa6c032014-11-25 10:54:38 -050015import HTMLParser
Julie Pichond1017642013-07-24 16:37:23 +010016import urllib
17import urllib2
18
Matthew Treinish6c072292014-01-29 19:15:52 +000019from tempest import config
Julie Pichond1017642013-07-24 16:37:23 +010020from tempest.scenario import manager
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090021from tempest import test
Julie Pichond1017642013-07-24 16:37:23 +010022
Matthew Treinish6c072292014-01-29 19:15:52 +000023CONF = config.CONF
24
Julie Pichond1017642013-07-24 16:37:23 +010025
Sean Dague8fa6c032014-11-25 10:54:38 -050026class HorizonHTMLParser(HTMLParser.HTMLParser):
27 csrf_token = None
28 region = None
29
30 def _find_name(self, attrs, name):
31 for attrpair in attrs:
32 if attrpair[0] == 'name' and attrpair[1] == name:
33 return True
34 return False
35
36 def _find_value(self, attrs):
37 for attrpair in attrs:
38 if attrpair[0] == 'value':
39 return attrpair[1]
40 return None
41
42 def handle_starttag(self, tag, attrs):
43 if tag == 'input':
44 if self._find_name(attrs, 'csrfmiddlewaretoken'):
45 self.csrf_token = self._find_value(attrs)
46 if self._find_name(attrs, 'region'):
47 self.region = self._find_value(attrs)
48
49
Masayuki Igawa2675f8f2014-07-17 13:46:26 +020050class TestDashboardBasicOps(manager.ScenarioTest):
Julie Pichond1017642013-07-24 16:37:23 +010051
52 """
53 This is a basic scenario test:
54 * checks that the login page is available
55 * logs in as a regular user
56 * checks that the user home page loads without error
57 """
58
59 @classmethod
Andrea Frittoliac20b5e2014-09-15 13:31:14 +010060 def resource_setup(cls):
Matthew Treinish6c072292014-01-29 19:15:52 +000061 if not CONF.service_available.horizon:
Julie Pichond1017642013-07-24 16:37:23 +010062 raise cls.skipException("Horizon support is required")
Masayuki Igawa60ea6c52014-10-15 17:32:14 +090063 cls.set_network_resources()
64 super(TestDashboardBasicOps, cls).resource_setup()
Julie Pichond1017642013-07-24 16:37:23 +010065
66 def check_login_page(self):
Matthew Treinish6c072292014-01-29 19:15:52 +000067 response = urllib2.urlopen(CONF.dashboard.dashboard_url)
Thomas Bechtold44587492015-02-09 10:04:03 +010068 self.assertIn("id_username", response.read())
Julie Pichond1017642013-07-24 16:37:23 +010069
Matthew Treinish643f4c62014-12-12 19:54:34 -050070 def user_login(self, username, password):
Julie Pichond1017642013-07-24 16:37:23 +010071 self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
Matthew Treinish6c072292014-01-29 19:15:52 +000072 response = self.opener.open(CONF.dashboard.dashboard_url).read()
Julie Pichond1017642013-07-24 16:37:23 +010073
74 # Grab the CSRF token and default region
Sean Dague8fa6c032014-11-25 10:54:38 -050075 parser = HorizonHTMLParser()
76 parser.feed(response)
Julie Pichond1017642013-07-24 16:37:23 +010077
78 # Prepare login form request
Matthew Treinish6c072292014-01-29 19:15:52 +000079 req = urllib2.Request(CONF.dashboard.login_url)
Julie Pichond1017642013-07-24 16:37:23 +010080 req.add_header('Content-type', 'application/x-www-form-urlencoded')
Matthew Treinish6c072292014-01-29 19:15:52 +000081 req.add_header('Referer', CONF.dashboard.dashboard_url)
Matthew Treinish643f4c62014-12-12 19:54:34 -050082 params = {'username': username,
83 'password': password,
Sean Dague8fa6c032014-11-25 10:54:38 -050084 'region': parser.region,
85 'csrfmiddlewaretoken': parser.csrf_token}
Julie Pichond1017642013-07-24 16:37:23 +010086 self.opener.open(req, urllib.urlencode(params))
87
88 def check_home_page(self):
Matthew Treinish6c072292014-01-29 19:15:52 +000089 response = self.opener.open(CONF.dashboard.dashboard_url)
Julie Pichond1017642013-07-24 16:37:23 +010090 self.assertIn('Overview', response.read())
91
Masayuki Igawa4ded9f02014-02-17 15:05:59 +090092 @test.services('dashboard')
Julie Pichond1017642013-07-24 16:37:23 +010093 def test_basic_scenario(self):
Matthew Treinish643f4c62014-12-12 19:54:34 -050094 creds = self.credentials()
Julie Pichond1017642013-07-24 16:37:23 +010095 self.check_login_page()
Matthew Treinish643f4c62014-12-12 19:54:34 -050096 self.user_login(creds.username, creds.password)
Julie Pichond1017642013-07-24 16:37:23 +010097 self.check_home_page()