|
| 1 | +# Copyright 2018 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Test various discovery docs""" |
| 16 | + |
| 17 | +import json |
| 18 | +import os.path |
| 19 | + |
| 20 | +import pytest |
| 21 | +import webtest |
| 22 | +import urllib |
| 23 | + |
| 24 | +import endpoints |
| 25 | +import endpoints.discovery_generator as discovery_generator |
| 26 | +from protorpc import message_types |
| 27 | +from protorpc import messages |
| 28 | +from protorpc import remote |
| 29 | + |
| 30 | +def make_collection(cls): |
| 31 | + return type( |
| 32 | + 'Collection_{}'.format(cls.__name__), |
| 33 | + (messages.Message,), |
| 34 | + { |
| 35 | + 'items': messages.MessageField(cls, 1, repeated=True), |
| 36 | + 'nextPageToken': messages.StringField(2) |
| 37 | + }) |
| 38 | + |
| 39 | +def load_expected_document(filename): |
| 40 | + try: |
| 41 | + pwd = os.path.dirname(os.path.realpath(__file__)) |
| 42 | + test_file = os.path.join(pwd, 'testdata', 'discovery', filename) |
| 43 | + with open(test_file) as f: |
| 44 | + return json.loads(f.read()) |
| 45 | + except IOError as e: |
| 46 | + print 'Could not find expected output file ' + test_file |
| 47 | + raise e |
| 48 | + |
| 49 | + |
| 50 | +class Foo(messages.Message): |
| 51 | + name = messages.StringField(1) |
| 52 | + value = messages.IntegerField(2, variant=messages.Variant.INT32) |
| 53 | + |
| 54 | +FooCollection = make_collection(Foo) |
| 55 | +FooResource = endpoints.ResourceContainer( |
| 56 | + Foo, |
| 57 | + id=messages.StringField(1, required=True), |
| 58 | +) |
| 59 | +FooIdResource = endpoints.ResourceContainer( |
| 60 | + message_types.VoidMessage, |
| 61 | + id=messages.StringField(1, required=True), |
| 62 | +) |
| 63 | +FooNResource = endpoints.ResourceContainer( |
| 64 | + message_types.VoidMessage, |
| 65 | + n = messages.IntegerField(1, required=True, variant=messages.Variant.INT32), |
| 66 | +) |
| 67 | + |
| 68 | +@endpoints.api( |
| 69 | + name='foo', version='v1', audiences=['audiences'], |
| 70 | + title='The Foo API', description='Just Foo Things', |
| 71 | + documentation='https://example.com', canonical_name='CanonicalName') |
| 72 | +class FooEndpoint(remote.Service): |
| 73 | + @endpoints.method(FooResource, Foo, name='foo.create', path='foos/{id}', http_method='PUT') |
| 74 | + def createFoo(self, request): |
| 75 | + pass |
| 76 | + @endpoints.method(FooIdResource, Foo, name='foo.get', path='foos/{id}', http_method='GET') |
| 77 | + def getFoo(self, request): |
| 78 | + pass |
| 79 | + @endpoints.method(FooResource, Foo, name='foo.update', path='foos/{id}', http_method='POST') |
| 80 | + def updateFoo(self, request): |
| 81 | + pass |
| 82 | + @endpoints.method(FooIdResource, Foo, name='foo.delete', path='foos/{id}', http_method='DELETE') |
| 83 | + def deleteFoo(self, request): |
| 84 | + pass |
| 85 | + @endpoints.method(FooNResource, FooCollection, name='foo.list', path='foos', http_method='GET') |
| 86 | + def listFoos(self, request): |
| 87 | + pass |
| 88 | + @endpoints.method(message_types.VoidMessage, FooCollection, name='toplevel', path='foos', http_method='POST') |
| 89 | + def toplevel(self, request): |
| 90 | + pass |
| 91 | + |
| 92 | + |
| 93 | +class Bar(messages.Message): |
| 94 | + name = messages.StringField(1, default='Jimothy') |
| 95 | + value = messages.IntegerField(2, default=42, variant=messages.Variant.INT32) |
| 96 | + active = messages.BooleanField(3, default=True) |
| 97 | + |
| 98 | +BarCollection = make_collection(Bar) |
| 99 | +BarResource = endpoints.ResourceContainer( |
| 100 | + Bar, |
| 101 | + id=messages.StringField(1, required=True), |
| 102 | +) |
| 103 | +BarIdResource = endpoints.ResourceContainer( |
| 104 | + message_types.VoidMessage, |
| 105 | + id=messages.StringField(1, required=True), |
| 106 | +) |
| 107 | +BarNResource = endpoints.ResourceContainer( |
| 108 | + message_types.VoidMessage, |
| 109 | + n = messages.IntegerField(1, required=True, variant=messages.Variant.INT32), |
| 110 | +) |
| 111 | + |
| 112 | +@endpoints.api(name='bar', version='v1') |
| 113 | +class BarEndpoint(remote.Service): |
| 114 | + @endpoints.method(BarResource, Bar, name='bar.create', path='bars/{id}', http_method='PUT') |
| 115 | + def createBar(self, request): |
| 116 | + pass |
| 117 | + @endpoints.method(BarIdResource, Bar, name='bar.get', path='bars/{id}', http_method='GET') |
| 118 | + def getBar(self, request): |
| 119 | + pass |
| 120 | + @endpoints.method(BarResource, Bar, name='bar.update', path='bars/{id}', http_method='POST') |
| 121 | + def updateBar(self, request): |
| 122 | + pass |
| 123 | + @endpoints.method(BarIdResource, Bar, name='bar.delete', path='bars/{id}', http_method='DELETE') |
| 124 | + def deleteBar(self, request): |
| 125 | + pass |
| 126 | + @endpoints.method(BarNResource, BarCollection, name='bar.list', path='bars', http_method='GET') |
| 127 | + def listBars(self, request): |
| 128 | + pass |
| 129 | + |
| 130 | + |
| 131 | +@endpoints.api(name='multipleparam', version='v1') |
| 132 | +class MultipleParameterEndpoint(remote.Service): |
| 133 | + @endpoints.method(endpoints.ResourceContainer( |
| 134 | + message_types.VoidMessage, |
| 135 | + parent = messages.StringField(1, required=True), |
| 136 | + query = messages.StringField(2, required=False), |
| 137 | + child = messages.StringField(3, required=True), |
| 138 | + queryb = messages.StringField(4, required=True), |
| 139 | + querya = messages.StringField(5, required=True), |
| 140 | + ), message_types.VoidMessage, name='param', path='param/{parent}/{child}') |
| 141 | + def param(self, request): |
| 142 | + pass |
| 143 | + |
| 144 | +@pytest.mark.parametrize('endpoint, json_filename', [ |
| 145 | + (FooEndpoint, 'foo_endpoint.json'), |
| 146 | + (BarEndpoint, 'bar_endpoint.json'), |
| 147 | + (MultipleParameterEndpoint, 'multiple_parameter_endpoint.json'), |
| 148 | +]) |
| 149 | +def test_discovery(endpoint, json_filename): |
| 150 | + generator = discovery_generator.DiscoveryGenerator() |
| 151 | + # JSON roundtrip so we get consistent string types |
| 152 | + actual = json.loads(generator.pretty_print_config_to_json( |
| 153 | + [endpoint], hostname='discovery-test.appspot.com')) |
| 154 | + expected = load_expected_document(json_filename) |
| 155 | + assert actual == expected |
0 commit comments