def assertUrlsEqual(self, url1, url2): """The order of query-strings is sometimes random in python 3 This compares urls disregarding the order. """ parsed_url_1 = urlparse(safe_nativestring(url1)) parse_qs_1 = parse_qs(parsed_url_1.query) parsed_url_2 = urlparse(safe_nativestring(url2)) parse_qs_2 = parse_qs(parsed_url_2.query) self.assertEqual(parsed_url_1[0], parsed_url_2[0]) self.assertEqual(parsed_url_1[1], parsed_url_2[1]) self.assertEqual(parsed_url_1[2], parsed_url_2[2]) self.assertEqual(parsed_url_1[3], parsed_url_2[3]) self.assertEqual(parse_qs_1, parse_qs_2)
def testRisk_TwoImages(self): risk = Risk() risk.type = "top5" risk.title = "Can your windows be locked?" risk.problem_description = "Not all your windows can be locked" risk.description = "<p>Locking windows is critical.</p>" risk.legal_reference = None risk.show_notapplicable = False risk.image = MockImage("hot stuff here") risk.caption = "Image caption 1" risk.image2 = MockImage("hot stuff here") risk.caption2 = "Image caption 2" root = self.root() view = ExportSurvey(None, None) view.include_images = True view.exportRisk(root, risk) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <risk type="top5">\n' " <title>Can your windows be locked?</title>\n" " <problem-description>Not all your windows can be locked" "</problem-description>\n" " <description><p>Locking windows is critical." "</p></description>\n" " <show-not-applicable>false</show-not-applicable>\n" ' <image caption="Image caption 1">aG90IHN0dWZmIGhlcmU=\n' "</image>\n" ' <image caption="Image caption 2">aG90IHN0dWZmIGhlcmU=\n' "</image>\n" " </risk>\n" "</root>\n", )
def testRisk_CalculatedEvaluation(self): risk = Risk() risk.type = "risk" risk.title = "Can your windows be locked?" risk.problem_description = "Not all your windows can be locked" risk.description = "<p>Locking windows is critical.</p>" risk.legal_reference = None risk.show_notapplicable = True risk.evaluation_method = "calculated" risk.default_probability = 1 risk.default_frequency = 4 risk.default_effect = 0 root = self.root() view = ExportSurvey(None, None) view.exportRisk(root, risk) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <risk type="risk">\n' " <title>Can your windows be locked?</title>\n" " <problem-description>Not all your windows can be " "locked</problem-description>\n" " <description><p>Locking windows is critical." "</p></description>\n" " <show-not-applicable>true</show-not-applicable>\n" ' <evaluation-method default-probability="small" ' 'default-frequency="regular">calculated</evaluation-method>\n' " </risk>\n" "</root>\n", )
def testProfileQuestion_LocationQuestions(self): profile = ProfileQuestion() profile.title = "Office buildings" profile.question = "Do you have an office building?" profile.label_multiple_present = "Do you have more than one building?" profile.label_single_occurance = "Enter the name of your building." profile.label_multiple_occurances = ( "Enter the names of each of your buildings." # noqa ) root = self.root() view = ExportSurvey(None, None) node = view.exportProfileQuestion(root, profile) self.assertTrue(node in root) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <profile-question>\n" " <title>Office buildings</title>\n" " <question>Do you have an office building?</question>\n" " <label-multiple-present>Do you have more than one building" "?</label-multiple-present>\n" " <label-single-occurance>Enter the name of your building." "</label-single-occurance>\n" " <label-multiple-occurances>Enter the names of each of your" " buildings.</label-multiple-occurances>\n" " <use-location-question>true</use-location-question>\n" " </profile-question>\n" "</root>\n", )
def handle_workspace_added(context, event): workspace = IWorkspace(context) for group_name in workspace.available_groups: group_name = safe_nativestring(group_name) group_id = "{}:{}".format(group_name, context.UID()) title = "{}: {}".format(group_name, context.Title()) add_group(group_id, title)
def testProfileQuestion_WithModule(self): profile = ProfileQuestion() profile.title = "Office buildings" profile.question = "Do you have an office buildings?" profile.description = "<p>Owning property brings risks.</p>" module = Module() module.title = "Office buildings" module.description = "<p>Owning property brings risks.</p>" module.optional = False module.solution_direction = None profile._setOb("1", module) root = self.root() view = ExportSurvey(None, None) view.exportProfileQuestion(root, profile) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <profile-question>\n" " <title>Office buildings</title>\n" " <question>Do you have an office buildings?</question>\n" " <description><p>Owning property brings " "risks.</p></description>\n" " <use-location-question>true</use-location-question>\n" ' <module optional="false">\n' " <title>Office buildings</title>\n" " <description><p>Owning property brings " "risks.</p></description>\n" " </module>\n" " </profile-question>\n" "</root>\n", )
def testSurvey_IntroductionNoText(self): surveygroup = SurveyGroup() surveygroup.title = "Generic sector" surveygroup._setOb("standard", Survey()) survey = surveygroup["standard"] # Acquisition wrap survey.title = "Standard" survey.introduction = "<p><br/></p>" survey.classification_code = None survey.evaluation_optional = False survey.language = "en-GB" root = self.root() view = ExportSurvey(None, None) view.exportSurvey(root, survey) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <survey>\n" " <title>Generic sector</title>\n" " <language>en-GB</language>\n" " <tool_type>classic</tool_type>\n" " <measures_text_handling>full</measures_text_handling>\n" " <integrated_action_plan>false</integrated_action_plan>\n" " <evaluation-algorithm>kinney</evaluation-algorithm>\n" " <evaluation-optional>false</evaluation-optional>\n" " </survey>\n" "</root>\n", )
def testModule_WithSubModule(self): module = Module() module.title = "Office buildings" module.description = "<p>Owning property brings risks.</p>" module.optional = False module.solution_direction = None submodule = Module() submodule.title = "Parking" submodule.description = "<p>All about parking garages.</p>" submodule.optional = False submodule.solution_direction = None module._setOb("1", submodule) root = self.root() view = ExportSurvey(None, None) view.exportModule(root, module) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <module optional="false">\n' " <title>Office buildings</title>\n" " <description><p>Owning property brings risks." "</p></description>\n" ' <module optional="false">\n' " <title>Parking</title>\n" " <description><p>All about parking garages." "</p></description>\n" " </module>\n" " </module>\n" "</root>\n", )
def testModule_WithRisk(self): module = Module() module.title = "Office buildings" module.description = "<p>Owning property brings risks.</p>" module.optional = False module.solution_direction = None risk = Risk() risk.type = "top5" risk.title = "Can your windows be locked?" risk.problem_description = "Not all your windows can be locked" risk.description = "<p>Locking windows is critical.</p>" risk.legal_reference = None risk.show_notapplicable = False module._setOb("1", risk) root = self.root() view = ExportSurvey(None, None) view.exportModule(root, module) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <module optional="false">\n' " <title>Office buildings</title>\n" " <description><p>Owning property brings risks." "</p></description>\n" ' <risk type="top5">\n' " <title>Can your windows be locked?</title>\n" " <problem-description>Not all your windows can be " "locked</problem-description>\n" " <description><p>Locking windows is critical." "</p></description>\n" " <show-not-applicable>false</show-not-applicable>\n" " </risk>\n" " </module>\n" "</root>\n", )
def testRisk_Minimal(self): risk = Risk() risk.type = "top5" risk.title = "Can your windows be locked?" risk.problem_description = "Not all your windows can be locked" risk.description = "<p>Locking windows is critical.</p>" risk.legal_reference = None risk.show_notapplicable = False root = self.root() view = ExportSurvey(None, None) node = view.exportRisk(root, risk) self.assertTrue(node in root) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <risk type="top5">\n' " <title>Can your windows be locked?</title>\n" " <problem-description>Not all your windows can be locked" "</problem-description>\n" " <description><p>Locking windows is " "critical.</p></description>\n" " <show-not-applicable>false</show-not-applicable>\n" " </risk>\n" "</root>\n", )
def _update_groups(self, old_groups, new_groups): workspace = self.workspace context = workspace.context uid = context.UID() workspace_groups = get_workspace_groups_plugin() if self.user is None: return for group_name in new_groups: if group_name not in workspace.available_groups: raise ValueError( "Unknown workspace group: {}".format(group_name)) if self.groups != new_groups: self.__dict__["groups"] = new_groups.copy() # Determine automatic groups new_groups = set(new_groups) old_groups = set(old_groups) for name, condition in workspace.auto_groups.items(): if name not in workspace.available_groups: raise Exception("Unknown workspace group: {}".format(name)) # only add the automatic groups if condition is satisfied, # otherwise remove it if condition(self): new_groups.add(name) else: old_groups.add(name) # Add to new groups for group_name in new_groups - old_groups: group_name = safe_nativestring(group_name) group_id = "{}:{}".format(group_name, uid) try: workspace_groups.addPrincipalToGroup(self.user, group_id) except KeyError: # group doesn't exist title = "{}: {}".format(group_name, context.Title()) add_group(group_id, title) workspace_groups.addPrincipalToGroup(self.user, group_id) # Remove from old groups for group_name in old_groups - new_groups: group_name = safe_nativestring(group_name) group_id = "{}:{}".format(group_name, uid) try: workspace_groups.removePrincipalFromGroup(self.user, group_id) except KeyError: # group doesn't exist pass
def _filename(self): """Return the document filename""" filename = _( "filename_report_actionplan", default="Action plan ${title}", mapping={"title": self.context.session.title}, ) filename = translate(filename, context=self.request) return safe_nativestring(filename) + ".docx"
def _filename(self): """Return the document filename""" filename = _( "filename_report_identification", default="Identification report ${title}", mapping=dict(title=self.context.session.title), ) filename = translate(filename, context=self.request) return safe_nativestring(filename) + ".docx"
def handle_workspace_modified(context, event): workspace = IWorkspace(context) gtool = api.portal.get_tool("portal_groups") for group_name in workspace.available_groups: group_name = safe_nativestring(group_name) group_id = "{}:{}".format(group_name, context.UID()) group_title = "{}: {}".format(group_name, context.Title()) group = gtool.getGroupById(group_id) if group is not None: group.setProperties(title=group_title)
def testProfileQuestion_with_description(self): profile = ProfileQuestion() profile.title = "Office buildings" profile.description = "<p>Owning property brings risks.</p>" root = self.root() view = ExportSurvey(None, None) view.exportProfileQuestion(root, profile) xml = safe_nativestring(etree.tostring(root, pretty_print=True)) self.assertTrue("<description><p>Owning property brings risks." "</p></description>" in xml)
def handle_workspace_removed(context, event): workspace = IWorkspace(context) workspace_groups = get_workspace_groups_plugin() for group_name in workspace.available_groups: group_name = safe_nativestring(group_name) group_id = "{}:{}".format(group_name, context.UID()) try: workspace_groups.removeGroup(group_id) except KeyError: # group already doesn't exist pass
def testModule_with_description(self): module = Module() module.title = "Office buildings" module.description = "<p>Owning property brings risks.</p>" module.solution_direction = None module.optional = False root = self.root() view = ExportSurvey(None, None) view.exportModule(root, module) xml = safe_nativestring(etree.tostring(root, pretty_print=True)) self.assertTrue("<description><p>Owning property brings risks." "</p></description>\n" in xml)
def verify_password(self, password): """Verify the given password against the one stored in the account table """ if not password: return False if not isinstance(password, six.string_types): return False if password == self.password: return True password = safe_nativestring(password) return bcrypt.checkpw(password, self.password)
def testImage_Minimal(self): image = MockImage("hot stuff here") root = self.root() view = ExportSurvey(None, None) view.include_images = True node = view.exportImage(root, image) self.assertTrue(node in root) self.assertEqual( safe_nativestring(etree.tostring(node, pretty_print=True)), '<image xmlns="http://xml.simplon.biz/euphorie/survey/1.0">' "aG90IHN0dWZmIGhlcmU=\n</image>\n", )
def _remove_all_groups(self): workspace = self.workspace uid = workspace.context.UID() workspace_groups = get_workspace_groups_plugin() groups = set(self.groups) | set(workspace.available_groups) for group_name in groups: group_name = safe_nativestring(group_name) group_id = "{}:{}".format(group_name, uid) try: workspace_groups.removePrincipalFromGroup(self.user, group_id) except KeyError: # group doesn't exist pass
def testImage_Full(self): image = MockImage("hot stuff here", "test.gif", "image/gif") root = self.root() view = ExportSurvey(None, None) view.include_images = True image = view.exportImage(root, image, "Captiøn") self.assertEqual( safe_nativestring(etree.tostring(image, pretty_print=True)), '<image xmlns="http://xml.simplon.biz/euphorie/survey/1.0" ' 'content-type="image/gif" filename="test.gif" ' 'caption="Captiøn">aG90IHN0dWZmIGhlcmU=\n' "</image>\n", )
def compareLayoutVsView(self, obj, path="", viewaction=None): if viewaction is None: if hasattr(aq_base(obj), 'getLayout'): viewaction = obj.getLayout() else: viewaction = obj.getTypeInfo().getActionInfo( 'object/view')['url'].split('/')[-1] self.layer['app'].REQUEST['ACTUAL_URL'] = obj.absolute_url() resolved = obj.restrictedTraverse(viewaction)() # rendering the view cooked the resource registry, # so commit the transaction so loading it via the testbrowser # doesn't cook it again transaction.commit() self.browser.open(obj.absolute_url() + path) body = safe_nativestring(self.browser.contents) resolved = safe_nativestring(resolved) # request/ACTUAL_URL is fubar in tests, remove lines that depend on it resolved = RE_REMOVE_DOCCONT.sub('', resolved) resolved = RE_REMOVE_SKIPNAV.sub('', resolved) resolved = RE_REMOVE_TABS.sub('', resolved) resolved = RE_REMOVE_AUTH.sub('"', resolved) body = RE_REMOVE_DOCCONT.sub('', body) body = RE_REMOVE_SKIPNAV.sub('', body) body = RE_REMOVE_TABS.sub('', body) body = RE_REMOVE_AUTH.sub('"', body) if not body: self.fail('No body in response') if not body == resolved: diff = difflib.unified_diff(body.split("\n"), resolved.split("\n")) self.fail("\n".join([line for line in diff]))
def testSolution_Minimal(self): solution = Solution() solution.description = "<p>Test description</p>" solution.action = "Sample action plan" solution.requirements = None root = self.root() view = ExportSurvey(None, None) node = view.exportSolution(root, solution) self.assertTrue(node in root) self.assertEqual( safe_nativestring(etree.tostring(node, pretty_print=True)), '<solution xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <description><p>Test description</p>" "</description>\n" " <action>Sample action plan</action>\n" "</solution>\n", )
def testModule_Minimal(self): module = Module() module.title = "Office buildings" module.optional = False module.solution_direction = None root = self.root() view = ExportSurvey(None, None) node = view.exportModule(root, module) self.assertTrue(node in root) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <module optional="false">\n' " <title>Office buildings</title>\n" " </module>\n" "</root>\n", )
def testSolution_Complete(self): solution = Solution() solution.description = "<p>Tést description</p>" solution.action = "Sample actiøn plan" solution.requirements = "Requîrements" root = self.root() view = ExportSurvey(None, None) node = view.exportSolution(root, solution) self.assertEqual( safe_nativestring(etree.tostring(node, pretty_print=True)), '<solution xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <description><p>Tést description</p>" "</description>\n" " <action>Sample actiøn plan</action>\n" " <requirements>Requîrements</requirements>\n" "</solution>\n", )
def hash_password(self): """hash the account password using bcrypt""" try: password = self.password except AttributeError: return if not password: return password = safe_nativestring(password) if BCRYPTED_PATTERN.match(password): # The password is already encrypted, do not encrypt it again # XXX this is broken with passwords that are actually an hash return self.password = safe_unicode( bcrypt.hashpw( password, bcrypt.gensalt(), ))
def testProfileQuestion_Minimal(self): profile = ProfileQuestion() profile.title = "Office buildings" profile.question = "Do you have an office building?" profile.use_location_question = False root = self.root() view = ExportSurvey(None, None) node = view.exportProfileQuestion(root, profile) self.assertTrue(node in root) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <profile-question>\n" " <title>Office buildings</title>\n" " <question>Do you have an office building?</question>\n" " <use-location-question>false</use-location-question>\n" " </profile-question>\n" "</root>\n", )
def testModule_SolutionDirectionNoText(self): module = Module() module.title = "Office buildings" module.description = "<p>Owning property brings risks.</p>" module.optional = False module.solution_direction = "<p><br/></p>" root = self.root() view = ExportSurvey(None, None) view.exportModule(root, module) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' ' <module optional="false">\n' " <title>Office buildings</title>\n" " <description><p>Owning property brings risks." "</p></description>\n" " </module>\n" "</root>\n", )
def testProfileQuestion_WithoutQuestion(self): profile = ProfileQuestion() profile.title = "Office buildings" profile.description = "<p>Owning property brings risks.</p>" profile.use_location_question = False root = self.root() view = ExportSurvey(None, None) view.exportProfileQuestion(root, profile) self.assertEqual( safe_nativestring(etree.tostring(root, pretty_print=True)), '<root xmlns="http://xml.simplon.biz/euphorie/survey/1.0">\n' " <profile-question>\n" " <title>Office buildings</title>\n" " <question>Office buildings</question>\n" " <description><p>Owning property brings risks." "</p></description>\n" " <use-location-question>false</use-location-question>\n" " </profile-question>\n" "</root>\n", )
def authenticate(login, password): """Try to authenticate a user using the given login and password. :param unicode login: login name :param unicode password: users password :return: :py:class:`Account <euphorie.client.model.Account>` instance If the credentials are valid the matching account is returned. For invalid credentials None is returned instead. """ if not login or not password: return None password = safe_nativestring(password) login = login.lower() accounts = Session().query( model.Account).filter(model.Account.loginname == login) for account in accounts: if account.verify_password(password): return account