From 02a9fe2618dab090b4572c8a0c247f51df96330d Mon Sep 17 00:00:00 2001 From: xhh Date: Fri, 16 Oct 2015 10:30:26 +0800 Subject: [PATCH] Add more test cases for global security --- .../swagger/codegen/DefaultGeneratorTest.java | 33 ++++++++++++++++--- .../test/resources/2_0/globalSecurity.json | 19 +++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java index 2828d8994d7..3ed3b131c85 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/DefaultGeneratorTest.java @@ -90,7 +90,7 @@ public class DefaultGeneratorTest { gen.opts(clientOptInput); Map> paths = gen.processPaths(swagger.getPaths()); - CodegenSecurity apiKey, petstoreAuth; + CodegenSecurity cs, apiKey, apiKey2, petstoreAuth; // security of "getPetById": api_key CodegenOperation getPetById = findCodegenOperationByOperationId(paths, "getPetById"); @@ -106,16 +106,41 @@ public class DefaultGeneratorTest { assertEquals(petstoreAuth.name, "petstore_auth"); assertEquals(petstoreAuth.type, "oauth2"); - // security of "loginUser": api_key (from global security) + // security of "loginUser": api_key, petstore_auth (from global security) CodegenOperation loginUser = findCodegenOperationByOperationId(paths, "loginUser"); - assertEquals(loginUser.authMethods.size(), 1); - apiKey = loginUser.authMethods.iterator().next(); + assertEquals(loginUser.authMethods.size(), 2); + cs = loginUser.authMethods.get(0); + if ("api_key".equals(cs.name)) { + apiKey = cs; + petstoreAuth = loginUser.authMethods.get(1); + } else { + petstoreAuth = cs; + apiKey = loginUser.authMethods.get(1); + } assertEquals(apiKey.name, "api_key"); assertEquals(apiKey.type, "apiKey"); + assertEquals(petstoreAuth.name, "petstore_auth"); + assertEquals(petstoreAuth.type, "oauth2"); // security of "logoutUser": null (override global security) CodegenOperation logoutUser = findCodegenOperationByOperationId(paths, "logoutUser"); assertNull(logoutUser.authMethods); + + // security of "getUserByName": api_key, api_key2 (override global security) + CodegenOperation getUserByName = findCodegenOperationByOperationId(paths, "getUserByName"); + assertEquals(getUserByName.authMethods.size(), 2); + cs = getUserByName.authMethods.get(0); + if ("api_key".equals(cs.name)) { + apiKey = cs; + apiKey2 = getUserByName.authMethods.get(1); + } else { + apiKey2 = cs; + apiKey = getUserByName.authMethods.get(1); + } + assertEquals(apiKey.name, "api_key"); + assertEquals(apiKey.type, "apiKey"); + assertEquals(apiKey2.name, "api_key2"); + assertEquals(apiKey2.type, "apiKey"); } @Test diff --git a/modules/swagger-codegen/src/test/resources/2_0/globalSecurity.json b/modules/swagger-codegen/src/test/resources/2_0/globalSecurity.json index 3d1548e769c..36fa87664ae 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/globalSecurity.json +++ b/modules/swagger-codegen/src/test/resources/2_0/globalSecurity.json @@ -738,7 +738,13 @@ "400": { "description": "Invalid username supplied" } - } + }, + "security": [ + { + "api_key": [], + "api_key2": [] + } + ] }, "put": { "tags": [ @@ -815,6 +821,11 @@ "name": "api_key", "in": "header" }, + "api_key2": { + "type": "apiKey", + "name": "api_key2", + "in": "query" + }, "petstore_auth": { "type": "oauth2", "authorizationUrl": "http://petstore.swagger.io/api/oauth/dialog", @@ -827,7 +838,11 @@ }, "security": [ { - "api_key": [] + "api_key": [], + "petstore_auth": [ + "write:pets", + "read:pets" + ] } ], "definitions": {