From patchwork Tue Apr 17 08:41:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 899130 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40QJdN58R0z9rxs for ; Tue, 17 Apr 2018 18:41:44 +1000 (AEST) 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="I+Gpc8sa"; 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 40QJdN3MZ2zF1wX for ; Tue, 17 Apr 2018 18:41:44 +1000 (AEST) 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="I+Gpc8sa"; 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=23.83.222.3; helo=anteater.ash.relay.mailchannels.net; 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="I+Gpc8sa"; dkim-atps=neutral Received: from anteater.ash.relay.mailchannels.net (anteater.ash.relay.mailchannels.net [23.83.222.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 40QJdC3CWszF1sD for ; Tue, 17 Apr 2018 18:41:33 +1000 (AEST) X-Sender-Id: 5xi41l16bi|x-authuser|stephen@that.guru Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 63A8F281141; Tue, 17 Apr 2018 08:41:27 +0000 (UTC) Received: from one.mxroute.com (unknown [100.96.25.18]) (Authenticated sender: 5xi41l16bi) by relay.mailchannels.net (Postfix) with ESMTPA id 3F37A28063C; Tue, 17 Apr 2018 08:41:26 +0000 (UTC) X-Sender-Id: 5xi41l16bi|x-authuser|stephen@that.guru Received: from one.mxroute.com (one-outgoing.mxroute.com [172.19.51.240]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.14.1); Tue, 17 Apr 2018 08:41:27 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: 5xi41l16bi|x-authuser|stephen@that.guru X-MailChannels-Auth-Id: 5xi41l16bi X-Battle-Continue: 626c2870371f3046_1523954487260_1885503210 X-MC-Loop-Signature: 1523954487259:3124894968 X-MC-Ingress-Time: 1523954487256 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=that.guru; s=default; h=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: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=8Ncedk/leDbh4YkHulL2qvab6/xgIrNN0naWLuMGctw=; b=I+Gpc8saoZ+H4M3T1YfODc3EMt yDhvQGl3TzEiBp/LvOEwtpl6q4VsPxJEwyomYfms44Ggn2XSOun3MUrBScoKn2UJxV2bsAj08/mXV ZyBKuAtV2UrFAJMAz2mAtEggmnV7qAiLefgzmZUIff1b+aUzbOqXx6HPf1ecZbxyAsq5rrakR5+5w /p4Ym8sIBqZgGdaY9mRS1rgAgx8cBfntACNhT//+pdotRK6aXEfgheDXQveRIw/kw2gObFtL3VMuz 18K1RQva+rSSKMi2fPZR2H5hJV5zmk2vJ2DOeazEHQxpRbeCCE7KMR0i2nOROM5oWFk5n/WZkOQ5M hEv3A+ug==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH] tests: Replace incorrect tests Date: Tue, 17 Apr 2018 09:41:10 +0100 Message-Id: <20180417084110.7760-1-stephen@that.guru> X-Mailer: git-send-email 2.14.3 X-AuthUser: stephen@that.guru X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.26 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" In commit 683792d1, the 'test_api.py' was split into multiple 'api/test_xyz.py' files. As part of this change, the tests for cover letter were mistakenly included in place of tests for checks. Correct this oversight. Signed-off-by: Stephen Finucane Reported-by: Veronika Kabatova Fixes: 683792d1 ("tests: Split 'test_rest_api'") Acked-by: Veronika Kabatova --- patchwork/tests/api/test_check.py | 140 ++++++++++++++++++++++---------------- 1 file changed, 81 insertions(+), 59 deletions(-) diff --git a/patchwork/tests/api/test_check.py b/patchwork/tests/api/test_check.py index 6e3d68b8..7b8139ad 100644 --- a/patchwork/tests/api/test_check.py +++ b/patchwork/tests/api/test_check.py @@ -38,84 +38,106 @@ else: @unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API') -class TestCoverLetterAPI(APITestCase): +class TestCheckAPI(APITestCase): fixtures = ['default_tags'] - @staticmethod - def api_url(item=None): + def api_url(self, item=None): if item is None: - return reverse('api-cover-list') - return reverse('api-cover-detail', args=[item]) - - def assertSerialized(self, cover_obj, cover_json): - self.assertEqual(cover_obj.id, cover_json['id']) - self.assertEqual(cover_obj.name, cover_json['name']) - self.assertIn(cover_obj.get_mbox_url(), cover_json['mbox']) - - # nested fields - - self.assertEqual(cover_obj.submitter.id, - cover_json['submitter']['id']) + return reverse('api-check-list', args=[self.patch.id]) + return reverse('api-check-detail', kwargs={ + 'patch_id': self.patch.id, 'check_id': item.id}) + + def setUp(self): + super(TestCheckAPI, self).setUp() + project = create_project() + self.user = create_maintainer(project) + self.patch = create_patch(project=project) + + def _create_check(self): + values = { + 'patch': self.patch, + 'user': self.user, + } + return create_check(**values) + + def assertSerialized(self, check_obj, check_json): + self.assertEqual(check_obj.id, check_json['id']) + self.assertEqual(check_obj.get_state_display(), check_json['state']) + self.assertEqual(check_obj.target_url, check_json['target_url']) + self.assertEqual(check_obj.context, check_json['context']) + self.assertEqual(check_obj.description, check_json['description']) def test_list(self): - """Validate we can list cover letters.""" + """Validate we can list checks on a patch.""" resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(0, len(resp.data)) - person_obj = create_person(email='test@example.com') - project_obj = create_project(linkname='myproject') - cover_obj = create_cover(project=project_obj, submitter=person_obj) + check_obj = self._create_check() - # anonymous user - resp = self.client.get(self.api_url()) - self.assertEqual(status.HTTP_200_OK, resp.status_code) - self.assertEqual(1, len(resp.data)) - self.assertSerialized(cover_obj, resp.data[0]) - - # authenticated user - user = create_user() - self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) - self.assertSerialized(cover_obj, resp.data[0]) - - # test filtering by project - resp = self.client.get(self.api_url(), {'project': 'myproject'}) - self.assertEqual([cover_obj.id], [x['id'] for x in resp.data]) - resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) - self.assertEqual(0, len(resp.data)) - - # test filtering by submitter, both ID and email - resp = self.client.get(self.api_url(), {'submitter': person_obj.id}) - self.assertEqual([cover_obj.id], [x['id'] for x in resp.data]) - resp = self.client.get(self.api_url(), { - 'submitter': 'test@example.com'}) - self.assertEqual([cover_obj.id], [x['id'] for x in resp.data]) - resp = self.client.get(self.api_url(), { - 'submitter': 'test@example.org'}) + self.assertSerialized(check_obj, resp.data[0]) + + # 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)) def test_detail(self): - """Validate we can get a specific cover letter.""" - cover_obj = create_cover() - - resp = self.client.get(self.api_url(cover_obj.id)) + """Validate we can get a specific check.""" + check = self._create_check() + resp = self.client.get(self.api_url(check)) self.assertEqual(status.HTTP_200_OK, resp.status_code) - self.assertSerialized(cover_obj, resp.data) + self.assertSerialized(check, resp.data) + + def test_create(self): + """Ensure creations can be performed by user of patch.""" + check = { + 'state': 'success', + 'target_url': 'http://t.co', + 'description': 'description', + '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) - def test_create_update_delete(self): - user = create_maintainer() - user.is_superuser = True - user.save() + user = create_user() self.client.force_authenticate(user=user) - - resp = self.client.post(self.api_url(), {'name': 'test cover'}) - self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code) - - resp = self.client.patch(self.api_url(), {'name': 'test cover'}) + resp = self.client.post(self.api_url(), check) + self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) + + def test_create_invalid(self): + """Ensure we handle invalid check states.""" + check = { + 'state': 'this-is-not-a-valid-state', + 'target_url': 'http://t.co', + 'description': 'description', + 'context': 'context', + } + + self.client.force_authenticate(user=self.user) + resp = self.client.post(self.api_url(), check) + self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code) + self.assertEqual(0, Check.objects.all().count()) + + def test_update_delete(self): + """Ensure updates and deletes aren't allowed""" + check = self._create_check() + self.user.is_superuser = True + self.user.save() + self.client.force_authenticate(user=self.user) + + resp = self.client.patch(self.api_url(check), {'target_url': 'fail'}) self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code) - resp = self.client.delete(self.api_url()) + resp = self.client.delete(self.api_url(check)) self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code)