<commit_before>"""Tests for openid.server.
"""
from openid.server import server
from openid import association, cryptutil, oidutil
import _memstore
import cgi

import unittest

from urlparse import urlparse

# In general, if you edit or add tests here, try to move in the direction
# of testing smaller units.  For testing the external interfaces, we'll be
# developing an implementation-agnostic testing suite.

class CatchLogs(object):
    def setUp(self):
        self.old_logger = oidutil.log
        oidutil.log = self.gotLogMessage
        self.messages = []

    def gotLogMessage(self, message):
        self.messages.append(message)

    def tearDown(self):
        oidutil.log = self.old_logger

# class ConstReturningApp(server.AppIface):
#     def __init__(self, truth, http_method, args,
#                  additional_args=None, signed=None):
#         self.truth = truth
#         self.args = args
#         self.http_method = http_method
#         if additional_args:
#             self.additional_args = additional_args
#         else:
#             self.additional_args = {}
#         self.signed = signed

#     def isAuthorized(self, unused, unused_):
#         return self.truth

#     def additionalFields(self):
#         return self.additional_args

#     def signedFields(self):
#         if self.signed is None:
#             return server.AppIface.signedFields(self)
#         else:
#             return self.signed

# class TestServerErrors(ServerTestCase):
#     oidServerClass = server.OpenIDServer
#     def setUp(self):
#         self.sv_url = 'http://id.server.url/'
#         self.id_url = 'http://foo.com/'
#         self.rt_url = 'http://return.to/rt'
#         self.tr_url = 'http://return.to/'

#         self.store = _memstore.MemoryStore()
#         self.server = self.oidServerClass(self.sv_url, self.store)

#     def test_getWithReturnTo(self):
#         args = {
#             'openid.mode': 'monkeydance',
#             'openid.identity': self.id_url,
#             'openid.return_to': self.rt_url,
#             }

#         status, info = self.server.getOpenIDResponse(
#             ConstReturningApp(False, 'GET', args))
#         self.failUnlessEqual(status, server.REDIRECT)
#         rt_base, resultArgs = info.split('?', 1)
#         resultArgs = cgi.parse_qs(resultArgs)
#         ra = resultArgs
#         self.failUnlessEqual(rt_base, self.rt_url)
#         self.failUnlessEqual(ra['openid.mode'], ['error'])
#         self.failUnless(ra['openid.error'])

#     def test_getBadArgs(self):
#         args = {
#             'openid.mode': 'zebradance',
#             'openid.identity': self.id_url,
#             }

#         status, info = self.server.getOpenIDResponse(
#             ConstReturningApp(False, 'GET', args))
#         self.failUnlessEqual(status, server.LOCAL_ERROR)
#         self.failUnless(info)

#     def test_getNoArgs(self):
#         status, info = self.server.getOpenIDResponse(
#             ConstReturningApp(False, 'GET', {}))
#         self.failUnlessEqual(status, server.DO_ABOUT)

#     def test_post(self):
#         args = {
#             'openid.mode': 'pandadance',
#             'openid.identity': self.id_url,
#             }

#         status, info = self.server.getOpenIDResponse(
#             ConstReturningApp(False, 'POST', args))
#         self.failUnlessEqual(status, server.REMOTE_ERROR)
#         resultArgs = kvform.kvToDict(info)
#         self.failUnless(resultArgs['error'])


class TestProtocolError(unittest.TestCase):
    def test_browserWithReturnTo(self):
        return_to = "http://rp.unittest/consumer"
        # will be a ProtocolError raised by Decode or CheckIDRequest.answer
        args = {
            'openid.mode': 'monkeydance',
            'openid.identity': 'http://wagu.unittest/',
            'openid.return_to': return_to,
            }
        e = server.ProtocolError(args, "plucky")
        self.failUnless(e.hasReturnTo())
        expected_args = {
            'openid.mode': ['error'],
            'error': ['plucky'],
            }

        rt_base, result_args = e.encodeToURL().split('?', 1)
        result_args = cgi.parse_qs(result_args)
        self.failUnlessEqual(result_args, expected_args)

    def test_noReturnTo(self):
        # will be a ProtocolError raised by Decode or CheckIDRequest.answer
        args = {
            'openid.mode': 'zebradance',
            'openid.identity': 'http://wagu.unittest/',
            }
        e = server.ProtocolError(args, "waffles")
        self.failIf(e.hasReturnTo())<commit_msg>[project @ test/server: remove old commented tests, as I believe they've been ported.]<commit_after>"""Tests for openid.server.
"""
from openid.server import server
from openid import association, cryptutil, oidutil
import _memstore
import cgi

import unittest

from urlparse import urlparse

# In general, if you edit or add tests here, try to move in the direction
# of testing smaller units.  For testing the external interfaces, we'll be
# developing an implementation-agnostic testing suite.

class CatchLogs(object):
    def setUp(self):
        self.old_logger = oidutil.log
        oidutil.log = self.gotLogMessage
        self.messages = []

    def gotLogMessage(self, message):
        self.messages.append(message)

    def tearDown(self):
        oidutil.log = self.old_logger

class TestProtocolError(unittest.TestCase):
    def test_browserWithReturnTo(self):
        return_to = "http://rp.unittest/consumer"
        # will be a ProtocolError raised by Decode or CheckIDRequest.answer
        args = {
            'openid.mode': 'monkeydance',
            'openid.identity': 'http://wagu.unittest/',
            'openid.return_to': return_to,
            }
        e = server.ProtocolError(args, "plucky")
        self.failUnless(e.hasReturnTo())
        expected_args = {
            'openid.mode': ['error'],
            'error': ['plucky'],
            }

        rt_base, result_args = e.encodeToURL().split('?', 1)
        result_args = cgi.parse_qs(result_args)
        self.failUnlessEqual(result_args, expected_args)

    def test_noReturnTo(self):
        # will be a ProtocolError raised by Decode or CheckIDRequest.answer
        args = {
            'openid.mode': 'zebradance',
            'openid.identity': 'http://wagu.unittest/',
            }
        e = server.ProtocolError(args, "waffles")
        self.failIf(e.hasReturnTo())
{'fraction_non_alphanumeric': 0.11759216052263183, 'fraction_numerical': 0.0004666355576294914, 'mean_word_length': 2.3985200845665964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4352035', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1925, 'n_words': 406}