Пример #1
0
    def _make_mock_openid_request(self, http_query):
        """ Make a mock OpenIDRequest for a given HTTP query """
        openid_request = Mock('OpenIDRequest')
        keys = ('mode', 'identity', 'trust_root', 'return_to')
        query_key_prefix = 'openid.'
        for key in keys:
            query_key = '%(query_key_prefix)s%(key)s' % vars()
            setattr(openid_request, key, http_query.get(query_key))
        openid_request.immediate = (
            openid_request.mode in ['checkid_immediate']
            )
        openid_request.answer.mock_returns = Stub_OpenIDResponse()

        return openid_request
Пример #2
0
 def test_main_calls_become_daemon(self):
     """ main() should attempt to become a daemon """
     params = self.valid_apps['simple']
     instance = params['instance']
     gracied.become_daemon = Mock('become_daemon')
     expect_stdout = """\
         Called become_daemon()
         """
     instance.main()
     self.failUnlessOutputCheckerMatch(expect_stdout,
                                       self.stdout_test.getvalue())
Пример #3
0
 def test_init_configures_logging(self):
     """ Gracie instance should configure logging """
     params = self.valid_apps['simple']
     args = params['args']
     logging_prev = gracied.logging
     gracied.logging = Mock('logging')
     expect_stdout = """\
        Called logging.basicConfig(...)
        """
     instance = self.app_class(**args)
     gracied.logging = logging_prev
     self.failUnlessOutputCheckerMatch(expect_stdout,
                                       self.stdout_test.getvalue())
Пример #4
0
 def test_wrong_arguments_invokes_parser_error(self):
     """ Wrong number of cmdline arguments should invoke parser error """
     gracied.OptionParser.error = Mock("OptionParser.error", )
     invalid_argv_params = [[
         "progname",
         "foo",
     ]]
     expect_stdout = """\
         Called OptionParser.error("...")
         """
     for argv in invalid_argv_params:
         args = dict(argv=argv)
         instance = self.app_class(**args)
         self.failUnlessOutputCheckerMatch(expect_stdout,
                                           self.stdout_test.getvalue())
Пример #5
0
    def _make_mock_openid_server(self, openid_request):
        """ Make a mock OpenIDServer for a given HTTP query """
        def stub_sign(obj):
            return obj

        openid_server = Mock('openid_server')

        if openid_request.mode:
            openid_server.decodeRequest.mock_returns = \
                openid_request
            openid_server.handleRequest.mock_returns = \
                Stub_OpenIDResponse()
            openid_server.encodeResponse.mock_returns = \
                Stub_OpenIDWebResponse()
        openid_server.signatory.sign = stub_sign

        return openid_server
Пример #6
0
 def test_send_to_handler_uses_handler(self):
     """ Response.send_to_handler should use specified handler """
     self.stdout_test = StringIO("")
     stdout_prev = sys.stdout
     sys.stdout = self.stdout_test
     for key, params in self.iterate_params():
         instance = params['instance']
         handler = Mock('HTTPRequestHandler')
         instance.send_to_handler(handler)
         expect_stdout = """\
             Called HTTPRequestHandler.send_response(...)
             ...Called HTTPRequestHandler.end_headers()
             Called HTTPRequestHandler.wfile.write(...)
             Called HTTPRequestHandler.wfile.close()
             """
         self.failUnlessOutputCheckerMatch(expect_stdout,
                                           self.stdout_test.getvalue())
     sys.stdout = stdout_prev
Пример #7
0
    def setUp(self):
        """ Set up test fixtures """

        self.server_class = httpserver.HTTPServer

        self.stub_handler_class = Stub_HTTPRequestHandler
        self.mock_handler_class = Mock('HTTPRequestHandler')

        self.server_bind_prev = httpserver.BaseHTTPServer.server_bind
        httpserver.BaseHTTPServer.server_bind = stub_server_bind

        self.stdout_prev = sys.stdout
        self.stdout_test = StringIO()
        sys.stdout = self.stdout_test

        self.valid_servers = {
            'simple': dict(address=('', 80), ),
        }

        for key, params in self.valid_servers.items():
            args = params.get('args')
            address = params.get('address')
            handler_class = params.setdefault('handler_class',
                                              self.stub_handler_class)
            opts = make_default_opts()
            gracie_server = params.setdefault('gracie_server',
                                              Stub_GracieServer(address, opts))
            if not args:
                args = dict(
                    server_address=params['address'],
                    RequestHandlerClass=params['handler_class'],
                    gracie_server=params['gracie_server'],
                )
            instance = self.server_class(**args)
            params['args'] = args
            params['instance'] = instance

        self.iterate_params = scaffold.make_params_iterator(
            default_params_dict=self.valid_servers)
Пример #8
0
    def setUp(self):
        """ Set up test fixtures """

        self.app_class = gracied.Gracie

        self.stdout_prev = sys.stdout
        self.stdout_test = StringIO()
        sys.stdout = self.stdout_test

        self.parser_error_prev = gracied.OptionParser.error
        gracied.OptionParser.error = Mock("OptionParser.error",
                                          raises=SystemExit)

        self.stub_server_class = Stub_GracieServer
        self.mock_server_class = Mock('GracieServer_class')
        self.mock_server_class.mock_returns = Mock('GracieServer')

        self.server_class_prev = gracied.GracieServer
        gracied.GracieServer = self.stub_server_class
        self.default_port_prev = gracied.default_port
        gracied.default_port = 7654

        self.valid_apps = {
            'simple':
            dict(),
            'argv_loglevel_debug':
            dict(options=["--log-level", "debug"], ),
            'change-host':
            dict(
                options=["--host", "frobnitz"],
                host="frobnitz",
            ),
            'change-port':
            dict(
                options=["--port", "9779"],
                port=9779,
            ),
            'change-address':
            dict(
                options=["--host", "frobnitz", "--port", "9779"],
                host="frobnitz",
                port=9779,
            ),
            'change-root-url':
            dict(
                options=["--root-url", "http://spudnik/spam"],
                root_url="http://spudnik/spam",
            ),
            'change-address-and-root-url':
            dict(
                options=[
                    "--host",
                    "frobnitz",
                    "--port",
                    "9779",
                    "--root-url",
                    "http://spudnik/spam",
                ],
                host="frobnitz",
                port=9779,
                root_url="http://frobnitz/spam",
            ),
        }

        for key, params in self.valid_apps.items():
            argv = ["progname"]
            options = params.get('options', None)
            if options:
                argv.extend(options)
            params['argv'] = argv
            args = dict(argv=argv)
            params['args'] = args
            instance = self.app_class(**args)
            params['instance'] = instance

        self.iterate_params = scaffold.make_params_iterator(
            default_params_dict=self.valid_apps)
Пример #9
0
    def setUp(self):
        """ Set up test fixtures """
        self.handler_class = httprequest.HTTPRequestHandler

        self.stdout_prev = sys.stdout
        self.stdout_test = StringIO()
        sys.stdout = self.stdout_test

        self.response_class_prev = httprequest.Response
        self.response_header_class_prev = httprequest.ResponseHeader
        self.page_class_prev = httprequest.pagetemplate.Page
        self.cookie_name_prev = httprequest.session_cookie_name
        self.dispatch_method_prev = self.handler_class._dispatch
        httprequest.Response = Mock('Response_class')
        httprequest.Response.mock_returns = Mock('Response')
        httprequest.ResponseHeader = Mock('ResponseHeader_class')
        httprequest.ResponseHeader.mock_returns = Mock('ResponseHeader')
        httprequest.pagetemplate.Page = Mock('Page_class')
        httprequest.pagetemplate.Page.mock_returns = Mock('Page')
        httprequest.session_cookie_name = "TEST_session"
        mock_openid_server = Mock('openid_server')

        self.valid_requests = {
            'get-bogus': dict(
                request = Stub_Request("GET", "/bogus"),
                ),
            'get-root': dict(
                request = Stub_Request("GET", "/"),
                ),
            'no-cookie': dict(
                request = Stub_Request("GET", "/"),
                ),
            'unknown-cookie': dict(
                request = Stub_Request("GET", "/",
                    header = [
                        ("Cookie", "TEST_session=DECAFBAD"),
                        ],
                    ),
                session_id = "DECAFBAD",
                ),
            'good-cookie': dict(
                identity_name = "fred",
                request = Stub_Request("GET", "/",
                    header = [
                        ("Cookie", "TEST_session=DEADBEEF-fred"),
                        ],
                    ),
                session = dict(
                    session_id = "DEADBEEF-fred",
                    username = "******",
                    ),
                ),
            'id-bogus': dict(
                identity_name = "bogus",
                request = Stub_Request("GET", "/id/bogus"),
                ),
            'id-fred': dict(
                identity_name = "fred",
                request = Stub_Request("GET", "/id/fred"),
                ),
            'logout': dict(
                request = Stub_Request("GET", "/logout"),
                ),
            'login': dict(
                request = Stub_Request("GET", "/login"),
                ),
            'nobutton-login': dict(
                request = Stub_Request("POST", "/login",
                    query = dict(
                        username="******",
                        password="******",
                        ),
                    ),
                ),
            'cancel-login': dict(
                request = Stub_Request("POST", "/login",
                    query = dict(
                        username="******",
                        password="******",
                        cancel="Cancel",
                        ),
                    ),
                ),
            'login-bogus': dict(
                identity_name = "bogus",
                request = Stub_Request("POST", "/login",
                    query = dict(
                        username="******",
                        password="******",
                        submit="Sign in",
                        ),
                    ),
                ),
            'login-fred-wrong': dict(
                identity_name = "fred",
                request = Stub_Request("POST", "/login",
                    query = dict(
                        username="******",
                        password="******",
                        submit="Sign in",
                        ),
                    ),
                ),
            'login-fred-okay': dict(
                identity_name = "fred",
                request = Stub_Request("POST", "/login",
                    query = dict(
                        username="******",
                        password="******",
                        submit="Sign in",
                        ),
                    ),
                ),
            'openid-no-query': dict(
                request = Stub_Request("GET", "/openidserver"),
                ),
            'openid-bogus-query': dict(
                request = Stub_Request("GET", "/openidserver",
                    query = {
                        "foo.bar": "spam",
                        "flim.flam": "",
                        "wibble.wobble": "eggs",
                        },
                    ),
                ),
            'openid-query-associate': dict(
                request = Stub_Request("GET", "/openidserver",
                    query = {
                        "openid.mode": "associate",
                        "openid.session_type": "",
                        },
                    ),
                ),
            'openid-query-checkid_immediate-no-session': dict(
                request = Stub_Request("GET", "/openidserver",
                    header = [],
                    query = {
                        "openid.mode": "checkid_immediate",
                        "openid.identity": "http://example.org:0/id/fred",
                        "openid.return_to": "http://example.com/",
                        },
                    ),
                ),
            'openid-query-checkid_setup-no-session': dict(
                request = Stub_Request("GET", "/openidserver",
                    header = [],
                    query = {
                        "openid.mode": "checkid_setup",
                        "openid.identity": "http://example.org:0/id/fred",
                        "openid.return_to": "http://example.com/",
                        },
                    ),
                ),
            'openid-query-checkid_immediate-other-session': dict(
                request = Stub_Request("GET", "/openidserver",
                    header = [
                        ("Cookie", "TEST_session=DEADBEEF-bill"),
                        ],
                    query = {
                        "openid.mode": "checkid_immediate",
                        "openid.identity": "http://example.org:0/id/fred",
                        "openid.return_to": "http://example.com/",
                        },
                    ),
                session = dict(
                    session_id = "DEADBEEF-bill",
                    username = "******",
                    ),
                ),
            'openid-query-checkid_setup-other-session': dict(
                request = Stub_Request("GET", "/openidserver",
                    header = [
                        ("Cookie", "TEST_session=DEADBEEF-bill"),
                        ],
                    query = {
                        "openid.mode": "checkid_setup",
                        "openid.identity": "http://example.org:0/id/fred",
                        "openid.return_to": "http://example.com/",
                        },
                    ),
                session = dict(
                    session_id = "DEADBEEF-bill",
                    username = "******",
                    ),
                ),
            'openid-query-checkid_immediate-right-session': dict(
                request = Stub_Request("GET", "/openidserver",
                    header = [
                        ("Cookie", "TEST_session=DEADBEEF-fred"),
                        ],
                    query = {
                        "openid.mode": "checkid_immediate",
                        "openid.identity": "http://example.org:0/id/fred",
                        "openid.return_to": "http://example.com/",
                        },
                    ),
                session = dict(
                    session_id = "DEADBEEF-fred",
                    username = "******",
                    ),
                ),
            'openid-query-checkid_setup-right-session': dict(
                request = Stub_Request("GET", "/openidserver",
                    header = [
                        ("Cookie", "TEST_session=DEADBEEF-fred"),
                        ],
                    query = {
                        "openid.mode": "checkid_setup",
                        "openid.identity": "http://example.org:0/id/fred",
                        "openid.return_to": "http://example.com/",
                        },
                    ),
                session = dict(
                    session_id = "DEADBEEF-fred",
                    username = "******",
                    ),
                ),
            'openid-cancel-login': dict(
                request = Stub_Request("POST", "/login",
                    header = [
                        ("Cookie", "TEST_session=DEADBEEF"),
                        ],
                    query = dict(
                        username = "******",
                        password = "******",
                        cancel = "Cancel",
                        ),
                    ),
                session = dict(
                    session_id = "DEADBEEF",
                    last_openid_request = Stub_OpenIDRequest(
                        http_query = {'openid.mode': "checkid_setup"},
                        params = dict(
                            identity = "http://example.org:0/id/fred",
                            trust_root = "http://example.com/",
                            return_to = "http://example.com/account",
                            ),
                        ),
                    ),
                ),
            'openid-login-bill-other': dict(
                identity_name = "fred",
                request = Stub_Request("POST", "/login",
                    header = [
                        ("Cookie", "TEST_session=DEADBEEF"),
                        ],
                    query = dict(
                        username="******",
                        password="******",
                        submit="Sign in",
                        ),
                    ),
                session = dict(
                    session_id = "DEADBEEF",
                    last_openid_request = Stub_OpenIDRequest(
                        http_query = {'openid.mode': "checkid_setup"},
                        params = dict(
                            identity = "http://example.org:0/id/fred",
                            trust_root = "http://example.com/",
                            return_to = "http://example.com/account",
                            ),
                        ),
                    ),
                ),
            'openid-login-fred-okay': dict(
                identity_name = "fred",
                request = Stub_Request("POST", "/login",
                    header = [
                        ("Cookie", "TEST_session=DEADBEEF"),
                        ],
                    query = dict(
                        username="******",
                        password="******",
                        submit="Sign in",
                        ),
                    ),
                session = dict(
                    session_id = "DEADBEEF",
                    last_openid_request = Stub_OpenIDRequest(
                        http_query = {'openid.mode': "checkid_setup"},
                        params = dict(
                            identity = "http://example.org:0/id/fred",
                            trust_root = "http://example.com/",
                            return_to = "http://example.com/account",
                            ),
                        ),
                    ),
                ),
            }

        logging.basicConfig(stream=self.stdout_test)

        for key, params in self.valid_requests.items():
            args = params.get('args')
            request = params['request']
            opts = Stub_OptionValues(dict(
                host = "example.org",
                port = 0,
                root_url = "http://example.org:0/",
                ))
            gracie_server = Stub_GracieServer(opts)
            sess_manager = gracie_server.sess_manager
            session = params.get('session', dict())
            sess_manager.create_session(session)
            server = params.setdefault(
                'server',
                gracie_server.http_server
                )
            mock_openid_request = self._make_mock_openid_request(
                request.query
                )
            mock_openid_server = self._make_mock_openid_server(
                mock_openid_request
                )
            server.gracie_server.openid_server = mock_openid_server
            if not args:
                args = dict(
                    request = request.connection(),
                    client_address = (opts.host, opts.port),
                    server = server,
                    )
            params['args'] = args

        self.iterate_params = scaffold.make_params_iterator(
            default_params_dict = self.valid_requests
            )

        version = Stub_GracieServer.version
        self.expect_server_version = "Gracie/%(version)s" % vars()
        python_version = sys.version.split()[0]
        self.expect_sys_version = "Python/%(python_version)s" % vars()