Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 1 | # 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 | |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame^] | 15 | from six.moves import html_parser as HTMLParser |
| 16 | from six.moves.urllib import parse |
| 17 | from six.moves.urllib import request |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 18 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 19 | from tempest import config |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 20 | from tempest.scenario import manager |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 21 | from tempest import test |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 22 | |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 23 | CONF = config.CONF |
| 24 | |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 25 | |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 26 | class 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 Igawa | 2675f8f | 2014-07-17 13:46:26 +0200 | [diff] [blame] | 50 | class TestDashboardBasicOps(manager.ScenarioTest): |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 51 | |
| 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 |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 60 | def skip_checks(cls): |
| 61 | super(TestDashboardBasicOps, cls).skip_checks() |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 62 | if not CONF.service_available.horizon: |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 63 | raise cls.skipException("Horizon support is required") |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 64 | |
| 65 | @classmethod |
| 66 | def setup_credentials(cls): |
Masayuki Igawa | 60ea6c5 | 2014-10-15 17:32:14 +0900 | [diff] [blame] | 67 | cls.set_network_resources() |
Emily Hugenbruch | 5e2d2a2 | 2015-02-25 21:35:45 +0000 | [diff] [blame] | 68 | super(TestDashboardBasicOps, cls).setup_credentials() |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 69 | |
| 70 | def check_login_page(self): |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame^] | 71 | response = request.urlopen(CONF.dashboard.dashboard_url) |
Thomas Bechtold | 4458749 | 2015-02-09 10:04:03 +0100 | [diff] [blame] | 72 | self.assertIn("id_username", response.read()) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 73 | |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 74 | def user_login(self, username, password): |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame^] | 75 | self.opener = request.build_opener(request.HTTPCookieProcessor()) |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 76 | response = self.opener.open(CONF.dashboard.dashboard_url).read() |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 77 | |
| 78 | # Grab the CSRF token and default region |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 79 | parser = HorizonHTMLParser() |
| 80 | parser.feed(response) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 81 | |
| 82 | # Prepare login form request |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame^] | 83 | req = request.Request(CONF.dashboard.login_url) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 84 | req.add_header('Content-type', 'application/x-www-form-urlencoded') |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 85 | req.add_header('Referer', CONF.dashboard.dashboard_url) |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 86 | params = {'username': username, |
| 87 | 'password': password, |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 88 | 'region': parser.region, |
| 89 | 'csrfmiddlewaretoken': parser.csrf_token} |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame^] | 90 | self.opener.open(req, parse.urlencode(params)) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 91 | |
| 92 | def check_home_page(self): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 93 | response = self.opener.open(CONF.dashboard.dashboard_url) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 94 | self.assertIn('Overview', response.read()) |
| 95 | |
Chris Hoge | 7579c1a | 2015-02-26 14:12:15 -0800 | [diff] [blame] | 96 | @test.idempotent_id('4f8851b1-0e69-482b-b63b-84c6e76f6c80') |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 97 | @test.services('dashboard') |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 98 | def test_basic_scenario(self): |
Andrea Frittoli | b21de6c | 2015-02-06 20:12:38 +0000 | [diff] [blame] | 99 | creds = self.os.credentials |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 100 | self.check_login_page() |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 101 | self.user_login(creds.username, creds.password) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 102 | self.check_home_page() |