From patchwork Tue Oct 30 11:31:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 990767 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42kqDl6ZJtz9s89 for ; Tue, 30 Oct 2018 22:36:39 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="HODP4rx7"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 42kqDl1p7YzDr6Q for ; Tue, 30 Oct 2018 22:36:39 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="HODP4rx7"; dkim-atps=neutral X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (mailfrom) smtp.mailfrom=that.guru (client-ip=185.234.75.18; helo=relay018.mxrelay.co; envelope-from=stephen@that.guru; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=that.guru Authentication-Results: lists.ozlabs.org; dkim=fail reason="key not found in DNS" (0-bit key; unprotected) header.d=that.guru header.i=@that.guru header.b="HODP4rx7"; dkim-atps=neutral Received: from relay018.mxrelay.co (relay018.mxrelay.co [185.234.75.18]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 42kq85172NzF1QC for ; Tue, 30 Oct 2018 22:32:37 +1100 (AEDT) Received: from filter002.mxroute.com (unknown [185.133.192.179]) by relay018.mxrelay.co (Postfix) with ESMTP id C89173F1C9 for ; Tue, 30 Oct 2018 11:32:04 +0000 (UTC) Received: from one.mxroute.com (one.mxroute.com [195.201.59.211]) by filter002.mxroute.com (Postfix) with ESMTPS id AC7373F3DB for ; Tue, 30 Oct 2018 11:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=fys0VRnLwLWYkpJVVQUX6XZTLOS6PoXQQ3HjnWCLmPM=; b=HODP4rx7VlOY8/UkKrsMz0n5Lq ehUYEjjzfjBSh+EGjOAFk6MD8xtfa0hL66mxFivZlHXiXGEOKun82H60Cqfnt0JbXEEYobqZe5xsN BpYU0iUiTetC/pc2wYRV5JtardBYFrozSCe5U24y53nOai7e5GV1FUMdfAxgoVzPBPzArKgyGhytx ZLYZJeaOHoeZyQS5Cfo6KUZHhOr1cCFQEmghLSqde94Tq/7gBmDLxg68Lhrd7KBL1RBwbAvMXbkWI Se93tLf4xWeBV654D4j3PmenNvCO3QA2FOhVgvz791FXJZqR5OOyr+7FAxpqxBTZ0Nq2c5sKCgIZM vt0r8lEQ==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH 10/13] tests: Add 'store_samples' decorator to 'test_check' Date: Tue, 30 Oct 2018 11:31:50 +0000 Message-Id: <20181030113153.7855-11-stephen@that.guru> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181030113153.7855-1-stephen@that.guru> References: <20181030113153.7855-1-stephen@that.guru> X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" Signed-off-by: Stephen Finucane --- patchwork/tests/api/test_check.py | 54 +++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/patchwork/tests/api/test_check.py b/patchwork/tests/api/test_check.py index 783a6154..42a6231a 100644 --- a/patchwork/tests/api/test_check.py +++ b/patchwork/tests/api/test_check.py @@ -9,6 +9,7 @@ from django.conf import settings from django.urls import reverse from patchwork.models import Check +from patchwork.tests.api import utils from patchwork.tests.utils import create_check from patchwork.tests.utils import create_patch from patchwork.tests.utils import create_maintainer @@ -55,12 +56,15 @@ class TestCheckAPI(APITestCase): self.assertEqual(check_obj.description, check_json['description']) self.assertEqual(check_obj.user.id, check_json['user']['id']) - def test_list(self): - """Validate we can list checks on a patch.""" + def test_list_empty(self): + """List checks when none are present.""" resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(0, len(resp.data)) + @utils.store_samples('check-list') + def test_list(self): + """List checks.""" check_obj = self._create_check() self._create_check(create_patch()) # second, unrelated patch @@ -69,23 +73,29 @@ class TestCheckAPI(APITestCase): self.assertEqual(1, len(resp.data)) self.assertSerialized(check_obj, resp.data[0]) + def test_list_filter_user(self): + """Filter checks by user.""" + check_obj = self._create_check() + # test filtering by owner, both ID and username resp = self.client.get(self.api_url(), {'user': self.user.id}) self.assertEqual([check_obj.id], [x['id'] for x in resp.data]) + resp = self.client.get(self.api_url(), {'user': self.user.username}) self.assertEqual([check_obj.id], [x['id'] for x in resp.data]) + resp = self.client.get(self.api_url(), {'user': 'otheruser'}) self.assertEqual(0, len(resp.data)) + @utils.store_samples('check-detail') def test_detail(self): - """Validate we can get a specific check.""" + """Show a check.""" check = self._create_check() resp = self.client.get(self.api_url(check)) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertSerialized(check, resp.data) - def test_create(self): - """Ensure creations can be performed by user of patch.""" + def _test_create(self, user): check = { 'state': 'success', 'target_url': 'http://t.co', @@ -93,19 +103,37 @@ class TestCheckAPI(APITestCase): 'context': 'context', } - self.client.force_authenticate(user=self.user) - resp = self.client.post(self.api_url(), check) - self.assertEqual(status.HTTP_201_CREATED, resp.status_code) - self.assertEqual(1, Check.objects.all().count()) - self.assertSerialized(Check.objects.first(), resp.data) + self.client.force_authenticate(user=user) + return self.client.post(self.api_url(), check) + + @utils.store_samples('check-create-error-forbidden') + def test_create_non_maintainer(self): + """Create a check as a non-maintainer. + Ensure creations can only be performed by maintainers. + """ user = create_user() - self.client.force_authenticate(user=user) - resp = self.client.post(self.api_url(), check) + + resp = self._test_create(user=user) self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) + @utils.store_samples('check-create') + def test_create_maintainer(self): + """Create a check as a maintainer. + + Ensure creations can only be performed by maintainers. + """ + resp = self._test_create(user=self.user) + self.assertEqual(status.HTTP_201_CREATED, resp.status_code) + self.assertEqual(1, Check.objects.all().count()) + self.assertSerialized(Check.objects.first(), resp.data) + + @utils.store_samples('check-create-error-bad-request') def test_create_invalid(self): - """Ensure we handle invalid check states.""" + """Create a check using invalid values. + + Ensure we handle invalid check states. + """ check = { 'state': 'this-is-not-a-valid-state', 'target_url': 'http://t.co',