@@ -24,15 +24,16 @@ import (
24
24
"context"
25
25
"encoding/json"
26
26
"fmt"
27
- "log"
28
27
"net/http"
29
28
"net/http/httptest"
30
- "os"
31
29
"testing"
32
30
31
+ "github.com/infracloudio/msbotbuilder-go/connector/auth"
32
+ "github.com/infracloudio/msbotbuilder-go/connector/client"
33
33
"github.com/infracloudio/msbotbuilder-go/core"
34
34
"github.com/infracloudio/msbotbuilder-go/core/activity"
35
35
"github.com/infracloudio/msbotbuilder-go/schema"
36
+
36
37
"github.com/stretchr/testify/assert"
37
38
)
38
39
@@ -57,34 +58,8 @@ var customHandler = activity.HandlerFuncs{
57
58
},
58
59
}
59
60
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
- }
74
61
func TestExample (t * testing.T ) {
75
62
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
-
88
63
// activity depicts a request as received from a client
89
64
activity := schema.Activity {
90
65
Type : schema .Message ,
@@ -105,17 +80,32 @@ func TestExample(t *testing.T) {
105
80
ServiceURL : srv .URL ,
106
81
}
107
82
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
+ })
115
103
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 ))
119
109
req .Header .Set ("Authorization" , "Bearer abc123" )
120
110
handler .ServeHTTP (rr , req )
121
111
assert .Equal (t , rr .Code , 200 , "Expect 200 response status" )
0 commit comments