From bbe022a8facb7f39b02f6c7c0841aeb1c450da6c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rn=20K=C3=BCnsem=C3=B6ller?=
 <jkuensem@physik.uni-bielefeld.de>
Date: Mon, 2 Aug 2021 16:43:38 +0200
Subject: [PATCH] TMSS-909: Fix invalid Swagger parameter config and add test
 that detects when Swagger breaks again

---
 .../tmss/tmssapp/viewsets/specification.py    |  7 ++-
 SAS/TMSS/backend/test/CMakeLists.txt          |  1 +
 SAS/TMSS/backend/test/t_swagger.py            | 63 +++++++++++++++++++
 SAS/TMSS/backend/test/t_swagger.run           |  6 ++
 SAS/TMSS/backend/test/t_swagger.sh            |  3 +
 5 files changed, 77 insertions(+), 3 deletions(-)
 create mode 100755 SAS/TMSS/backend/test/t_swagger.py
 create mode 100755 SAS/TMSS/backend/test/t_swagger.run
 create mode 100755 SAS/TMSS/backend/test/t_swagger.sh

diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py
index 3d85e66c8d5..406513c6979 100644
--- a/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py
+++ b/SAS/TMSS/backend/src/tmss/tmssapp/viewsets/specification.py
@@ -20,6 +20,7 @@ from rest_framework.response import Response as RestResponse
 
 from drf_yasg.utils import swagger_auto_schema
 from drf_yasg.openapi import Parameter
+from drf_yasg import openapi
 
 from lofar.sas.tmss.tmss.tmssapp.viewsets.lofar_viewset import LOFARViewSet, LOFARNestedViewSet, AbstractTemplateViewSet, LOFARCopyViewSet, LOFARFilterBackend
 from lofar.sas.tmss.tmss.tmssapp import models
@@ -81,14 +82,14 @@ class SchedulingUnitObservingStrategyTemplateViewSet(LOFARViewSet):
     @swagger_auto_schema(responses={status.HTTP_201_CREATED: 'The newly created scheduling unit',
                                     status.HTTP_403_FORBIDDEN: 'forbidden'},
                          operation_description="Create a new SchedulingUnit based on this SchedulingUnitObservingStrategyTemplate, with the given <name> and <description> and make it a child of the given <scheduling_set_id>",
+                         request_body=openapi.Schema(type=openapi.TYPE_OBJECT,
+                                                     description="a JSON dict containing the override values for the parameters in the template"),
                          manual_parameters=[Parameter(name='scheduling_set_id', required=True, type='integer', in_='query',
                                                       description="the id of the scheduling_set which will be the parent of the newly created scheduling_unit"),
                                             Parameter(name='name', required=False, type='string', in_='query',
                                                       description="The name for the newly created scheduling_unit"),
                                             Parameter(name='description', required=False, type='string', in_='query',
-                                                      description="The description for the newly created scheduling_unit"),
-                                            Parameter(name='requirements_doc_overrides', required=False, type='dict', in_='body',
-                                                      description="a JSON dict containing the override values for the parameters in the template")
+                                                      description="The description for the newly created scheduling_unit")
                                             ])
     @action(methods=['post'], detail=True)
     def create_scheduling_unit(self, request, pk=None):
diff --git a/SAS/TMSS/backend/test/CMakeLists.txt b/SAS/TMSS/backend/test/CMakeLists.txt
index a90367a2eef..d8e4d47175e 100644
--- a/SAS/TMSS/backend/test/CMakeLists.txt
+++ b/SAS/TMSS/backend/test/CMakeLists.txt
@@ -40,6 +40,7 @@ if(BUILD_TESTING)
     lofar_add_test(t_complex_serializers)
     lofar_add_test(t_observation_strategies_specification_and_scheduling_test)
     lofar_add_test(t_reservations)
+    lofar_add_test(t_swagger)
 
     set_tests_properties(t_scheduling PROPERTIES TIMEOUT 300)
     set_tests_properties(t_tmssapp_scheduling_REST_API PROPERTIES TIMEOUT 300)
diff --git a/SAS/TMSS/backend/test/t_swagger.py b/SAS/TMSS/backend/test/t_swagger.py
new file mode 100755
index 00000000000..47cce09f1cd
--- /dev/null
+++ b/SAS/TMSS/backend/test/t_swagger.py
@@ -0,0 +1,63 @@
+#!/usr/bin/env python3
+
+# Copyright (C) 2018    ASTRON (Netherlands Institute for Radio Astronomy)
+# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands
+#
+# This file is part of the LOFAR software suite.
+# The LOFAR software suite 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 3 of the License, or
+# (at your option) any later version.
+#
+# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+
+# $Id:  $
+
+
+# This test checks functionality of our swagger API
+
+import unittest
+import logging
+logger = logging.getLogger('lofar.'+__name__)
+logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO)
+
+from lofar.common.test_utils import exit_with_skipped_code_if_skip_integration_tests
+exit_with_skipped_code_if_skip_integration_tests()
+
+# Do Mandatory setup step:
+# use setup/teardown magic for tmss test database, ldap server and django server
+# (ignore pycharm unused import statement, python unittests does use at RunTime the tmss_test_environment_unittest_setup module)
+from lofar.sas.tmss.test.tmss_test_environment_unittest_setup import *
+from lofar.sas.tmss.test.tmss_test_data_django_models import *
+from django.contrib.auth import get_user_model
+User = get_user_model()
+
+from django.test import TestCase
+
+# import and setup test data creator
+from lofar.sas.tmss.test.tmss_test_data_rest import TMSSRESTTestDataCreator
+test_data_creator = TMSSRESTTestDataCreator(BASE_URL, AUTH)
+
+
+class BasicSwaggerTestCase(TestCase):
+    """
+    Test that the Swagger main page loads successfully.
+    """
+
+    def test_generator_template_list_apiformat(self):
+        r = requests.get(BASE_URL + '/swagger/', auth=AUTH)
+        self.assertEqual(r.status_code, 200)
+        self.assertNotIn("Failed to load API definition", r.content.decode('utf8'))
+        self.assertNotIn("Fetch error", r.content.decode('utf8'))
+        self.assertIn("LOFAR Internal API", r.content.decode('utf8'))
+
+
+if __name__ == "__main__":
+    unittest.main()
+
diff --git a/SAS/TMSS/backend/test/t_swagger.run b/SAS/TMSS/backend/test/t_swagger.run
new file mode 100755
index 00000000000..0c449f04f7d
--- /dev/null
+++ b/SAS/TMSS/backend/test/t_swagger.run
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+# Run the unit test
+source python-coverage.sh
+python_coverage_test "*tmss*" t_swagger.py
+
diff --git a/SAS/TMSS/backend/test/t_swagger.sh b/SAS/TMSS/backend/test/t_swagger.sh
new file mode 100755
index 00000000000..f17d024e62b
--- /dev/null
+++ b/SAS/TMSS/backend/test/t_swagger.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+./runctest.sh t_swagger
\ No newline at end of file
-- 
GitLab