From nobody Sun Dec 22 08:43:38 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=patchew-devel-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1526563168063783.0458415911575; Thu, 17 May 2018 06:19:28 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1A767D596E; Thu, 17 May 2018 13:19:27 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0148230012BA; Thu, 17 May 2018 13:19:25 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 7E3D81801254; Thu, 17 May 2018 13:19:25 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w4HDJOIA001417 for ; Thu, 17 May 2018 09:19:24 -0400 Received: by smtp.corp.redhat.com (Postfix) id 896FF3001723; Thu, 17 May 2018 13:19:24 +0000 (UTC) Received: from mx1.redhat.com (ext-mx08.extmail.prod.ext.phx2.redhat.com [10.5.110.32]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 82B493001720 for ; Thu, 17 May 2018 13:19:22 +0000 (UTC) Received: from mail-pg0-f44.google.com (mail-pg0-f44.google.com [74.125.83.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 552A5C025D16 for ; Thu, 17 May 2018 13:19:11 +0000 (UTC) Received: by mail-pg0-f44.google.com with SMTP id a13-v6so1834438pgu.4 for ; Thu, 17 May 2018 06:19:11 -0700 (PDT) Received: from localhost.localdomain ([188.166.187.36]) by smtp.gmail.com with ESMTPSA id t1-v6sm6449963pgu.41.2018.05.17.06.19.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 May 2018 06:19:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=Eqltbt30tDyw8eL5YbbgNMNz+JG6WwaZBAqCYIZyyjQ=; b=H8UbWoSVIvqxtfpUmu4+4zXFePlt0feRXbBneTgyrp+LW2mmMiTAP8pJ1bV4++KyTc AtWHZJtEnlyT/TDIQok6ewuNH0Sz6O9qblqpCsV+lfuCeToEJIL3cAPlNx6VHjDJ1S+B iNoNRiZ7+JeXIf/Q6VaXl1LifzMnBb7ABlxUDZhnp9Y6Lc75zLHeJeZ4FhhKsrO/4cUU VeHAz0EL6mK+w1zuJvvE7ZUDU+O6tEVU0sxIs9yLBY6PpTMKxVCwgOQJpmHqnvb6F/3y K535Vj3Ap3ZL/c/RG32RaD1/hgPDIU1e4UBPkQNnN2F0vuUAjNLvj2H2t/GrqDNnNj8N /7Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Eqltbt30tDyw8eL5YbbgNMNz+JG6WwaZBAqCYIZyyjQ=; b=pfKZpV98JhWIdvgz4y1o+9ylh4HoDIP4DfxfAF7JBNxipsNaOj21i6Km2FzKwiZ2dm cCPKygTUIGuYV/c1h8IP5idaGNOMYG53WoXIT7kW5lN7UQVONHKQ0cUXqQw12GeRvA2V j9AUH12LogetWqfvIT0fpEdTo505B51PsjmUBEeFWPQ6KAHmFo+GlQQuZtu8cMif5CBB 2PUKYA/fmWsgD02Toj1BVUSMvWNh5PzJsjegX8qaO8VOshpYsq79gyqskQpqp1nctXS5 fb3Ndtocb6GvSy23ecP9hTQCf9zUPMd8aiBsEOwFGQ5/3+q6K13hKd4vcyelGdI3LQN7 wSdA== X-Gm-Message-State: ALKqPwfHhLja993xAaYt3XkvBOV40VZ69Wl9edIrhWtpouF17mkij+Gd M2/Zjrs2y9Y5aqSZXex2H4v5ZdYl X-Google-Smtp-Source: AB8JxZpyueIi5SAkqVAfsrmpyUtjV0S+6fSYnB5pdgak1324PjJDarutYLJiQ6tl/FsvacVN2+Zi3w== X-Received: by 2002:a62:4544:: with SMTP id s65-v6mr5215151pfa.150.1526563150013; Thu, 17 May 2018 06:19:10 -0700 (PDT) From: Shubham Jain To: patchew-devel@redhat.com Date: Thu, 17 May 2018 18:49:01 +0530 Message-Id: <20180517131901.95532-1-shubhamjain7495@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 17 May 2018 13:19:11 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 17 May 2018 13:19:11 +0000 (UTC) for IP:'74.125.83.44' DOMAIN:'mail-pg0-f44.google.com' HELO:'mail-pg0-f44.google.com' FROM:'shubhamjain7495@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.951 (DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS, T_DKIM_INVALID) 74.125.83.44 mail-pg0-f44.google.com 74.125.83.44 mail-pg0-f44.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.32 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-loop: patchew-devel@redhat.com Cc: Shubham Jain Subject: [Patchew-devel] [PATCH] test: more testcases around authorization X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 17 May 2018 13:19:27 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The test check for: - user that is not a maintainer of any project should not result in any mes= sage being imported - user that is not a maintainer of a project, but is in the importer groups= , should result in the message being imported to all recognized projects - user that is a maintainer of a project and is not in the importer group, = should result in the message being imported to recognized & maintained proj= ects --- api/rest.py | 19 +++++++++++++++---- tests/test_rest.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++= ---- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/api/rest.py b/api/rest.py index 94b3162..3499250 100644 --- a/api/rest.py +++ b/api/rest.py @@ -48,6 +48,12 @@ class PatchewPermission(permissions.BasePermission): def has_project_permission(self, request, view, obj): return obj.maintained_by(request.user) =20 + def has_maintainer_permission(self, request, view): + for p in Project.objects.all(): + if p.maintained_by(request.user): + return True + return False + =20 def has_message_permission(self, request, view, obj): return obj.project.maintained_by(request.user) =20 @@ -60,7 +66,8 @@ class PatchewPermission(permissions.BasePermission): def has_generic_permission(self, request, view): return (request.method in permissions.SAFE_METHODS) or \ self.is_superuser(request) or \ - self.has_group_permission(request, view) + self.has_group_permission(request, view) or \ + self.has_maintainer_permission(request, view) =20 def has_permission(self, request, view): return self.has_generic_permission(request, view) or \ @@ -394,9 +401,13 @@ class MessagesViewSet(BaseMessageViewSet): parser_classes =3D (JSONParser, MessagePlainTextParser, ) =20 def create(self, request, *args, **kwargs): - projects =3D [p for p in Project.objects.all() if p.recognizes(Mbo= xMessage(self.request.data['mbox']))] - if 'importers' not in self.request.user.groups.all(): - projects =3D set(projects) & set([p for p in Project.objects.a= ll() if p.maintained_by(self.request.user)]) + m =3D MboxMessage(self.request.data['mbox']) + user_groups =3D request.user.groups.all() + user_group_names =3D [grp.name for grp in user_groups] + if request.user.is_superuser or 'importers' in user_group_names: + projects =3D [p for p in Project.objects.all() if p.recognizes= (m)] + else: + projects =3D [p for p in Project.objects.all() if p.maintaine= d_by(self.request.user)] results =3D [] for project in projects: serializer =3D self.get_serializer(data=3Drequest.data) diff --git a/tests/test_rest.py b/tests/test_rest.py index 2da5459..af21c7b 100755 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -274,7 +274,7 @@ class RestTest(PatchewTestCase): self.assertEqual(resp_after.status_code, 404) self.assertEqual(resp_reply_after.status_code, 404) =20 - def test_create_message(self): + def test_json_create_project_message(self): dp =3D self.get_data_path("0022-another-simple-patch.json.gz") with open(dp, "r") as f: data =3D f.read() @@ -286,7 +286,7 @@ class RestTest(PatchewTestCase): self.assertEqual(resp.data['subject'], "[Qemu-devel] [PATCH v2 10/= 27] imx_fec: Reserve full 4K " "page for the register file") =20 - def test_create_text_message(self): + def test_text_create_project_message(self): dp =3D self.get_data_path("0004-multiple-patch-reviewed.mbox.gz") with open(dp, "r") as f: data =3D f.read() @@ -297,7 +297,7 @@ class RestTest(PatchewTestCase): self.assertEqual(resp_get.status_code, 200) self.assertEqual(resp.data['subject'], "[Qemu-devel] [PATCH v4 0/2= ] Report format specific info for LUKS block driver") =20 - def test_create_message_without_project_pk(self): + def test_json_create_message(self): dp =3D self.get_data_path("0024-multiple-project-patch.json.gz") with open(dp, "r") as f: data =3D f.read() @@ -311,7 +311,7 @@ class RestTest(PatchewTestCase): resp_get2 =3D self.api_client.get(self.PROJECT_BASE_2 + "messages/= 20180223132311.26555-2-marcandre.lureau@redhat.com/") self.assertEqual(resp_get2.status_code, 200) =20 - def test_create_text_message_without_project_pk(self): + def test_text_create_message(self): dp =3D self.get_data_path("0023-multiple-project-patch.mbox.gz") with open(dp, "r") as f: data =3D f.read() @@ -325,6 +325,52 @@ class RestTest(PatchewTestCase): resp_get2 =3D self.api_client.get(self.PROJECT_BASE_2 + "messages/= 20180223132311.26555-2-marcandre.lureau@redhat.com/") self.assertEqual(resp_get2.status_code, 200) =20 + def test_without_login_create_message(self): + dp =3D self.get_data_path("0022-another-simple-patch.json.gz") + with open(dp, "r") as f: + data =3D f.read() + resp =3D self.api_client.post(self.PROJECT_BASE + "messages/", dat= a, content_type=3D'message/rfc822') + self.assertEqual(resp.status_code, 403) + + def test_non_maintainer_create_message(self): + self.create_user(username=3D"test", password=3D"userpass") + self.api_client.login(username=3D"test", password=3D"userpass") + dp =3D self.get_data_path("0023-multiple-project-patch.mbox.gz") + with open(dp, "r") as f: + data =3D f.read() + resp =3D self.api_client.post(self.REST_BASE + "messages/", data, = content_type=3D'message/rfc822') + self.assertEqual(resp.status_code, 403) + + def test_maintainer_create_message(self): + test =3D self.create_user(username=3D"test", password=3D"userpass") + self.api_client.login(username=3D"test", password=3D"userpass") + self.p.maintainers =3D (test, ) + dp =3D self.get_data_path("0023-multiple-project-patch.mbox.gz") + with open(dp, "r") as f: + data =3D f.read() + resp =3D self.api_client.post(self.REST_BASE + "messages/", data, = content_type=3D'message/rfc822') + self.assertEqual(resp.status_code, 201) + self.assertEqual(resp.data['count'], 1) + resp_get =3D self.api_client.get(self.PROJECT_BASE + "messages/201= 80223132311.26555-2-marcandre.lureau@redhat.com/") + self.assertEqual(resp_get.status_code, 200) + resp_get2 =3D self.api_client.get(self.PROJECT_BASE_2 + "messages/= 20180223132311.26555-2-marcandre.lureau@redhat.com/") + self.assertEqual(resp_get2.status_code, 404) + + def test_importer_create_message(self): + dp =3D self.get_data_path("0023-multiple-project-patch.mbox.gz") + with open(dp, "r") as f: + data =3D f.read() + test =3D self.create_user(username=3D"test", password=3D"userpass"= , groups=3D['importers']) + self.api_client.login(username=3D"test", password=3D"userpass") + resp =3D self.api_client.post(self.REST_BASE + "messages/", data, = content_type=3D'message/rfc822') + self.assertEqual(resp.status_code, 201) + self.assertEqual(resp.data['count'], 2) + resp_get =3D self.api_client.get(self.PROJECT_BASE + "messages/201= 80223132311.26555-2-marcandre.lureau@redhat.com/") + self.assertEqual(resp_get.status_code, 200) + self.assertEqual(resp_get.data['subject'], "[Qemu-devel] [PATCH 1/= 7] SecurityPkg/Tcg2Pei: drop Tcg2PhysicalPresenceLib dependency") + resp_get2 =3D self.api_client.get(self.PROJECT_BASE_2 + "messages/= 20180223132311.26555-2-marcandre.lureau@redhat.com/") + self.assertEqual(resp_get2.status_code, 200) + def test_message(self): series =3D self.apply_and_retrieve('0001-simple-patch.mbox.gz', self.p.id, '20160628014747.20971-= 1-famz@redhat.com') --=20 2.14.3 (Apple Git-98) _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel