Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for UnsupportedOperationException in user_change event #407

Merged
merged 1 commit into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.slack.api.bolt.middleware.builtin;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -16,13 +24,6 @@
import com.slack.api.model.event.MemberJoinedChannelEvent;
import com.slack.api.model.event.MemberLeftChannelEvent;
import com.slack.api.util.json.GsonFactory;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* Filters some events that may be generated by this app for Events API.
Expand Down Expand Up @@ -75,7 +76,14 @@ public Response apply(Request req, Response resp, MiddlewareChain chain) throws
JsonObject eventElem = extractEventElem(eventRequest.getRequestBodyAsString());
if (eventElem != null) {
JsonElement eventUserIdElem = eventElem.get("user");
String eventBotUserId = eventUserIdElem != null ? eventUserIdElem.getAsString() : null;
String eventBotUserId = null;
if (eventUserIdElem != null) {
if (eventUserIdElem.isJsonObject()) {
eventBotUserId = eventUserIdElem.getAsJsonObject().get("id").getAsString();
} else {
eventBotUserId = eventUserIdElem.getAsString();
}
}
JsonElement botIdElem = eventElem.get("bot_id");
if (eventBotUserId == null && botIdElem != null) {
String botId = botIdElem.getAsString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
package test_locally.middleware;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static util.MockSlackApi.InvalidToken;
import static util.MockSlackApi.ValidToken;

import util.MockSlackApiServer;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.slack.api.Slack;
import com.slack.api.SlackConfig;
import com.slack.api.app_backend.events.payload.MemberJoinedChannelPayload;
import com.slack.api.app_backend.events.payload.MessagePayload;
import com.slack.api.app_backend.events.payload.UserChangePayload;
import com.slack.api.bolt.middleware.MiddlewareChain;
import com.slack.api.bolt.middleware.builtin.IgnoringSelfEvents;
import com.slack.api.bolt.request.RequestHeaders;
import com.slack.api.bolt.request.builtin.EventRequest;
import com.slack.api.bolt.response.Response;
import com.slack.api.methods.MethodsClient;
import com.slack.api.model.User;
import com.slack.api.model.event.MemberJoinedChannelEvent;
import com.slack.api.model.event.MessageEvent;
import com.slack.api.model.event.UserChangeEvent;
import com.slack.api.util.json.GsonFactory;
import org.junit.Test;
import util.MockSlackApiServer;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;
import static util.MockSlackApi.InvalidToken;
import static util.MockSlackApi.ValidToken;

public class IgnoringSelfEventsTest {

Expand Down Expand Up @@ -77,6 +84,30 @@ public String findAndSaveBotUserId(MethodsClient client, String botId) {
assertEquals(404L, result.getStatusCode().longValue());
}

@Test
public void ignored_withUserObject() throws Exception {
IgnoringSelfEvents middleware = new IgnoringSelfEvents(SlackConfig.DEFAULT) {
@Override
public String findAndSaveBotUserId(MethodsClient client, String botId) {
return "U123BOT";
}
};
Map<String, List<String>> rawHeaders = new HashMap<>();
RequestHeaders headers = new RequestHeaders(rawHeaders);
UserChangePayload payload = new UserChangePayload();
payload.setTeamId("T123");
UserChangeEvent event = new UserChangeEvent();
User user = new User();
user.setId("U123BOT");
event.setUser(user);
payload.setEvent(event);
EventRequest req = new EventRequest(GsonFactory.createSnakeCase().toJson(payload), headers);
req.getContext().setBotUserId("U123BOT");
Response resp = new Response();
Response result = middleware.apply(req, resp, chain);
assertEquals(200L, result.getStatusCode().longValue());
}

@Test
public void not_ignored_no_botUserId() throws Exception {
IgnoringSelfEvents middleware = new IgnoringSelfEvents(SlackConfig.DEFAULT) {
Expand Down