From patchwork Tue Apr 30 06:03:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Axtens X-Patchwork-Id: 1093001 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) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44tWHT4J8qz9s7T for ; Tue, 30 Apr 2019 16:06:13 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=axtens.net Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=axtens.net header.i=@axtens.net header.b="BKmFQI1U"; 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 44tWHT35XpzDqMP for ; Tue, 30 Apr 2019 16:06:13 +1000 (AEST) X-Original-To: patchwork@lists.ozlabs.org Delivered-To: patchwork@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=axtens.net (client-ip=2607:f8b0:4864:20::543; helo=mail-pg1-x543.google.com; envelope-from=dja@axtens.net; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=axtens.net Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=axtens.net header.i=@axtens.net header.b="BKmFQI1U"; dkim-atps=neutral Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 44tWDg6CsGzDqRr for ; Tue, 30 Apr 2019 16:03:47 +1000 (AEST) Received: by mail-pg1-x543.google.com with SMTP id i21so2880477pgi.12 for ; Mon, 29 Apr 2019 23:03:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=FVneJPaSsxXB8Ke5glQOMAqq8iLXtHDSWYUIA6UbKYs=; b=BKmFQI1UJkQoPoKL/zF/YLUNL7yERTIWdUM9vQkHiJEJ2ZxBXECM7gLI2cq5Qsu7b2 Gz7R0wnBHqrDbV2JU8w2BxhQTvJaIWGYkIui1mjXM8Yyy//Cj3i98vvCNfebxiqII5uX D8MnZdyvyk5MCUQFRKuP6cr6E19qBmSS8TUD0= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=FVneJPaSsxXB8Ke5glQOMAqq8iLXtHDSWYUIA6UbKYs=; b=mxcAxNZz7xq1aq6Z4JAu5fcCBBAvEOlL+xtl5QfacwylhAh8Azm0CCbtAo9VBJIYqy 2TWSucIphdTZoGtQNyQ+4fqCp1rviIwg+uyiKqrbkg60SrrVo5frH0gUEeCtx05Ir9Oq VuXoFJnNHt7qYIj7qJGOgj8f0IkDDQjXDs22kw6SKZkiGNRvUx5A/ES+4ARzD9k3f6tW rBwmQFbyPI7yeJDX8VrUsR8Ar79tgfYgDyYeMfamQnfAAo3FCifMxUFgqYvoljd8i8+N 1yjvqPSv6Pb60lHxfrWvnzoVSB2YOnmhN3wvk0ARSqlbeTn5ISvvIiEge4iqwc5gk9vi QsMg== X-Gm-Message-State: APjAAAVBxMvTPOWmNubdByrbPiUlkiqaayKF7yGt6mWY9sLBOuRfOHEd 3nIXeZCtE3WO+efgiIa8PIJeorwWCMA= X-Google-Smtp-Source: APXvYqwLzDdGJeH/3EPpUBBCx8a9t2LFudEXFPDFevXdzhX0yCjsKFhG6IyFUNoI49cUEYT4MSFi8A== X-Received: by 2002:a63:f444:: with SMTP id p4mr64191784pgk.32.1556604224239; Mon, 29 Apr 2019 23:03:44 -0700 (PDT) Received: from localhost (203-217-53-131.dyn.iinet.net.au. [203.217.53.131]) by smtp.gmail.com with ESMTPSA id t13sm71161075pgo.14.2019.04.29.23.03.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 29 Apr 2019 23:03:43 -0700 (PDT) From: Daniel Axtens To: patchwork@lists.ozlabs.org Subject: [PATCH 06/10] REST: A check must specify a state Date: Tue, 30 Apr 2019 16:03:04 +1000 Message-Id: <20190430060308.10432-7-dja@axtens.net> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190430060308.10432-1-dja@axtens.net> References: <20190430060308.10432-1-dja@axtens.net> MIME-Version: 1.0 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: , Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Patchwork" The Ozlabs crew noticed that a check without a state caused a KeyError in data['state']. Mark state as mandatory, check for it, and add a test. Reported-by: Russell Currey Reported-by: Jeremy Kerr Signed-off-by: Daniel Axtens (backported from commit 7a20ccda99e48dab643d1fbd7e170fe3e4c47185 No Swagger schema changes in the stable backport.) Signed-off-by: Daniel Axtens --- patchwork/api/check.py | 4 ++++ patchwork/tests/api/test_check.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/patchwork/api/check.py b/patchwork/api/check.py index 62e6fd19e761..1498abbbffb2 100644 --- a/patchwork/api/check.py +++ b/patchwork/api/check.py @@ -26,6 +26,7 @@ from rest_framework.generics import RetrieveAPIView from rest_framework.serializers import CurrentUserDefault from rest_framework.serializers import HiddenField from rest_framework.serializers import HyperlinkedModelSerializer +from rest_framework.serializers import ValidationError from patchwork.api.base import CheckHyperlinkedIdentityField from patchwork.api.base import MultipleFieldLookupMixin @@ -50,6 +51,9 @@ class CheckSerializer(HyperlinkedModelSerializer): user = UserSerializer(default=CurrentUserDefault()) def run_validation(self, data): + if 'state' not in data or data['state'] == '': + raise ValidationError({'state': ["A check must have a state."]}) + for val, label in Check.STATE_CHOICES: if label != data['state']: continue diff --git a/patchwork/tests/api/test_check.py b/patchwork/tests/api/test_check.py index f5a8eca155a9..e3ad099cf656 100644 --- a/patchwork/tests/api/test_check.py +++ b/patchwork/tests/api/test_check.py @@ -147,6 +147,22 @@ class TestCheckAPI(APITestCase): self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code) self.assertEqual(0, Check.objects.all().count()) + def test_create_missing_state(self): + """Create a check using invalid values. + + Ensure we handle the state being absent. + """ + check = { + '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_create_invalid_patch(self): """Ensure we handle non-existent patches.""" check = {