Skip to content

Commit

Permalink
Add mock server along with updated test methods
Browse files Browse the repository at this point in the history
  • Loading branch information
HussainLatiff committed Jul 4, 2024
1 parent 657f635 commit a88c1e6
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 65 deletions.
34 changes: 33 additions & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.9.1"
distribution-version = "2201.9.0"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -205,6 +214,9 @@ dependencies = [
{org = "ballerina", name = "lang.value"},
{org = "ballerina", name = "observe"}
]
modules = [
{org = "ballerina", packageName = "log", moduleName = "log"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -248,6 +260,9 @@ dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "os", moduleName = "os"}
]

[[package]]
org = "ballerina"
Expand All @@ -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"
Expand Down Expand Up @@ -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"}
]
Expand Down
61 changes: 61 additions & 0 deletions ballerina/tests/mock_server.bal
Original file line number Diff line number Diff line change
@@ -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();
}
83 changes: 19 additions & 64 deletions ballerina/tests/tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit a88c1e6

Please sign in to comment.