def test_delete_with_existing_comments(self): """ Delete a section with existing comments. This should fail, because sections are protected. """ section = MotionCommentSection(name="test_name_ecMCq;ymwuZZ723kD)2k") section.save() motion = Motion( title="test_title_SlqfMw(waso0saWMPqcZ", text="test_text_f30skclqS9wWF=xdfaSL", ) motion.save() comment = MotionComment(comment="test_comment_dlkMD23m)(D9020m0/Zd", motion=motion, section=section) comment.save() response = self.client.delete( reverse("motioncommentsection-detail", args=[section.pk])) self.assertEqual(response.data["args"][0], '"test_title_SlqfMw(waso0saWMPqcZ"') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(MotionCommentSection.objects.count(), 1)
def test_delete(self): """ Delete a section. """ section = MotionCommentSection(name="test_name_ecMCq;ymwuZZ723kD)2k") section.save() response = self.client.delete( reverse("motioncommentsection-detail", args=[section.pk])) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(MotionCommentSection.objects.count(), 0)
def test_retrieve(self): """ Checks, if the sections can be seen by a manager. """ section = MotionCommentSection(name="test_name_f3jOF3m8fp.<qiqmf32=") section.save() response = self.client.get(reverse("motioncommentsection-list")) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(isinstance(response.data, list)) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]["name"], "test_name_f3jOF3m8fp.<qiqmf32=")
def test_delete_no_permission(self): """ Try to delete a section without can_manage permissions """ self.admin.groups.remove(self.group_in) inform_changed_data(self.admin) section = MotionCommentSection(name="test_name_wl2oxmmhe/2kd92lwPSi") section.save() response = self.client.delete( reverse("motioncommentsection-detail", args=[section.pk])) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(MotionCommentSection.objects.count(), 1)
def test_update(self): """ Update a section name. """ section = MotionCommentSection(name="test_name_dlfgNDf37ND(g3fNf43g") section.save() response = self.client.put( reverse("motioncommentsection-detail", args=[section.pk]), {"name": "test_name_ekjfen3n)F§zn83f§Fge"}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(MotionCommentSection.objects.count(), 1) self.assertEqual(MotionCommentSection.objects.get().name, "test_name_ekjfen3n)F§zn83f§Fge")
def test_retrieve_non_manager_no_read_permission(self): """ Checks, if sections are removed, if the user is a non manager and is in any of the read_groups. """ self.admin.groups.remove(self.group_in) inform_changed_data(self.admin) section = MotionCommentSection(name="test_name_f3jOF3m8fp.<qiqmf32=") section.save() section.read_groups.add(self.group_out) response = self.client.get(reverse("motioncommentsection-list")) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(isinstance(response.data, list)) self.assertEqual(len(response.data), 0)
def test_update_no_permission(self): """ Try to update a section without can_manage permissions. """ self.admin.groups.remove(self.group_in) section = MotionCommentSection(name="test_name_wl2oxmmhe/2kd92lwPSi") section.save() response = self.client.patch( reverse("motioncommentsection-list"), {"name": "test_name_2slmDMwmqqcmC92mcklw"}, ) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(MotionCommentSection.objects.count(), 1) self.assertEqual(MotionCommentSection.objects.get().name, "test_name_wl2oxmmhe/2kd92lwPSi")
def test_update_groups(self): """ Update one of the groups. """ section = MotionCommentSection(name="test_name_f3jFq3hShf/(fh2qlPOp") section.save() section.read_groups.add(2) section.write_groups.add(3) response = self.client.patch( reverse("motioncommentsection-detail", args=[section.pk]), {"name": "test_name_gkk3FhfhpmQMhC,Y378c", "read_groups_id": [2, 4]}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(MotionCommentSection.objects.count(), 1) comment = MotionCommentSection.objects.get() self.assertEqual(comment.name, "test_name_gkk3FhfhpmQMhC,Y378c") self.assertEqual(list(comment.read_groups.values_list("pk", flat=True)), [2, 4]) self.assertEqual(list(comment.write_groups.values_list("pk", flat=True)), [3])
def test_retrieve_non_manager_with_read_permission(self): """ Checks, if the sections can be seen by a non manager, but he is in one of the read_groups. """ self.admin.groups.remove( self.group_in) # group_in has motions.can_manage permission self.admin.groups.add(self.group_out) # group_out does not. inform_changed_data(self.admin) section = MotionCommentSection(name="test_name_f3mMD28LMcm29Coelwcm") section.save() section.read_groups.add(self.group_out, self.group_in) inform_changed_data(section) response = self.client.get(reverse("motioncommentsection-list")) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]["name"], "test_name_f3mMD28LMcm29Coelwcm")
def setUp(self): self.client = APIClient() self.client.login(username="******", password="******") self.section1 = MotionCommentSection(name="test_name_hponzp<zp7NUJKLAykbX") self.section1.save() self.section2 = MotionCommentSection(name="test_name_eix,b<bojbP'JO;<kVKL") self.section2.save() self.section3 = MotionCommentSection(name="test_name_ojMOeigSIOfhmpouweqc") self.section3.save()
def setUp(self): self.client = APIClient() self.client.login(username="******", password="******") self.admin = get_user_model().objects.get() self.group_out = get_group_model().objects.get( pk=GROUP_DELEGATE_PK ) # The admin should not be in this group # Put the admin into the staff group, becaust in the admin group, he has all permissions for # every single comment section. self.admin.groups.add(GROUP_STAFF_PK) self.admin.groups.remove(GROUP_ADMIN_PK) inform_changed_data(self.admin) self.group_in = get_group_model().objects.get(pk=GROUP_STAFF_PK) self.motion = Motion( title="test_title_SlqfMw(waso0saWMPqcZ", text="test_text_f30skclqS9wWF=xdfaSL", ) self.motion.save() self.section_no_groups = MotionCommentSection( name='test_name_gj4F§(fj"(edm"§F3f3fs' ) self.section_no_groups.save() self.section_read = MotionCommentSection(name="test_name_2wv30(d2S&kvelkakl39") self.section_read.save() self.section_read.read_groups.add( self.group_in, self.group_out ) # Group out for testing multiple groups self.section_read.write_groups.add(self.group_out) self.section_read_write = MotionCommentSection( name="test_name_a3m9sd0(Mw2%slkrv30," ) self.section_read_write.save() self.section_read_write.read_groups.add(self.group_in) self.section_read_write.write_groups.add(self.group_in)
class TestMotionCommentSectionSorting(TestCase): """ Tests sorting of comment sections. """ def setUp(self): self.client = APIClient() self.client.login(username="******", password="******") self.section1 = MotionCommentSection( name="test_name_hponzp<zp7NUJKLAykbX") self.section1.save() self.section2 = MotionCommentSection( name="test_name_eix,b<bojbP'JO;<kVKL") self.section2.save() self.section3 = MotionCommentSection( name="test_name_ojMOeigSIOfhmpouweqc") self.section3.save() def test_simple(self): response = self.client.post(reverse("motioncommentsection-sort"), {"ids": [3, 2, 1]}) self.assertEqual(response.status_code, status.HTTP_200_OK) section1 = MotionCommentSection.objects.get(pk=1) self.assertEqual(section1.weight, 3) section2 = MotionCommentSection.objects.get(pk=2) self.assertEqual(section2.weight, 2) section3 = MotionCommentSection.objects.get(pk=3) self.assertEqual(section3.weight, 1) def test_wrong_data(self): response = self.client.post(reverse("motioncommentsection-sort"), {"ids": "some_string"}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assert_not_changed() def test_wrong_id_type(self): response = self.client.post(reverse("motioncommentsection-sort"), {"ids": [1, 2, "some_string"]}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assert_not_changed() def test_missing_id(self): response = self.client.post(reverse("motioncommentsection-sort"), {"ids": [3, 1]}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assert_not_changed() def test_duplicate_id(self): response = self.client.post(reverse("motioncommentsection-sort"), {"ids": [3, 2, 1, 1]}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assert_not_changed() def test_wrong_id(self): response = self.client.post(reverse("motioncommentsection-sort"), {"ids": [3, 4, 1]}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assert_not_changed() def assert_not_changed(self): """ Asserts, that every comment section has the default weight of 10000. """ for section in MotionCommentSection.objects.all(): self.assertEqual(section.weight, 10000)
class ManageComments(TestCase): """ Tests the manage_comment view. Tests creation/updating and deletion of motion comments. """ def setUp(self): self.client = APIClient() self.client.login(username="******", password="******") self.admin = get_user_model().objects.get() self.group_out = get_group_model().objects.get( pk=GROUP_DELEGATE_PK) # The admin should not be in this group # Put the admin into the staff group, becaust in the admin group, he has all permissions for # every single comment section. self.admin.groups.add(GROUP_STAFF_PK) self.admin.groups.remove(GROUP_ADMIN_PK) inform_changed_data(self.admin) self.group_in = get_group_model().objects.get(pk=GROUP_STAFF_PK) self.motion = Motion( title="test_title_SlqfMw(waso0saWMPqcZ", text="test_text_f30skclqS9wWF=xdfaSL", ) self.motion.save() self.section_no_groups = MotionCommentSection( name='test_name_gj4F§(fj"(edm"§F3f3fs') self.section_no_groups.save() self.section_read = MotionCommentSection( name="test_name_2wv30(d2S&kvelkakl39") self.section_read.save() self.section_read.read_groups.add( self.group_in, self.group_out) # Group out for testing multiple groups self.section_read.write_groups.add(self.group_out) self.section_read_write = MotionCommentSection( name="test_name_a3m9sd0(Mw2%slkrv30,") self.section_read_write.save() self.section_read_write.read_groups.add(self.group_in) self.section_read_write.write_groups.add(self.group_in) def test_wrong_data_type(self): response = self.client.post( reverse("motion-manage-comments", args=[self.motion.pk]), None) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data["detail"], "You have to provide a section_id of type int.") def test_wrong_comment_data_type(self): response = self.client.post( reverse("motion-manage-comments", args=[self.motion.pk]), { "section_id": self.section_read_write.id, "comment": [32, "no_correct_data"], }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data["detail"], "The comment should be a string.") def test_non_existing_section(self): response = self.client.post( reverse("motion-manage-comments", args=[self.motion.pk]), {"section_id": 42}) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data["detail"], "A comment section with id {0} does not exist.") self.assertEqual(response.data["args"][0], "42") def test_create_comment(self): response = self.client.post( reverse("motion-manage-comments", args=[self.motion.pk]), { "section_id": self.section_read_write.pk, "comment": "test_comment_fk3jrnfwsdg%fj=feijf", }, ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(MotionComment.objects.count(), 1) comment = MotionComment.objects.get() self.assertEqual(comment.comment, "test_comment_fk3jrnfwsdg%fj=feijf") def test_update_comment(self): comment = MotionComment( motion=self.motion, section=self.section_read_write, comment="test_comment_fji387fqwdf&ff=)Fe3j", ) comment.save() response = self.client.post( reverse("motion-manage-comments", args=[self.motion.pk]), { "section_id": self.section_read_write.pk, "comment": "test_comment_fk3jrnfwsdg%fj=feijf", }, ) self.assertEqual(response.status_code, status.HTTP_200_OK) comment = MotionComment.objects.get() self.assertEqual(comment.comment, "test_comment_fk3jrnfwsdg%fj=feijf") def test_delete_comment(self): comment = MotionComment( motion=self.motion, section=self.section_read_write, comment='test_comment_5CJ"8f23jd3j2,r93keZ', ) comment.save() response = self.client.delete( reverse("motion-manage-comments", args=[self.motion.pk]), {"section_id": self.section_read_write.pk}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(MotionComment.objects.count(), 0) def test_delete_not_existing_comment(self): """ This should fail silently; no error, if the user wants to delete a not existing comment. """ response = self.client.delete( reverse("motion-manage-comments", args=[self.motion.pk]), {"section_id": self.section_read_write.pk}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(MotionComment.objects.count(), 0) def test_create_comment_no_write_permission(self): response = self.client.post( reverse("motion-manage-comments", args=[self.motion.pk]), { "section_id": self.section_read.pk, "comment": "test_comment_f38jfwqfj830fj4j(FU3", }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(MotionComment.objects.count(), 0) self.assertEqual( response.data["detail"], "You are not allowed to see or write to the comment section.", ) def test_update_comment_no_write_permission(self): comment = MotionComment( motion=self.motion, section=self.section_read, comment="test_comment_jg38dwiej2D832(D§dk)", ) comment.save() response = self.client.post( reverse("motion-manage-comments", args=[self.motion.pk]), { "section_id": self.section_read.pk, "comment": "test_comment_fk3jrnfwsdg%fj=feijf", }, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) comment = MotionComment.objects.get() self.assertEqual(comment.comment, "test_comment_jg38dwiej2D832(D§dk)") def test_delete_comment_no_write_permission(self): comment = MotionComment( motion=self.motion, section=self.section_read, comment="test_comment_fej(NF§kfePOF383o8DN", ) comment.save() response = self.client.delete( reverse("motion-manage-comments", args=[self.motion.pk]), {"section_id": self.section_read.pk}, ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(MotionComment.objects.count(), 1) comment = MotionComment.objects.get() self.assertEqual(comment.comment, "test_comment_fej(NF§kfePOF383o8DN")