def test_browser_get(self): import datetime from z3c.saconfig import Session from euphorie.client.model import SurveySession from euphorie.content.tests.utils import BASIC_SURVEY from euphorie.client.tests.utils import addAccount from euphorie.client.tests.utils import addSurvey from euphorie.client.api.authentication import generate_token from tno.euphorie.model import DutchCompany from Products.Five.testbrowser import Browser self.loginAsPortalOwner() addSurvey(self.portal, BASIC_SURVEY) account = addAccount(password='******') survey_session = SurveySession( title=u'Dummy session', created=datetime.datetime(2012, 4, 22, 23, 5, 12), modified=datetime.datetime(2012, 4, 23, 11, 50, 30), zodb_path='nl/ict/software-development', account=account) survey_session.dutch_company = DutchCompany( title=u'Acme B.V.', employees='40h', email='*****@*****.**', submit_date=datetime.date(2012, 6, 6)) Session.add(survey_session) browser = Browser() browser.addHeader('X-Euphorie-Token', generate_token(account)) browser.handleErrors = False browser.open( 'http://nohost/plone/client/api/users/1/sessions/1/' 'report-actionplan') self.assertEqual(browser.headers['Content-Type'], 'application/rtf') self.assertTrue('Bedrijfsnaam' in browser.contents)
def test_with_introduction(self): import datetime import json from z3c.saconfig import Session from euphorie.client.model import SurveySession from euphorie.client.api.authentication import generate_token from euphorie.content.tests.utils import BASIC_SURVEY from euphorie.client.tests.utils import addAccount from euphorie.client.tests.utils import addSurvey self.loginAsPortalOwner() addSurvey(self.portal, BASIC_SURVEY) survey = self.portal.client['nl']['ict']['software-development'] survey.introduction = u'<p>Fancy intro.</p>' account = addAccount(password='******') survey_session = SurveySession( title=u'Dummy session', created=datetime.datetime(2012, 4, 22, 23, 5, 12), modified=datetime.datetime(2012, 4, 23, 11, 50, 30), zodb_path='nl/ict/software-development', account=account) Session.add(survey_session) browser = Browser() browser.addHeader('X-Euphorie-Token', generate_token(account)) browser.open('http://nohost/plone/client/api/users/1/sessions/1') self.assertEqual(browser.headers['Content-Type'], 'application/json') response = json.loads(browser.contents) self.assertTrue('introduction' in response) self.assertEqual(response['introduction'], u'<p>Fancy intro.</p>')
def test_browser(self): import datetime from z3c.saconfig import Session from euphorie.client.model import SurveySession from euphorie.client.api.authentication import generate_token from euphorie.content.tests.utils import BASIC_SURVEY from euphorie.client.tests.utils import addAccount from euphorie.client.tests.utils import addSurvey self.loginAsPortalOwner() addSurvey(self.portal, BASIC_SURVEY) account = addAccount(password='******') survey_session = SurveySession( title=u'Dummy session', created=datetime.datetime(2012, 4, 22, 23, 5, 12), modified=datetime.datetime(2012, 4, 23, 11, 50, 30), zodb_path='nl/ict/software-development', account=account) Session.add(survey_session) browser = Browser() browser.addHeader('X-Euphorie-Token', generate_token(account)) browser.handleErrors = False browser.open('http://nohost/plone/client/api/users/1/' 'sessions/1/report-timeline') self.assertEqual( browser.headers['Content-Type'], 'application/vnd.openxmlformats-' 'officedocument.spreadsheetml.sheet')
def test_get_empty_profile(self): import datetime import json from z3c.saconfig import Session from euphorie.client.model import SurveySession from euphorie.content.tests.utils import PROFILE_SURVEY from euphorie.client.tests.utils import addAccount from euphorie.client.tests.utils import addSurvey from euphorie.client.api.authentication import generate_token self.loginAsPortalOwner() addSurvey(self.portal, PROFILE_SURVEY) account = addAccount(password='******') survey_session = SurveySession( title=u'Dummy session', created=datetime.datetime(2012, 4, 22, 23, 5, 12), modified=datetime.datetime(2012, 4, 23, 11, 50, 30), zodb_path='nl/ict/software-development', account=account) Session.add(survey_session) browser = Browser() browser.addHeader('X-Euphorie-Token', generate_token(account)) browser.open( 'http://nohost/plone/client/api/users/1/sessions/1/profile') self.assertEqual(browser.headers['Content-Type'], 'application/json') response = json.loads(browser.contents) self.assertEqual(set(response), set(['id', 'type', 'title', 'profile'])) self.assertEqual(response['id'], 1) self.assertEqual(response['type'], 'profile') self.assertEqual(response['title'], u'Dummy session') self.assertEqual(response['profile'], [{ 'id': u'1', 'question': u'List all your departments:', 'value': [] }])
def _create_session(self, dbsession=None): if dbsession is None: dbsession = Session() session = SurveySession(account=self.account, zodb_path="survey") dbsession.add(session) dbsession.flush() return session
def test_get_no_company_data_present(self): import datetime import json from z3c.saconfig import Session from euphorie.client.model import SurveySession from euphorie.content.tests.utils import BASIC_SURVEY from euphorie.client.tests.utils import addAccount from euphorie.client.tests.utils import addSurvey from euphorie.client.api.authentication import generate_token from Products.Five.testbrowser import Browser self.loginAsPortalOwner() addSurvey(self.portal, BASIC_SURVEY) account = addAccount(password='******') survey_session = SurveySession( title=u'Dummy session', created=datetime.datetime(2012, 4, 22, 23, 5, 12), modified=datetime.datetime(2012, 4, 23, 11, 50, 30), zodb_path='nl/ict/software-development', account=account) Session.add(survey_session) browser = Browser() browser.addHeader('X-Euphorie-Token', generate_token(account)) browser.open( 'http://nohost/plone/client/api/users/1/sessions/1/company') self.assertEqual(browser.headers['Content-Type'], 'application/json') response = json.loads(browser.contents) self.assertEqual(response['type'], 'company')
def test_render_value(self): request = testRequest() request.survey = None view = self.ActionPlanTimeline(None, request) view.session = SurveySession(title=u'Acme') view.render() response = request.response self.assertEqual( response.headers['content-type'], 'application/vnd.openxmlformats-' 'officedocument.spreadsheetml.sheet') self.assertEqual(response.headers['content-disposition'], 'attachment; filename="Timeline for Acme.xlsx"')
def create_context(self): from euphorie.client.model import SurveySession from euphorie.client.model import Company company = Company( country=u'nl', employees='1-9', conductor='both', referer='other', workers_participated=True, needs_met=False, recommend_tool=True, ) return SurveySession(company=company)
def _make_risk(self, login): from sqlalchemy.orm import object_session from euphorie.client.tests.utils import addAccount from euphorie.client.model import Risk from euphorie.client.model import SurveySession account = addAccount(login=login) session = object_session(account) risk = Risk(risk_id='15', path='01', zodb_path='/foo/bar/1', session=SurveySession(account=account, zodb_path='/foo/bar')) session.add(risk) return risk
def test_put_too_much_data(self): import mock from euphorie.client.tests.utils import addAccount from euphorie.client.model import SurveySession from euphorie.content.survey import Survey account = addAccount(password='******') survey_session = SurveySession(title=u'Dummy session', zodb_path='nl/ict/software-development', account=account) view = self.View(survey_session, None) view.input = {'5': True} survey = Survey(id='survey') view.survey = mock.Mock(return_value=survey) response = view.do_PUT() self.assertEqual(response['type'], 'error')
def test_getitem_removed_session(self): # Corner case: admin removed survey, but survey session still exists import mock from sqlalchemy.orm import object_session from euphorie.client.tests.utils import addAccount from euphorie.client.model import SurveySession account = addAccount() survey_session = SurveySession(title=u'Dummy', zodb_path='does/not/exist', account=account) object_session(account).add(survey_session) sessions = self.Sessions('sessions', None, account) with mock.patch('euphorie.client.api.sessions.get_survey') \ as mock_get: mock_get.return_value = None self.assertRaises(KeyError, sessions.__getitem__, '1')
def test_put_not_all_questions_answered(self): import mock from euphorie.content.survey import Survey from euphorie.content.profilequestion import ProfileQuestion from euphorie.client.tests.utils import addAccount from euphorie.client.model import SurveySession account = addAccount(password='******') survey_session = SurveySession(title=u'Dummy session', zodb_path='nl/ict/software-development', account=account) view = self.View(survey_session, None) view.input = {} self.portal.survey = Survey(id='survey') survey = self.portal.survey survey['5'] = ProfileQuestion(id='5', question=u'Locations') view.survey = mock.Mock(return_value=survey) response = view.do_PUT() self.assertEqual(response['type'], 'error')
def test_put_no_profile(self): import mock from z3c.saconfig import Session from euphorie.client.tests.utils import addAccount from euphorie.client.model import SurveySession from euphorie.content.survey import Survey account = addAccount(password='******') survey_session = SurveySession(title=u'Dummy session', zodb_path='nl/ict/software-development', account=account) view = self.View(survey_session, None) view.input = {} Session.add(survey_session) survey = Survey(id='survey') view.survey = mock.Mock(return_value=survey) with mock.patch('euphorie.client.api.profile.extractProfile') \ as mock_extractProfile: mock_extractProfile.return_value = {} response = view.do_PUT() self.assertEqual(response['profile'], [])
def test_get(self): import datetime import json from z3c.saconfig import Session from euphorie.client.model import SurveySession from euphorie.client.api.authentication import generate_token from euphorie.content.tests.utils import BASIC_SURVEY from euphorie.client.tests.utils import addAccount from euphorie.client.tests.utils import addSurvey self.loginAsPortalOwner() addSurvey(self.portal, BASIC_SURVEY) account = addAccount(password='******') survey_session = SurveySession( title=u'Dummy session', created=datetime.datetime(2012, 4, 22, 23, 5, 12), modified=datetime.datetime(2012, 4, 23, 11, 50, 30), zodb_path='nl/ict/software-development', account=account) Session.add(survey_session) browser = Browser() browser.addHeader('X-Euphorie-Token', generate_token(account)) browser.open('http://nohost/plone/client/api/users/1/sessions/1') self.assertEqual(browser.headers['Content-Type'], 'application/json') response = json.loads(browser.contents) self.assertEqual( set(response), set([ 'id', 'survey', 'type', 'created', 'modified', 'title', 'next-step' ])) self.assertEqual(response['id'], 1) self.assertEqual(response['survey'], 'nl/ict/software-development') self.assertEqual(response['type'], 'session') self.assertEqual(response['title'], 'Dummy session') self.assertEqual(response['created'], '2012-04-22T23:05:12') self.assertEqual(response['modified'], '2012-04-23T11:50:30') self.assertEqual( response['next-step'], 'http://nohost/plone/client/api/users/1/sessions/1/' 'identification')
def test_getitem_valid_session(self): import mock from sqlalchemy.orm import object_session from Acquisition import aq_base from Acquisition import aq_parent from euphorie.client.tests.utils import addAccount from euphorie.client.model import SurveySession account = addAccount() survey_session = SurveySession(title=u'Dummy', zodb_path='survey/path', account=account) object_session(account).add(survey_session) request = mock.Mock() request.language = None sessions = self.Sessions('sessions', request, account) with mock.patch('euphorie.client.api.sessions.get_survey') \ as mock_get: mock_survey = mock_get(request, 'survey/path') mock_survey.language = None result = sessions['1'] self.assertTrue(aq_base(result) is survey_session) self.assertTrue(aq_parent(result) is sessions) self.assertTrue(request.survey is mock_survey)
def _create_session(self, dbsession, loginname='jane'): session = SurveySession(account=Account(loginname=loginname, password=u'john'), zodb_path='survey') dbsession.add(session) return session