diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index d905f28..241180e 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -5,7 +5,7 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.9.1" +distribution-version = "2201.9.0" [[package]] org = "ballerina" @@ -147,6 +147,15 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "lang.error" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"} +] + [[package]] org = "ballerina" name = "lang.int" @@ -205,6 +214,9 @@ dependencies = [ {org = "ballerina", name = "lang.value"}, {org = "ballerina", name = "observe"} ] +modules = [ + {org = "ballerina", packageName = "log", moduleName = "log"} +] [[package]] org = "ballerina" @@ -248,6 +260,9 @@ dependencies = [ {org = "ballerina", name = "io"}, {org = "ballerina", name = "jballerina.java"} ] +modules = [ + {org = "ballerina", packageName = "os", moduleName = "os"} +] [[package]] org = "ballerina" @@ -258,6 +273,20 @@ dependencies = [ {org = "ballerina", name = "time"} ] +[[package]] +org = "ballerina" +name = "test" +version = "0.0.0" +scope = "testOnly" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.array"}, + {org = "ballerina", name = "lang.error"} +] +modules = [ + {org = "ballerina", packageName = "test", moduleName = "test"} +] + [[package]] org = "ballerina" name = "time" @@ -296,7 +325,10 @@ version = "0.1.0" dependencies = [ {org = "ballerina", name = "constraint"}, {org = "ballerina", name = "http"}, + {org = "ballerina", name = "log"}, {org = "ballerina", name = "mime"}, + {org = "ballerina", name = "os"}, + {org = "ballerina", name = "test"}, {org = "ballerina", name = "url"}, {org = "ballerinai", name = "observe"} ] diff --git a/ballerina/tests/mock_server.bal b/ballerina/tests/mock_server.bal new file mode 100644 index 0000000..83775ac --- /dev/null +++ b/ballerina/tests/mock_server.bal @@ -0,0 +1,61 @@ +// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org). +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +import ballerina/http; +import ballerina/log; + +listener http:Listener httpServer = new(9090); + + +http:Service mockService = service object { + +resource function get users/[string user_id]() returns UserResponse|http:Response { + UserResponse response = { + id: "688069266636800112", + username: "exampleUser", + discriminator: "1234", + public_flags: 0, + flags: 1, + bot: false, + system: false, + avatar: "avatar_url", + banner: "banner_url", + accent_color: 16777215, + global_name: "globalExampleUser" + }; + return response; +} +resource function get channels/[string channel_id]/invites() returns anydata { + anydata response = [1, "string", true]; + return response; + } + +resource function get channels/[string channel_id]/webhooks() returns anydata { + anydata response = [1, "string", true]; + return response; + } + +}; + + function init() returns error? { + if isLiveServer { + log:printInfo("Skiping mock server initialization as the tests are running on live server"); + return; + } + log:printInfo("Initiating mock server"); + check httpServer.attach(mockService, "/"); + check httpServer.'start(); +} \ No newline at end of file diff --git a/ballerina/tests/tests.bal b/ballerina/tests/tests.bal index 7055b81..5adee66 100644 --- a/ballerina/tests/tests.bal +++ b/ballerina/tests/tests.bal @@ -15,79 +15,34 @@ // under the License. import ballerina/test; +import ballerina/os; -configurable string clientId = "Client ID"; -configurable string clientSecret = "Client Secret"; -configurable string[] scopes = ?; -@test:Config{} -function testGetToken() returns error? { - Client discord =check new({ - auth: { - clientId: clientId, - clientSecret: clientSecret, - scopes: ["identify"] - } - }); - - OAuth2GetKeys getToken = check discord->/oauth2/keys(); -} +configurable boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true"; +configurable string CliId = isLiveServer ? os:getEnv("Client_ID") : "test"; +configurable string CliScre = isLiveServer ? os:getEnv("Client_Secret") : "test"; +configurable string serviceUrl = isLiveServer ? os:getEnv("DISCORD_URL") : "http://localhost:9090/"; +ConnectionConfig config = {auth:{ + token: "BearerToken" +}}; +final Client discord = check new Client(config, serviceUrl); @test:Config{} function testGetUser() returns error? { - string user_id = "User ID"; - Client discord = check new({ - auth: { - clientId: clientId, - clientSecret: clientSecret, - scopes: ["identify", "email"] - } - }); - + string user_id = "User Id"; UserResponse getUserInfo = check discord->/users/[user_id](); + test:assertTrue((getUserInfo.length()> 0)); } -// Test case for the endpoint to retrieve details about a specific guild @test:Config{} -function testGetGuild() returns error? { - string guild_id = "Guild ID"; - Client discord = check new({ - auth: { - clientId: clientId, - clientSecret: clientSecret, - scopes: ["guilds"] - } - }); - - PrivateGuildMemberResponse getGuildInfo = check discord->/users/\@me/guilds/[guild_id]/member(); +function testGetVoiceChannel() returns error? { + string channel_id = "Voice Channel ID"; + anydata getVoiceChannelInfo = check discord->/channels/[channel_id]/invites(); + test:assertTrue((getVoiceChannelInfo.count()> 0)); } -// Test case for the endpoint to edit a message @test:Config{} -function testGetChannel() returns error? { - string channel_id = "Text Channel ID"; - string message_id = "Message ID"; - Client discord = check new({ - auth: { - clientId: clientId, - clientSecret: clientSecret, - scopes: ["guilds"] - } - }); - - MessageResponse getGuildChannel = check discord->/channels/[channel_id]/messages/[message_id](); +function testGetWebhook() returns error? { + string channel_id = "Channel ID"; + anydata getWebhookInfo = check discord->/channels/[channel_id]/webhooks(); + test:assertTrue((getWebhookInfo.count()> 0)); } - -// Test case for the endpoint to create a voice channel invite -@test:Config{} -function testGetVoiceChannel() returns error? { - string channel_id = "Voice Channel ID"; - Client discord = check new({ - auth: { - clientId: clientId, - clientSecret: clientSecret, - scopes: ["voice"] - } - }); - - anydata getVoiceChannelInfo = check discord->/channels/[channel_id]/invites(); -} \ No newline at end of file