Skip to content

Commit

Permalink
Fixed conversation update activity handler failing with infinite loop #…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbawaskar authored and PrasadG193 committed Sep 16, 2020
1 parent e07a0cf commit 3aad8b8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
22 changes: 22 additions & 0 deletions core/bot_framework_adapter_fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package core

import (
"context"

"github.com/infracloudio/msbotbuilder-go/connector/auth"
"github.com/infracloudio/msbotbuilder-go/schema"
)

// All the mocks and stubs for BotFrameworkAdapter goes here.

// MockTokenValidator stub for bypassing the authentication
type MockTokenValidator struct {
}

// AuthenticateRequest mock implementation for authentication
func (jv *MockTokenValidator) AuthenticateRequest(ctx context.Context, activity schema.Activity, authHeader string, credentials auth.CredentialProvider, channelService string) (auth.ClaimsIdentity, error) {
claims := map[string]interface{}{
"1": "1",
}
return auth.NewClaimIdentity(claims, true), nil
}
66 changes: 28 additions & 38 deletions core/bot_framework_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/infracloudio/msbotbuilder-go/connector/auth"
"github.com/infracloudio/msbotbuilder-go/connector/client"
"github.com/infracloudio/msbotbuilder-go/core"
"github.com/infracloudio/msbotbuilder-go/core/activity"
"github.com/infracloudio/msbotbuilder-go/schema"

"github.com/stretchr/testify/assert"
)

Expand All @@ -57,34 +58,8 @@ var customHandler = activity.HandlerFuncs{
},
}

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"),
AppPassword: os.Getenv("APP_PASSWORD"),
}

// Make an adapter to perform operations with the Bot Framework using this library.
adapter, err := core.NewBotAdapter(setting)
if err != nil {
log.Fatal(err)
}

// activity depicts a request as received from a client
activity := schema.Activity{
Type: schema.Message,
Expand All @@ -105,17 +80,32 @@ func TestExample(t *testing.T) {
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)
}
handler := http.HandlerFunc(processMessage)
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := context.Background()
setting := core.AdapterSetting{
AppID: "asdasd",
AppPassword: "cfg.MicrosoftTeams.AppPassword",
}
setting.CredentialProvider = auth.SimpleCredentialProvider{
AppID: setting.AppID,
Password: setting.AppPassword,
}
clientConfig, err := client.NewClientConfig(setting.CredentialProvider, auth.ToChannelFromBotLoginURL[0])
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
connectorClient, err := client.NewClient(clientConfig)
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
adapter := core.BotFrameworkAdapter{setting, &core.MockTokenValidator{}, connectorClient}
act, err := adapter.ParseRequest(ctx, req)
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
err = adapter.ProcessActivity(ctx, act, customHandler)
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
})
rr := httptest.NewRecorder()
bodyJson, _ := json.Marshal(activity)
bodyBytes := bytes.NewReader(bodyJson)
req, _ := http.NewRequest(http.MethodPost, "/api/messages", bodyBytes)
bodyJSON, err := json.Marshal(activity)
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
bodyBytes := bytes.NewReader(bodyJSON)
req, err := http.NewRequest(http.MethodPost, "/api/messages", bodyBytes)
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
req.Header.Set("Authorization", "Bearer abc123")
handler.ServeHTTP(rr, req)
assert.Equal(t, rr.Code, 200, "Expect 200 response status")
Expand Down

0 comments on commit 3aad8b8

Please sign in to comment.