Skip to content

Commit

Permalink
Merge pull request #156 from Chilliwiddit/master
Browse files Browse the repository at this point in the history
Implement connector tests
  • Loading branch information
NipunaRanasinghe authored Jul 2, 2024
2 parents 522b8db + 73bba1e commit 21f67cd
Show file tree
Hide file tree
Showing 3 changed files with 373 additions and 0 deletions.
64 changes: 64 additions & 0 deletions ballerina/tests/README.md
Original file line number Diff line number Diff line change
@@ -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.

```toml
isTestOnLiveServer = true
token = "<your-slack-token>"
```

#### Using Environment Variables

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

```bash
export IS_LIVE_SERVER=true
export token = "<your-slack-token>"
```

Then, run the following command to run the tests:

```bash
./gradlew clean test -Pgroups="live_tests"
```
234 changes: 234 additions & 0 deletions ballerina/tests/mock_server.bal
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"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();
}
75 changes: 75 additions & 0 deletions ballerina/tests/test_cases.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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/log;
import ballerina/os;
import ballerina/test;

configurable boolean isLiveServer = os:getEnv("IS_LIVE_SERVER") == "true";
configurable string token = isLiveServer ? os:getEnv("SLACK_TOKEN") : "test";
configurable string serviceUrl = isLiveServer ? "https://slack.com/api" : "http://localhost:9090/";

ConnectionConfig slackConfig = {
auth: {
token
}
};

Client slack = test:mock(Client);

@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 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_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 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 slack->/users\.profile\.get();
test:assertTrue(check response.ok, "The ok attribute should be true");
}

0 comments on commit 21f67cd

Please sign in to comment.