Tres Seaver
2010-10-01 924f24fd8a190e4792ce3fcd1aad3c2695d7ff25
Add a logout method to the API for application convenience.
4 files modified
75 ■■■■■ changed files
CHANGES.txt 4 ●●●● patch | view | raw | blame | history
repoze/who/api.py 10 ●●●●● patch | view | raw | blame | history
repoze/who/interfaces.py 11 ●●●●● patch | view | raw | blame | history
repoze/who/tests/test_api.py 50 ●●●●● patch | view | raw | blame | history
CHANGES.txt
@@ -4,8 +4,8 @@
After 2.0a3 (unreleased)
------------------------
- Added a ``login`` method to the ``repoze.who.api.API`` object, as a
  convenience for application-driven login code, which would
- Add ``login`` and ``logout`` methods to the ``repoze.who.api.API`` object,
  as a convenience for application-driven login / logout code, which would
  otherwise need to use private methods of the API, and reach down into
  its plugins.
repoze/who/api.py
@@ -249,6 +249,16 @@
            headers = identifier.forget(self.environ, None)
            return None, headers
    def logout(self, identifier_name=None):
        """ See IAPI.
        """
        if identifier_name is not None:
            identifier = self.name_registry[identifier_name]
        else:
            identifier = self.identifiers[0][1]
        # Pretend that the given identifier extracted the identity.
        return identifier.forget(self.environ, None)
    def _identify(self):
        """ See IAPI.
        """
repoze/who/interfaces.py
@@ -65,6 +65,17 @@
          "forget" headers.
        """
    def logout(identifier_name=None):
        """ -> (headers)
        o This is an API for browser-based application logout.
        o If 'identifier_name' is passed, use it to look up the identifier;
          othewise, use the first configured identifier.
        o Returned headers will be "forget" headers.
        """
class IPlugin(Interface):
    pass
repoze/who/tests/test_api.py
@@ -685,6 +685,56 @@
        self.assertEqual(headers, FORGET_HEADERS)
    def test_login_wo_identifier_name_miss(self):
        FORGET_HEADERS = [('Spam', 'Blah')]
        class _Identifier:
            def identify(self, environ):
                pass
            def remember(self, environ, identity):
                pass
            def forget(self, environ, identity):
                return FORGET_HEADERS
        class _BogusIdentifier:
            def identify(self, environ):
                pass
            def remember(self, environ, identity):
                pass
            def forget(self, environ, identity):
                pass
        environ = self._makeEnviron()
        identifiers = [('valid', _Identifier()),
                       ('bogus', _BogusIdentifier()),
                      ]
        api = self._makeOne(identifiers=identifiers,
                            environ=environ)
        headers = api.logout()
        self.assertEqual(headers, FORGET_HEADERS)
    def test_logout_w_identifier_name(self):
        FORGET_HEADERS = [('Spam', 'Blah')]
        class _Identifier:
            def identify(self, environ):
                pass
            def remember(self, environ, identity):
                pass
            def forget(self, environ, identity):
                return FORGET_HEADERS
        class _BogusIdentifier:
            def identify(self, environ):
                pass
            def remember(self, environ, identity):
                pass
            def forget(self, environ, identity):
                pass
        environ = self._makeEnviron()
        identifiers = [('bogus', _BogusIdentifier()),
                       ('valid', _Identifier()),
                      ]
        api = self._makeOne(identifiers=identifiers,
                            environ=environ)
        headers = api.logout('valid')
        self.assertEqual(headers, FORGET_HEADERS)
    def test_logout_wo_identifier_name(self):
        REMEMBER_HEADERS = [('Foo', 'Bar'), ('Baz', 'Qux')]
        FORGET_HEADERS = [('Spam', 'Blah')]
        class _Identifier: