Skip to content

Commit

Permalink
Add negative tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HindujaB committed May 2, 2024
1 parent 653e601 commit f30ff6d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
69 changes: 63 additions & 6 deletions ballerina-tests/artifacts-tests/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// under the License.

import ballerina/http;
import ballerina/lang.value;
import ballerina/test;
import ballerinax/wso2.controlplane as cp;

Expand Down Expand Up @@ -89,13 +90,69 @@ public function testGetBallerinaListenerArtifacts() returns error? {
test:assertTrue(artifact is cp:ListenerDetail, "Invalid response received");
}

service /hello on new http:Listener(testPort) {
@test:Config {}
function testNegativeRegisterClient() returns error? {
http:Client icpClient = check new (testURL,
auth = {
username: "Non-admin",
password: "Non-admin"
},
secureSocket = {
enable: false
}
);
anydata|error result = icpClient->/management/login();
test:assertTrue(result is error, "Invalid response received");
error e = <error>result;
map<value:Cloneable> & readonly detail = e.detail();
value:Cloneable & readonly unionResult = detail["body"];
test:assertTrue(unionResult is map<value:Cloneable>, "Invalid response received");
if (unionResult is map<value:Cloneable>) {
test:assertEquals(unionResult["message"], "Invalid credentials", "Invalid error message received");
}
test:assertEquals(e.message(), "Internal Server Error", "Invalid error message received");
}

resource function get greeting() returns string {
return "Hello, World!";
@test:Config {}
function testNegativeInvalidTokenNode() returns error? {
http:Client rmClient = check new (testURL,
secureSocket = {
enable: false
},
auth = {
token: "Invalid-token"
}
);
cp:Node|error node = rmClient->/management();
test:assertTrue(node is error, "Invalid response received");
error e = <error>node;
map<value:Cloneable> & readonly detail = e.detail();
value:Cloneable & readonly unionResult = detail["body"];
test:assertTrue(unionResult is map<value:Cloneable>, "Invalid response received");
if (unionResult is map<value:Cloneable>) {
test:assertEquals(unionResult["message"], "Invalid JWT.", "Invalid error message received");
}
test:assertEquals(e.message(), "Internal Server Error", "Invalid error message received");
}

resource function get albums/[string title]/[string user]/[string...]() returns string|http:NotFound {
return "Hello, World!";
}
@test:Config {}
function testNegativeInvalidTokenArtifacts() returns error? {
http:Client rmClient = check new (testURL,
secureSocket = {
enable: false
},
auth = {
token: "Invalid-token"
}
);
cp:Artifacts|error node = rmClient->/management/services();
test:assertTrue(node is error, "Invalid response received");
error e = <error>node;
map<value:Cloneable> & readonly detail = e.detail();
value:Cloneable & readonly unionResult = detail["body"];
test:assertTrue(unionResult is map<value:Cloneable>, "Invalid response received");
if (unionResult is map<value:Cloneable>) {
test:assertEquals(unionResult["message"], "Invalid JWT.", "Invalid error message received");
}
test:assertEquals(e.message(), "Internal Server Error", "Invalid error message received");
}
2 changes: 1 addition & 1 deletion ballerina/icpService.bal
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ service /management on securedEP {
resource function get login(@http:Header string Authorization) returns AccessTokenResponse|error {
boolean isValid = check authenticateRequest(Authorization);
if (!isValid) {
return error("Invalid credentials provided");
return error http:ClientAuthError("Invalid credentials");
}
return {AccessToken: check generateJwtToken()};
}
Expand Down

0 comments on commit f30ff6d

Please sign in to comment.