Repository navigation
Expand file tree
/
Copy pathSocketModeAppTest.java
More file actions
156 lines (141 loc) · 5.64 KB
/
Copy pathSocketModeAppTest.java
File metadata and controls
156 lines (141 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package test_locally;
import com.slack.api.Slack;
import com.slack.api.SlackConfig;
import com.slack.api.bolt.App;
import com.slack.api.bolt.AppConfig;
import com.slack.api.bolt.response.Response;
import com.slack.api.bolt.socket_mode.SocketModeApp;
import com.slack.api.model.event.AppMentionEvent;
import com.slack.api.socket_mode.SocketModeClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.socket_mode.MockWebApiServer;
import util.socket_mode.MockWebSocketServer;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.*;
public class SocketModeAppTest {
static final String VALID_BOT_TOKEN = "xoxb-valid-123123123123123123123123123123123123";
static final String VALID_APP_TOKEN = "xapp-valid-123123123123123123123123123123123123";
MockWebSocketServer wsServer = new MockWebSocketServer();
MockWebApiServer webApiServer = new MockWebApiServer();
SlackConfig config = new SlackConfig();
Slack slack = Slack.getInstance(config);
@Before
public void setup() throws Exception {
webApiServer.start();
wsServer.start();
config.setMethodsEndpointUrlPrefix(webApiServer.getMethodsEndpointPrefix());
}
@After
public void tearDown() throws Exception {
webApiServer.stop();
wsServer.stop();
}
// -------------------------------------------------
// Default implementation
// -------------------------------------------------
@Test
public void payloadHandling() throws Exception {
App app = new App(AppConfig.builder()
.slack(slack)
.singleTeamBotToken(VALID_BOT_TOKEN)
.build());
AtomicBoolean commandCalled = new AtomicBoolean(false);
AtomicBoolean actionCalled = new AtomicBoolean(false);
AtomicBoolean eventCalled = new AtomicBoolean(false);
AtomicBoolean requestObserved = new AtomicBoolean(false);
app.command("/hi-socket-mode", (req, ctx) -> {
assertTrue(requestObserved.get());
commandCalled.set(true);
return ctx.ack("Hello!");
});
app.blockAction("a", (req, ctx) -> {
actionCalled.set(true);
return Response.builder().body("Thanks").build();
});
app.event(AppMentionEvent.class, (req, ctx) -> {
eventCalled.set(ctx.getRetryNum() == 2 && ctx.getRetryReason().equals("timeout"));
return ctx.ack();
});
SocketModeApp socketModeApp = new SocketModeApp(VALID_APP_TOKEN, app);
app.use((request, response, chain) -> {
assertTrue(request.isSocketMode());
assertNotNull(request.getSocketModeEnvelopeId());
assertNotNull(request.getSocketModeDispatchDelayNanos());
assertTrue(request.getSocketModeDispatchDelayNanos() >= 0);
requestObserved.set(true);
return chain.next(request);
});
socketModeApp.startAsync();
try {
Thread.sleep(3_000L);
assertTrue(commandCalled.get());
assertTrue(actionCalled.get());
assertTrue(eventCalled.get());
} finally {
socketModeApp.stop();
}
}
@Test
public void issue_937_it_should_not_be_connected_before_start_method_call() throws Exception {
App app = new App(AppConfig.builder()
.slack(slack)
.singleTeamBotToken(VALID_BOT_TOKEN)
.build());
SocketModeApp socketModeApp = new SocketModeApp(VALID_APP_TOKEN, app);
try {
assertTrue(socketModeApp.isClientStopped());
assertNull(socketModeApp.getClient());
// Check again to make sure if the auto-connection does not work in this scenario
Thread.sleep(3_000L);
assertTrue(socketModeApp.isClientStopped());
assertNull(socketModeApp.getClient());
} finally {
socketModeApp.close();
}
}
// -------------------------------------------------
// JavaWebSocket implementation
// -------------------------------------------------
@Test
public void payloadHandling_JavaWebSocket() throws Exception {
App app = new App(AppConfig.builder()
.slack(slack)
.singleTeamBotToken(VALID_BOT_TOKEN)
.build());
AtomicBoolean commandCalled = new AtomicBoolean(false);
AtomicBoolean actionCalled = new AtomicBoolean(false);
AtomicBoolean requestObserved = new AtomicBoolean(false);
app.command("/hi-socket-mode", (req, ctx) -> {
assertTrue(requestObserved.get());
commandCalled.set(true);
return ctx.ack("Hello!");
});
app.blockAction("a", (req, ctx) -> {
actionCalled.set(true);
return Response.builder().body("Thanks").build();
});
SocketModeApp socketModeApp = new SocketModeApp(
VALID_APP_TOKEN,
SocketModeClient.Backend.JavaWebSocket,
app
);
app.use((request, response, chain) -> {
assertTrue(request.isSocketMode());
assertNotNull(request.getSocketModeEnvelopeId());
assertNotNull(request.getSocketModeDispatchDelayNanos());
assertTrue(request.getSocketModeDispatchDelayNanos() >= 0);
requestObserved.set(true);
return chain.next(request);
});
socketModeApp.startAsync();
try {
Thread.sleep(3_000L);
assertTrue(commandCalled.get());
assertTrue(actionCalled.get());
} finally {
socketModeApp.stop();
}
}
}