Skip to content

Commit

Permalink
Added MS teams mock server for bot framework testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sbawaskar authored and PrasadG193 committed Sep 16, 2020
1 parent 248c740 commit e07a0cf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
65 changes: 53 additions & 12 deletions core/bot_framework_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,59 @@
package core_test

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/infracloudio/msbotbuilder-go/core"
"github.com/infracloudio/msbotbuilder-go/core/activity"
"github.com/infracloudio/msbotbuilder-go/schema"
"github.com/stretchr/testify/assert"
)

func Example() {
func serverMock() *httptest.Server {
handler := http.NewServeMux()
handler.HandleFunc("/v3/conversations/abcd1234/activities", msTeamsMockMock)

srv := httptest.NewServer(handler)

return srv
}

func msTeamsMockMock(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("{\"id\":\"1\"}"))
}

// Create a handler that defines operations to be performed on respective events.
// Following defines the operation to be performed on the 'message' event.
var customHandler = activity.HandlerFuncs{
OnMessageFunc: func(turn *activity.TurnContext) (schema.Activity, error) {
return turn.SendActivity(activity.MsgOptionText("Echo: " + turn.Activity.Text))
},
}

func processMessage(w http.ResponseWriter, req *http.Request) {
ctx := context.Background()
setting := core.AdapterSetting{
AppID: "asdasd",
AppPassword: "cfg.MicrosoftTeams.AppPassword",
}
adapter, err := core.NewBotAdapter(setting)
act, err := adapter.ParseRequest(ctx, req)
err = adapter.ProcessActivity(ctx, act, customHandler)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
func TestExample(t *testing.T) {
srv := serverMock()
// Load settings from environment variables to AdapterSetting.
setting := core.AdapterSetting{
AppID: os.Getenv("APP_ID"),
Expand All @@ -44,14 +85,6 @@ func Example() {
log.Fatal(err)
}

// Create a handler that defines operations to be performed on respective events.
// Following defines the operation to be performed on the 'message' event.
var customHandler = activity.HandlerFuncs{
OnMessageFunc: func(turn *activity.TurnContext) (schema.Activity, error) {
return turn.SendActivity(activity.MsgOptionText("Echo: " + turn.Activity.Text))
},
}

// activity depicts a request as received from a client
activity := schema.Activity{
Type: schema.Message,
Expand All @@ -67,15 +100,23 @@ func Example() {
ID: "1234abcd",
Name: "SteveW",
},
Text: "Message from Teams Client",
ReplyToID: "5d5cdc723",
Text: "Message from Teams Client",
ReplyToID: "5d5cdc723",
ServiceURL: srv.URL,
}

// Pass the activity and handler to the adapter for proecssing
ctx := context.Background()
err = adapter.ProcessActivity(ctx, activity, customHandler)
if err != nil {
fmt.Println("Failed to process request", err)
return
}
handler := http.HandlerFunc(processMessage)
rr := httptest.NewRecorder()
bodyJson, _ := json.Marshal(activity)
bodyBytes := bytes.NewReader(bodyJson)
req, _ := http.NewRequest(http.MethodPost, "/api/messages", bodyBytes)
req.Header.Set("Authorization", "Bearer abc123")
handler.ServeHTTP(rr, req)
assert.Equal(t, rr.Code, 200, "Expect 200 response status")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/lestrrat-go/jwx v0.9.0
github.com/pkg/errors v0.9.0
github.com/stretchr/testify v1.4.0 // indirect
github.com/stretchr/testify v1.4.0
)

0 comments on commit e07a0cf

Please sign in to comment.