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 test_first_is_scenario_id(self): scenario = ScenarioF.create() scenariorow = eie.ScenarioRow(scenario, ({},)) column = list(scenariorow.columns())[0] self.assertEquals(column.value, scenario.id)
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_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 test_external_water(self): # If external water is sea, and the scenariobreach has # waterlevels, it should add a link to a externalwater graph # image. The string is HTML, so it should be a SafeString. The # link depends on the scenariobreach id. scenario = ScenarioF.create() externalwater = ExternalWaterF.create( type=models.ExternalWater.TYPE_SEA) breach = BreachF.create(externalwater=externalwater) waterlevelset = WaterlevelSetF.create() WaterlevelF.create(waterlevelset=waterlevelset) scenariobreach = ScenarioBreachF.create( scenario=scenario, breach=breach, waterlevelset=waterlevelset) fields = extra_infowindow_information_fields( _('External Water'), scenario) self.assertEquals(len(fields), 1) value = fields[0].value_str self.assertTrue(isinstance(value, SafeString)) self.assertTrue('get_externalwater_graph_infowindow' in value) self.assertTrue(str(scenariobreach.id) in value)
def test_scenario(self): # Should add scenario ID scenario = ScenarioF.create() fields = extra_infowindow_information_fields( 'Scenario', scenario) self.assertEquals(len(fields), 1) self.assertEquals(fields[0].value_str, str(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_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_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 test_disallowed_scenario_id(self): """Tests that using a scenario id that exists but isn't in allowed scenario id fails.""" scenario = ScenarioF.create() allowed_ids = set((scenario.id + 1,)) errors = eie.import_scenario_row( eie.ImportedHeader(), 66, [mock.MagicMock(value=scenario.id)], allowed_ids) self.assertEquals(len(errors), 1) self.assertTrue("66" in errors[0])
def test_rest_calls_value_for_inputfield(self): field_value = 3 inputfield = InputFieldF.build(type=InputField.TYPE_INTEGER) with mock.patch( 'flooding_lib.models.Scenario.value_for_inputfield', return_value=field_value) as patched: headers = ({}, {'inputfield': inputfield}) scenariorow = eie.ScenarioRow(ScenarioF.build(), headers) columns = scenariorow.columns() columns.next() # Skip scenario id self.assertEquals(columns.next().value, field_value) patched.assert_called_with(inputfield)
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 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_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_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_scenarios(self): scenario = ScenarioF.build() fieldinfo = eie.FieldInfo([scenario]) for s in fieldinfo.scenarios(): self.assertTrue(s is scenario)
def setUp(self): """Create a scenario, let the first field of the row contain a cell with that scenario's ID.""" self.scenario = ScenarioF.create() self.rowstart = [MockCell(unicode(self.scenario.id))] self.allowed_ids = set((self.scenario.id,))