diff --git a/README.md b/README.md index 2fbf1ec..33c6660 100644 --- a/README.md +++ b/README.md @@ -10,25 +10,25 @@ Follow these steps to create a Discord developer account. ### Step 1: Login to Discord developer page - +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. - +Create Discord Account Complete the account creation process by including the relavant information in the given fields. ### Step 2: Make a new Discord application - +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 - +Name and Create the App 1. Proceed by giving the Discord Application a name and click on the terms of service. @@ -36,7 +36,7 @@ Once in the Discord developer portal is open, click on the `New Application` but ### Step 4: Obtain the Client ID and Client Secret - +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. diff --git a/ballerina/Ballerina.toml b/ballerina/Ballerina.toml index 3d874d4..9ac9143 100644 --- a/ballerina/Ballerina.toml +++ b/ballerina/Ballerina.toml @@ -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 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/Module.md b/ballerina/Module.md index 6722000..0805781 100644 --- a/ballerina/Module.md +++ b/ballerina/Module.md @@ -10,25 +10,25 @@ Follow these steps to create a Discord developer account. ### Step 1: Login to Discord developer page - +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. - +Create Discord Account Complete the account creation process by including the relavant information in the given fields. ### Step 2: Make a new Discord application - +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 - +Name and Create the App 1. Proceed by giving the Discord Application a name and click on the terms of service. @@ -36,7 +36,7 @@ Once in the Discord developer portal is open, click on the `New Application` but ### Step 4: Obtain the Client ID and Client Secret - +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. diff --git a/ballerina/tests/README.md b/ballerina/tests/README.md new file mode 100644 index 0000000..9974934 --- /dev/null +++ b/ballerina/tests/README.md @@ -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 = "" +userId = "" +``` + +#### Using Environment Variables + +Alternatively, you can set your authentication credentials as environment variables: + +```bash +export IS_LIVE_SERVER=true +export TOKEN="" +export DISCORD_USER_ID="" +``` + +Then, run the following command to run the tests: + +```bash + ./gradlew clean test -Pgroups="live_tests" +``` \ No newline at end of file diff --git a/ballerina/tests/mock_server.bal b/ballerina/tests/mock_server.bal new file mode 100644 index 0000000..a7cef73 --- /dev/null +++ b/ballerina/tests/mock_server.bal @@ -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(); +} diff --git a/ballerina/tests/tests.bal b/ballerina/tests/tests.bal new file mode 100644 index 0000000..ff237e3 --- /dev/null +++ b/ballerina/tests/tests.bal @@ -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); +} diff --git a/ballerina/types.bal b/ballerina/types.bal index 9fd4cf6..fbf978e 100644 --- a/ballerina/types.bal +++ b/ballerina/types.bal @@ -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?; diff --git a/build-config/resources/Ballerina.toml b/build-config/resources/Ballerina.toml index d54d9d0..185261e 100644 --- a/build-config/resources/Ballerina.toml +++ b/build-config/resources/Ballerina.toml @@ -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