Skip to content

Commit

Permalink
Merge pull request #9 from HussainLatiff/main
Browse files Browse the repository at this point in the history
Add Test cases
  • Loading branch information
NipunaRanasinghe authored Jul 5, 2024
2 parents 5ce9bf5 + 7497e55 commit eb1de7d
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 14 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@ Follow these steps to create a Discord developer account.

### Step 1: Login to Discord developer page

<img src="https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/discord-dev-page.png?raw=true">
<img src="https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/discord-dev-page.png?raw=true" alt="Discord Dev Page">

Visit [Discord developer portal](https://discord.com/login?redirect_to=%2Fdevelopers) by logging into your Discord account.

If you do not have a Discord account already, [create a new discord account](https://discord.com/login) by clicking on the `Register` hyperlink below the `Log In` button when opening the Discord developer page.

<img src= "https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/create-acc.png?raw=true">
<img src= "https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/create-acc.png?raw=true" alt="Create Discord Account">

Complete the account creation process by including the relavant information in the given fields.

### Step 2: Make a new Discord application

<img src="https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/make-new-app.png?raw=true">
<img src="https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/make-new-app.png?raw=true" alt="Make New Application">

Once in the Discord developer portal is open, click on the `New Application` button as displayed above to start the process.

### Step 3: Name the Discord Application

<img src="https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/create-app.png?raw=true">
<img src="https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/create-app.png?raw=true" alt="Name and Create the App">

1. Proceed by giving the Discord Application a name and click on the terms of service.

2. Finally complete the naming process by clicking on the `next` button.

### Step 4: Obtain the Client ID and Client Secret

<img src="https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/obtain-client-id.png?raw=true">
<img src="https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/obtain-client-id.png?raw=true" alt="Obtain Client ID and Secret">

Under the `OAuth2` section found on the left-sided list, locate the Client's Information as shown on the screen. To implement the functionalities provided by Discord's API, you will need the Client ID and Client Secret.

Expand Down
2 changes: 1 addition & 1 deletion ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = ["Apache-2.0"]
authors = ["Ballerina"]
keywords = ["Communication/discord", "Cost/Free"]
icon = "icon.png"
repository = "https://github.com/HussainLatiff/module-ballerinax-discord"
repository = "https://github.com/ballerina-platform/module-ballerinax-discord"

[build-options]
observabilityIncluded = true
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
10 changes: 5 additions & 5 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@ Follow these steps to create a Discord developer account.

### Step 1: Login to Discord developer page

<img src="https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/discord-dev-page.png?raw=true">
<img src="https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/discord-dev-page.png?raw=true" alt="Discord Dev Page">

Visit [Discord developer portal](https://discord.com/login?redirect_to=%2Fdevelopers) by logging into your Discord account.

If you do not have a Discord account already, [create a new discord account](https://discord.com/login) by clicking on the `Register` hyperlink below the `Log In` button when opening the Discord developer page.

<img src= "https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/create-acc.png?raw=true">
<img src= "https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/create-acc.png?raw=true" alt="Create Discord Account">

Complete the account creation process by including the relavant information in the given fields.

### Step 2: Make a new Discord application

<img src="https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/make-new-app.png?raw=true">
<img src="https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/make-new-app.png?raw=true" alt="Make New Application">

Once in the Discord developer portal is open, click on the `New Application` button as displayed above to start the process.

### Step 3: Name the Discord Application

<img src="https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/create-app.png?raw=true">
<img src="https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/create-app.png?raw=true" alt="Name and Create the App">

1. Proceed by giving the Discord Application a name and click on the terms of service.

2. Finally complete the naming process by clicking on the `next` button.

### Step 4: Obtain the Client ID and Client Secret

<img src="https://github.com/HussainLatiff/module-ballerinax-discord/blob/main/docs/setup/resources/obtain-client-id.png?raw=true">
<img src="https://github.com/ballerina-platform/module-ballerinax-discord/blob/main/docs/setup/resources/obtain-client-id.png?raw=true" alt="Obtain Client ID and Secret">

Under the `OAuth2` section found on the left-sided list, locate the Client's Information as shown on the screen. To implement the functionalities provided by Discord's API, you will need the Client ID and Client Secret.

Expand Down
66 changes: 66 additions & 0 deletions ballerina/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Running Tests

There are two test environments for running the Discord connector tests. The default test environment is the mock server for Discord API. The other test environment is the actual Discord API.

You can run the tests in either of these environments and each has its own compatible set of tests.

Test Groups | Environment
-------------|---------------------------------------------------
mock_tests | Mock server for Discord API (Defualt Environment)
live_tests | Discord API

## Running Tests in the Mock Server

To execute the tests on the mock server, ensure that the `IS_LIVE_SERVER` environment variable is either set to `false` or unset before initiating the tests.

This environment variable can be configured within the `Config.toml` file located in the tests directory or specified as an environmental variable.

#### Using a Config.toml File

Create a `Config.toml` file in the tests directory and the following content:

```toml
isLiveServer = false
```

#### Using Environment Variables

Alternatively, you can set your authentication credentials as environment variables:

```bash
export IS_LIVE_SERVER=false
```

Then, run the following command to run the tests:

```bash
./gradlew clean test
```

## Running Tests Against Discord Live API

#### Using a Config.toml File

Create a `Config.toml` file in the tests directory and add your authentication credentials, to create a [Discord bearer token use this guide](https://github.com/discord-net/Discord.Net/blob/dev/docs/guides/bearer_token/bearer_token_guide.md).

```toml
isTestOnLiveServer = true
token = "<your-discord-bearer-token>"
userId = "<your-discord-user-id>"
```

#### Using Environment Variables

Alternatively, you can set your authentication credentials as environment variables:

```bash
export IS_LIVE_SERVER=true
export TOKEN="<your-discord-bearer-token>"
export DISCORD_USER_ID="<your-discord-user-id>"
```

Then, run the following command to run the tests:

```bash
./gradlew clean test -Pgroups="live_tests"
```
52 changes: 52 additions & 0 deletions ballerina/tests/mock_server.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 userId]() returns UserResponse|http:Response => {
id: userId,
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"
};

resource function get channels/[string channelId]/invites() returns anydata => ["Invite1", "Invite2"];

resource function get channels/[string channelId]/webhooks() returns anydata => ["Webhook1", "Webhook2"];
};

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();
}
58 changes: 58 additions & 0 deletions ballerina/tests/tests.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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/os;
import ballerina/test;

configurable boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true";
configurable string token = isLiveServer ? os:getEnv("TOKEN") : "z4HhmDy5ghijpIRL1YFzhCeVFabcdef";
configurable string serviceUrl = isLiveServer ? "https://discord.com/api/v10" : "http://localhost:9090";
configurable string userId = isLiveServer ? os:getEnv("DISCORD_USER_ID") :"688069266636800112";

string voiceChannelId = "1160951610135019725";
string channelId = "893493941398294611";

ConnectionConfig config = {
auth: {
token
}
};

final Client discord = check new Client(config, serviceUrl);

@test:Config {
groups: ["live_tests", "mock_tests"]
}
function testGetUser() returns error? {
UserResponse user = check discord->/users/[userId]();
test:assertEquals(user.id, userId);
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
function testGetVoiceChannel() returns error? {
anydata[] voiceChannels = check discord->/channels/[voiceChannelId]/invites();
test:assertTrue(voiceChannels.length() > 0);
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
function testGetWebhook() returns error? {
anydata[] channelWebhooks = check discord->/channels/[channelId]/webhooks();
test:assertTrue(channelWebhooks.length() > 0);
}
2 changes: 1 addition & 1 deletion ballerina/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,7 @@ public type UserResponse record {
string? avatar?;
string discriminator;
int:Signed32 public_flags;
@constraint:Int {minValue: 1, maxValue: -1}
@constraint:Int {minValue: -1, maxValue: 1}
int flags;
boolean? bot?;
boolean? system?;
Expand Down
2 changes: 1 addition & 1 deletion build-config/resources/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = ["Apache-2.0"]
authors = ["Ballerina"]
keywords = ["Communication/discord", "Cost/Free"]
icon = "icon.png"
repository = "https://github.com/HussainLatiff/module-ballerinax-discord"
repository = "https://github.com/ballerina-platform/module-ballerinax-discord"

[build-options]
observabilityIncluded = true

0 comments on commit eb1de7d

Please sign in to comment.