def test_get_measures_order_by_start_date(self): dbsession = Session() session = self._create_session(dbsession) module = session.addChild( model.Module( session=session, zodb_path='1', module_id='1', )) module.addChild( model.Risk(session=session, zodb_path='1/2', risk_id='1', identification='no', action_plans=[ model.ActionPlan(action_plan=u'Plan 2', planning_start=datetime.date( 2011, 12, 15)), model.ActionPlan(action_plan=u'Plan 1', planning_start=datetime.date( 2011, 11, 15)) ])) request = testRequest() request.survey = mock.Mock() request.survey.restrictedTraverse = lambda x: object request.survey.ProfileQuestions = lambda: [] setRequest(request) view = self.ActionPlanTimeline(None, request) view.session = session measures = view.get_measures() self.assertEqual(len(measures), 2) self.assertEqual([row[2].action_plan for row in measures], [u'Plan 1', u'Plan 2'])
def beforeTearDown(self): from euphorie.client import model from euphorie.client import utils from z3c.saconfig import Session Session.remove() model.metadata.drop_all(Session.bind) utils.setRequest(None)
def test_get_measures_filter_on_session(self): dbsession = Session() sessions = [] for login in ['jane', 'john']: session = self._create_session(dbsession, loginname=login) module = session.addChild( model.Module( session=session, zodb_path='1', module_id='1', )) module.addChild( model.Risk(session=session, zodb_path='1/2', risk_id='1', identification='no', action_plans=[ model.ActionPlan( action_plan=u'Measure 1 for %s' % login) ])) sessions.append(session) request = testRequest() request.survey = mock.Mock() request.survey.restrictedTraverse = lambda x: object request.survey.ProfileQuestions = lambda: [] setRequest(request) view = self.ActionPlanTimeline(None, request) view.session = sessions[0] measures = view.get_measures() self.assertEqual(len(measures), 1) self.assertEqual(measures[0][2].action_plan, 'Measure 1 for jane')
def testUnpublishWithActiveSession(self): """When a survey gets unpublished, while it's still in an active session, then WebHelpers.survey_url must return None, not fail. """ survey = self.createSurvey() client_survey = getSite().client.nl.ict['software-development'] request = testRequest() request.client = client_survey utils.setRequest(request) account = model.Account(loginname="jane", password=u"john") mgr = session.SessionManagerFactory() mgr.start(u"Test session", client_survey, account) mgr.session.zodb_path = '/'.join(client_survey.getPhysicalPath()) helpers = utils.WebHelpers(survey, request) self.assertEqual( helpers.survey_url(), client_survey.absolute_url()) self.handleSurveyUnpublish(survey, None) helpers = utils.WebHelpers(survey, request) self.assertEqual( helpers.survey_url(), None)
def test_create_workbook_no_problem_description(self): dbsession = Session() session = self._create_session(dbsession) module = model.Module( zodb_path='1', path='001', title=u'Top-level Module title', ) risk = model.Risk(zodb_path='1/2/3', risk_id='1', title=u'Risk title', priority='high', identification='no', path='001002003', comment=u'Risk comment') request = testRequest() request.survey = mock.Mock() request.survey.ProfileQuestions = lambda: [] zodb_node = mock.Mock() zodb_node.title = u'Risk title.' zodb_node.problem_description = u' ' request.survey.restrictedTraverse.return_value = zodb_node setRequest(request) view = self.ActionPlanTimeline(None, request) view.session = session view.getRisks = lambda x: [(module, risk)] sheet = view.create_workbook().worksheets[0] self.assertEqual(sheet.cell('J2').value, u'Risk title')
def test_create_workbook_empty_session(self): # If there are no risks only the header row should be generated. request = testRequest() request.survey = None setRequest(request) view = self.ActionPlanTimeline(None, request) view.getModulePaths = lambda: [] book = view.create_workbook() self.assertEqual(len(book.worksheets), 1) sheet = book.worksheets[0] self.assertEqual(len(sheet.rows), 1)
def publishTraverse(self, request, name): from euphorie.client.utils import setRequest setRequest(request) request.client = self.context if name == 'api': return access_api(request).__of__(self.context) ifaces = [iface for iface in directlyProvidedBy(request) if not IBrowserSkinType.providedBy(iface)] directlyProvides(request, IOSHAClientSkinLayer, ifaces) return super(ClientPublishTraverser, self).publishTraverse(request, name)
def publishTraverse(self, request, name): from euphorie.client.utils import setRequest setRequest(request) request.client = self.context ifaces = [ iface for iface in directlyProvidedBy(request) if not IBrowserSkinType.providedBy(iface) ] directlyProvides(request, IOSHAClientSkinLayer, ifaces) return super(ClientPublishTraverser, self).publishTraverse(request, name)
def _get_view(self, name, obj, survey_session=None, client=None): """Get's a view with a proper fresh request. If survey_session is set the SessionManager will be configured """ old_request = getRequest() request = self.get_client_request(client=client) if survey_session is not None: request.other["euphorie.session"] = survey_session try: setRequest(request) yield api.content.get_view(name, obj, request) finally: setRequest(old_request)
def testCachedSessionId(self): from euphorie.client.tests.utils import testRequest from euphorie.client.utils import setRequest from euphorie.client import session request = testRequest() marker = [] ses = Mock(id=marker) request.other["euphorie.session"] = ses mgr = session.SessionManagerFactory() setRequest(request) try: self.failUnless(mgr.id is marker) finally: setRequest(None)
def setUp(self): super(SurveySessionTests, self).setUp() self.loginAsPortalOwner() addSurvey(self.portal, SURVEY) self.survey = self.portal.client.nl["ict"]["software-development"] class DummyObj(object): problem_description = u'A Tricky Problem' self.survey.restrictedTraverse = lambda path: DummyObj() self.request = self.portal.REQUEST self.request.survey = self.survey self.request.other["euphorie.session"] = createSurveySession() utils.setRequest(self.request) interface.alsoProvides(self.request, interfaces.IOSHAClientSkinLayer)
def setupSession(self, view): from euphorie.client.utils import setRequest from euphorie.client.model import SurveySession from AccessControl.SecurityManagement import getSecurityManager from AccessControl.SecurityManagement import setSecurityManager from AccessControl.SecurityManagement import newSecurityManager setRequest(self.portal.REQUEST) sm = getSecurityManager() # Add stub for copySessionData since it tries to run SQL code which # SQLite does not handle (UPDATE FROM) _copySessionData = SurveySession.copySessionData SurveySession.copySessionData = lambda *a: None try: newSecurityManager(None, self.session.account) return view.setupSession() finally: SurveySession.copySessionData = _copySessionData setSecurityManager(sm) setRequest(None)
def testNewSession(self): import mock from euphorie.client.tests.utils import testRequest from euphorie.client.utils import setRequest from euphorie.client import model from euphorie.client import session request = testRequest() mgr = session.SessionManagerFactory() request.client = survey = object() setRequest(request) try: account = model.Account(loginname="jane", password=u"john") with mock.patch('euphorie.client.session.create_survey_session') \ as mock_create: survey_session = mock.Mock() survey_session.id = 43 mock_create.return_value = survey_session ses = mgr.start(u"Test session", survey, account) self.assertTrue(ses is survey_session) self.failUnless(request.other["euphorie.session"] is ses) finally: setRequest(None)
def test_get_measures_return_risks_without_measures(self): dbsession = Session() session = self._create_session(dbsession) module = session.addChild( model.Module( session=session, zodb_path='1', module_id='1', )) module.addChild( model.Risk(session=session, zodb_path='1/2', risk_id='1', identification='no')) request = testRequest() request.survey = mock.Mock() request.survey.restrictedTraverse = lambda x: object request.survey.ProfileQuestions = lambda: [] setRequest(request) view = self.ActionPlanTimeline(None, request) view.session = session measures = view.get_measures() self.assertEqual(len(measures), 1) self.assertEqual(measures[0][2], None)
def publishTraverse(self, request, name): setRequest(request) request.client = self.context directlyProvides(request, IOSHAClientSkinLayer) return super(HomePagePublishTraverser, self).publishTraverse(request, name)
def setUp(self): from ..utils import setRequest from .utils import testRequest setRequest(testRequest()) super(SessionManagerFactoryTests, self).setUp()
def tearDown(self): from ..utils import setRequest setRequest(None) super(SessionManagerFactoryTests, self).tearDown()
def setUp(self): super(SurveySessionTests, self).setUp() self.loginAsPortalOwner() addSurvey(self.portal, SURVEY) self.survey = self.portal.client.nl["ict"]["software-development"] class DummyObj(object): problem_description = u'A Tricky Problem' self.survey.restrictedTraverse = lambda path: DummyObj() self.request = self.portal.REQUEST self.request.survey = self.survey self.request.other["euphorie.session"] = createSurveySession() utils.setRequest(self.request) interface.alsoProvides(self.request, interfaces.IOSHAClientSkinLayer) self.module = model.Module( zodb_path='1', path='001001', title=u'Shops are clean - Somerset West', ) self.risk1 = model.Risk( zodb_path='504/277/444', risk_id='1', title=u"Le conducteur est-il prot\xe9g\xe9 des autres v\xe9hicules lorsqu'il circule au sol ?", priority='medium', identification='no', path='001001001', ) self.risk2 = model.Risk( zodb_path='504/277/444', risk_id='1', title=u"Le conducteur effectue-t-il toutes ses man\u0153uvres d'accroche/d\xe9croche depuis le sol ?", priority='high', identification='no', path='001001002', ) self.risk3_no = model.Risk( zodb_path='504/277/385', risk_id='1', title=u'Le conducteur descend-il de sa cabine en utilisant les marches ?', priority='low', identification='no', path='001001003', ) self.risk3_yes = model.Risk( zodb_path='504/277/385', risk_id='1', title=u'Le conducteur descend-il de sa cabine en utilisant les marches ?', priority='low', identification='yes', path='001001003', ) self.risk4 = model.Risk( zodb_path='504/277/383', risk_id='1', title=u'Un autre risque ?', priority='medium', identification=None, path='001001004', ) self.risk5 = model.Risk( zodb_path='504/277/444', risk_id='1', title=u'Encore un autre risque ?', priority='low', identification=None, path='001001005', postponed=True, )
def setUp(self): super(SurveySessionTests, self).setUp() self.loginAsPortalOwner() addSurvey(self.portal, SURVEY) self.survey = self.portal.client.nl["ict"]["software-development"] class DummyObj(object): problem_description = "A Tricky Problem" self.survey.restrictedTraverse = lambda path: DummyObj() self.request = self.portal.REQUEST self.request.survey = self.survey self.request.other["euphorie.session"] = createSurveySession() utils.setRequest(self.request) interface.alsoProvides(self.request, interfaces.IOSHAClientSkinLayer) self.module = model.Module( zodb_path="1", path="001001", title="Shops are clean - Somerset West", ) self.risk1 = model.Risk( zodb_path="504/277/444", risk_id="1", title= "Le conducteur est-il prot\xe9g\xe9 des autres v\xe9hicules lorsqu'il circule au sol ?", # noqa: E501 priority="medium", identification="no", path="001001001", ) self.risk2 = model.Risk( zodb_path="504/277/444", risk_id="1", title= "Le conducteur effectue-t-il toutes ses man\u0153uvres d'accroche/d\xe9croche depuis le sol ?", # noqa: E501 priority="high", identification="no", path="001001002", ) self.risk3_no = model.Risk( zodb_path="504/277/385", risk_id="1", title= "Le conducteur descend-il de sa cabine en utilisant les marches ?", priority="low", identification="no", path="001001003", ) self.risk3_yes = model.Risk( zodb_path="504/277/385", risk_id="1", title= "Le conducteur descend-il de sa cabine en utilisant les marches ?", priority="low", identification="yes", path="001001003", ) self.risk4 = model.Risk( zodb_path="504/277/383", risk_id="1", title="Un autre risque ?", priority="medium", identification=None, path="001001004", ) self.risk5 = model.Risk( zodb_path="504/277/444", risk_id="1", title="Encore un autre risque ?", priority="low", identification=None, path="001001005", postponed=True, )