Skip to content

Commit

Permalink
Update mock_service and test ballerina files
Browse files Browse the repository at this point in the history
  • Loading branch information
vish-mv committed Jul 2, 2024
1 parent aa484ff commit 22f461d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
2 changes: 0 additions & 2 deletions ballerina/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ To do this, refer to [Ballerina Twitter Connector](https://github.com/ballerina-
And You need Find Your User ID For run some of the tests.
Via This Website you can find it [Twitter ID Finder](https://twiteridfinder.com/).


# Running Tests

There are two test environments for running the Twitter(X) connector tests. The default test environment is the mock server for Twitter API. The other test environment is the actual Twitter (X) API.
Expand Down
44 changes: 14 additions & 30 deletions ballerina/tests/mock_service.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
// specific language governing permissions and limitations
// under the License.


import ballerina/http;
import ballerina/log;

listener http:Listener httpListener = new (9090);

http:Service mockService = service object {


# Remove a bookmarked Post
#
# + id - The ID of the authenticated source User whose bookmark is to be removed.
Expand All @@ -31,10 +29,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function delete users/[UserIdMatchesAuthenticatedUser id]/bookmarks/[TweetId tweet_id]() returns BookmarkMutationResponse|http:Response {
BookmarkMutationResponse response = {
return {
"data":{"bookmarked":false}
};
return response;
}

# Causes the User (in the path) to unlike the specified Post
Expand All @@ -45,10 +42,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function delete users/[UserIdMatchesAuthenticatedUser id]/likes/[TweetId tweet_id]() returns UsersLikesDeleteResponse|http:Response {
UsersLikesDeleteResponse response = {
return {
"data":{"liked":false}
};
return response;
}

# Causes the User (in the path) to unretweet the specified Post
Expand All @@ -59,10 +55,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function delete users/[UserIdMatchesAuthenticatedUser id]/retweets/[TweetId source_tweet_id]() returns UsersRetweetsDeleteResponse|http:Response {
UsersRetweetsDeleteResponse response = {
return {
"data":{"retweeted":false}
};
return response;
}

# Unfollow User
Expand All @@ -73,10 +68,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function delete users/[UserIdMatchesAuthenticatedUser source_user_id]/following/[UserId target_user_id]() returns UsersFollowingDeleteResponse|http:Response {
UsersFollowingDeleteResponse response = {
return {
"data":{"following":false}
};
return response;
}

# Unmute User by User ID
Expand All @@ -87,10 +81,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function delete users/[UserIdMatchesAuthenticatedUser source_user_id]/muting/[UserId target_user_id]() returns MuteUserMutationResponse|http:Response {
MuteUserMutationResponse response = {
return {
"data":{"muting":false}
};
return response;
}

# Post lookup by Post ID
Expand All @@ -106,17 +99,15 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function get tweets/[TweetId id](("attachments"|"author_id"|"card_uri"|"context_annotations"|"conversation_id"|"created_at"|"edit_controls"|"edit_history_tweet_ids"|"entities"|"geo"|"id"|"in_reply_to_user_id"|"lang"|"non_public_metrics"|"note_tweet"|"organic_metrics"|"possibly_sensitive"|"promoted_metrics"|"public_metrics"|"referenced_tweets"|"reply_settings"|"scopes"|"source"|"text"|"username"|"withheld")[]? tweet\.fields, ("attachments.media_keys"|"attachments.media_source_tweet"|"attachments.poll_ids"|"author_id"|"edit_history_tweet_ids"|"entities.mentions.username"|"geo.place_id"|"in_reply_to_user_id"|"entities.note.mentions.username"|"referenced_tweets.id"|"referenced_tweets.id.author_id"|"author_screen_name")[]? expansions, ("alt_text"|"duration_ms"|"height"|"media_key"|"non_public_metrics"|"organic_metrics"|"preview_image_url"|"promoted_metrics"|"public_metrics"|"type"|"url"|"variants"|"width")[]? media\.fields, ("duration_minutes"|"end_datetime"|"id"|"options"|"voting_status")[]? poll\.fields, ("connection_status"|"created_at"|"description"|"entities"|"id"|"location"|"most_recent_tweet_id"|"name"|"pinned_tweet_id"|"profile_image_url"|"protected"|"public_metrics"|"receives_your_dm"|"subscription_type"|"url"|"username"|"verified"|"verified_type"|"withheld")[]? user\.fields, ("contained_within"|"country"|"country_code"|"full_name"|"geo"|"id"|"name"|"place_type")[]? place\.fields) returns Get2TweetsIdResponse|http:Response {
Get2TweetsIdResponse response = {
return {
"data":{"edit_history_tweet_ids":["1806286701704462623"],"id":"1806286701704462623","text":"aasbcascbasjbc"}
};
return response;
}

resource function get users/'by/username/[string username](("connection_status"|"created_at"|"description"|"entities"|"id"|"location"|"most_recent_tweet_id"|"name"|"pinned_tweet_id"|"profile_image_url"|"protected"|"public_metrics"|"receives_your_dm"|"subscription_type"|"url"|"username"|"verified"|"verified_type"|"withheld")[]? user\.fields, ("most_recent_tweet_id"|"pinned_tweet_id")[]? expansions, ("attachments"|"author_id"|"card_uri"|"context_annotations"|"conversation_id"|"created_at"|"edit_controls"|"edit_history_tweet_ids"|"entities"|"geo"|"id"|"in_reply_to_user_id"|"lang"|"non_public_metrics"|"note_tweet"|"organic_metrics"|"possibly_sensitive"|"promoted_metrics"|"public_metrics"|"referenced_tweets"|"reply_settings"|"scopes"|"source"|"text"|"username"|"withheld")[]? tweet\.fields) returns Get2UsersByUsernameUsernameResponse|http:Response {
Get2UsersByUsernameUsernameResponse response = {
return {
"data":{"id":"350224247","name":"Kumar Sangakkara","username":"KumarSanga2"}
};
return response;
}

# Creation of a Post
Expand All @@ -125,10 +116,9 @@ http:Service mockService = service object {
# http:Created (The request has succeeded.)
# http:Response (The request has failed.)
resource function post tweets(@http:Payload TweetCreateRequest payload) returns TweetCreateResponse|http:Response {
TweetCreateResponse response = {
return {
"data":{"id":"1807808193139204482","text":"Twitter Test at[1719850035,0.227505100]","edit_history_tweet_ids":["1807808193139204482"]}
};
return response;
}

# Add Post to Bookmarks
Expand All @@ -138,10 +128,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function post users/[UserIdMatchesAuthenticatedUser id]/bookmarks(@http:Payload BookmarkAddRequest payload) returns BookmarkMutationResponse|http:Response {
BookmarkMutationResponse response ={
return {
"data":{"bookmarked":true}
};
return response;
}

# Follow User
Expand All @@ -151,10 +140,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function post users/[UserIdMatchesAuthenticatedUser id]/following(@http:Payload UsersFollowingCreateRequest payload) returns UsersFollowingCreateResponse|http:Response {
UsersFollowingCreateResponse response = {
return {
"data":{"following":true,"pending_follow":false}
};
return response;
}

# Causes the User (in the path) to like the specified Post
Expand All @@ -164,10 +152,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function post users/[UserIdMatchesAuthenticatedUser id]/likes(@http:Payload UsersLikesCreateRequest payload) returns UsersLikesCreateResponse|http:Response {
UsersLikesCreateResponse response = {
return {
"data":{"liked":true}
};
return response;
}

# Mute User by User ID.
Expand All @@ -177,10 +164,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function post users/[UserIdMatchesAuthenticatedUser id]/muting(@http:Payload MuteUserRequest payload) returns MuteUserMutationResponse|http:Response {
MuteUserMutationResponse response = {
return {
"data":{"muting":true}
};
return response;
}

# Causes the User (in the path) to repost the specified Post.
Expand All @@ -190,10 +176,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function post users/[UserIdMatchesAuthenticatedUser id]/retweets(@http:Payload UsersRetweetsCreateRequest payload) returns UsersRetweetsCreateResponse|http:Response {
UsersRetweetsCreateResponse response ={
return {
"data":{"retweeted":true,"rest_id":"1807808194787590411"}
};
return response;
}

# User lookup by IDs
Expand All @@ -206,10 +191,9 @@ http:Service mockService = service object {
# http:Ok (The request has succeeded.)
# http:Response (The request has failed.)
resource function get users(UserId[] ids, ("connection_status"|"created_at"|"description"|"entities"|"id"|"location"|"most_recent_tweet_id"|"name"|"pinned_tweet_id"|"profile_image_url"|"protected"|"public_metrics"|"receives_your_dm"|"subscription_type"|"url"|"username"|"verified"|"verified_type"|"withheld")[]? user\.fields, ("most_recent_tweet_id"|"pinned_tweet_id")[]? expansions, ("attachments"|"author_id"|"card_uri"|"context_annotations"|"conversation_id"|"created_at"|"edit_controls"|"edit_history_tweet_ids"|"entities"|"geo"|"id"|"in_reply_to_user_id"|"lang"|"non_public_metrics"|"note_tweet"|"organic_metrics"|"possibly_sensitive"|"promoted_metrics"|"public_metrics"|"referenced_tweets"|"reply_settings"|"scopes"|"source"|"text"|"username"|"withheld")[]? tweet\.fields) returns Get2UsersResponse|http:Response {
Get2UsersResponse response = {
return {
"data":[{"id":"350224247","name":"Kumar Sangakkara","username":"KumarSanga2"}]
};
return response;
}
};

Expand Down
17 changes: 16 additions & 1 deletion ballerina/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ configurable string userId = isLiveServer ? os:getEnv("TWITTER_USER_ID") : "test
configurable string token = isLiveServer ? os:getEnv("TWITTER_TOKEN") : "test";
configurable string serviceUrl = isLiveServer ? "https://api.twitter.com/2" : "http://localhost:9090";

ConnectionConfig config = {auth: {token: token}};
ConnectionConfig config = {auth: {token}};
final Client twitter = check new Client(config, serviceUrl);

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testPostTweet() returns error? {
time:Utc utc = time:utcNow();
Expand All @@ -39,6 +40,7 @@ isolated function testPostTweet() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testgetUserIdByUseName() returns error? {
Get2UsersByUsernameUsernameResponse response = check twitter->/users/'by/username/["KumarSanga2"];
Expand All @@ -47,6 +49,7 @@ isolated function testgetUserIdByUseName() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testUserLikeAPost() returns error? {
UsersLikesCreateResponse response = check twitter->/users/[userId]/likes.post(
Expand All @@ -59,6 +62,7 @@ isolated function testUserLikeAPost() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testUserUnlikeAPost() returns error? {
UsersLikesDeleteResponse response = check twitter->/users/[userId]/likes/["1806286701704462623"].delete();
Expand All @@ -67,6 +71,7 @@ isolated function testUserUnlikeAPost() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testPostLookup() returns error? {
Get2TweetsIdResponse response = check twitter->/tweets/["1806286701704462623"]();
Expand All @@ -75,6 +80,7 @@ isolated function testPostLookup() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testBookmarkPost() returns error? {
BookmarkMutationResponse response = check twitter->/users/[userId]/bookmarks.post(
Expand All @@ -85,6 +91,7 @@ isolated function testBookmarkPost() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testBookmarkDelete() returns error? {
BookmarkMutationResponse response = check twitter->/users/[userId]/bookmarks/["1806286701704462623"].delete();
Expand All @@ -93,6 +100,7 @@ isolated function testBookmarkDelete() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testRetweet() returns error? {
UsersRetweetsCreateResponse response = check twitter->/users/[userId]/retweets.post(
Expand All @@ -103,6 +111,7 @@ isolated function testRetweet() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testDeleteRetweet() returns error? {
UsersRetweetsDeleteResponse response = check twitter->/users/[userId]/retweets/["1806286701704462623"].delete();
Expand All @@ -111,6 +120,7 @@ isolated function testDeleteRetweet() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testFollowSpecificUser() returns error? {
UsersFollowingCreateResponse response = check twitter->/users/[userId]/following.post(
Expand All @@ -123,6 +133,7 @@ isolated function testFollowSpecificUser() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function testUnfollowSpecificUser() returns error? {
UsersFollowingDeleteResponse response = check twitter->/users/[userId]/following/["1803011651249278976"].delete();
Expand All @@ -131,6 +142,7 @@ isolated function testUnfollowSpecificUser() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function muteSpecificUser() returns error? {
MuteUserMutationResponse response = check twitter->/users/[userId]/muting.post(
Expand All @@ -143,14 +155,17 @@ isolated function muteSpecificUser() returns error? {
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}

isolated function unmuteSpecificUser() returns error? {
MuteUserMutationResponse response = check twitter->/users/[userId]/muting/["1803011651249278976"].delete();
test:assertTrue(response?.data !is ());
test:assertTrue(response?.errors is ());
}

@test:Config {
groups: ["live_tests", "mock_tests"]
}
isolated function findSpecificUser() returns error? {
Get2UsersResponse response = check twitter->/users(ids = ["1803011651249278976"]);
Expand Down

0 comments on commit 22f461d

Please sign in to comment.