1#############################################################################
2#
3# Copyright (c) 2011 Zope Foundation and Contributors.
4# All Rights Reserved.
5#
6# This software is subject to the provisions of the Zope Public License,
7# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11# FOR A PARTICULAR PURPOSE.
12#
13##############################################################################
14
15from __future__ import with_statement
16import unittest
17import zope.publisher.testing
18import zope.security.management
19
20
21class InteractionHelperTest(unittest.TestCase):
22
23    def tearDown(self):
24        zope.security.management.endInteraction()
25
26    def test_create_interaction_should_return_principal(self):
27        principal = zope.publisher.testing.create_interaction(
28            'foo', groups=['bar'], description='desc')
29        interaction = zope.security.management.getInteraction()
30        request = interaction.participations[0]
31        self.assertEqual('foo', request.principal.id)
32        self.assertEqual(principal.groups, request.principal.groups)
33        self.assertEqual('desc', request.principal.description)
34
35    def test_usable_as_contextmanager(self):
36        with zope.publisher.testing.interaction('foo'):
37            interaction = zope.security.management.getInteraction()
38            request = interaction.participations[0]
39            self.assertEqual('foo', request.principal.id)
40        self.assertFalse(zope.security.management.queryInteraction())
41
42    def test_contextmanager_ends_interaction_on_exception(self):
43        try:
44            with zope.publisher.testing.interaction('foo'):
45                raise RuntimeError()
46        except RuntimeError:
47            pass
48        self.assertFalse(zope.security.management.queryInteraction())
49