• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Zope / 6263629025

21 Sep 2023 03:12PM UTC coverage: 82.146% (-0.01%) from 82.159%
6263629025

Pull #1164

github

web-flow
[pre-commit.ci lite] apply automatic fixes
Pull Request #1164: Move all linters to pre-commit.

4353 of 6963 branches covered (0.0%)

Branch coverage included in aggregate %.

487 of 487 new or added lines in 186 files covered. (100.0%)

27394 of 31684 relevant lines covered (86.46%)

0.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.59
/src/Testing/ZopeTestCase/PortalTestCase.py
1
##############################################################################
2
#
3
# Copyright (c) 2005 Zope Foundation and Contributors.
4
#
5
# This software is subject to the provisions of the Zope Public License,
6
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
7
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
# FOR A PARTICULAR PURPOSE.
11
#
12
##############################################################################
13
"""Abstract base test case for working with CMF-style portals.
1✔
14

15
This class maintains a fixture consisting of:
16

17
  - a portal object (self.portal)
18
  - a user folder inside the portal
19
  - a default user with role 'Member' inside the user folder
20
  - the default user's memberarea (self.folder)
21
  - the default user is logged in
22

23
The twist is that the portal object itself is *not* created
24
by the PortalTestCase class! Subclasses must make sure
25
getPortal() returns a usable portal object to the setup code.
26
"""
27

28
from AccessControl import getSecurityManager
1✔
29
from AccessControl.SecurityManagement import newSecurityManager
1✔
30
from AccessControl.SecurityManagement import noSecurityManager
1✔
31
from Acquisition import aq_base
1✔
32
from zope.interface import implementer
1✔
33

34
from . import base
1✔
35
from . import interfaces
1✔
36
from . import utils
1✔
37
from .ZopeTestCase import user_name
1✔
38
from .ZopeTestCase import user_password
1✔
39

40

41
portal_name = 'portal'
1✔
42

43

44
@implementer(interfaces.IPortalTestCase,
1✔
45
             interfaces.IPortalSecurity)
46
class PortalTestCase(base.TestCase):
1✔
47
    """Base test case for testing CMF-style portals."""
48

49
    _configure_portal = 1
1✔
50

51
    def setUp(self):
1✔
52
        """Sets up the fixture.
53

54
        Do not override, use the hooks instead.
55
        """
56
        try:
1✔
57
            self.beforeSetUp()
1✔
58
            self.app = self._app()
1✔
59
            self.portal = self._portal()
1✔
60
            self._setup()
1✔
61
            self.afterSetUp()
1✔
62
        except Exception:
1✔
63
            self._clear()
1✔
64
            raise
1✔
65

66
    def _portal(self):
1✔
67
        """Returns the portal object for a test."""
68
        return self.getPortal()
1✔
69

70
    def _setup(self):
1✔
71
        """Configures the portal.
72

73
        Framework authors may override.
74
        """
75
        if self._configure_portal:
1✔
76
            self._setupUserFolder()
1✔
77
            self._setupUser()
1✔
78
            self.login()
1✔
79
            self._setupHomeFolder()
1✔
80

81
    def _setupUserFolder(self):
1✔
82
        """Creates the user folder if missing."""
83
        if not hasattr(aq_base(self.portal), 'acl_users'):
1✔
84
            self.portal.manage_addUserFolder()
1✔
85

86
    def _setupUser(self):
1✔
87
        """Creates the default user."""
88
        uf = self.portal.acl_users
1✔
89
        uf.userFolderAddUser(user_name, user_password, ['Member'], [])
1✔
90

91
    def _setupHomeFolder(self):
1✔
92
        """Creates the default user's home folder."""
93
        self.createMemberarea(user_name)
1✔
94
        pm = self.portal.portal_membership
1✔
95
        self.folder = pm.getHomeFolder(user_name)
1✔
96

97
    def _refreshSkinData(self):
1✔
98
        """Refreshes the skin cache."""
99
        if hasattr(aq_base(self.portal), 'clearCurrentSkin'):
1!
100
            self.portal.clearCurrentSkin()
1✔
101
        else:  # CMF 1.4
102
            self.portal._v_skindata = None
×
103
        try:
1✔
104
            self.portal.setupCurrentSkin(self.app.REQUEST)
1✔
105
        except TypeError:
1✔
106
            self.portal.setupCurrentSkin()
1✔
107

108
    # Portal interface
109

110
    def getPortal(self):
1✔
111
        """Returns the portal object to the setup code. Will typically be
112
        overridden by subclasses to return the object serving as the "portal".
113

114
        Note: This method should not be called by tests!
115
        """
116
        return getattr(self.app, portal_name)
×
117

118
    def createMemberarea(self, name):
1✔
119
        """Creates a memberarea for the specified user.
120

121
        Subclasses may override to provide a customized or more
122
        lightweight version of the memberarea.
123
        """
124
        pm = self.portal.portal_membership
1✔
125
        if hasattr(aq_base(pm), 'createMemberArea'):
1✔
126
            pm.createMemberArea(name)
1✔
127
        else:  # CMF 1.4
128
            pm.createMemberarea(name)
1✔
129

130
    # Security interface
131

132
    def setRoles(self, roles, name=user_name):
1✔
133
        """Changes the user's roles."""
134
        uf = self.portal.acl_users
1✔
135
        uf.userFolderEditUser(name, None, utils.makelist(roles), [])
1✔
136
        if name == getSecurityManager().getUser().getId():
1✔
137
            self.login(name)
1✔
138

139
    def setPermissions(self, permissions, role='Member'):
1✔
140
        """Changes the permissions assigned to role."""
141
        self.portal.manage_role(role, utils.makelist(permissions))
1✔
142

143
    def login(self, name=user_name):
1✔
144
        """Logs in."""
145
        uf = self.portal.acl_users
1✔
146
        user = uf.getUserById(name)
1✔
147
        if not hasattr(user, 'aq_base'):
1✔
148
            user = user.__of__(uf)
1✔
149
        newSecurityManager(None, user)
1✔
150

151
    def logout(self):
1✔
152
        """Logs out."""
153
        noSecurityManager()
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc