<commit_before>"""
Tests for session-based authentication.
"""
from axiom import store
from exponent.auth import errors, session, common
from twisted.trial import unittest
from txampext import commandtests


class RequestSesssionTests(unittest.TestCase, commandtests.CommandTestMixin):
    """
    Tests for the AMP command to request a session identifier.
    """
    command = session.RequestSession
    argumentObjects = argumentStrings = {}
    responseObjects = responseStrings = {"sessionIdentifier": "sid"}



class LoginSessionTests(unittest.TestCase, commandtests.CommandTestMixin):
    """
    Tests for the session-based login AMP command.
    """
    command = session.LoginWithSession
    argumentObjects = argumentStrings = {
        "userIdentifier": "uid",
        "sessionIdentifier": "sid"
    }
    responseObjects = responseStrings = {
        "sessionIdentifier": "newsid"
    }



class LoginTests(unittest.TestCase):
    """
    Tests for authenticating using sessions.
    """
    def setUp(self):
        self.store = store.Store()
        self.user = common.User(store=store.Store(), uid="uid")
        session._Session(store=self.user.store, identifier="sid")
        self._locator = session.Locator(self.store)


    def _login(self, userIdentifier=None, sessionIdentifier=None):
        """
        Attempts to authenticate as the user using a session.

        :param userIdentifier: The user identifier to use. If unspecified or
            or ``None``, uses the test user identifier.
        :param sessionIdentifier: The session identifier to use. If
            unspecified, uses the first valid session identifier for the
            current user.
        """
        if userIdentifier is None:
            userIdentifier = self.user.identifier
        if sessionIdentifier is None:
            sessionIdentifier = self._getCurrentIdentifier()

        return self._locator.login(userIdentifier, sessionIdentifier)


    def _getCurrentIdentifier(self):
        """
        Returns the current identifier, as a string.
        """
        return self.user.store.findUnique(session._Session).identifier


    def test_cantLoginWithInvalidSessionIdentifier(self):
        """
        Tests that users can not log in with an invalid session identifier.
        """
        d = self._login(self.user.uid, "BOGUS")
        self.assertFailure(d, errors.BadCredentials)
        return d


    def test_cantLoginWithInvalidUserIdentifier(self):
        """
        Tests that users can not log in with an invalid user identifier.
        """
        d = self._login("BOGUS", self._getCurrentIdentifier())
        self.assertFailure(d, errors.BadCredentials)
        return d


    def test_cantLoginTwiceWithSameSessionIdentfiier(self):
        """
        Tests that session identifiers are invalidated after single use.
        """
        oldIdentifier = self._getCurrentIdentifier()
        d = self._login(self.user.uid, oldIdentifier)<commit_msg>Fix test<commit_after>"""
Tests for session-based authentication.
"""
from axiom import store
from exponent.auth import errors, session, common
from twisted.trial import unittest
from txampext import commandtests


class RequestSesssionTests(unittest.TestCase, commandtests.CommandTestMixin):
    """
    Tests for the AMP command to request a session identifier.
    """
    command = session.RequestSession
    argumentObjects = argumentStrings = {}
    responseObjects = responseStrings = {"sessionIdentifier": "sid"}



class LoginSessionTests(unittest.TestCase, commandtests.CommandTestMixin):
    """
    Tests for the session-based login AMP command.
    """
    command = session.LoginWithSession
    argumentObjects = argumentStrings = {
        "userIdentifier": "uid",
        "sessionIdentifier": "sid"
    }
    responseObjects = responseStrings = {
        "sessionIdentifier": "newsid"
    }



class LoginTests(unittest.TestCase):
    """
    Tests for authenticating using sessions.
    """
    def setUp(self):
        self.store = store.Store()
        self.user = common.User(store=store.Store(), uid="uid")
        session._Session(store=self.user.store, identifier="sid")
        self._locator = session.Locator(self.store)


    def _login(self, userIdentifier=None, sessionIdentifier=None):
        """
        Attempts to authenticate as the user using a session.

        :param userIdentifier: The user identifier to use. If unspecified or
            or ``None``, uses the test user identifier.
        :param sessionIdentifier: The session identifier to use. If
            unspecified, uses the first valid session identifier for the
            current user.
        """
        if userIdentifier is None:
            userIdentifier = self.user.identifier
        if sessionIdentifier is None:
            sessionIdentifier = self._getCurrentIdentifier()

        return self._locator.login(userIdentifier, sessionIdentifier)


    def _getCurrentIdentifier(self):
        """
        Returns the current identifier, as a string.
        """
        return self.user.store.findUnique(session._Session).identifier


    def test_cantLoginWithInvalidSessionIdentifier(self):
        """
        Tests that users can not log in with an invalid session identifier.
        """
        d = self._login(self.user.uid, "BOGUS")
        self.assertFailure(d, errors.BadCredentials)
        return d


    def test_cantLoginWithInvalidUserIdentifier(self):
        """
        Tests that users can not log in with an invalid user identifier.
        """
        d = self._login("BOGUS", self._getCurrentIdentifier())
        self.assertFailure(d, errors.BadCredentials)
        return d


    def test_cantLoginTwiceWithSameSessionIdentfiier(self):
        """
        Tests that session identifiers are invalidated after single use.
        """
        oldIdentifier = self._getCurrentIdentifier()
        d = self._login(self.user.uid, oldIdentifier)
        self.assertFailure(d, errors.BadCredentials)
{'fraction_non_alphanumeric': 0.08774558774558774, 'fraction_numerical': 0.0, 'mean_word_length': 2.8407928388746804, 'pattern_counts': {'":': 8, '<': 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': '4208888', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1481, 'n_words': 436}