|
| 1 | +# |
| 2 | +# Copyright (c) 2021 Nitric Technologies Pty Ltd. |
| 3 | +# |
| 4 | +# This file is part of Nitric Python 3 SDK. |
| 5 | +# See https://github.com/nitrictech/python-sdk for further info. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | +from unittest import IsolatedAsyncioTestCase |
| 20 | +from unittest.mock import patch, AsyncMock, Mock |
| 21 | + |
| 22 | +import pytest |
| 23 | +from grpclib import GRPCError, Status |
| 24 | + |
| 25 | +from nitric.api.exception import InternalException |
| 26 | +from nitric.faas import HttpMethod, MethodOptions, ApiWorkerOptions, HttpContext, HttpRequest |
| 27 | +from nitric.proto.nitric.resource.v1 import ( |
| 28 | + ResourceDeclareRequest, |
| 29 | + ApiResource, |
| 30 | + Resource, |
| 31 | + ResourceType, |
| 32 | + ResourceDetailsResponse, |
| 33 | + ApiResourceDetails, |
| 34 | + ResourceDetailsRequest, |
| 35 | + ApiScopes, |
| 36 | + ApiSecurityDefinition, |
| 37 | + ApiSecurityDefinitionJwt, |
| 38 | +) |
| 39 | + |
| 40 | +from nitric.resources import api, ApiOptions, JwtSecurityDefinition |
| 41 | +from nitric.resources.apis import Method, Route, RouteOptions |
| 42 | + |
| 43 | + |
| 44 | +class Object(object): |
| 45 | + pass |
| 46 | + |
| 47 | + |
| 48 | +class ApiTest(IsolatedAsyncioTestCase): |
| 49 | + def test_create_default_api(self): |
| 50 | + mock_declare = AsyncMock() |
| 51 | + mock_response = Object() |
| 52 | + mock_declare.return_value = mock_response |
| 53 | + |
| 54 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 55 | + api("test-api") |
| 56 | + |
| 57 | + # Check expected values were passed to Stub |
| 58 | + mock_declare.assert_called_with( |
| 59 | + resource_declare_request=ResourceDeclareRequest( |
| 60 | + resource=Resource(type=ResourceType.Api, name="test-api"), |
| 61 | + api=ApiResource(security={}, security_definitions={}), |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + def test_create_api_throws_error(self): |
| 66 | + mock_declare = AsyncMock() |
| 67 | + mock_declare.side_effect = GRPCError(Status.INTERNAL, "test-error") |
| 68 | + |
| 69 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 70 | + with pytest.raises(InternalException) as e: |
| 71 | + api("test-api-error") |
| 72 | + |
| 73 | + def test_cached_api(self): |
| 74 | + mock_declare = AsyncMock() |
| 75 | + mock_response = Object() |
| 76 | + mock_declare.return_value = mock_response |
| 77 | + |
| 78 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 79 | + api("test-api-cached") |
| 80 | + |
| 81 | + api("test-api-cached") |
| 82 | + |
| 83 | + # Check expected values were passed to Stub |
| 84 | + mock_declare.assert_called_once_with( |
| 85 | + resource_declare_request=ResourceDeclareRequest( |
| 86 | + resource=Resource(type=ResourceType.Api, name="test-api-cached"), |
| 87 | + api=ApiResource(security={}, security_definitions={}), |
| 88 | + ) |
| 89 | + ) |
| 90 | + |
| 91 | + def test_create_api_with_empty_options(self): |
| 92 | + mock_declare = AsyncMock() |
| 93 | + mock_response = Object() |
| 94 | + mock_declare.return_value = mock_response |
| 95 | + |
| 96 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 97 | + api("test-api-empty-options", ApiOptions()) |
| 98 | + |
| 99 | + # Check expected values were passed to Stub |
| 100 | + mock_declare.assert_called_with( |
| 101 | + resource_declare_request=ResourceDeclareRequest( |
| 102 | + resource=Resource(type=ResourceType.Api, name="test-api-empty-options"), |
| 103 | + api=ApiResource(security={}, security_definitions={}), |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + def test_create_api_with_base_path(self): |
| 108 | + mock_declare = AsyncMock() |
| 109 | + mock_response = Object() |
| 110 | + mock_declare.return_value = mock_response |
| 111 | + |
| 112 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 113 | + test_api = api("test-api-base-path", ApiOptions(path="/api/v1")) |
| 114 | + |
| 115 | + # Check expected values were passed to Stub |
| 116 | + mock_declare.assert_called_with( |
| 117 | + resource_declare_request=ResourceDeclareRequest( |
| 118 | + resource=Resource(type=ResourceType.Api, name="test-api-base-path"), |
| 119 | + api=ApiResource(security={}, security_definitions={}), |
| 120 | + ) |
| 121 | + ) |
| 122 | + |
| 123 | + assert test_api.path == "/api/v1" |
| 124 | + |
| 125 | + def test_create_api_with_security_definition(self): |
| 126 | + mock_declare = AsyncMock() |
| 127 | + mock_response = Object() |
| 128 | + mock_declare.return_value = mock_response |
| 129 | + |
| 130 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 131 | + api( |
| 132 | + "test-api-security-definition", |
| 133 | + ApiOptions( |
| 134 | + security_definitions={ |
| 135 | + "user": JwtSecurityDefinition( |
| 136 | + issuer="https://example-issuer.com", audiences=["test-audience", "other-audience"] |
| 137 | + ) |
| 138 | + }, |
| 139 | + security={"user": ["test:read", "test:write"]}, |
| 140 | + ), |
| 141 | + ) |
| 142 | + |
| 143 | + # Check expected values were passed to Stub |
| 144 | + mock_declare.assert_called_with( |
| 145 | + resource_declare_request=ResourceDeclareRequest( |
| 146 | + resource=Resource(type=ResourceType.Api, name="test-api-security-definition"), |
| 147 | + api=ApiResource( |
| 148 | + security_definitions={ |
| 149 | + "user": ApiSecurityDefinition( |
| 150 | + jwt=ApiSecurityDefinitionJwt( |
| 151 | + issuer="https://example-issuer.com", audiences=["test-audience", "other-audience"] |
| 152 | + ) |
| 153 | + ) |
| 154 | + }, |
| 155 | + security={"user": ApiScopes(scopes=["test:read", "test:write"])}, |
| 156 | + ), |
| 157 | + ) |
| 158 | + ) |
| 159 | + |
| 160 | + async def test_get_api_url(self): |
| 161 | + mock_declare = AsyncMock() |
| 162 | + mock_response = Object() |
| 163 | + mock_declare.return_value = mock_response |
| 164 | + |
| 165 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 166 | + test_api = api("test-api-get-url") |
| 167 | + |
| 168 | + assert test_api is not None |
| 169 | + |
| 170 | + # Test URL called |
| 171 | + mock_details = AsyncMock() |
| 172 | + mock_details.return_value = ResourceDetailsResponse(api=ApiResourceDetails(url="https://google-api.com/")) |
| 173 | + |
| 174 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.details", mock_details): |
| 175 | + url = await test_api.url() |
| 176 | + |
| 177 | + assert url == "https://google-api.com/" |
| 178 | + |
| 179 | + # Check expected values were passed to Stub |
| 180 | + mock_details.assert_called_once_with( |
| 181 | + resource_details_request=ResourceDetailsRequest( |
| 182 | + resource=Resource(type=ResourceType.Api, name="test-api-get-url") |
| 183 | + ) |
| 184 | + ) |
| 185 | + |
| 186 | + async def test_get_api_url_throws_error(self): |
| 187 | + mock_declare = AsyncMock() |
| 188 | + mock_response = Object() |
| 189 | + mock_declare.return_value = mock_response |
| 190 | + |
| 191 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 192 | + test_api = api("test-api-get-url") |
| 193 | + |
| 194 | + assert test_api is not None |
| 195 | + |
| 196 | + # Test URL called |
| 197 | + mock_details = AsyncMock() |
| 198 | + mock_details.side_effect = GRPCError(Status.INTERNAL, "test error") |
| 199 | + |
| 200 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.details", mock_details): |
| 201 | + with pytest.raises(InternalException) as e: |
| 202 | + await test_api.url() |
| 203 | + |
| 204 | + def test_api_route(self): |
| 205 | + mock_declare = AsyncMock() |
| 206 | + mock_response = Object() |
| 207 | + mock_declare.return_value = mock_response |
| 208 | + |
| 209 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 210 | + test_api = api("test-api-route", ApiOptions(path="/api/v2/")) |
| 211 | + |
| 212 | + test_route = test_api._route("/hello") |
| 213 | + |
| 214 | + assert test_route.path == "/api/v2/hello" |
| 215 | + assert test_route.middleware == [] |
| 216 | + assert test_route.api.name == test_api.name |
| 217 | + |
| 218 | + def test_define_route(self): |
| 219 | + mock_declare = AsyncMock() |
| 220 | + mock_response = Object() |
| 221 | + mock_declare.return_value = mock_response |
| 222 | + |
| 223 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 224 | + test_api = api("test-api-define-route", ApiOptions(path="/api/v2/")) |
| 225 | + |
| 226 | + test_route = Route(test_api, "/hello", opts=RouteOptions()) |
| 227 | + |
| 228 | + assert test_route.path == "/api/v2/hello" |
| 229 | + assert test_route.middleware == [] |
| 230 | + assert test_route.api.name == test_api.name |
| 231 | + |
| 232 | + def test_define_method(self): |
| 233 | + mock_declare = AsyncMock() |
| 234 | + mock_response = Object() |
| 235 | + mock_declare.return_value = mock_response |
| 236 | + |
| 237 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 238 | + test_api = api("test-api-define-method", ApiOptions(path="/api/v2/")) |
| 239 | + |
| 240 | + test_route = Route(test_api, "/hello", opts=RouteOptions()) |
| 241 | + |
| 242 | + test_method = Method( |
| 243 | + route=test_route, |
| 244 | + methods=[HttpMethod.GET, HttpMethod.POST], |
| 245 | + opts=MethodOptions(security={"user": ["test:delete"]}), |
| 246 | + ) |
| 247 | + |
| 248 | + assert test_method.methods == [HttpMethod.GET, HttpMethod.POST] |
| 249 | + assert test_method.route == test_route |
| 250 | + assert test_method.server is not None |
| 251 | + |
| 252 | + assert isinstance(test_method.server._opts, ApiWorkerOptions) |
| 253 | + assert test_method.server._opts.methods == ["GET", "POST"] |
| 254 | + assert test_method.server._opts.api == "test-api-define-method" |
| 255 | + assert test_method.server._opts.opts.security == {"user": ["test:delete"]} |
| 256 | + |
| 257 | + def test_api_get(self): |
| 258 | + mock_declare = AsyncMock() |
| 259 | + |
| 260 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 261 | + test_api = api("test-api-get", ApiOptions(path="/api/v2/")) |
| 262 | + |
| 263 | + test_api.get("/hello")(lambda ctx: ctx) |
| 264 | + |
| 265 | + assert len(test_api.routes) == 1 |
| 266 | + assert test_api.routes[0].path == "/api/v2/hello" |
| 267 | + |
| 268 | + def test_api_post(self): |
| 269 | + mock_declare = AsyncMock() |
| 270 | + |
| 271 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 272 | + test_api = api("test-api-post", ApiOptions(path="/api/v2/")) |
| 273 | + |
| 274 | + test_api.post("/hello")(lambda ctx: ctx) |
| 275 | + |
| 276 | + assert len(test_api.routes) == 1 |
| 277 | + assert test_api.routes[0].path == "/api/v2/hello" |
| 278 | + |
| 279 | + def test_api_delete(self): |
| 280 | + mock_declare = AsyncMock() |
| 281 | + |
| 282 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 283 | + test_api = api("test-api-delete", ApiOptions(path="/api/v2/")) |
| 284 | + |
| 285 | + test_api.delete("/hello")(lambda ctx: ctx) |
| 286 | + |
| 287 | + assert len(test_api.routes) == 1 |
| 288 | + assert test_api.routes[0].path == "/api/v2/hello" |
| 289 | + |
| 290 | + def test_api_put(self): |
| 291 | + mock_declare = AsyncMock() |
| 292 | + |
| 293 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 294 | + test_api = api("test-api-put", ApiOptions(path="/api/v2/")) |
| 295 | + |
| 296 | + test_api.put("/hello")(lambda ctx: ctx) |
| 297 | + |
| 298 | + assert len(test_api.routes) == 1 |
| 299 | + assert test_api.routes[0].path == "/api/v2/hello" |
| 300 | + |
| 301 | + def test_api_patch(self): |
| 302 | + mock_declare = AsyncMock() |
| 303 | + |
| 304 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 305 | + test_api = api("test-api-patch", ApiOptions(path="/api/v2/")) |
| 306 | + |
| 307 | + test_api.patch("/hello")(lambda ctx: ctx) |
| 308 | + |
| 309 | + assert len(test_api.routes) == 1 |
| 310 | + assert test_api.routes[0].path == "/api/v2/hello" |
| 311 | + |
| 312 | + def test_api_all(self): |
| 313 | + mock_declare = AsyncMock() |
| 314 | + |
| 315 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 316 | + test_api = api("test-api-all", ApiOptions(path="/api/v2/")) |
| 317 | + |
| 318 | + test_api.all("/hello")(lambda ctx: ctx) |
| 319 | + |
| 320 | + assert len(test_api.routes) == 1 |
| 321 | + assert test_api.routes[0].path == "/api/v2/hello" |
| 322 | + |
| 323 | + def test_api_methods(self): |
| 324 | + mock_declare = AsyncMock() |
| 325 | + |
| 326 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 327 | + test_api = api("test-api-methods", ApiOptions(path="/api/v2/")) |
| 328 | + |
| 329 | + test_api.methods([HttpMethod.GET], "/hello")(lambda ctx: ctx) |
| 330 | + |
| 331 | + assert len(test_api.routes) == 1 |
| 332 | + assert test_api.routes[0].path == "/api/v2/hello" |
| 333 | + |
| 334 | + def test_api_options(self): |
| 335 | + mock_declare = AsyncMock() |
| 336 | + |
| 337 | + with patch("nitric.proto.nitric.resource.v1.ResourceServiceStub.declare", mock_declare): |
| 338 | + test_api = api("test-api-options", ApiOptions(path="/api/v2/")) |
| 339 | + |
| 340 | + test_api.options("/hello")(lambda ctx: ctx) |
| 341 | + |
| 342 | + assert len(test_api.routes) == 1 |
| 343 | + assert test_api.routes[0].path == "/api/v2/hello" |
0 commit comments