Skip to content

Commit

Permalink
added more mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
hasanaburayyan committed May 21, 2024
1 parent 927a9d4 commit 44b9bd5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
4 changes: 3 additions & 1 deletion slack/context.w
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ pub inflight class EventContext {
/// Internally used for mocking event context
pub inflight class EventContext_Mock extends EventContext {
pub thread: Thread;
pub channel: Channel;
new (rawContext: Json, botToken: str) {
super({}, "");
super(rawContext, "");
let callBackEvent = events.CallBackEvent.fromJson(rawContext["event"]);
this.thread = new Thread_Mock(callBackEvent.channel, callBackEvent.event_ts, botToken);
this.channel = new Channel_Mock(callBackEvent.channel, botToken);
}
}
36 changes: 36 additions & 0 deletions slack/lib.test.w
Original file line number Diff line number Diff line change
@@ -1,11 +1,47 @@
bring cloud;
bring expect;
bring http;
bring "./lib.w" as slack;
bring "./message.w" as msg;
bring "./events.w" as events;

let token = new cloud.Secret();
let app = new slack.App(botToken: token);

app.onEvent("app_mention", inflight (ctx, event) => {
// Have the call just return the response from posting to thread (for testing)
let res = ctx.thread.post("message");
return res;
});

test "app_mention event" {
let endpoint = app.api.url;
let callbackEvent: events.CallBackEvent = {
user: "FakeUser",
type: "app_mention",
ts: "00000000",
team: "FakeTeam",
event_ts: "0000000",
channel: "FakeChannel"
};

let slackEvent = {
type: "event_callback",
event: callbackEvent
};

let res = http.post("{endpoint}/slack/events", {
body: Json.stringify(slackEvent)
});

expect.equal(Json.parse(res.body),
{
status: 200,
body: "Totally sent that string to the thread :)"
}
);
}

test "test sending plain text to channel" {
let channel = app.channel("ABC123");
let res = channel.post("Hi");
Expand Down
20 changes: 13 additions & 7 deletions slack/lib.w
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ pub struct AppProps {


pub class App {
api: cloud.Api;
eventHandlers: MutMap<inflight(context.EventContext, Json):void>;
pub api: cloud.Api;
eventHandlers: MutMap<inflight(context.EventContext, Json):Json?>;
ignoreBots: bool;
botToken: cloud.Secret;

isTest: bool;

new(props: AppProps) {
this.eventHandlers = MutMap<inflight (context.EventContext, Json): void>{};
this.eventHandlers = MutMap<inflight (context.EventContext, Json): Json?>{};
this.ignoreBots = props?.ignoreBots ?? true;
this.botToken = props.botToken;
this.api = new cloud.Api();
Expand All @@ -52,26 +52,32 @@ pub class App {

if eventRequest.type == "event_callback" {
let callBackEvent = events.CallBackEvent.fromJson(Json.parse(req.body!)["event"]);

if this.ignoreBots {
if callBackEvent.bot_id != nil || callBackEvent.app_id != nil {
return {};
}
}

if let handler = this.eventHandlers.tryGet(callBackEvent.type) {
if this.isTest {
handler(new context.EventContext_Mock(Json.parse(req.body!), this.botToken.value()), Json.parse(req.body!));
return {
status: 200,
body: Json.stringify(handler(new context.EventContext_Mock(Json.parse(req.body!), ""), Json.parse(req.body!)))
};
} else {
// TODO: pass bot token as cloud.Secret rather than str once: https://github.com/winglang/winglibs/pull/229 is complete
handler(new context.EventContext(Json.parse(req.body!), this.botToken.value()), Json.parse(req.body!));
return {
status: 200,
body: Json.stringify(handler(new context.EventContext(Json.parse(req.body!), this.botToken.value()), Json.parse(req.body!)))
};
}
}
}
});
}

/// Register an event handler
pub onEvent(eventName: str, handler: inflight(context.EventContext, Json): void) {
pub onEvent(eventName: str, handler: inflight(context.EventContext, Json): Json?) {
this.eventHandlers.set(eventName, handler);
}

Expand Down

0 comments on commit 44b9bd5

Please sign in to comment.