Generalize event.

This commit is contained in:
François de Metz 2016-06-28 19:32:41 +02:00
parent 4176375480
commit 75be330318
4 changed files with 40 additions and 6 deletions

View File

@ -31,7 +31,7 @@ func (fs *FilestackWebhook) eventHandler(w http.ResponseWriter, r *http.Request)
return return
} }
event := &DialogEvent{} event := &FilestackEvent{}
err = json.Unmarshal(body, event) err = json.Unmarshal(body, event)
if err != nil { if err != nil {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)

View File

@ -2,20 +2,20 @@ package filestack
import "strconv" import "strconv"
type DialogEvent struct { type FilestackEvent struct {
Action string `json:"action"` Action string `json:"action"`
TimeStamp int64 `json:"timestamp"` TimeStamp int64 `json:"timestamp"`
Id int `json:"id"` Id int `json:"id"`
} }
func (de *DialogEvent) Tags() map[string]string { func (fe *FilestackEvent) Tags() map[string]string {
return map[string]string{ return map[string]string{
"action": de.Action, "action": fe.Action,
} }
} }
func (de *DialogEvent) Fields() map[string]interface{} { func (fe *FilestackEvent) Fields() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"id": strconv.Itoa(de.Id), "id": strconv.Itoa(fe.Id),
} }
} }

View File

@ -39,3 +39,18 @@ func DialogOpenJSON() string {
} }
}` }`
} }
func UploadJSON() string {
return `{
"action":"fp.upload",
"timestamp":1443444905,
"id":100946,
"text":{
"url":"https://www.filestackapi.com/api/file/WAunDTTqQfCNWwUUyf6n",
"client":"Facebook",
"type":"image/jpeg",
"filename":"1579337399020824.jpg",
"size":139154
}
}`
}

View File

@ -44,3 +44,22 @@ func TestParseError(t *testing.T) {
t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusBadRequest) t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusBadRequest)
} }
} }
func TestUploadEvent(t *testing.T) {
var acc testutil.Accumulator
fs := &FilestackWebhook{Path: "/filestack", acc: &acc}
resp := postWebhooks(fs, UploadJSON())
if resp.Code != http.StatusOK {
t.Errorf("POST returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
}
fields := map[string]interface{}{
"id": "100946",
}
tags := map[string]string{
"action": "fp.upload",
}
acc.AssertContainsTaggedFields(t, "filestack_webhooks", fields, tags)
}