From 8924ec05dcb9ea787a16291849ccc9a00c12a6dc Mon Sep 17 00:00:00 2001 From: Nivedhith Date: Fri, 28 Jun 2024 10:58:39 +0530 Subject: [PATCH 1/9] Add test cases --- ballerina/tests/test_cases.bal | 160 +++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 ballerina/tests/test_cases.bal diff --git a/ballerina/tests/test_cases.bal b/ballerina/tests/test_cases.bal new file mode 100644 index 0000000..329ac5f --- /dev/null +++ b/ballerina/tests/test_cases.bal @@ -0,0 +1,160 @@ +import ballerina/test; +import ballerina/io; + + +configurable string value = ""; + + +@test:Config{} +function testGetPresence() returns error?{//has to indicate that there is a possibility of an error if we have to have "check" + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/users\.getPresence();//the response is stored in a variable + io:println(response); + + var b = {"ok":true,"presence":"away","online":false,"auto_away":false,"manual_away":false,"connection_count":0}; //expected response + + boolean ok_attribute = check response.ok; + + test:assertTrue(ok_attribute, "The ok attribute was not equal to true");//testing responses + +} + +@test:Config{} +function testConversationsList() returns error?{ + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/conversations\.list(); + + boolean ok_attribute = check response.ok; + + test:assertFalse(ok_attribute, "The ok attribute was not equal to false"); +} + +@test:Config{} +function testMeMessage1() returns error?{ + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/chat\.meMessage.post({ channel : "general", text : "This is a Test" }); + + boolean ok_attribute = check response.ok; + + test:assertFalse(ok_attribute, "The ok attribute was not equal to false"); +} + +@test:Config{} +function testMeMessage2() returns error?{ + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/chat\.postMessage.post({ channel : "general", text: "This is a Test" }); + + string text = check response.message.text; + + test:assertEquals(text, "This is a Test", "The two texts were not the same"); +} + +@test:Config{} +function testScheduledMessages() returns error?{ + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/chat\.scheduledMessages\.list(); + + boolean ok_attribute = check response.ok; + + test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); +} + +@test:Config{} +function testPostMessage1() returns error?{ + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/chat\.postMessage.post({ channel : "general", text : "This is a Test" }); + + boolean ok_attribute = check response.ok; + + test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); +} + +@test:Config{} +function testPostMessage2() returns error?{ + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/chat\.postMessage.post({ channel : "general", text: "This is a Test" }); + + string text = check response.message.text; + + test:assertEquals(text, "This is a Test", "The two texts were not the same"); +} + +@test:Config{} +function testUsersList() returns error?{ + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/users\.list(); + + boolean ok_attribute = check response.ok; + + test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); +} + +@test:Config{} +function testConversationsInfo() returns error?{ + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/conversations\.info({ channel : "general"}); + + boolean ok_attribute = check response.ok; + + test:assertFalse(ok_attribute, "The ok attribute was not equal to false"); +} + +@test:Config{} +function testUsersProfileGet() returns error?{ + Client cl = check new({ + auth: { + token: value + } + }); + + json response = check cl->/users\.profile\.get(); + + boolean ok_attribute = check response.ok; + + test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); +} \ No newline at end of file From b23b0b1cc666f82a08b04bdd7c2eec585ec135ed Mon Sep 17 00:00:00 2001 From: Nivedhith Date: Fri, 28 Jun 2024 16:17:50 +0530 Subject: [PATCH 2/9] Create examples --- examples/README.md | 34 +++++++++++++++++++++++++ examples/build.sh | 47 +++++++++++++++++++++++++++++++++++ examples/companySurvey.bal | 20 +++++++++++++++ examples/productLaunch.bal | 22 ++++++++++++++++ examples/securityIncident.bal | 21 ++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 examples/README.md create mode 100644 examples/build.sh create mode 100644 examples/companySurvey.bal create mode 100644 examples/productLaunch.bal create mode 100644 examples/securityIncident.bal diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..28ec345 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,34 @@ +# Examples + +This directory contains a collection of sample code examples written in Ballerina. These examples demonstrate various +use cases of the module. You can follow the instructions below to build and run these examples. + +## Running an Example + +Execute the following commands to build an example from the source. + +* To build an example + + `bal build ` + + +* To run an example + + `bal run ` + +## Building the Examples with the Local Module + +**Warning**: Because of the absence of support for reading local repositories for single Ballerina files, the bala of +the module is manually written to the central repository as a workaround. Consequently, the bash script may modify your +local Ballerina repositories. + +Execute the following commands to build all the examples against the changes you have made to the module locally. + +* To build all the examples + + `./build.sh build` + + +* To run all the examples + + `./build.sh run` \ No newline at end of file diff --git a/examples/build.sh b/examples/build.sh new file mode 100644 index 0000000..a0a2729 --- /dev/null +++ b/examples/build.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +BAL_EXAMPLES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +BAL_CENTRAL_DIR="$HOME/.ballerina/repositories/central.ballerina.io/" +BAL_HOME_DIR="$BAL_EXAMPLES_DIR/../ballerina" + +set -e + +case "$1" in +build) + BAL_CMD="build" + ;; +run) + BAL_CMD="run" + ;; +*) + echo "Invalid command provided: '$1'. Please provide 'build' or 'run' as the command." + exit 1 + ;; +esac + +# Read Ballerina package name +slack=$(awk -F'"' '/^name/ {print $2}' "$BAL_HOME_DIR/Ballerina.toml") + +# Push the package to the local repository +cd "$BAL_HOME_DIR" && + bal pack && + bal push --repository=local + +# Remove the cache directories in the repositories +cacheDirs=($(ls -d "$BAL_CENTRAL_DIR"/cache-* 2>/dev/null)) +for dir in "${cacheDirs[@]}"; do + [ -d "$dir" ] && rm -r "$dir" +done +echo "Successfully cleaned the cache directories" + +# Update the central repository +BAL_DESTINATION_DIR="$HOME/.ballerina/repositories/central.ballerina.io/bala/ballerinax/$slack" +BAL_SOURCE_DIR="$HOME/.ballerina/repositories/local/bala/ballerinax/$slack" +[ -d "$BAL_DESTINATION_DIR" ] && rm -r "$BAL_DESTINATION_DIR" +[ -d "$BAL_SOURCE_DIR" ] && cp -r "$BAL_SOURCE_DIR" "$BAL_DESTINATION_DIR" +echo "Successfully updated the local central repositories" + +# Loop through examples in the examples directory +find "$BAL_EXAMPLES_DIR" -type f -name "*.bal" | while read -r BAL_EXAMPLE_FILE; do + bal "$BAL_CMD" --offline "$BAL_EXAMPLE_FILE" +done diff --git a/examples/companySurvey.bal b/examples/companySurvey.bal new file mode 100644 index 0000000..51cbee8 --- /dev/null +++ b/examples/companySurvey.bal @@ -0,0 +1,20 @@ +import ballerina/io; +import ballerinax/slack; + + +public function companySurvey() returns error?{ + slack:Client cl = check new({ + auth: { + token: value + } + }); + + json response1 = check cl->/conversations\.create.post({name : "survey-coordination"}); + io:println(response1); + + json response2 = check cl->/chat\.postEphemeral.post({channel : "survey-coordination" , user : "sample-user"}); + io:println(response2); + + json response3 = check cl->/conversations\.replies({channel : "survey-coordination", ts : "1234567890.123456"}); + io:println(response3); +} \ No newline at end of file diff --git a/examples/productLaunch.bal b/examples/productLaunch.bal new file mode 100644 index 0000000..2e1943a --- /dev/null +++ b/examples/productLaunch.bal @@ -0,0 +1,22 @@ +import ballerina/io; +import ballerinax/slack; + + +configurable string value = ""; + + public function productLaunch() returns error?{ + slack:Client cl = check new({ + auth: { + token: value + } + }); + + json response1 = check cl->/conversations\.create.post({name : "product-launch"}); + io:println(response1); + + json response2 = check cl->/conversations\.invite.post({channel: "product-launch", users: "sample-user"}); + io:println(response2); + + json response3 = check cl->/chat\.scheduleMessage.post({channel : "product-launch", text : "Merry Christmas!", post_at : "1735110000"}); + io:println(response3); + } \ No newline at end of file diff --git a/examples/securityIncident.bal b/examples/securityIncident.bal new file mode 100644 index 0000000..1e8a5d2 --- /dev/null +++ b/examples/securityIncident.bal @@ -0,0 +1,21 @@ +import ballerina/io; +import ballerinax/slack; + + +public function securityIncident() returns error?{ + slack:Client cl = check new({ + auth: { + token: value + } + }); + + json response1 = check cl->/conversations\.create.post({name : "incident-response"}); + io:println(response1); + + var response2 = check cl->/admin\.conversations\.restrictAccess\.addGroup.post({channel_id: "C123" , group_id: "123",token: value}); + io:println(response2); + + json response3 = check cl->/chat\.postMessage.post({channel : "incident-response" , text : "There has been a security incident"}); + io:println(response3); + +} \ No newline at end of file From ce391165884b3039c623bc0da9af4299aaec0a2b Mon Sep 17 00:00:00 2001 From: Nivedhith Date: Tue, 2 Jul 2024 11:36:51 +0530 Subject: [PATCH 3/9] Revert "Create examples" --- examples/README.md | 34 ------------------------- examples/build.sh | 47 ----------------------------------- examples/companySurvey.bal | 20 --------------- examples/productLaunch.bal | 22 ---------------- examples/securityIncident.bal | 21 ---------------- 5 files changed, 144 deletions(-) delete mode 100644 examples/README.md delete mode 100644 examples/build.sh delete mode 100644 examples/companySurvey.bal delete mode 100644 examples/productLaunch.bal delete mode 100644 examples/securityIncident.bal diff --git a/examples/README.md b/examples/README.md deleted file mode 100644 index 28ec345..0000000 --- a/examples/README.md +++ /dev/null @@ -1,34 +0,0 @@ -# Examples - -This directory contains a collection of sample code examples written in Ballerina. These examples demonstrate various -use cases of the module. You can follow the instructions below to build and run these examples. - -## Running an Example - -Execute the following commands to build an example from the source. - -* To build an example - - `bal build ` - - -* To run an example - - `bal run ` - -## Building the Examples with the Local Module - -**Warning**: Because of the absence of support for reading local repositories for single Ballerina files, the bala of -the module is manually written to the central repository as a workaround. Consequently, the bash script may modify your -local Ballerina repositories. - -Execute the following commands to build all the examples against the changes you have made to the module locally. - -* To build all the examples - - `./build.sh build` - - -* To run all the examples - - `./build.sh run` \ No newline at end of file diff --git a/examples/build.sh b/examples/build.sh deleted file mode 100644 index a0a2729..0000000 --- a/examples/build.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash - -BAL_EXAMPLES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -BAL_CENTRAL_DIR="$HOME/.ballerina/repositories/central.ballerina.io/" -BAL_HOME_DIR="$BAL_EXAMPLES_DIR/../ballerina" - -set -e - -case "$1" in -build) - BAL_CMD="build" - ;; -run) - BAL_CMD="run" - ;; -*) - echo "Invalid command provided: '$1'. Please provide 'build' or 'run' as the command." - exit 1 - ;; -esac - -# Read Ballerina package name -slack=$(awk -F'"' '/^name/ {print $2}' "$BAL_HOME_DIR/Ballerina.toml") - -# Push the package to the local repository -cd "$BAL_HOME_DIR" && - bal pack && - bal push --repository=local - -# Remove the cache directories in the repositories -cacheDirs=($(ls -d "$BAL_CENTRAL_DIR"/cache-* 2>/dev/null)) -for dir in "${cacheDirs[@]}"; do - [ -d "$dir" ] && rm -r "$dir" -done -echo "Successfully cleaned the cache directories" - -# Update the central repository -BAL_DESTINATION_DIR="$HOME/.ballerina/repositories/central.ballerina.io/bala/ballerinax/$slack" -BAL_SOURCE_DIR="$HOME/.ballerina/repositories/local/bala/ballerinax/$slack" -[ -d "$BAL_DESTINATION_DIR" ] && rm -r "$BAL_DESTINATION_DIR" -[ -d "$BAL_SOURCE_DIR" ] && cp -r "$BAL_SOURCE_DIR" "$BAL_DESTINATION_DIR" -echo "Successfully updated the local central repositories" - -# Loop through examples in the examples directory -find "$BAL_EXAMPLES_DIR" -type f -name "*.bal" | while read -r BAL_EXAMPLE_FILE; do - bal "$BAL_CMD" --offline "$BAL_EXAMPLE_FILE" -done diff --git a/examples/companySurvey.bal b/examples/companySurvey.bal deleted file mode 100644 index 51cbee8..0000000 --- a/examples/companySurvey.bal +++ /dev/null @@ -1,20 +0,0 @@ -import ballerina/io; -import ballerinax/slack; - - -public function companySurvey() returns error?{ - slack:Client cl = check new({ - auth: { - token: value - } - }); - - json response1 = check cl->/conversations\.create.post({name : "survey-coordination"}); - io:println(response1); - - json response2 = check cl->/chat\.postEphemeral.post({channel : "survey-coordination" , user : "sample-user"}); - io:println(response2); - - json response3 = check cl->/conversations\.replies({channel : "survey-coordination", ts : "1234567890.123456"}); - io:println(response3); -} \ No newline at end of file diff --git a/examples/productLaunch.bal b/examples/productLaunch.bal deleted file mode 100644 index 2e1943a..0000000 --- a/examples/productLaunch.bal +++ /dev/null @@ -1,22 +0,0 @@ -import ballerina/io; -import ballerinax/slack; - - -configurable string value = ""; - - public function productLaunch() returns error?{ - slack:Client cl = check new({ - auth: { - token: value - } - }); - - json response1 = check cl->/conversations\.create.post({name : "product-launch"}); - io:println(response1); - - json response2 = check cl->/conversations\.invite.post({channel: "product-launch", users: "sample-user"}); - io:println(response2); - - json response3 = check cl->/chat\.scheduleMessage.post({channel : "product-launch", text : "Merry Christmas!", post_at : "1735110000"}); - io:println(response3); - } \ No newline at end of file diff --git a/examples/securityIncident.bal b/examples/securityIncident.bal deleted file mode 100644 index 1e8a5d2..0000000 --- a/examples/securityIncident.bal +++ /dev/null @@ -1,21 +0,0 @@ -import ballerina/io; -import ballerinax/slack; - - -public function securityIncident() returns error?{ - slack:Client cl = check new({ - auth: { - token: value - } - }); - - json response1 = check cl->/conversations\.create.post({name : "incident-response"}); - io:println(response1); - - var response2 = check cl->/admin\.conversations\.restrictAccess\.addGroup.post({channel_id: "C123" , group_id: "123",token: value}); - io:println(response2); - - json response3 = check cl->/chat\.postMessage.post({channel : "incident-response" , text : "There has been a security incident"}); - io:println(response3); - -} \ No newline at end of file From f98477e807d07e93524a91be97d4c6f3eb4f8d69 Mon Sep 17 00:00:00 2001 From: Nivedhith Date: Tue, 2 Jul 2024 11:45:45 +0530 Subject: [PATCH 4/9] Shorten number of test cases from 10 to 5 --- ballerina/tests/test_cases.bal | 168 +++++++++------------------------ 1 file changed, 47 insertions(+), 121 deletions(-) diff --git a/ballerina/tests/test_cases.bal b/ballerina/tests/test_cases.bal index 329ac5f..755724a 100644 --- a/ballerina/tests/test_cases.bal +++ b/ballerina/tests/test_cases.bal @@ -1,127 +1,72 @@ -import ballerina/test; -import ballerina/io; +// 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/io; +import ballerina/os; +import ballerina/test; -configurable string value = ""; +configurable boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true"; +configurable string value = "token"; +Client cl = check new ({ + auth: { + token: value + } +}); -@test:Config{} -function testGetPresence() returns error?{//has to indicate that there is a possibility of an error if we have to have "check" - Client cl = check new({ - auth: { - token: value - } - }); +@test:Config { + groups: ["live_tests", "mock_tests"] +} +function testGetPresence() returns error? { //has to indicate that there is a possibility of an error if we have to have "check" - json response = check cl->/users\.getPresence();//the response is stored in a variable + json response = check cl->/users\.getPresence(); //the response is stored in a variable io:println(response); - var b = {"ok":true,"presence":"away","online":false,"auto_away":false,"manual_away":false,"connection_count":0}; //expected response - boolean ok_attribute = check response.ok; - test:assertTrue(ok_attribute, "The ok attribute was not equal to true");//testing responses + test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); //testing responses } -@test:Config{} -function testConversationsList() returns error?{ - Client cl = check new({ - auth: { - token: value - } - }); - - json response = check cl->/conversations\.list(); - - boolean ok_attribute = check response.ok; - - test:assertFalse(ok_attribute, "The ok attribute was not equal to false"); +@test:Config { + groups: ["live_tests", "mock_tests"] } - -@test:Config{} -function testMeMessage1() returns error?{ - Client cl = check new({ - auth: { - token: value - } - }); - - json response = check cl->/chat\.meMessage.post({ channel : "general", text : "This is a Test" }); - - boolean ok_attribute = check response.ok; - - test:assertFalse(ok_attribute, "The ok attribute was not equal to false"); -} - -@test:Config{} -function testMeMessage2() returns error?{ - Client cl = check new({ - auth: { - token: value - } - }); - - json response = check cl->/chat\.postMessage.post({ channel : "general", text: "This is a Test" }); - - string text = check response.message.text; - - test:assertEquals(text, "This is a Test", "The two texts were not the same"); -} - -@test:Config{} -function testScheduledMessages() returns error?{ - Client cl = check new({ - auth: { - token: value - } - }); - - json response = check cl->/chat\.scheduledMessages\.list(); +function testPostMessage_1() returns error? { + json response = check cl->/chat\.postMessage.post({channel: "general", text: "This is a Test"}); boolean ok_attribute = check response.ok; test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); } -@test:Config{} -function testPostMessage1() returns error?{ - Client cl = check new({ - auth: { - token: value - } - }); - - json response = check cl->/chat\.postMessage.post({ channel : "general", text : "This is a Test" }); - - boolean ok_attribute = check response.ok; - - test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); +@test:Config { + groups: ["live_tests", "mock_tests"] } - -@test:Config{} -function testPostMessage2() returns error?{ - Client cl = check new({ - auth: { - token: value - } - }); - - json response = check cl->/chat\.postMessage.post({ channel : "general", text: "This is a Test" }); +function testPostMessage_2() returns error? { + json response = check cl->/chat\.postMessage.post({channel: "general", text: "This is a Test"}); string text = check response.message.text; test:assertEquals(text, "This is a Test", "The two texts were not the same"); } -@test:Config{} -function testUsersList() returns error?{ - Client cl = check new({ - auth: { - token: value - } - }); - +@test:Config { + groups: ["live_tests", "mock_tests"] +} +function testUsersList() returns error? { json response = check cl->/users\.list(); boolean ok_attribute = check response.ok; @@ -129,32 +74,13 @@ function testUsersList() returns error?{ test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); } -@test:Config{} -function testConversationsInfo() returns error?{ - Client cl = check new({ - auth: { - token: value - } - }); - - json response = check cl->/conversations\.info({ channel : "general"}); - - boolean ok_attribute = check response.ok; - - test:assertFalse(ok_attribute, "The ok attribute was not equal to false"); +@test:Config { + groups: ["live_tests", "mock_tests"] } - -@test:Config{} -function testUsersProfileGet() returns error?{ - Client cl = check new({ - auth: { - token: value - } - }); - +function testUsersProfileGet() returns error? { json response = check cl->/users\.profile\.get(); boolean ok_attribute = check response.ok; test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); -} \ No newline at end of file +} From cf7d119dbc7fc002249c635d172bb27ac62b391e Mon Sep 17 00:00:00 2001 From: Nivedhith Date: Tue, 2 Jul 2024 11:46:42 +0530 Subject: [PATCH 5/9] Create ReadMe file --- ballerina/tests/README.md | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 ballerina/tests/README.md diff --git a/ballerina/tests/README.md b/ballerina/tests/README.md new file mode 100644 index 0000000..3905e09 --- /dev/null +++ b/ballerina/tests/README.md @@ -0,0 +1,64 @@ +# Running Tests + +There are two test environments for running the Slack connector tests. The default test environment is the mock server for the Slack API. The other test environment is the actual Slack 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 Slack API (Default Environment) | +| live_tests | Slack 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 Slack Live API + +#### Using a Config.toml File + +Create a `Config.toml` file in the tests directory and add your authentication credentials a + +```toml +isTestOnLiveServer = true +token = "" +``` + +#### Using Environment Variables + +Alternatively, you can set your authentication credentials as environment variables: + +```bash +export IS_LIVE_SERVER=true +export token = "" +``` + +Then, run the following command to run the tests: + +```bash + ./gradlew clean test -Pgroups="live_tests" +``` From 4cf7f0465d5c97a1de14229954b444d21e493869 Mon Sep 17 00:00:00 2001 From: Nivedhith Date: Tue, 2 Jul 2024 11:46:55 +0530 Subject: [PATCH 6/9] Create Mock Service --- ballerina/tests/mock_tests.bal | 243 +++++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 ballerina/tests/mock_tests.bal diff --git a/ballerina/tests/mock_tests.bal b/ballerina/tests/mock_tests.bal new file mode 100644 index 0000000..fd7e7fc --- /dev/null +++ b/ballerina/tests/mock_tests.bal @@ -0,0 +1,243 @@ +// 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 httpListener = new (9090); + +http:Service mockAPI = service object { + + # Get Presence + # + # + return - Get Presence + resource isolated function get users\.getPresence() returns json => { + response: { + "ok": true, + "presence": "away", + "online": false, + "auto_away": false, + "manual_away": false, + "connection_count": 0 + } + }; + + # Post Message + # + # + channel - The channel to post the message to + # + message - The message to be posted + # + return - json + resource isolated function post chat\.postMessage(@http:Payload string channel, string text) returns json => { + response: { + "ok": true, + "channel": "C078KJ7SW78", + "ts": "1719830298.784769", + "message": { + "user": "U078KJ7RX1U", + "type": "message", + "ts": "1719830298.784769", + "bot_id": "B07986E3N4E", + "app_id": "A079SKHD00Z", + "text": "This is a Test", + "team": "T078S42MC59", + "bot_profile": { + "id": "B07986E3N4E", + "app_id": "A079SKHD00Z", + "name": "test-app", + "icons": { + "image_36": "https://a.slack-edge.com/80588/img/plugins/app/bot_36.png", + "image_48": "https://a.slack-edge.com/80588/img/plugins/app/bot_48.png", + "image_72": "https://a.slack-edge.com/80588/img/plugins/app/service_72.png" + }, + "deleted": false, + "updated": 1719218992, + "team_id": "T078S42MC59" + }, + "blocks": [ + { + "type": "rich_text", + "block_id": "I/L", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "This is a Test" + } + ] + } + ] + } + ] + } + } + }; + + # List Users + # + # + return - List Users + resource isolated function get users\.list() returns json => { + response: { + "ok": true, + "members": [ + { + "id": "USLACKBOT", + "team_id": "T078S42MC59", + "name": "slackbot", + "deleted": false, + "color": "757575", + "real_name": "Slackbot", + "tz": "America/Los_Angeles", + "tz_label": "Pacific Daylight Time", + "tz_offset": -25200, + "profile": { + "title": "", + "phone": "", + "skype": "", + "real_name": "Slackbot", + "real_name_normalized": "Slackbot", + "display_name": "Slackbot", + "display_name_normalized": "Slackbot", + "fields": {}, + "status_text": "", + "status_emoji": "", + "status_emoji_display_info": [], + "status_expiration": 0, + "avatar_hash": "sv41d8cd98f0", + "always_active": true, + "first_name": "slackbot", + "last_name": "", + "image_24": "https://a.slack-edge.com/80588/img/slackbot_24.png", + "image_32": "https://a.slack-edge.com/80588/img/slackbot_32.png", + "image_48": "https://a.slack-edge.com/80588/img/slackbot_48.png", + "image_72": "https://a.slack-edge.com/80588/img/slackbot_72.png", + "image_192": "https://a.slack-edge.com/80588/marketing/img/avatars/slackbot/avatar-slackbot.png", + "image_512": "https://a.slack-edge.com/80588/img/slackbot_512.png", + "status_text_canonical": "", + "team": "T078S42MC59" + }, + "is_admin": false, + "is_owner": false, + "is_primary_owner": false, + "is_restricted": false, + "is_ultra_restricted": false, + "is_bot": false, + "is_app_user": false, + "updated": 0, + "is_email_confirmed": false, + "who_can_share_contact_card": "EVERYONE" + }, + { + "id": "U078KJ7RX1U", + "team_id": "T078S42MC59", + "name": "adib", + "deleted": false, + "color": "9f69e7", + "real_name": "Adib Samoon", + "tz": "Asia/Colombo", + "tz_label": "Sri Lanka Standard Time", + "tz_offset": 19800, + "profile": { + "title": "", + "phone": "", + "skype": "", + "real_name": "Adib Samoon", + "real_name_normalized": "Adib Samoon", + "display_name": "Adib Samoon", + "display_name_normalized": "Adib Samoon", + "fields": null, + "status_text": "", + "status_emoji": "", + "status_emoji_display_info": [], + "status_expiration": 0, + "avatar_hash": "g22cb1d82fff", + "email": "adib@wso2.com", + "first_name": "Adib", + "last_name": "Samoon", + "image_24": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-24.png", + "image_32": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-32.png", + "image_48": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-48.png", + "image_72": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-72.png", + "image_192": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-192.png", + "image_512": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-512.png", + "status_text_canonical": "", + "team": "T078S42MC59" + }, + "is_admin": true, + "is_owner": true, + "is_primary_owner": true, + "is_restricted": false, + "is_ultra_restricted": false, + "is_bot": false, + "is_app_user": false, + "updated": 1718859479, + "is_email_confirmed": true, + "has_2fa": false, + "who_can_share_contact_card": "EVERYONE" + } + ], + "cache_ts": 1719830510, + "response_metadata": { + "next_cursor": "" + } + } + }; + + # Get User Profile + # + # + return - Get User Profile + resource isolated function get users\.profile\.get() returns json => { + response: { + "ok": true, + "profile": { + "title": "", + "phone": "", + "skype": "", + "real_name": "Adib Samoon", + "real_name_normalized": "Adib Samoon", + "display_name": "Adib Samoon", + "display_name_normalized": "Adib Samoon", + "fields": {}, + "status_text": "", + "status_emoji": "", + "status_emoji_display_info": [], + "status_expiration": 0, + "avatar_hash": "g22cb1d82fff", + "email": "adib@wso2.com", + "first_name": "Adib", + "last_name": "Samoon", + "image_24": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-24.png", + "image_32": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-32.png", + "image_48": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-48.png", + "image_72": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-72.png", + "image_192": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-192.png", + "image_512": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-512.png", + "status_text_canonical": "" + } + } + }; +}; + +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 httpListener.attach(mockAPI, "/"); + check httpListener.'start(); +} From 576070982a4b513411763c1d6a3dbcc710616995 Mon Sep 17 00:00:00 2001 From: Nipuna Ranasinghe Date: Tue, 2 Jul 2024 14:17:30 +0530 Subject: [PATCH 7/9] Fix README issues --- ballerina/tests/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ballerina/tests/README.md b/ballerina/tests/README.md index 3905e09..dae5a42 100644 --- a/ballerina/tests/README.md +++ b/ballerina/tests/README.md @@ -5,7 +5,7 @@ There are two test environments for running the Slack connector tests. The defau 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 Slack API (Default Environment) | | live_tests | Slack API | @@ -41,7 +41,7 @@ Then, run the following command to run the tests: #### Using a Config.toml File -Create a `Config.toml` file in the tests directory and add your authentication credentials a +Create a `Config.toml` file in the tests directory and add your authentication credentials. ```toml isTestOnLiveServer = true From 306ad58fdb3299e117a0e91187bd98c828abec51 Mon Sep 17 00:00:00 2001 From: Nipuna Ranasinghe Date: Tue, 2 Jul 2024 14:20:13 +0530 Subject: [PATCH 8/9] Fix minor issues in the tests suite --- ballerina/tests/test_cases.bal | 63 ++++++++++++++-------------------- 1 file changed, 26 insertions(+), 37 deletions(-) diff --git a/ballerina/tests/test_cases.bal b/ballerina/tests/test_cases.bal index 755724a..06fe722 100644 --- a/ballerina/tests/test_cases.bal +++ b/ballerina/tests/test_cases.bal @@ -14,73 +14,62 @@ // specific language governing permissions and limitations // under the License. -import ballerina/io; +import ballerina/log; import ballerina/os; import ballerina/test; configurable boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true"; -configurable string value = "token"; +configurable string token = isLiveServer ? os:getEnv("SLACK_TOKEN") : "test"; +configurable string serviceUrl = isLiveServer ? "https://slack.com/api" : "http://localhost:9090/"; -Client cl = check new ({ +ConnectionConfig slackConfig = { auth: { - token: value + token } -}); +}; -@test:Config { - groups: ["live_tests", "mock_tests"] -} -function testGetPresence() returns error? { //has to indicate that there is a possibility of an error if we have to have "check" - - json response = check cl->/users\.getPresence(); //the response is stored in a variable - io:println(response); +Client slack = test:mock(Client); - boolean ok_attribute = check response.ok; - - test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); //testing responses +@test:BeforeSuite +function setup() returns error? { + if (isLiveServer) { + log:printInfo("Running tests on actual server"); + } else { + log:printInfo("Running tests on mock server"); + } + slack = check new (slackConfig, serviceUrl); } @test:Config { groups: ["live_tests", "mock_tests"] } -function testPostMessage_1() returns error? { - json response = check cl->/chat\.postMessage.post({channel: "general", text: "This is a Test"}); - - boolean ok_attribute = check response.ok; - - test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); +function testGetPresence() returns error? { + json response = check slack->/users\.getPresence(); + test:assertTrue(check response.ok, "The ok attribute was not equal to true"); } @test:Config { groups: ["live_tests", "mock_tests"] } -function testPostMessage_2() returns error? { - json response = check cl->/chat\.postMessage.post({channel: "general", text: "This is a Test"}); - - string text = check response.message.text; - - test:assertEquals(text, "This is a Test", "The two texts were not the same"); +function testPostMessage_1() returns error? { + json response = check slack->/chat\.postMessage.post({channel: "general", text: "This is a Test"}); + test:assertTrue(check response.ok, "The ok attribute should be true"); + test:assertEquals(check response.message.text, "This is a Test", "The message text is not equal to the expected value"); } @test:Config { groups: ["live_tests", "mock_tests"] } function testUsersList() returns error? { - json response = check cl->/users\.list(); - - boolean ok_attribute = check response.ok; - - test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); + json response = check slack->/users\.list(); + test:assertTrue(check response.ok, "The ok attribute should be true"); } @test:Config { groups: ["live_tests", "mock_tests"] } function testUsersProfileGet() returns error? { - json response = check cl->/users\.profile\.get(); - - boolean ok_attribute = check response.ok; - - test:assertTrue(ok_attribute, "The ok attribute was not equal to true"); + json response = check slack->/users\.profile\.get(); + test:assertTrue(check response.ok, "The ok attribute should be true"); } From 74ec8df190f34a9f88da04b12ab5755af3d071a5 Mon Sep 17 00:00:00 2001 From: Nipuna Ranasinghe Date: Tue, 2 Jul 2024 14:20:34 +0530 Subject: [PATCH 9/9] Fix mock server responses --- ballerina/tests/mock_server.bal | 234 ++++++++++++++++++++++++++++++ ballerina/tests/mock_tests.bal | 243 -------------------------------- 2 files changed, 234 insertions(+), 243 deletions(-) create mode 100644 ballerina/tests/mock_server.bal delete mode 100644 ballerina/tests/mock_tests.bal diff --git a/ballerina/tests/mock_server.bal b/ballerina/tests/mock_server.bal new file mode 100644 index 0000000..6e8697c --- /dev/null +++ b/ballerina/tests/mock_server.bal @@ -0,0 +1,234 @@ +// 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 httpListener = new (9090); + +http:Service mockAPI = service object { + + # Get Presence + # + # + return - Get Presence + resource isolated function get users\.getPresence() returns json => { + "ok": true, + "presence": "away", + "online": false, + "auto_away": false, + "manual_away": false, + "connection_count": 0 + }; + + # Post Message + # + # + channel - The channel to post the message to + # + return - json + resource isolated function post chat\.postMessage(@http:Payload string channel, string text) returns json => { + "ok": true, + "channel": "C078KJ7SW78", + "ts": "1719830298.784769", + "message": { + "user": "U078KJ7RX1U", + "type": "message", + "ts": "1719830298.784769", + "bot_id": "B07986E3N4E", + "app_id": "A079SKHD00Z", + "text": "This is a Test", + "team": "T078S42MC59", + "bot_profile": { + "id": "B07986E3N4E", + "app_id": "A079SKHD00Z", + "name": "test-app", + "icons": { + "image_36": "https://a.slack-edge.com/80588/img/plugins/app/bot_36.png", + "image_48": "https://a.slack-edge.com/80588/img/plugins/app/bot_48.png", + "image_72": "https://a.slack-edge.com/80588/img/plugins/app/service_72.png" + }, + "deleted": false, + "updated": 1719218992, + "team_id": "T078S42MC59" + }, + "blocks": [ + { + "type": "rich_text", + "block_id": "I/L", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "This is a Test" + } + ] + } + ] + } + ] + } + }; + + # List Users + # + # + return - List Users + resource isolated function get users\.list() returns json => { + "ok": true, + "members": [ + { + "id": "USLACKBOT", + "team_id": "T078S42MC59", + "name": "slackbot", + "deleted": false, + "color": "757575", + "real_name": "Slackbot", + "tz": "America/Los_Angeles", + "tz_label": "Pacific Daylight Time", + "tz_offset": -25200, + "profile": { + "title": "", + "phone": "", + "skype": "", + "real_name": "Slackbot", + "real_name_normalized": "Slackbot", + "display_name": "Slackbot", + "display_name_normalized": "Slackbot", + "fields": {}, + "status_text": "", + "status_emoji": "", + "status_emoji_display_info": [], + "status_expiration": 0, + "avatar_hash": "sv41d8cd98f0", + "always_active": true, + "first_name": "slackbot", + "last_name": "", + "image_24": "https://a.slack-edge.com/80588/img/slackbot_24.png", + "image_32": "https://a.slack-edge.com/80588/img/slackbot_32.png", + "image_48": "https://a.slack-edge.com/80588/img/slackbot_48.png", + "image_72": "https://a.slack-edge.com/80588/img/slackbot_72.png", + "image_192": "https://a.slack-edge.com/80588/marketing/img/avatars/slackbot/avatar-slackbot.png", + "image_512": "https://a.slack-edge.com/80588/img/slackbot_512.png", + "status_text_canonical": "", + "team": "T078S42MC59" + }, + "is_admin": false, + "is_owner": false, + "is_primary_owner": false, + "is_restricted": false, + "is_ultra_restricted": false, + "is_bot": false, + "is_app_user": false, + "updated": 0, + "is_email_confirmed": false, + "who_can_share_contact_card": "EVERYONE" + }, + { + "id": "U078KJ7RX1U", + "team_id": "T078S42MC59", + "name": "adib", + "deleted": false, + "color": "9f69e7", + "real_name": "Adib Samoon", + "tz": "Asia/Colombo", + "tz_label": "Sri Lanka Standard Time", + "tz_offset": 19800, + "profile": { + "title": "", + "phone": "", + "skype": "", + "real_name": "Adib Samoon", + "real_name_normalized": "Adib Samoon", + "display_name": "Adib Samoon", + "display_name_normalized": "Adib Samoon", + "fields": null, + "status_text": "", + "status_emoji": "", + "status_emoji_display_info": [], + "status_expiration": 0, + "avatar_hash": "g22cb1d82fff", + "email": "adib@wso2.com", + "first_name": "Adib", + "last_name": "Samoon", + "image_24": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-24.png", + "image_32": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-32.png", + "image_48": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-48.png", + "image_72": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-72.png", + "image_192": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-192.png", + "image_512": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-512.png", + "status_text_canonical": "", + "team": "T078S42MC59" + }, + "is_admin": true, + "is_owner": true, + "is_primary_owner": true, + "is_restricted": false, + "is_ultra_restricted": false, + "is_bot": false, + "is_app_user": false, + "updated": 1718859479, + "is_email_confirmed": true, + "has_2fa": false, + "who_can_share_contact_card": "EVERYONE" + } + ], + "cache_ts": 1719830510, + "response_metadata": { + "next_cursor": "" + } + }; + + # Get User Profile + # + # + return - Get User Profile + resource isolated function get users\.profile\.get() returns json => { + "ok": true, + "profile": { + "title": "", + "phone": "", + "skype": "", + "real_name": "Adib Samoon", + "real_name_normalized": "Adib Samoon", + "display_name": "Adib Samoon", + "display_name_normalized": "Adib Samoon", + "fields": {}, + "status_text": "", + "status_emoji": "", + "status_emoji_display_info": [], + "status_expiration": 0, + "avatar_hash": "g22cb1d82fff", + "email": "adib@wso2.com", + "first_name": "Adib", + "last_name": "Samoon", + "image_24": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-24.png", + "image_32": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-32.png", + "image_48": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-48.png", + "image_72": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-72.png", + "image_192": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-192.png", + "image_512": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-512.png", + "status_text_canonical": "" + } + }; +}; + +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 httpListener.attach(mockAPI, "/"); + check httpListener.'start(); +} diff --git a/ballerina/tests/mock_tests.bal b/ballerina/tests/mock_tests.bal deleted file mode 100644 index fd7e7fc..0000000 --- a/ballerina/tests/mock_tests.bal +++ /dev/null @@ -1,243 +0,0 @@ -// 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 httpListener = new (9090); - -http:Service mockAPI = service object { - - # Get Presence - # - # + return - Get Presence - resource isolated function get users\.getPresence() returns json => { - response: { - "ok": true, - "presence": "away", - "online": false, - "auto_away": false, - "manual_away": false, - "connection_count": 0 - } - }; - - # Post Message - # - # + channel - The channel to post the message to - # + message - The message to be posted - # + return - json - resource isolated function post chat\.postMessage(@http:Payload string channel, string text) returns json => { - response: { - "ok": true, - "channel": "C078KJ7SW78", - "ts": "1719830298.784769", - "message": { - "user": "U078KJ7RX1U", - "type": "message", - "ts": "1719830298.784769", - "bot_id": "B07986E3N4E", - "app_id": "A079SKHD00Z", - "text": "This is a Test", - "team": "T078S42MC59", - "bot_profile": { - "id": "B07986E3N4E", - "app_id": "A079SKHD00Z", - "name": "test-app", - "icons": { - "image_36": "https://a.slack-edge.com/80588/img/plugins/app/bot_36.png", - "image_48": "https://a.slack-edge.com/80588/img/plugins/app/bot_48.png", - "image_72": "https://a.slack-edge.com/80588/img/plugins/app/service_72.png" - }, - "deleted": false, - "updated": 1719218992, - "team_id": "T078S42MC59" - }, - "blocks": [ - { - "type": "rich_text", - "block_id": "I/L", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "This is a Test" - } - ] - } - ] - } - ] - } - } - }; - - # List Users - # - # + return - List Users - resource isolated function get users\.list() returns json => { - response: { - "ok": true, - "members": [ - { - "id": "USLACKBOT", - "team_id": "T078S42MC59", - "name": "slackbot", - "deleted": false, - "color": "757575", - "real_name": "Slackbot", - "tz": "America/Los_Angeles", - "tz_label": "Pacific Daylight Time", - "tz_offset": -25200, - "profile": { - "title": "", - "phone": "", - "skype": "", - "real_name": "Slackbot", - "real_name_normalized": "Slackbot", - "display_name": "Slackbot", - "display_name_normalized": "Slackbot", - "fields": {}, - "status_text": "", - "status_emoji": "", - "status_emoji_display_info": [], - "status_expiration": 0, - "avatar_hash": "sv41d8cd98f0", - "always_active": true, - "first_name": "slackbot", - "last_name": "", - "image_24": "https://a.slack-edge.com/80588/img/slackbot_24.png", - "image_32": "https://a.slack-edge.com/80588/img/slackbot_32.png", - "image_48": "https://a.slack-edge.com/80588/img/slackbot_48.png", - "image_72": "https://a.slack-edge.com/80588/img/slackbot_72.png", - "image_192": "https://a.slack-edge.com/80588/marketing/img/avatars/slackbot/avatar-slackbot.png", - "image_512": "https://a.slack-edge.com/80588/img/slackbot_512.png", - "status_text_canonical": "", - "team": "T078S42MC59" - }, - "is_admin": false, - "is_owner": false, - "is_primary_owner": false, - "is_restricted": false, - "is_ultra_restricted": false, - "is_bot": false, - "is_app_user": false, - "updated": 0, - "is_email_confirmed": false, - "who_can_share_contact_card": "EVERYONE" - }, - { - "id": "U078KJ7RX1U", - "team_id": "T078S42MC59", - "name": "adib", - "deleted": false, - "color": "9f69e7", - "real_name": "Adib Samoon", - "tz": "Asia/Colombo", - "tz_label": "Sri Lanka Standard Time", - "tz_offset": 19800, - "profile": { - "title": "", - "phone": "", - "skype": "", - "real_name": "Adib Samoon", - "real_name_normalized": "Adib Samoon", - "display_name": "Adib Samoon", - "display_name_normalized": "Adib Samoon", - "fields": null, - "status_text": "", - "status_emoji": "", - "status_emoji_display_info": [], - "status_expiration": 0, - "avatar_hash": "g22cb1d82fff", - "email": "adib@wso2.com", - "first_name": "Adib", - "last_name": "Samoon", - "image_24": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-24.png", - "image_32": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-32.png", - "image_48": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-48.png", - "image_72": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-72.png", - "image_192": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-192.png", - "image_512": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-512.png", - "status_text_canonical": "", - "team": "T078S42MC59" - }, - "is_admin": true, - "is_owner": true, - "is_primary_owner": true, - "is_restricted": false, - "is_ultra_restricted": false, - "is_bot": false, - "is_app_user": false, - "updated": 1718859479, - "is_email_confirmed": true, - "has_2fa": false, - "who_can_share_contact_card": "EVERYONE" - } - ], - "cache_ts": 1719830510, - "response_metadata": { - "next_cursor": "" - } - } - }; - - # Get User Profile - # - # + return - Get User Profile - resource isolated function get users\.profile\.get() returns json => { - response: { - "ok": true, - "profile": { - "title": "", - "phone": "", - "skype": "", - "real_name": "Adib Samoon", - "real_name_normalized": "Adib Samoon", - "display_name": "Adib Samoon", - "display_name_normalized": "Adib Samoon", - "fields": {}, - "status_text": "", - "status_emoji": "", - "status_emoji_display_info": [], - "status_expiration": 0, - "avatar_hash": "g22cb1d82fff", - "email": "adib@wso2.com", - "first_name": "Adib", - "last_name": "Samoon", - "image_24": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-24.png", - "image_32": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-32.png", - "image_48": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-48.png", - "image_72": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-72.png", - "image_192": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-192.png", - "image_512": "https://secure.gravatar.com/avatar/22cb1d82fffbdffa0fb80651f9afbb5a.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0025-512.png", - "status_text_canonical": "" - } - } - }; -}; - -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 httpListener.attach(mockAPI, "/"); - check httpListener.'start(); -}