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

adds postephemeral handler to slacktest to audit outgoing messages #1105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
92 changes: 92 additions & 0 deletions slacktest/handlers.go
Expand Up @@ -211,6 +211,98 @@ func (sts *Server) postMessageHandler(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(resp)
}

// handle chat.postEphemeralMessage
func (sts *Server) postEphemeralHandler(w http.ResponseWriter, r *http.Request) {
serverAddr := r.Context().Value(ServerBotHubNameContextKey).(string)
data, err := ioutil.ReadAll(r.Body)
if err != nil {
msg := fmt.Sprintf("error reading body: %s", err.Error())
log.Printf(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
values, vErr := url.ParseQuery(string(data))
if vErr != nil {
msg := fmt.Sprintf("Unable to decode query params: %s", vErr.Error())
log.Printf(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}

ts := time.Now().Unix()
resp := &struct {
Ok bool `json:"ok"`
Channel string `json:"channel"`
Ts string `json:"ts"`
Text string `json:"text"`
}{
Ok: true,
Channel: values.Get("channel"),
Ts: fmt.Sprintf("%d", ts),
Text: values.Get("text"),
}

m := slack.Message{}
m.Type = "message"
m.Channel = values.Get("channel")
m.Timestamp = fmt.Sprintf("%d", ts)
m.Text = values.Get("text")
m.ThreadTimestamp = values.Get("thread_ts")
m.User = values.Get("user")
if values.Get("as_user") != "true" {
m.Username = defaultNonBotUserName
} else {
m.Username = BotNameFromContext(r.Context())
}
attachments := values.Get("attachments")
if attachments != "" {
decoded, err := url.QueryUnescape(attachments)
if err != nil {
msg := fmt.Sprintf("Unable to decode attachments: %s", err.Error())
log.Printf(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
var attaches []slack.Attachment
aJErr := json.Unmarshal([]byte(decoded), &attaches)
if aJErr != nil {
msg := fmt.Sprintf("Unable to decode attachments string to json: %s", aJErr.Error())
log.Printf(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
m.Attachments = attaches
}
blocks := values.Get("blocks")
if blocks != "" {
decoded, err := url.QueryUnescape(blocks)
if err != nil {
msg := fmt.Sprintf("Unable to decode blocks: %s", err.Error())
log.Printf(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
var decodedBlocks slack.Blocks
dbJErr := json.Unmarshal([]byte(decoded), &decodedBlocks)
if dbJErr != nil {
msg := fmt.Sprintf("Unable to decode blocks string to json: %s", dbJErr.Error())
log.Printf(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
m.Blocks = decodedBlocks
}
jsonMessage, jsonErr := json.Marshal(m)
if jsonErr != nil {
msg := fmt.Sprintf("Unable to marshal message: %s", jsonErr.Error())
log.Printf(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
}
go sts.queueForWebsocket(string(jsonMessage), serverAddr)
_ = json.NewEncoder(w).Encode(resp)
}

// RTMConnectHandler generates a valid connection
func RTMConnectHandler(w http.ResponseWriter, r *http.Request) {
_, err := ioutil.ReadAll(r.Body)
Expand Down
10 changes: 10 additions & 0 deletions slacktest/handlers_test.go
Expand Up @@ -32,6 +32,16 @@ func TestPostMessageHandler(t *testing.T) {
assert.NotEmpty(t, tstamp, "timestamp should not be empty")
}

func TestPostEphemeralHandler(t *testing.T) {
s := NewTestServer()
go s.Start()

client := slack.New("ABCDEFG", slack.OptionAPIURL(s.GetAPIURL()))
tstamp, err := client.PostEphemeral("fake_channel", "fake_user", slack.MsgOptionText("some ephemeral text", false), slack.MsgOptionPostMessageParameters(slack.PostMessageParameters{}))
assert.NoError(t, err, "should not error out")
assert.NotEmpty(t, tstamp, "timestamp should not be empty")
}

func TestServerCreateConversationHandler(t *testing.T) {
s := NewTestServer()
go s.Start()
Expand Down
1 change: 1 addition & 0 deletions slacktest/server.go
Expand Up @@ -50,6 +50,7 @@ func NewTestServer(custom ...binder) *Server {
s.Handle("/rtm.start", rtmStartHandler)
s.Handle("/rtm.connect", RTMConnectHandler)
s.Handle("/chat.postMessage", s.postMessageHandler)
s.Handle("/chat.postEphemeral", s.postEphemeralHandler)
s.Handle("/conversations.create", createConversationHandler)
s.Handle("/conversations.setTopic", setConversationTopicHandler)
s.Handle("/conversations.setPurpose", setConversationPurposeHandler)
Expand Down