def testScenarioInMultipleProjects(self): # Check whether user can see a scenario, given that user has # rights in an 'extra' project the scenario is in # User is in some group and has view rights user, _ = User.objects.get_or_create(username='******') group = Group() group.save() user.groups.add(group) user.userpermission_set.add( UserPermission( permission=UserPermission.PERMISSION_SCENARIO_VIEW, user=user)) # There are two projects project1 = ProjectF.create() project2 = ProjectF.create() # An approved scenario is originally in project1 and approved there scenario = ScenarioF.create(status_cache=Scenario.STATUS_APPROVED) scenario.set_project(project1) scenarioproject = ScenarioProject.objects.get( scenario=scenario, project=project1) scenarioproject.approved = True scenarioproject.save() # Our user's group has view rights in project2 group.projectgrouppermission_set.add( ProjectGroupPermission( group=group, project=project2, permission=UserPermission.PERMISSION_SCENARIO_VIEW)) pm = permission_manager.UserPermissionManager(user) # We can't see the scenario self.assertEquals(len(pm.get_scenarios()), 0) # If we add the scenario to the second project project2.add_scenario(scenario) # Then we still can't, because it's not approved there self.assertEquals(len(pm.get_scenarios()), 0) # But once it's approved in project2... scenarioproject2 = ScenarioProject.objects.get( scenario=scenario, project=project2) scenarioproject2.approved = True scenarioproject2.save() # Then we can! self.assertEquals(len(pm.get_scenarios()), 1)
def testScenarioInMultipleProjects(self): # Check whether user can see a scenario, given that user has # rights in an 'extra' project the scenario is in # User is in some group and has view rights user, _ = User.objects.get_or_create(username='******') group = Group() group.save() user.groups.add(group) user.userpermission_set.add( UserPermission(permission=UserPermission.PERMISSION_SCENARIO_VIEW, user=user)) # There are two projects project1 = ProjectF.create() project2 = ProjectF.create() # An approved scenario is originally in project1 and approved there scenario = ScenarioF.create(status_cache=Scenario.STATUS_APPROVED) scenario.set_project(project1) scenarioproject = ScenarioProject.objects.get(scenario=scenario, project=project1) scenarioproject.approved = True scenarioproject.save() # Our user's group has view rights in project2 group.projectgrouppermission_set.add( ProjectGroupPermission( group=group, project=project2, permission=UserPermission.PERMISSION_SCENARIO_VIEW)) pm = permission_manager.UserPermissionManager(user) # We can't see the scenario self.assertEquals(len(pm.get_scenarios()), 0) # If we add the scenario to the second project project2.add_scenario(scenario) # Then we still can't, because it's not approved there self.assertEquals(len(pm.get_scenarios()), 0) # But once it's approved in project2... scenarioproject2 = ScenarioProject.objects.get(scenario=scenario, project=project2) scenarioproject2.approved = True scenarioproject2.save() # Then we can! self.assertEquals(len(pm.get_scenarios()), 1)
def test_writes_headers_to_sheet(self, rowmock): """Function should use the result of fieldinfo.headers() to write the header.""" headers = [ {"headername": "test1", "fieldname": "testfield1", "fieldtype": "int", "fieldhint": "het is groen"}, {"headername": "test1", "fieldname": "testfield2", "fieldtype": "string", "fieldhint": "het is groen"}, ] with mock.patch("flooding_lib.excel_import_export.FieldInfo.headers", return_value=headers): with mock.patch("flooding_lib.excel_import_export.write_domeinlijst", return_value=True): project = ProjectF.create() scenario = ScenarioF.create() scenario.set_project(project) filename = "/tmp/testfile.xls" eie.create_excel_file(project, [scenario], filename) workbook = xlrd.open_workbook(filename) self.assertEquals(workbook.nsheets, 2) sheet = workbook.sheet_by_index(0) self.assertEquals(sheet.cell_value(0, 0), "test1") self.assertEquals(sheet.cell_value(1, 0), "testfield1") self.assertEquals(sheet.cell_value(2, 0), "int") self.assertEquals(sheet.cell_value(3, 0), "het is groen") self.assertEquals(sheet.cell_value(0, 1), "test1") self.assertEquals(sheet.cell_value(1, 1), "testfield2") self.assertEquals(sheet.cell_value(2, 1), "string") self.assertEquals(sheet.cell_value(3, 1), "het is groen") os.remove(filename)
def test_calls_approval_worksheet(self, mocked_create): project = ProjectF.create() scenario = ScenarioF.create() scenario.set_project(project) eie.create_excel_file(project, [scenario], include_approval=True) self.assertTrue(mocked_create.called)
def testGetScenariosPermissionScenarioView(self): user, _ = User.objects.get_or_create(username='******') group = Group() group.save() user.groups.add(group) user.userpermission_set.add( UserPermission(permission=UserPermission.PERMISSION_SCENARIO_VIEW, user=user)) project = ProjectF.create() # User can only see approved scenarios scenario = ScenarioF.create(status_cache=Scenario.STATUS_APPROVED) scenario.set_project(project) scenarioproject = ScenarioProject.objects.get(scenario=scenario, project=project) scenarioproject.approved = True scenarioproject.save() # So can't see this one scenario2 = ScenarioF.create(status_cache=Scenario.STATUS_WAITING) scenario2.set_project(project) group.projectgrouppermission_set.add( ProjectGroupPermission( group=group, project=project, permission=UserPermission.PERMISSION_SCENARIO_VIEW)) pm = permission_manager.UserPermissionManager(user) self.assertTrue( pm.check_permission(UserPermission.PERMISSION_SCENARIO_VIEW)) scenarios = pm.get_scenarios() self.assertEquals(len(scenarios), 1) self.assertEquals(scenarios[0].id, scenario.id)
def testGetScenariosPermissionScenarioView(self): user, _ = User.objects.get_or_create(username='******') group = Group() group.save() user.groups.add(group) user.userpermission_set.add( UserPermission( permission=UserPermission.PERMISSION_SCENARIO_VIEW, user=user)) project = ProjectF.create() # User can only see approved scenarios scenario = ScenarioF.create(status_cache=Scenario.STATUS_APPROVED) scenario.set_project(project) scenarioproject = ScenarioProject.objects.get( scenario=scenario, project=project) scenarioproject.approved = True scenarioproject.save() # So can't see this one scenario2 = ScenarioF.create(status_cache=Scenario.STATUS_WAITING) scenario2.set_project(project) group.projectgrouppermission_set.add( ProjectGroupPermission( group=group, project=project, permission=UserPermission.PERMISSION_SCENARIO_VIEW)) pm = permission_manager.UserPermissionManager(user) self.assertTrue(pm.check_permission( UserPermission.PERMISSION_SCENARIO_VIEW)) scenarios = pm.get_scenarios() self.assertEquals(len(scenarios), 1) self.assertEquals(scenarios[0].id, scenario.id)
def setUp(self): self.approvalobjecttype = testapprovalmodels.ApprovalObjectTypeF() self.approvalrule = approvalmodels.ApprovalRule.objects.create(name="some rule", description="some description") self.approvalobjecttype.approvalrule.add(self.approvalrule) self.scenario = ScenarioF.create() self.project = ProjectF.create(approval_object_type=self.approvalobjecttype) self.scenario.set_project(self.project)
def test_writes_data_to_sheet(self): the_cells = (eie.Cell(value='2', pattern=''), eie.Cell(value='3', pattern='')) class Row(object): def columns(self): return the_cells with mock.patch('flooding_lib.excel_import_export.FieldInfo.headers', return_value=()): with mock.patch('flooding_lib.excel_import_export.FieldInfo.rows', return_value=[Row()]): project = ProjectF.create() scenario = ScenarioF.create() scenario.set_project(project) filename = '/tmp/testfile.xls' eie.create_excel_file(project, [scenario], filename) workbook = xlrd.open_workbook(filename) self.assertEquals(workbook.nsheets, 2) sheet = workbook.sheet_by_index(0) self.assertEquals(sheet.cell_value(4, 0), '2') self.assertEquals(sheet.cell_value(4, 1), '3') os.remove(filename)
def testGetScenariosUserDoesntHavePermission(self): # If the user doesn't have the required permission, an empty # queryset is returned. scenario = ScenarioF.create(status_cache=Scenario.STATUS_APPROVED) project = ProjectF.create() scenario.set_project(project) scenarioproject = ScenarioProject.objects.get(scenario=scenario, project=project) scenarioproject.approved = True scenarioproject.save() user, _ = User.objects.get_or_create(username='******') group = Group() group.save() user.groups.add(group) group.projectgrouppermission_set.add( ProjectGroupPermission( group=group, project=project, permission=UserPermission.PERMISSION_SCENARIO_VIEW)) pm = permission_manager.UserPermissionManager(user) self.assertFalse( pm.check_permission(UserPermission.PERMISSION_SCENARIO_VIEW)) self.assertEquals(0, len(pm.get_scenarios())) user.userpermission_set.add( UserPermission(permission=UserPermission.PERMISSION_SCENARIO_VIEW, user=user)) self.assertTrue( pm.check_permission(UserPermission.PERMISSION_SCENARIO_VIEW)) self.assertEquals(1, len(pm.get_scenarios()))
def test_no_scenarios(self): """If there are no scenarios, function should return None and there should be no file.""" project = ProjectF.create() filename = "/tmp/create_excel_file.xls" workbook = eie.create_excel_file(project, (), filename) self.assertEquals(workbook, None) self.assertFalse(os.path.exists(filename))
def setUp(self): self.approvalobjecttype = testapprovalmodels.ApprovalObjectTypeF() self.approvalrule = approvalmodels.ApprovalRule.objects.create( name="some rule", description="some description") self.approvalobjecttype.approvalrule.add(self.approvalrule) self.scenario = ScenarioF.create() self.project = ProjectF.create( approval_object_type=self.approvalobjecttype) self.scenario.set_project(self.project)
def test_calls_update_status(self, mocked_update_status): approvalobjecttype = ApprovalObjectTypeF.create() scenario = ScenarioF.create() project = ProjectF.create(approval_object_type=approvalobjecttype) scenario.set_project(project) approval_object = scenario.approval_object(project) approval_object.update_scenario_status() self.assertTrue(mocked_update_status.called)
def test_calls_update_status(self, mocked_update_status): approvalobjecttype = ApprovalObjectTypeF.create() scenario = ScenarioF.create() project = ProjectF.create( approval_object_type=approvalobjecttype) scenario.set_project(project) approval_object = scenario.approval_object(project) approval_object.update_scenario_status() self.assertTrue(mocked_update_status.called)
def setUp(self): # Lots of setup InputFieldF.create(name="Scenario Identificatie") self.approvalobjecttype = testapprovalmodels.ApprovalObjectTypeF() self.approvalrule = approvalmodels.ApprovalRule.objects.create(name="some rule", description="some description") self.approvalobjecttype.approvalrule.add(self.approvalrule) self.project = ProjectF.create(approval_object_type=self.approvalobjecttype) self.scenario = ScenarioF.create(name="scenario name") self.scenario.set_project(self.project)
def test_creates_file(self): """If called with a project, TestCreateExcelFile creates an Excel file and returns the path of that file.""" project = ProjectF.create() # Add a scenario scenario = ScenarioF.create() scenario.set_project(project) filename = "/tmp/create_excel_file.xls" eie.create_excel_file(project, [scenario], filename) self.assertTrue(os.path.exists(filename)) os.remove(filename)
def setUp(self): # Lots of setup InputFieldF.create(name='Scenario Identificatie') self.approvalobjecttype = testapprovalmodels.ApprovalObjectTypeF() self.approvalrule = approvalmodels.ApprovalRule.objects.create( name="some rule", description="some description") self.approvalobjecttype.approvalrule.add(self.approvalrule) self.project = ProjectF.create( approval_object_type=self.approvalobjecttype) self.scenario = ScenarioF.create(name="scenario name") self.scenario.set_project(self.project)
def test_put_row_in_worksheet_row_returns_errors(self, patched_rollback): scenario = ScenarioF.create() project = ProjectF.create() scenario.set_project(project) worksheet = MockWorksheet() worksheet.write(eie.HEADER_ROWS, 1, scenario.id) rule_dict = dict() allowed_scenario_ids = [] # This will give an error with mock.patch("flooding_lib.excel_import_export.get_worksheet", return_value=worksheet): with mock.patch("flooding_lib.excel_import_export.find_approval_rules", return_value=([], rule_dict)): eie.import_upload_excel_file_for_approval(project, "remco", "somepath.xls", allowed_scenario_ids) self.assertTrue(patched_rollback.called)
def test_writes_headers_to_sheet(self, rowmock): """Function should use the result of fieldinfo.headers() to write the header.""" headers = [ { 'headername': 'test1', 'fieldname': 'testfield1', 'fieldtype': 'int', 'fieldhint': 'het is groen' }, { 'headername': 'test1', 'fieldname': 'testfield2', 'fieldtype': 'string', 'fieldhint': 'het is groen' }, ] with mock.patch('flooding_lib.excel_import_export.FieldInfo.headers', return_value=headers): with mock.patch( 'flooding_lib.excel_import_export.write_domeinlijst', return_value=True): project = ProjectF.create() scenario = ScenarioF.create() scenario.set_project(project) filename = '/tmp/testfile.xls' eie.create_excel_file(project, [scenario], filename) workbook = xlrd.open_workbook(filename) self.assertEquals(workbook.nsheets, 2) sheet = workbook.sheet_by_index(0) self.assertEquals(sheet.cell_value(0, 0), 'test1') self.assertEquals(sheet.cell_value(1, 0), 'testfield1') self.assertEquals(sheet.cell_value(2, 0), 'int') self.assertEquals(sheet.cell_value(3, 0), 'het is groen') self.assertEquals(sheet.cell_value(0, 1), 'test1') self.assertEquals(sheet.cell_value(1, 1), 'testfield2') self.assertEquals(sheet.cell_value(2, 1), 'string') self.assertEquals(sheet.cell_value(3, 1), 'het is groen') os.remove(filename)
def test_put_row_in_worksheet_row_returns_errors(self, patched_rollback): scenario = ScenarioF.create() project = ProjectF.create() scenario.set_project(project) worksheet = MockWorksheet() worksheet.write(eie.HEADER_ROWS, 1, scenario.id) rule_dict = dict() allowed_scenario_ids = [] # This will give an error with mock.patch('flooding_lib.excel_import_export.get_worksheet', return_value=worksheet): with mock.patch( 'flooding_lib.excel_import_export.find_approval_rules', return_value=([], rule_dict)): eie.import_upload_excel_file_for_approval( project, 'remco', 'somepath.xls', allowed_scenario_ids) self.assertTrue(patched_rollback.called)
def test_scenarios(self): """If we have a Group named 'demo group', and a scenario in a project where that groep has view permission, we should be able to see it.""" group = Group(name='demo group') group.save() project = ProjectF.create() group.projectgrouppermission_set.add( ProjectGroupPermission( group=group, project=project, permission=UserPermission.PERMISSION_SCENARIO_VIEW)) scenario = ScenarioF.create(status_cache=Scenario.STATUS_APPROVED) scenario.set_project(project) pm = permission_manager.AnonymousPermissionManager() self.assertTrue(scenario.id in (s.id for s in pm.get_scenarios()))
def test_writes_data_to_sheet(self): the_cells = (eie.Cell(value="2", pattern=""), eie.Cell(value="3", pattern="")) class Row(object): def columns(self): return the_cells with mock.patch("flooding_lib.excel_import_export.FieldInfo.headers", return_value=()): with mock.patch("flooding_lib.excel_import_export.FieldInfo.rows", return_value=[Row()]): project = ProjectF.create() scenario = ScenarioF.create() scenario.set_project(project) filename = "/tmp/testfile.xls" eie.create_excel_file(project, [scenario], filename) workbook = xlrd.open_workbook(filename) self.assertEquals(workbook.nsheets, 2) sheet = workbook.sheet_by_index(0) self.assertEquals(sheet.cell_value(4, 0), "2") self.assertEquals(sheet.cell_value(4, 1), "3") os.remove(filename)
def testGetScenariosUserDoesntHavePermission(self): # If the user doesn't have the required permission, an empty # queryset is returned. scenario = ScenarioF.create(status_cache=Scenario.STATUS_APPROVED) project = ProjectF.create() scenario.set_project(project) scenarioproject = ScenarioProject.objects.get( scenario=scenario, project=project) scenarioproject.approved = True scenarioproject.save() user, _ = User.objects.get_or_create(username='******') group = Group() group.save() user.groups.add(group) group.projectgrouppermission_set.add( ProjectGroupPermission( group=group, project=project, permission=UserPermission.PERMISSION_SCENARIO_VIEW)) pm = permission_manager.UserPermissionManager(user) self.assertFalse(pm.check_permission( UserPermission.PERMISSION_SCENARIO_VIEW)) self.assertEquals(0, len(pm.get_scenarios())) user.userpermission_set.add( UserPermission( permission=UserPermission.PERMISSION_SCENARIO_VIEW, user=user)) self.assertTrue(pm.check_permission( UserPermission.PERMISSION_SCENARIO_VIEW)) self.assertEquals(1, len(pm.get_scenarios()))