Skip to content

Commit 3aad8b8

Browse files
sbawaskarPrasadG193
authored andcommitted
Fixed conversation update activity handler failing with infinite loop #47
1 parent e07a0cf commit 3aad8b8

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

core/bot_framework_adapter_fake.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package core
2+
3+
import (
4+
"context"
5+
6+
"github.com/infracloudio/msbotbuilder-go/connector/auth"
7+
"github.com/infracloudio/msbotbuilder-go/schema"
8+
)
9+
10+
// All the mocks and stubs for BotFrameworkAdapter goes here.
11+
12+
// MockTokenValidator stub for bypassing the authentication
13+
type MockTokenValidator struct {
14+
}
15+
16+
// AuthenticateRequest mock implementation for authentication
17+
func (jv *MockTokenValidator) AuthenticateRequest(ctx context.Context, activity schema.Activity, authHeader string, credentials auth.CredentialProvider, channelService string) (auth.ClaimsIdentity, error) {
18+
claims := map[string]interface{}{
19+
"1": "1",
20+
}
21+
return auth.NewClaimIdentity(claims, true), nil
22+
}

core/bot_framework_adapter_test.go

+28-38
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ import (
2424
"context"
2525
"encoding/json"
2626
"fmt"
27-
"log"
2827
"net/http"
2928
"net/http/httptest"
30-
"os"
3129
"testing"
3230

31+
"github.com/infracloudio/msbotbuilder-go/connector/auth"
32+
"github.com/infracloudio/msbotbuilder-go/connector/client"
3333
"github.com/infracloudio/msbotbuilder-go/core"
3434
"github.com/infracloudio/msbotbuilder-go/core/activity"
3535
"github.com/infracloudio/msbotbuilder-go/schema"
36+
3637
"github.com/stretchr/testify/assert"
3738
)
3839

@@ -57,34 +58,8 @@ var customHandler = activity.HandlerFuncs{
5758
},
5859
}
5960

60-
func processMessage(w http.ResponseWriter, req *http.Request) {
61-
ctx := context.Background()
62-
setting := core.AdapterSetting{
63-
AppID: "asdasd",
64-
AppPassword: "cfg.MicrosoftTeams.AppPassword",
65-
}
66-
adapter, err := core.NewBotAdapter(setting)
67-
act, err := adapter.ParseRequest(ctx, req)
68-
err = adapter.ProcessActivity(ctx, act, customHandler)
69-
if err != nil {
70-
http.Error(w, err.Error(), http.StatusBadRequest)
71-
return
72-
}
73-
}
7461
func TestExample(t *testing.T) {
7562
srv := serverMock()
76-
// Load settings from environment variables to AdapterSetting.
77-
setting := core.AdapterSetting{
78-
AppID: os.Getenv("APP_ID"),
79-
AppPassword: os.Getenv("APP_PASSWORD"),
80-
}
81-
82-
// Make an adapter to perform operations with the Bot Framework using this library.
83-
adapter, err := core.NewBotAdapter(setting)
84-
if err != nil {
85-
log.Fatal(err)
86-
}
87-
8863
// activity depicts a request as received from a client
8964
activity := schema.Activity{
9065
Type: schema.Message,
@@ -105,17 +80,32 @@ func TestExample(t *testing.T) {
10580
ServiceURL: srv.URL,
10681
}
10782

108-
// Pass the activity and handler to the adapter for proecssing
109-
ctx := context.Background()
110-
err = adapter.ProcessActivity(ctx, activity, customHandler)
111-
if err != nil {
112-
fmt.Println("Failed to process request", err)
113-
}
114-
handler := http.HandlerFunc(processMessage)
83+
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
84+
ctx := context.Background()
85+
setting := core.AdapterSetting{
86+
AppID: "asdasd",
87+
AppPassword: "cfg.MicrosoftTeams.AppPassword",
88+
}
89+
setting.CredentialProvider = auth.SimpleCredentialProvider{
90+
AppID: setting.AppID,
91+
Password: setting.AppPassword,
92+
}
93+
clientConfig, err := client.NewClientConfig(setting.CredentialProvider, auth.ToChannelFromBotLoginURL[0])
94+
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
95+
connectorClient, err := client.NewClient(clientConfig)
96+
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
97+
adapter := core.BotFrameworkAdapter{setting, &core.MockTokenValidator{}, connectorClient}
98+
act, err := adapter.ParseRequest(ctx, req)
99+
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
100+
err = adapter.ProcessActivity(ctx, act, customHandler)
101+
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
102+
})
115103
rr := httptest.NewRecorder()
116-
bodyJson, _ := json.Marshal(activity)
117-
bodyBytes := bytes.NewReader(bodyJson)
118-
req, _ := http.NewRequest(http.MethodPost, "/api/messages", bodyBytes)
104+
bodyJSON, err := json.Marshal(activity)
105+
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
106+
bodyBytes := bytes.NewReader(bodyJSON)
107+
req, err := http.NewRequest(http.MethodPost, "/api/messages", bodyBytes)
108+
assert.Nil(t, err, fmt.Sprintf("Failed with error %s", err))
119109
req.Header.Set("Authorization", "Bearer abc123")
120110
handler.ServeHTTP(rr, req)
121111
assert.Equal(t, rr.Code, 200, "Expect 200 response status")

0 commit comments

Comments
 (0)