Skip to content

Commit

Permalink
Add test case for CORS Policy generation from APKConf
Browse files Browse the repository at this point in the history
  • Loading branch information
SanojPunchihewa authored and tharindu1st committed Jul 2, 2023
1 parent 9a81440 commit 7463a0b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
29 changes: 29 additions & 0 deletions runtime/config-deployer-service/ballerina/tests/APIClientTest.bal
Original file line number Diff line number Diff line change
@@ -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? {
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
openapi: 3.0.1
info:
title: test-cors
description: 'A simple HTTP Request & Response Service.<br/> <br/> <b>Run locally:
</b> <code>$ docker run -p 80:80 kennethreitz/httpbin</code>'
contact:
url: https://kennethreitz.org
email: [email protected]
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: {}

0 comments on commit 7463a0b

Please sign in to comment.