The tests for not_found were broken, this fixes it and adds tests for the other statuses.

I changed the parent class of HTTP_Status because it calls self.info which is provided by Plugable. This wasn't a problem at runtime because Backend provides self.log.

rob
>From 78c13cfdf81e8aa731f953bbc8293bb6dd2b89fb Mon Sep 17 00:00:00 2001
From: Rob Crittenden <rcrit...@redhat.com>
Date: Wed, 29 Feb 2012 16:12:58 -0500
Subject: [PATCH] subclass HTTP_Status from plugable.Plugin, fix not_found
 tests

HTTP_Status needs to subclass from Plugin because it does its own logging.

Add tests for other methods of HTTP_Status
---
 ipaserver/rpcserver.py                 |    3 +-
 tests/test_ipaserver/test_rpcserver.py |   53 +++++++++++++++++++++++++++----
 2 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/ipaserver/rpcserver.py b/ipaserver/rpcserver.py
index c383f0482171e264c379aa594568f036feafe915..271f7717b6343c3f7569a6e955bb5ae814cc27e9 100644
--- a/ipaserver/rpcserver.py
+++ b/ipaserver/rpcserver.py
@@ -26,6 +26,7 @@ Also see the `ipalib.rpc` module.
 from cgi import parse_qs
 from xml.sax.saxutils import escape
 from xmlrpclib import Fault
+from ipalib import plugable
 from ipalib.backend import Executioner
 from ipalib.errors import PublicError, InternalError, CommandError, JSONError, ConversionError, CCacheError, RefererError, InvalidSessionPassword
 from ipalib.request import context, Connection, destroy_context
@@ -96,7 +97,7 @@ _unauthorized_template = """<html>
 </body>
 </html>"""
 
-class HTTP_Status(object):
+class HTTP_Status(plugable.Plugin):
     def not_found(self, environ, start_response, url, message):
         """
         Return a 404 Not Found error.
diff --git a/tests/test_ipaserver/test_rpcserver.py b/tests/test_ipaserver/test_rpcserver.py
index 15ca9dc08f77d5bed11d819c3297d17e5aeea2a4..97632b05d454b0ece60181ceeb6980b95e6909a1 100644
--- a/tests/test_ipaserver/test_rpcserver.py
+++ b/tests/test_ipaserver/test_rpcserver.py
@@ -46,28 +46,67 @@ class StartResponse(object):
 
 
 def test_not_found():
-    f = rpcserver.not_found
+    f = rpcserver.HTTP_Status()
     t = rpcserver._not_found_template
     s = StartResponse()
 
     # Test with an innocent URL:
-    d = dict(SCRIPT_NAME='/ipa', PATH_INFO='/foo/stuff')
+    url = '/ipa/foo/stuff'
     assert_equal(
-        f(d, s),
+        f.not_found(None, s, url, None),
         [t % dict(url='/ipa/foo/stuff')]
     )
     assert s.status == '404 Not Found'
-    assert s.headers == [('Content-Type', 'text/html')]
+    assert s.headers == [('Content-Type', 'text/html; charset=utf-8')]
 
     # Test when URL contains any of '<>&'
     s.reset()
-    d = dict(SCRIPT_NAME='&nbsp;', PATH_INFO='<script>do_bad_stuff();</script>')
+    url ='&nbsp;' + '<script>do_bad_stuff();</script>'
     assert_equal(
-        f(d, s),
+        f.not_found(None, s, url, None),
         [t % dict(url='&amp;nbsp;&lt;script&gt;do_bad_stuff();&lt;/script&gt;')]
     )
     assert s.status == '404 Not Found'
-    assert s.headers == [('Content-Type', 'text/html')]
+    assert s.headers == [('Content-Type', 'text/html; charset=utf-8')]
+
+
+def test_bad_request():
+    f = rpcserver.HTTP_Status()
+    t = rpcserver._bad_request_template
+    s = StartResponse()
+
+    assert_equal(
+        f.bad_request(None, s, 'illegal request'),
+        [t % dict(message='illegal request')]
+    )
+    assert s.status == '400 Bad Request'
+    assert s.headers == [('Content-Type', 'text/html; charset=utf-8')]
+
+
+def test_internal_error():
+    f = rpcserver.HTTP_Status()
+    t = rpcserver._internal_error_template
+    s = StartResponse()
+
+    assert_equal(
+        f.internal_error(None, s, 'request failed'),
+        [t % dict(message='request failed')]
+    )
+    assert s.status == '500 Internal Server Error'
+    assert s.headers == [('Content-Type', 'text/html; charset=utf-8')]
+
+
+def test_internal_error():
+    f = rpcserver.HTTP_Status()
+    t = rpcserver._unauthorized_template
+    s = StartResponse()
+
+    assert_equal(
+        f.unauthorized(None, s, 'unauthorized'),
+        [t % dict(message='unauthorized')]
+    )
+    assert s.status == '401 Unauthorized'
+    assert s.headers == [('Content-Type', 'text/html; charset=utf-8')]
 
 
 
-- 
1.7.6

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to