-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from HussainLatiff/main
Add Test cases
- Loading branch information
Showing
9 changed files
with
222 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters