openapi: Move openapi.py into zerver/openapi.py.

Fixes #14006
This commit is contained in:
harshavardhanpb 2020-02-23 22:40:42 +05:30 committed by showell
parent ed859617e4
commit cac4feb263
5 changed files with 13 additions and 13 deletions

View File

@ -5,7 +5,7 @@ import ujson
from django.utils.html import escape as escape_html from django.utils.html import escape as escape_html
from markdown.extensions import Extension from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor from markdown.preprocessors import Preprocessor
from zerver.lib.openapi import get_openapi_parameters from zerver.openapi.openapi import get_openapi_parameters
from typing import Any, Dict, Optional, List from typing import Any, Dict, Optional, List
import markdown import markdown

View File

@ -10,7 +10,7 @@ from typing import Any, Dict, Optional, List, Tuple
import markdown import markdown
import zerver.openapi.python_examples import zerver.openapi.python_examples
from zerver.lib.openapi import get_openapi_fixture, openapi_spec from zerver.openapi.openapi import get_openapi_fixture, openapi_spec
MACRO_REGEXP = re.compile(r'\{generate_code_example(\(\s*(.+?)\s*\))*\|\s*(.+?)\s*\|\s*(.+?)\s*(\(\s*(.+)\s*\))?\}') MACRO_REGEXP = re.compile(r'\{generate_code_example(\(\s*(.+?)\s*\))*\|\s*(.+?)\s*\|\s*(.+?)\s*(\(\s*(.+)\s*\))?\}')
CODE_EXAMPLE_REGEX = re.compile(r'\# \{code_example\|\s*(.+?)\s*\}') CODE_EXAMPLE_REGEX = re.compile(r'\# \{code_example\|\s*(.+?)\s*\}')

View File

@ -6,7 +6,7 @@ import sys
from functools import wraps from functools import wraps
from zerver.lib import mdiff from zerver.lib import mdiff
from zerver.lib.openapi import validate_against_openapi_schema from zerver.openapi.openapi import validate_against_openapi_schema
from zerver.models import get_realm, get_user from zerver.models import get_realm, get_user

View File

@ -9,12 +9,12 @@ from unittest.mock import patch, MagicMock
from django.http import HttpResponse from django.http import HttpResponse
import zerver.lib.openapi as openapi import zerver.openapi.openapi as openapi
from zerver.lib.bugdown.api_code_examples import generate_curl_example, \ from zerver.lib.bugdown.api_code_examples import generate_curl_example, \
render_curl_example, parse_language_and_options render_curl_example, parse_language_and_options
from zerver.lib.request import _REQ from zerver.lib.request import _REQ
from zerver.lib.test_classes import ZulipTestCase from zerver.lib.test_classes import ZulipTestCase
from zerver.lib.openapi import ( from zerver.openapi.openapi import (
get_openapi_fixture, get_openapi_parameters, get_openapi_fixture, get_openapi_parameters,
validate_against_openapi_schema, to_python_type, validate_against_openapi_schema, to_python_type,
SchemaError, openapi_spec, get_openapi_paths SchemaError, openapi_spec, get_openapi_paths
@ -38,7 +38,7 @@ VARMAP = {
class OpenAPIToolsTest(ZulipTestCase): class OpenAPIToolsTest(ZulipTestCase):
"""Make sure that the tools we use to handle our OpenAPI specification """Make sure that the tools we use to handle our OpenAPI specification
(located in zerver/lib/openapi.py) work as expected. (located in zerver/openapi/openapi.py) work as expected.
These tools are mostly dedicated to fetching parts of the -already parsed- These tools are mostly dedicated to fetching parts of the -already parsed-
specification, and comparing them to objects returned by our REST API. specification, and comparing them to objects returned by our REST API.
@ -155,7 +155,7 @@ class OpenAPIToolsTest(ZulipTestCase):
self.assertNotEqual(openapi_spec.last_update, 0) self.assertNotEqual(openapi_spec.last_update, 0)
# Now verify calling it again doesn't call reload # Now verify calling it again doesn't call reload
with mock.patch('zerver.lib.openapi.openapi_spec.reload') as mock_reload: with mock.patch('zerver.openapi.openapi.openapi_spec.reload') as mock_reload:
get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD) get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD)
self.assertFalse(mock_reload.called) self.assertFalse(mock_reload.called)
@ -835,7 +835,7 @@ class TestCurlExampleGeneration(ZulipTestCase):
] ]
self.assertEqual(generated_curl_example, expected_curl_example) self.assertEqual(generated_curl_example, expected_curl_example)
@patch("zerver.lib.openapi.OpenAPISpec.spec") @patch("zerver.openapi.openapi.OpenAPISpec.spec")
def test_generate_and_render_curl_with_default_examples(self, spec_mock: MagicMock) -> None: def test_generate_and_render_curl_with_default_examples(self, spec_mock: MagicMock) -> None:
spec_mock.return_value = self.spec_mock_without_examples spec_mock.return_value = self.spec_mock_without_examples
generated_curl_example = self.curl_example("/mark_stream_as_read", "POST") generated_curl_example = self.curl_example("/mark_stream_as_read", "POST")
@ -849,7 +849,7 @@ class TestCurlExampleGeneration(ZulipTestCase):
] ]
self.assertEqual(generated_curl_example, expected_curl_example) self.assertEqual(generated_curl_example, expected_curl_example)
@patch("zerver.lib.openapi.OpenAPISpec.spec") @patch("zerver.openapi.openapi.OpenAPISpec.spec")
def test_generate_and_render_curl_with_invalid_method(self, spec_mock: MagicMock) -> None: def test_generate_and_render_curl_with_invalid_method(self, spec_mock: MagicMock) -> None:
spec_mock.return_value = self.spec_mock_with_invalid_method spec_mock.return_value = self.spec_mock_with_invalid_method
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
@ -872,7 +872,7 @@ class TestCurlExampleGeneration(ZulipTestCase):
] ]
self.assertEqual(generated_curl_example, expected_curl_example) self.assertEqual(generated_curl_example, expected_curl_example)
@patch("zerver.lib.openapi.OpenAPISpec.spec") @patch("zerver.openapi.openapi.OpenAPISpec.spec")
def test_generate_and_render_curl_with_object(self, spec_mock: MagicMock) -> None: def test_generate_and_render_curl_with_object(self, spec_mock: MagicMock) -> None:
spec_mock.return_value = self.spec_mock_using_object spec_mock.return_value = self.spec_mock_using_object
generated_curl_example = self.curl_example("/endpoint", "GET") generated_curl_example = self.curl_example("/endpoint", "GET")
@ -885,19 +885,19 @@ class TestCurlExampleGeneration(ZulipTestCase):
] ]
self.assertEqual(generated_curl_example, expected_curl_example) self.assertEqual(generated_curl_example, expected_curl_example)
@patch("zerver.lib.openapi.OpenAPISpec.spec") @patch("zerver.openapi.openapi.OpenAPISpec.spec")
def test_generate_and_render_curl_with_object_without_example(self, spec_mock: MagicMock) -> None: def test_generate_and_render_curl_with_object_without_example(self, spec_mock: MagicMock) -> None:
spec_mock.return_value = self.spec_mock_using_object_without_example spec_mock.return_value = self.spec_mock_using_object_without_example
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.curl_example("/endpoint", "GET") self.curl_example("/endpoint", "GET")
@patch("zerver.lib.openapi.OpenAPISpec.spec") @patch("zerver.openapi.openapi.OpenAPISpec.spec")
def test_generate_and_render_curl_with_array_without_example(self, spec_mock: MagicMock) -> None: def test_generate_and_render_curl_with_array_without_example(self, spec_mock: MagicMock) -> None:
spec_mock.return_value = self.spec_mock_using_array_without_example spec_mock.return_value = self.spec_mock_using_array_without_example
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
self.curl_example("/endpoint", "GET") self.curl_example("/endpoint", "GET")
@patch("zerver.lib.openapi.OpenAPISpec.spec") @patch("zerver.openapi.openapi.OpenAPISpec.spec")
def test_generate_and_render_curl_with_param_in_path(self, spec_mock: MagicMock) -> None: def test_generate_and_render_curl_with_param_in_path(self, spec_mock: MagicMock) -> None:
spec_mock.return_value = self.spec_mock_using_param_in_path spec_mock.return_value = self.spec_mock_using_param_in_path
generated_curl_example = self.curl_example("/endpoint/{param1}", "GET") generated_curl_example = self.curl_example("/endpoint/{param1}", "GET")