diff --git a/runtime/config-deployer-service/ballerina/tests/APIClientTest.bal b/runtime/config-deployer-service/ballerina/tests/APIClientTest.bal index b8693507e..bff1d34f9 100644 --- a/runtime/config-deployer-service/ballerina/tests/APIClientTest.bal +++ b/runtime/config-deployer-service/ballerina/tests/APIClientTest.bal @@ -1,5 +1,7 @@ import ballerina/test; +import config_deployer_service.model; import config_deployer_service.org.wso2.apk.config.model as runtimeModels; +import ballerina/io; @test:Config {dataProvider: APIToAPKConfDataProvider} public isolated function testFromAPIModelToAPKConf(runtimeModels:API api, APKConf expected) returns error? { @@ -8,6 +10,33 @@ public isolated function testFromAPIModelToAPKConf(runtimeModels:API api, APKCon test:assertEquals(apkConf, expected, "APKConf is not equal to expected APKConf"); } +@test:Config {} +public isolated function testCORSPolicyGenerationFromAPKConf() returns error? { + + GenerateK8sResourcesBody body = {}; + body.apkConfiguration = {fileName: "API_CORS.apk-conf", fileContent: check io:fileReadBytes("./tests/resources/API_CORS.apk-conf")}; + body.definitionFile = {fileName: "api_cors.yaml", fileContent: check io:fileReadBytes("./tests/resources/api_cors.yaml")}; + body.apiType = "REST"; + APIClient apiClient = new; + + model:APIArtifact apiArtifact = check apiClient.prepareArtifact(body.apkConfiguration, body.definitionFile); + + model:CORSPolicy? corsPolicySpecExpected = { + enabled: true, + accessControlAllowCredentials: true, + accessControlAllowOrigins: ["wso2.com"], + accessControlAllowHeaders: ["Content-Type","Authorization"], + accessControlAllowMethods: ["GET"] + }; + + foreach model:APIPolicy apiPolicy in apiArtifact.apiPolicies { + model:APIPolicyData? policyData = apiPolicy.spec.default; + if (policyData is model:APIPolicyData) { + test:assertEquals(policyData.cORSPolicy, corsPolicySpecExpected, "CORS Policy is not equal to expected CORS Policy"); + } + } +} + public function APIToAPKConfDataProvider() returns map<[runtimeModels:API, APKConf]>|error { runtimeModels:API api = runtimeModels:newAPI1(); api.setName("testAPI"); diff --git a/runtime/config-deployer-service/ballerina/tests/resources/API_CORS.apk-conf b/runtime/config-deployer-service/ballerina/tests/resources/API_CORS.apk-conf new file mode 100644 index 000000000..2794c840c --- /dev/null +++ b/runtime/config-deployer-service/ballerina/tests/resources/API_CORS.apk-conf @@ -0,0 +1,29 @@ +--- +organization: "wso2" +name: "test-cors" +context: "/test_cors" +version: "2.0.0" +type: "REST" +endpointConfigurations: + production: + endpoint: "https://httpbin.org" +operations: + - target: "/anything" + verb: "GET" + authTypeEnabled: true + scopes: [] +vhosts: + production: ["gw.am.wso2.com"] + sandbox: [] +corsConfiguration: + corsConfigurationEnabled: true + accessControlAllowOrigins: + - "wso2.com" + accessControlAllowCredentials: true + accessControlAllowHeaders: + - "Content-Type" + - "Authorization" + accessControlAllowMethods: + - "GET" + accessControlAllowMaxAge: 3600 + diff --git a/runtime/config-deployer-service/ballerina/tests/resources/api_cors.yaml b/runtime/config-deployer-service/ballerina/tests/resources/api_cors.yaml new file mode 100644 index 000000000..c5323dc79 --- /dev/null +++ b/runtime/config-deployer-service/ballerina/tests/resources/api_cors.yaml @@ -0,0 +1,27 @@ +openapi: 3.0.1 +info: + title: test-cors + description: 'A simple HTTP Request & Response Service.

Run locally: + $ docker run -p 80:80 kennethreitz/httpbin' + contact: + url: https://kennethreitz.org + email: me@kennethreitz.org + version: 2.0.0 +servers: +- url: https://httpbin.org +tags: +- name: HTTP Methods + description: Testing different HTTP verbs +- name: Auth + description: Auth methods +paths: + /anything: + get: + tags: + - Anything + summary: Returns anything passed in request data. + responses: + 200: + description: Anything passed in request + content: {} +components: {}