def test_openid_initiate__with_KeyError(self, make_consumer):
        from nuorisovaalit.views.login import openid_initiate

        openid_url = u'http://example.com/id/john.doe'
        renderer = self.config.testing_add_template('templates/login_failed.pt')
        consumer = mock.Mock()
        consumer.begin.side_effect = KeyError()
        make_consumer.return_value = consumer

        openid_initiate(openid_url, DummyRequest())

        renderer.assert_(message=u'')
    def test_openid_initiate__with_DiscoveryFailure(self, make_consumer):
        from nuorisovaalit.views.login import openid_initiate
        from openid.yadis.discover import DiscoveryFailure

        openid_url = u'http://example.com/id/john.doe'
        renderer = self.config.testing_add_template('templates/login_failed.pt')
        consumer = mock.Mock()
        consumer.begin.side_effect = DiscoveryFailure(None, None)
        make_consumer.return_value = consumer

        openid_initiate(openid_url, DummyRequest())

        renderer.assert_(message=u'Yhteydenotto OpenID-palvelimelle epäonnistui tilapäisesti.')
    def test_openid_initiate__successful(self, make_consumer):
        from nuorisovaalit.views.login import openid_initiate
        from webob.exc import HTTPFound

        self.config.add_route('openid-response', '/openid-response')

        openid_url = u'http://example.com/id/john.doe'
        redirect_url = u'http://example.com/some-url'
        consumer = mock.Mock()
        auth_request = mock.Mock()
        auth_request.redirectURL.return_value = redirect_url
        consumer.begin.return_value = auth_request
        make_consumer.return_value = consumer

        response = openid_initiate(openid_url, DummyRequest())

        self.assertTrue(isinstance(response, HTTPFound))
        self.assertEquals(redirect_url, response.location)