def test_check_populated_content(self, key, role): """Test populated content for revision if ACL doesn't exists.""" content = {} if key: content[key] = {"id": self.user_id} expected = content.copy() expected["access_control_list"] = [] role_dict = {} if role: role_name, role_id = role expected["access_control_list"].append({ "display_name": role_name, "ac_role_id": role_id, "context_id": None, "created_at": None, "object_type": self.object_type, "updated_at": None, "object_id": self.object_id, "modified_by_id": None, "person_id": self.user_id, "modified_by": None, "id": None, }) role_dict[role_id] = role_name obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = self.object_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) with mock.patch("ggrc.access_control.role.get_custom_roles_for", return_value=role_dict) as get_roles: self.assertEqual(revision.content, expected) get_roles.assert_called_once_with(self.object_type)
def test_populated_content_cavs(self, content, expected_content): """Test populated cavs content for revision if start content is {0}.""" obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = self.object_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) self.assertEqual(expected_content, revision.populate_cavs())
def test_populated_status(self, content, expected_content, resource_type): """Test populated content with status '{0}' to '{1}' in Model '{2}'.""" obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = resource_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) self.assertEqual(revision.populate_status(), expected_content)
def test_populated_readonly(self, resource_type, content, expected_content): """Populated readonly if content={1} for {0}.""" obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = resource_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) self.assertEqual(revision.populate_readonly(), expected_content)
def test_populate_type(self): """Test populated content with type missing in _content field""" obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = "Program" revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), {}) self.assertEqual(revision.populate_type(), {"type": "Program"})
def test_populated_content_no_role(self, key): """Test populated content for revision without roles.""" content = {key: {"id": self.user_id}} expected = {"access_control_list": []} obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = self.object_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) with mock.patch("ggrc.access_control.role.get_custom_roles_for", return_value={}) as get_roles: self.assertEqual(revision.populate_acl(), expected) get_roles.assert_called_once_with(self.object_type)
def test_populated_content_labels(self, content, expected): """Test populated content for old revision with label.""" obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = self.object_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) with mock.patch("ggrc.access_control.role.get_custom_roles_for", return_value={},): self.assertEqual(revision.populate_labels()["labels"], expected)
def test_populated_content_no_user(self, user_dict): """Test populated content for revision without user id.""" content = {"principal_assessor": user_dict} role_dict = {1: "Principal Assignees"} expected = {"access_control_list": []} obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = self.object_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) with mock.patch("ggrc.access_control.role.get_custom_roles_for", return_value=role_dict) as get_roles: self.assertEqual(revision.populate_acl(), expected) get_roles.assert_called_once_with(self.object_type)
def test_populated_content_evidence(self, content, expected_evidence): """Test display names for document evidence in revision content. The display name should contain link and title, like we used to have in slugs. """ obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = self.object_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) self.assertEqual( revision._document_evidence_hack(), expected_evidence, )
def test_internal_acl_filter(self, acl_entries): """Test filtering of internal roles for {}.""" obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = "Control" for acl_entry in acl_entries: acl_entry.update({"person_id": 1, "ac_role_id": 1}) content = {"access_control_list": acl_entries} role_dict = mock.MagicMock() with mock.patch("ggrc.access_control.role.get_custom_roles_for", return_value=role_dict): revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) for acl in revision.content["access_control_list"]: self.assertIsNone(acl.get("parent_id"))
def test_populated_content_evidence(self, content, expected_evidence): """Test display names for document evidence in revision content. The display name should contain link and title, like we used to have in slugs. Document epic: 'document_evidence' renamed to 'documents_file' """ obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = "Control" revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) self.assertEqual( # pylint: disable=protected-access revision._document_evidence_hack(), expected_evidence, )
def test_populated_review_status_display_name(self, content, expected_result, resource_type): """Populated from '{0}' to '{1}' for {2}""" obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = resource_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) # emulate situation when review_status is already populated if exists populated = dict() if 'review_status' in content: populated['review_status'] = content['review_status'] # ensure that correct review_status_display_name is added for Control revision.populate_review_status_display_name(populated) self.assertEqual(populated, expected_result)
def test_populated_content_urls(self, content): """Test populated content for revision with urls.""" expected = [{'display_name': 'url1', 'document_type': 'REFERENCE_URL', 'id': None, 'link': 'url1', 'title': 'url1'}, {'display_name': 'url2', 'document_type': 'REFERENCE_URL', 'id': None, 'link': 'url2', 'title': 'url2'}] obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = self.object_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) with mock.patch("ggrc.access_control.role.get_custom_roles_for", return_value={}): self.assertEqual(revision.content["reference_url"], expected)
def test_modify_revision_content(self, cad_id, expected_cad_id, expected): """Test _get_modified_revision_content function""" cad = Mock(id=cad_id, title="new title") content = { "custom_attribute_definitions": [{ "id": expected_cad_id, "title": "title" }] } obj = Mock(resource_type="Program") revision = all_models.Revision(obj, MagicMock(), MagicMock(), content) with patch("ggrc.views._get_revisions_by_type", return_value=[revision]): result = [ c for c in views._get_modified_revision_content( cad, revision.resource_type) ] self.assertEqual(result, expected)
def test_populated_content_urls(self, content): """Test populated content for revision with urls.""" dates_in_content = "created_at" in content if dates_in_content: expected_created_at = "2017-07-15T15:49:14" expected_updated_at = "2017-08-20T13:32:42" else: # Revision's own dates should be used as a fallback expected_created_at = "2017-11-12T13:14:15" expected_updated_at = "2018-11-12T13:14:15" expected = [{ 'display_name': 'www.url-foo.com', 'document_type': 'REFERENCE_URL', 'id': None, 'link': 'www.url-foo.com', 'title': 'www.url-foo.com', 'created_at': expected_created_at, 'updated_at': expected_updated_at, }, { 'display_name': 'www.refurl-bar.com', 'document_type': 'REFERENCE_URL', 'id': None, 'link': 'www.refurl-bar.com', 'title': 'www.refurl-bar.com', 'created_at': expected_created_at, 'updated_at': expected_updated_at, }] obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = self.object_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) revision.created_at = datetime.datetime(2017, 11, 12, 13, 14, 15) revision.updated_at = datetime.datetime(2018, 11, 12, 13, 14, 15) with mock.patch("ggrc.access_control.role.get_custom_roles_for", return_value={}): self.assertEqual( revision.populate_reference_url()["reference_url"], expected)
def test_check_populated_content(self, key, role, object_type): """Test populated content for revision if ACL doesn't exists.""" content = {} if key: content[key] = {"id": self.user_id} expected = {"access_control_list": []} role_dict = {} if role: role_name, role_id = role expected["access_control_list"].append({ "display_name": role_name, "ac_role_id": role_id, "context_id": None, "created_at": None, "object_type": object_type, "updated_at": None, "object_id": self.object_id, "modified_by_id": None, "person_id": self.user_id, # Frontend require data in such format "person": { "id": self.user_id, "type": "Person", "href": "/api/people/{}".format(self.user_id) }, "modified_by": None, "id": None, }) role_dict[role_id] = role_name obj = mock.Mock() obj.id = self.object_id obj.__class__.__name__ = object_type revision = all_models.Revision(obj, mock.Mock(), mock.Mock(), content) with mock.patch("ggrc.access_control.role.get_custom_roles_for", return_value=role_dict) as get_roles: self.assertEqual(revision.populate_acl(), expected) get_roles.assert_called_once_with(object_type)