Report to user if an LDAP error occurs
[cascardo/ipsilon.git] / tests / helpers / http.py
index 0da7ee2..8c0a291 100755 (executable)
@@ -1,22 +1,6 @@
 #!/usr/bin/python
 #
-# Copyright (C) 2014  Simo Sorce <simo@redhat.com>
-#
-# see file 'COPYING' for use and warranty information
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
+# Copyright (C) 2014 Ipsilon project Contributors, for license see COPYING
 
 from lxml import html
 import requests
@@ -46,7 +30,7 @@ class PageTree(object):
 
     def first_value(self, rule):
         result = self.tree.xpath(rule)
-        if type(result) is list:
+        if isinstance(result, list):
             if len(result) > 0:
                 result = result[0]
             else:
@@ -55,7 +39,7 @@ class PageTree(object):
 
     def all_values(self, rule):
         result = self.tree.xpath(rule)
-        if type(result) is list:
+        if isinstance(result, list):
             return result
         return [result]
 
@@ -94,8 +78,9 @@ class HttpSessions(object):
         session = self.get_session(url)
         allow_redirects = False
         if krb:
-            # In at least the test instance we don't get back a negotiate
-            # blob to do mutual authentication against.
+            # python-requests-kerberos isn't too bright about doing mutual
+            # authentication and it tries to do it on any non-401 response
+            # which doesn't work in our case since we follow redirects.
             kerberos_auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
             kwargs['auth'] = kerberos_auth
             allow_redirects = True
@@ -136,7 +121,7 @@ class HttpSessions(object):
         return values
 
     def handle_login_form(self, idp, page):
-        if type(page) != PageTree:
+        if not isinstance(page, PageTree):
             raise TypeError("Expected PageTree object")
 
         srv = self.servers[idp]
@@ -166,7 +151,7 @@ class HttpSessions(object):
                 {'headers': headers, 'data': payload}]
 
     def handle_return_form(self, page):
-        if type(page) != PageTree:
+        if not isinstance(page, PageTree):
             raise TypeError("Expected PageTree object")
 
         try:
@@ -192,7 +177,7 @@ class HttpSessions(object):
                 {'headers': headers, 'data': payload}]
 
     def handle_openid_form(self, page):
-        if type(page) != PageTree:
+        if not isinstance(page, PageTree):
             raise TypeError("Expected PageTree object")
 
         if not page.first_value('//title/text()') == \
@@ -222,7 +207,7 @@ class HttpSessions(object):
                 {'headers': headers, 'data': payload}]
 
     def handle_openid_consent_form(self, page):
-        if type(page) != PageTree:
+        if not isinstance(page, PageTree):
             raise TypeError("Expected PageTree object")
 
         try:
@@ -264,7 +249,6 @@ class HttpSessions(object):
         args = {}
 
         while True:
-            # pylint: disable=star-args
             r = self.access(action, url, krb=krb, **args)
             if r.status_code == 303 or r.status_code == 302:
                 if not follow_redirect:
@@ -316,7 +300,7 @@ class HttpSessions(object):
                 raise ValueError("Unhandled status (%d) on url %s" % (
                                  r.status_code, url))
 
-    def auth_to_idp(self, idp, krb=False):
+    def auth_to_idp(self, idp, krb=False, rule=None, expected=None):
 
         srv = self.servers[idp]
         target_url = '%s/%s/' % (srv['baseuri'], idp)
@@ -332,8 +316,12 @@ class HttpSessions(object):
 
         page = self.fetch_page(idp, url, krb=krb)
 
-        page.expected_value('//div[@id="welcome"]/p/text()',
-                            'Welcome %s!' % srv['user'])
+        if rule is None:
+            rule = '//div[@id="welcome"]/p/text()'
+        if expected is None:
+            expected = 'Welcome %s!' % srv['user']
+
+        page.expected_value(rule, expected)
 
     def logout_from_idp(self, idp):