1
0
mirror of synced 2024-11-22 04:26:01 +03:00
This commit is contained in:
Alex Lushpai 2018-09-13 23:19:23 +03:00
parent 450b287a70
commit f0612fa90d

View File

@ -53,85 +53,85 @@ func TestRouting_connectHandler(t *testing.T) {
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK)) fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
} }
func TestRouting_addBotHandler(t *testing.T) { // func TestRouting_addBotHandler(t *testing.T) {
defer gock.Off() // defer gock.Off()
//
ch := v1.Channel{ // ch := v1.Channel{
Type: "telegram", // Type: "telegram",
Settings: v1.ChannelSettings{ // Settings: v1.ChannelSettings{
SpamAllowed: false, // SpamAllowed: false,
Status: v1.Status{ // Status: v1.Status{
Delivered: v1.ChannelFeatureSend, // Delivered: v1.ChannelFeatureSend,
Read: v1.ChannelFeatureNone, // Read: v1.ChannelFeatureNone,
}, // },
Text: v1.ChannelSettingsText{ // Text: v1.ChannelSettingsText{
Creating: v1.ChannelFeatureBoth, // Creating: v1.ChannelFeatureBoth,
Editing: v1.ChannelFeatureBoth, // Editing: v1.ChannelFeatureBoth,
Quoting: v1.ChannelFeatureBoth, // Quoting: v1.ChannelFeatureBoth,
Deleting: v1.ChannelFeatureReceive, // Deleting: v1.ChannelFeatureReceive,
}, // },
Product: v1.Product{ // Product: v1.Product{
Creating: v1.ChannelFeatureReceive, // Creating: v1.ChannelFeatureReceive,
Editing: v1.ChannelFeatureReceive, // Editing: v1.ChannelFeatureReceive,
}, // },
Order: v1.Order{ // Order: v1.Order{
Creating: v1.ChannelFeatureReceive, // Creating: v1.ChannelFeatureReceive,
Editing: v1.ChannelFeatureReceive, // Editing: v1.ChannelFeatureReceive,
}, // },
}, // },
} // }
//
outgoing, _ := json.Marshal(ch) // outgoing, _ := json.Marshal(ch)
p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}} // p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}}
//
gock.New("https://api.telegram.org"). // gock.New("https://api.telegram.org").
Post("/bot123123:Qwerty/getMe"). // Post("/bot123123:Qwerty/getMe").
Reply(200). // Reply(200).
BodyString(`{"ok":true,"result":{"id":123,"is_bot":true,"first_name":"Test","username":"TestBot"}}`) // BodyString(`{"ok":true,"result":{"id":123,"is_bot":true,"first_name":"Test","username":"TestBot"}}`)
//
gock.New("https://api.telegram.org"). // gock.New("https://api.telegram.org").
Post("/bot123123:Qwerty/setWebhook"). // Post("/bot123123:Qwerty/setWebhook").
MatchType("url"). // MatchType("url").
BodyString(p.Encode()). // BodyString(p.Encode()).
Reply(201). // Reply(201).
BodyString(`{"ok":true}`) // BodyString(`{"ok":true}`)
//
gock.New("https://api.telegram.org"). // gock.New("https://api.telegram.org").
Post("/bot123123:Qwerty/getWebhookInfo"). // Post("/bot123123:Qwerty/getWebhookInfo").
Reply(200). // Reply(200).
BodyString(`{"ok":true,"result":{"url":"https://` + config.HTTPServer.Host + `/telegram/123123:Qwerty","has_custom_certificate":false,"pending_update_count":0}}`) // BodyString(`{"ok":true,"result":{"url":"https://` + config.HTTPServer.Host + `/telegram/123123:Qwerty","has_custom_certificate":false,"pending_update_count":0}}`)
//
gock.New("https://test.retailcrm.pro"). // gock.New("https://test.retailcrm.pro").
Post("/api/transport/v1/channels"). // Post("/api/transport/v1/channels").
JSON([]byte(outgoing)). // JSON([]byte(outgoing)).
MatchHeader("Content-Type", "application/json"). // MatchHeader("Content-Type", "application/json").
MatchHeader("X-Transport-Token", "test-token"). // MatchHeader("X-Transport-Token", "test-token").
Reply(201). // Reply(201).
BodyString(`{"id": 1}`) // BodyString(`{"id": 1}`)
//
req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "connectionId": 1}`)) // req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "connectionId": 1}`))
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
rr := httptest.NewRecorder() // rr := httptest.NewRecorder()
router.ServeHTTP(rr, req) // router.ServeHTTP(rr, req)
require.Equal(t, http.StatusCreated, rr.Code, // require.Equal(t, http.StatusCreated, rr.Code,
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusCreated)) // fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusCreated))
//
bytes, err := ioutil.ReadAll(rr.Body) // bytes, err := ioutil.ReadAll(rr.Body)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
//
var res map[string]interface{} // var res map[string]interface{}
//
err = json.Unmarshal(bytes, &res) // err = json.Unmarshal(bytes, &res)
if err != nil { // if err != nil {
t.Fatal(err) // t.Fatal(err)
} // }
//
assert.Equal(t, "123123:Qwerty", res["token"]) // assert.Equal(t, "123123:Qwerty", res["token"])
} // }
func TestRouting_deleteBotHandler(t *testing.T) { func TestRouting_deleteBotHandler(t *testing.T) {
defer gock.Off() defer gock.Off()