This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
/
base.py
108 lines (80 loc) · 3.18 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: utf-8 -*-
"""
Base module for unittesting
---------------------------
"""
import unittest2 as unittest
from plone.app.controlpanel.tests import ControlPanelTestCase
from plone.app.testing import applyProfile
from plone.app.testing import FunctionalTesting
from plone.app.testing import IntegrationTesting
from plone.app.testing import login
from plone.app.testing import PLONE_FIXTURE
from plone.app.testing import PloneSandboxLayer
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.app.testing import TEST_USER_NAME
from plone.testing import z2
from Products.CMFPlone.tests.utils import MockMailHost
from Products.MailHost.interfaces import IMailHost
from zope.site.hooks import getSite
import logging
class MockedLoggingHandler(logging.Handler):
debug = []
warning = []
info = []
error = []
def emit(self, record):
getattr(self.__class__, record.levelname.lower()).append(record.getMessage())
@classmethod
def reset(cls):
for attr in dir(cls):
if isinstance(getattr(cls, attr), list):
setattr(cls, attr, [])
class NiteowebClick2sellLayer(PloneSandboxLayer):
defaultBases = (PLONE_FIXTURE,)
def setUpZope(self, app, configurationContext):
"""Set up Zope."""
# Load ZCML
import niteoweb.click2sell
self.loadZCML(package=niteoweb.click2sell)
z2.installProduct(app, 'niteoweb.click2sell')
def setUpPloneSite(self, portal):
"""Set up Plone."""
# Install into Plone site using portal_setup
applyProfile(portal, 'niteoweb.click2sell:default')
# Login as Manager
setRoles(portal, TEST_USER_ID, ['Manager'])
login(portal, TEST_USER_NAME)
# Mock mailhost
portal.email_from_address = 'info@example.org'
mockmailhost = MockMailHost('MailHost')
portal.MailHost = mockmailhost
sm = getSite().getSiteManager()
sm.registerUtility(component=mockmailhost, provided=IMailHost)
# Commit so that the test browser sees these objects
portal.portal_catalog.clearFindAndRebuild()
import transaction
transaction.commit()
# add a logging handler that stores everything in a list so we can
# later assert on it
logger = logging.getLogger('niteoweb.click2sell')
logger.addHandler(MockedLoggingHandler())
def tearDownZope(self, app):
"""Tear down Zope."""
z2.uninstallProduct(app, 'niteoweb.click2sell')
FIXTURE = NiteowebClick2sellLayer()
INTEGRATION_TESTING = IntegrationTesting(
bases=(FIXTURE,), name="NiteowebClick2sellLayer:Integration")
FUNCTIONAL_TESTING = FunctionalTesting(
bases=(FIXTURE,), name="NiteowebClick2sellLayer:Functional")
class IntegrationTestCase(unittest.TestCase):
"""Base class for integration tests."""
layer = INTEGRATION_TESTING
class FunctionalTestCase(unittest.TestCase):
"""Base class for functional tests."""
layer = FUNCTIONAL_TESTING
class Click2SellControlPanelTestCase(FunctionalTestCase, ControlPanelTestCase):
"""Test case used for control panel tests, with some convenience
methods from plone.app.controlpanel.
"""