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 | |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 15 | import HTMLParser |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 16 | import urllib |
| 17 | import urllib2 |
| 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 |
Andrea Frittoli | ac20b5e | 2014-09-15 13:31:14 +0100 | [diff] [blame] | 60 | def resource_setup(cls): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 61 | if not CONF.service_available.horizon: |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 62 | raise cls.skipException("Horizon support is required") |
Masayuki Igawa | 60ea6c5 | 2014-10-15 17:32:14 +0900 | [diff] [blame] | 63 | cls.set_network_resources() |
| 64 | super(TestDashboardBasicOps, cls).resource_setup() |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 65 | |
| 66 | def check_login_page(self): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 67 | response = urllib2.urlopen(CONF.dashboard.dashboard_url) |
Thomas Bechtold | 4458749 | 2015-02-09 10:04:03 +0100 | [diff] [blame] | 68 | self.assertIn("id_username", response.read()) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 69 | |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 70 | def user_login(self, username, password): |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 71 | self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 72 | response = self.opener.open(CONF.dashboard.dashboard_url).read() |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 73 | |
| 74 | # Grab the CSRF token and default region |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 75 | parser = HorizonHTMLParser() |
| 76 | parser.feed(response) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 77 | |
| 78 | # Prepare login form request |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 79 | req = urllib2.Request(CONF.dashboard.login_url) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 80 | req.add_header('Content-type', 'application/x-www-form-urlencoded') |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 81 | req.add_header('Referer', CONF.dashboard.dashboard_url) |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 82 | params = {'username': username, |
| 83 | 'password': password, |
Sean Dague | 8fa6c03 | 2014-11-25 10:54:38 -0500 | [diff] [blame] | 84 | 'region': parser.region, |
| 85 | 'csrfmiddlewaretoken': parser.csrf_token} |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 86 | self.opener.open(req, urllib.urlencode(params)) |
| 87 | |
| 88 | def check_home_page(self): |
Matthew Treinish | 6c07229 | 2014-01-29 19:15:52 +0000 | [diff] [blame] | 89 | response = self.opener.open(CONF.dashboard.dashboard_url) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 90 | self.assertIn('Overview', response.read()) |
| 91 | |
Masayuki Igawa | 4ded9f0 | 2014-02-17 15:05:59 +0900 | [diff] [blame] | 92 | @test.services('dashboard') |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 93 | def test_basic_scenario(self): |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 94 | creds = self.credentials() |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 95 | self.check_login_page() |
Matthew Treinish | 643f4c6 | 2014-12-12 19:54:34 -0500 | [diff] [blame] | 96 | self.user_login(creds.username, creds.password) |
Julie Pichon | d101764 | 2013-07-24 16:37:23 +0100 | [diff] [blame] | 97 | self.check_home_page() |