From f30ff6d195b52a186be4a09787d13b203d0ae188 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Thu, 2 May 2024 20:57:40 +0530 Subject: [PATCH] Add negative tests --- .../artifacts-tests/tests/test.bal | 69 +++++++++++++++++-- ballerina/icpService.bal | 2 +- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/ballerina-tests/artifacts-tests/tests/test.bal b/ballerina-tests/artifacts-tests/tests/test.bal index da5fa44..c9f5cd3 100644 --- a/ballerina-tests/artifacts-tests/tests/test.bal +++ b/ballerina-tests/artifacts-tests/tests/test.bal @@ -15,6 +15,7 @@ // under the License. import ballerina/http; +import ballerina/lang.value; import ballerina/test; import ballerinax/wso2.controlplane as cp; @@ -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 = result; + map & readonly detail = e.detail(); + value:Cloneable & readonly unionResult = detail["body"]; + test:assertTrue(unionResult is map, "Invalid response received"); + if (unionResult is map) { + 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 = node; + map & readonly detail = e.detail(); + value:Cloneable & readonly unionResult = detail["body"]; + test:assertTrue(unionResult is map, "Invalid response received"); + if (unionResult is map) { + 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 = node; + map & readonly detail = e.detail(); + value:Cloneable & readonly unionResult = detail["body"]; + test:assertTrue(unionResult is map, "Invalid response received"); + if (unionResult is map) { + test:assertEquals(unionResult["message"], "Invalid JWT.", "Invalid error message received"); + } + test:assertEquals(e.message(), "Internal Server Error", "Invalid error message received"); } diff --git a/ballerina/icpService.bal b/ballerina/icpService.bal index a27339b..5bbae53 100644 --- a/ballerina/icpService.bal +++ b/ballerina/icpService.bal @@ -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()}; }