From patchwork Mon Apr 9 21:02:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Finucane X-Patchwork-Id: 896419 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 40KjWf4yCmz9rxp for ; Tue, 10 Apr 2018 07:05:50 +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="l8ngooxG"; 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 40KjWf3QtkzDrLk for ; Tue, 10 Apr 2018 07:05:50 +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="l8ngooxG"; 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.30; helo=caracal.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="l8ngooxG"; dkim-atps=neutral Received: from caracal.ash.relay.mailchannels.net (caracal.ash.relay.mailchannels.net [23.83.222.30]) (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 40KjT206zPzF2B0 for ; Tue, 10 Apr 2018 07:03:32 +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 E0AFF3E2D05 for ; Mon, 9 Apr 2018 21:03:26 +0000 (UTC) Received: from one.mxroute.com (unknown [100.96.11.60]) (Authenticated sender: 5xi41l16bi) by relay.mailchannels.net (Postfix) with ESMTPA id D282D3E2CFD for ; Mon, 9 Apr 2018 21:03:11 +0000 (UTC) X-Sender-Id: 5xi41l16bi|x-authuser|stephen@that.guru Received: from one.mxroute.com (one-outgoing.mxroute.com [172.18.58.245]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.14.1); Mon, 09 Apr 2018 21:03:26 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: 5xi41l16bi|x-authuser|stephen@that.guru X-MailChannels-Auth-Id: 5xi41l16bi X-Eight-Chief: 3e92d8131d6bc7cd_1523307792135_1119263198 X-MC-Loop-Signature: 1523307792135:1144067260 X-MC-Ingress-Time: 1523307792134 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=na4g3tbvdL8tEDhsnOA5duoWlyPCcfXGqtlbCcUQNI4=; b=l8ngooxGzilcnq3e0HAVUJev8k 7yZJ4huZPcr3zFDPMWemqdPXHNruoZ9PMsTugJJcDlKvpWbgxvfmXPMvZfB07JjOyIwkmymCRoSxM x1jjXlcM36Fh7Q34cjD7kyJDUgLLC7DJCx1pfsBK9fX6r12jGf48WgJ1zUSLmciU49RuAyYNIqEbB ucqexZu8TjTjJ4JEcU5RA6XgVK7+zAql5dIJXSP0SKUI6XflrQvS1vklQ7bfwlIzxpmXdEHlMJL3B QrugyXwTu/FEGE53LtLv6zIQDA5LHVok5eOsuWwQfUTXSLU7Y7N45JXRdp6RfUA65FjQaRRP00TBP UHM0NzYA==; From: Stephen Finucane To: patchwork@lists.ozlabs.org Subject: [PATCH v2 3/6] tests: Add events API tests Date: Mon, 9 Apr 2018 22:02:53 +0100 Message-Id: <20180409210256.19649-4-stephen@that.guru> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180409210256.19649-1-stephen@that.guru> References: <20180409210256.19649-1-stephen@that.guru> 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" This will make sure we don't regress in upcoming changes. Signed-off-by: Stephen Finucane --- patchwork/tests/api/test_event.py | 173 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 patchwork/tests/api/test_event.py diff --git a/patchwork/tests/api/test_event.py b/patchwork/tests/api/test_event.py new file mode 100644 index 00000000..ffc2489a --- /dev/null +++ b/patchwork/tests/api/test_event.py @@ -0,0 +1,173 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2018 Stephen Finucane +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import unittest + +from django.conf import settings + +from patchwork.compat import reverse +from patchwork.models import Event +from patchwork.tests.utils import create_cover +from patchwork.tests.utils import create_maintainer +from patchwork.tests.utils import create_patch +from patchwork.tests.utils import create_series_patch +from patchwork.tests.utils import create_state +from patchwork.tests.utils import create_user + + +if settings.ENABLE_REST_API: + from rest_framework import status + from rest_framework.test import APITestCase +else: + # stub out APITestCase + from django.test import TestCase + APITestCase = TestCase # noqa + + +@unittest.skipUnless(settings.ENABLE_REST_API, 'requires ENABLE_REST_API') +class TestEventAPI(APITestCase): + fixtures = ['default_tags'] + _category_payload_fields = { + Event.CATEGORY_COVER_CREATED: ['cover'], + Event.CATEGORY_PATCH_CREATED: ['patch'], + Event.CATEGORY_PATCH_COMPLETED: ['patch', 'series'], + Event.CATEGORY_PATCH_STATE_CHANGED: ['patch', 'previous_state', + 'current_state'], + Event.CATEGORY_PATCH_DELEGATED: ['patch', 'previous_delegate', + 'current_delegate'], + Event.CATEGORY_CHECK_CREATED: ['patch', 'check'], + Event.CATEGORY_SERIES_CREATED: ['series'], + Event.CATEGORY_SERIES_COMPLETED: ['series'], + } + + @staticmethod + def api_url(): + return reverse('api-event-list') + + def assertSerialized(self, event_objs, event_jsons, categories): + # NOTE(stephenfin): This assertSerialized is a little different due to + # the need to validate multiple events in some situation + self.assertTrue(len(event_objs) == len(event_jsons) == len(categories)) + + for event_obj, event_json, category in zip( + event_objs, event_jsons, categories): + self.assertEqual(event_obj.id, event_json['id']) + self.assertEqual(event_obj.category, category) + self.assertEqual(event_obj.category, event_json['category']) + + # nested fields + + self.assertEqual(event_obj.project.id, + event_json['project']['id']) + + payload_fields = self._category_payload_fields[category] + self.assertEqual(len(event_json['payload']), len(payload_fields)) + self.assertEqual(set(event_json['payload']), set(payload_fields)) + + def test_list(self): + """Validate we can list bundles.""" + resp = self.client.get(self.api_url()) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(0, len(resp.data)) + + # cover_created event + cover = create_cover() + + resp = self.client.get(self.api_url()) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(1, len(resp.data)) + self.assertSerialized(Event.objects.filter(cover=cover.id), resp.data, + [Event.CATEGORY_COVER_CREATED]) + + resp = self.client.get(self.api_url(), {'cover': cover.id}) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(1, len(resp.data)) + self.assertSerialized(Event.objects.filter(cover=cover.id), resp.data, + [Event.CATEGORY_COVER_CREATED]) + + # patch_created, patch_state_changed, patch_delegated + patch = create_patch() + + resp = self.client.get(self.api_url(), {'patch': patch.id}) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(1, len(resp.data)) + self.assertSerialized(Event.objects.filter(patch=patch.id), resp.data, + [Event.CATEGORY_PATCH_CREATED]) + + user = create_user() + patch.delegate = user + patch.save() + + resp = self.client.get(self.api_url(), {'patch': patch.id}) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(2, len(resp.data)) + self.assertSerialized(Event.objects.filter(patch=patch.id), resp.data, + [Event.CATEGORY_PATCH_DELEGATED, + Event.CATEGORY_PATCH_CREATED]) + + state = create_state() + patch.state = state + patch.save() + + resp = self.client.get(self.api_url(), {'patch': patch.id}) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(3, len(resp.data)) + self.assertSerialized(Event.objects.filter(patch=patch.id), resp.data, + [Event.CATEGORY_PATCH_STATE_CHANGED, + Event.CATEGORY_PATCH_DELEGATED, + Event.CATEGORY_PATCH_CREATED]) + + # patch_completed, series_created, series_completed + patch = create_series_patch() + + resp = self.client.get(self.api_url(), {'patch': patch.patch.id}) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(2, len(resp.data)) + self.assertSerialized(Event.objects.filter(patch=patch.patch.id), + resp.data, + [Event.CATEGORY_PATCH_COMPLETED, + Event.CATEGORY_PATCH_CREATED]) + + resp = self.client.get(self.api_url(), {'series': patch.series.id}) + self.assertEqual(status.HTTP_200_OK, resp.status_code) + self.assertEqual(3, len(resp.data)) + # TODO(stephenfin): The creation of the series- and patch-completed + # events could be racey. + self.assertSerialized(Event.objects.filter(series=patch.series.id), + resp.data, + [Event.CATEGORY_SERIES_COMPLETED, + Event.CATEGORY_PATCH_COMPLETED, + Event.CATEGORY_SERIES_CREATED]) + + # test filtering by invalid project + resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) + self.assertEqual(0, len(resp.data)) + + def test_create(self): + """Ensure creates aren't allowed. + + There is no detail endpoint so updates and deletes aren't possible. + """ + user = create_maintainer() + user.is_superuser = True + user.save() + self.client.force_authenticate(user=user) + + resp = self.client.post(self.api_url(), {'category': 'cover-created'}) + self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code)