From 2b1ee5ca5bc6c60c688e13150a6858bd3a2e2bb1 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Wed, 19 Sep 2018 16:34:13 +0300 Subject: [PATCH] update for order & products, minor fixes --- Jenkinsfile | 1 - Makefile | 2 +- src/routing.go | 30 ++++----- src/routing_test.go | 161 ++++++++++++++++++++++---------------------- 4 files changed, 95 insertions(+), 99 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 8906707..89489b7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -17,7 +17,6 @@ pipeline { steps { sh 'cp config_test.yml.dist config_test.yml' compose 'up -d --build postgres_test' - compose 'run --rm mg_telegram_test make migrate_test' } } diff --git a/Makefile b/Makefile index f8a1515..0630871 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ test: deps fmt @echo "==> Running tests" @cd $(ROOT_DIR) && go test ./... -v -cpu 2 -jenkins_test: deps +jenkins_test: migrate_test @echo "==> Running tests (result in test-report.xml)" @go get -v -u github.com/jstemmer/go-junit-report @go test ./... -v -cpu 2 -race | /go/bin/go-junit-report -set-exit-code > ./test-report.xml diff --git a/src/routing.go b/src/routing.go index 2d36722..046cd74 100644 --- a/src/routing.go +++ b/src/routing.go @@ -497,27 +497,25 @@ func mgWebhookHandler(c *gin.Context) { case "message_sent": var mb string if msg.Data.Type == v1.MsgTypeProduct { - mb = fmt.Sprintf( - "[%s](%s)", - msg.Data.Product.Name, - msg.Data.Product.Url, - ) + mb = fmt.Sprintf("%s\n", msg.Data.Product.Name) + if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 { mb += fmt.Sprintf( - "\n%v %s", + "\n%v %s\n", msg.Data.Product.Cost.Value, currency[strings.ToLower(msg.Data.Product.Cost.Currency)], ) } - if msg.Data.Product.Img != "" { - mb = fmt.Sprintf("\n%s", msg.Data.Product.Img) + if msg.Data.Product.Url != "" { + mb += msg.Data.Product.Url + } else { + mb += msg.Data.Product.Img } - } else if msg.Data.Type == v1.MsgTypeOrder { - mb = "**Заказ" + mb = "Заказ" - if msg.Data.Order.Number != ""{ + if msg.Data.Order.Number != "" { mb += " " + msg.Data.Order.Number } @@ -525,10 +523,10 @@ func mgWebhookHandler(c *gin.Context) { mb += fmt.Sprintf(" (%s)", msg.Data.Order.Date) } - mb += "**\n" + mb += "\n" if len(msg.Data.Order.Items) > 0 { - for _, v := range msg.Data.Order.Items { - mb += fmt.Sprintf("%s %v x %v %s\n", v.Name, v.Quantity.Value, v.Price.Value, currency[strings.ToLower(v.Price.Currency)]) + for _, v := range msg.Data.Order.Items { + mb += fmt.Sprintf("%s %v x %v %s\n", v.Name, v.Quantity.Value, v.Price.Value, currency[strings.ToLower(v.Price.Currency)]) } } @@ -538,10 +536,6 @@ func mgWebhookHandler(c *gin.Context) { } m := tgbotapi.NewMessage(cid, mb) - if msg.Data.Type == v1.MsgTypeProduct || msg.Data.Type == v1.MsgTypeOrder { - m.ParseMode = "Markdown" - } - if msg.Data.QuoteExternalID != "" { qid, err := strconv.Atoi(msg.Data.QuoteExternalID) if err != nil { diff --git a/src/routing_test.go b/src/routing_test.go index 24d9028..e86ac04 100644 --- a/src/routing_test.go +++ b/src/routing_test.go @@ -1,7 +1,9 @@ package main import ( + "encoding/json" "fmt" + "io/ioutil" "net/http" "net/http/httptest" "net/url" @@ -13,6 +15,7 @@ import ( "github.com/h2non/gock" "github.com/retailcrm/mg-transport-api-client-go/v1" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var router *gin.Engine @@ -50,85 +53,85 @@ func TestRouting_connectHandler(t *testing.T) { fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK)) } -// func TestRouting_addBotHandler(t *testing.T) { -// defer gock.Off() -// -// ch := v1.Channel{ -// Type: "telegram", -// Settings: v1.ChannelSettings{ -// SpamAllowed: false, -// Status: v1.Status{ -// Delivered: v1.ChannelFeatureSend, -// Read: v1.ChannelFeatureNone, -// }, -// Text: v1.ChannelSettingsText{ -// Creating: v1.ChannelFeatureBoth, -// Editing: v1.ChannelFeatureBoth, -// Quoting: v1.ChannelFeatureBoth, -// Deleting: v1.ChannelFeatureReceive, -// }, -// Product: v1.Product{ -// Creating: v1.ChannelFeatureReceive, -// Editing: v1.ChannelFeatureReceive, -// }, -// Order: v1.Order{ -// Creating: v1.ChannelFeatureReceive, -// Editing: v1.ChannelFeatureReceive, -// }, -// }, -// } -// -// outgoing, _ := json.Marshal(ch) -// p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}} -// -// gock.New("https://api.telegram.org"). -// Post("/bot123123:Qwerty/getMe"). -// Reply(200). -// BodyString(`{"ok":true,"result":{"id":123,"is_bot":true,"first_name":"Test","username":"TestBot"}}`) -// -// gock.New("https://api.telegram.org"). -// Post("/bot123123:Qwerty/setWebhook"). -// MatchType("url"). -// BodyString(p.Encode()). -// Reply(201). -// BodyString(`{"ok":true}`) -// -// gock.New("https://api.telegram.org"). -// Post("/bot123123:Qwerty/getWebhookInfo"). -// Reply(200). -// 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"). -// Post("/api/transport/v1/channels"). -// JSON([]byte(outgoing)). -// MatchHeader("Content-Type", "application/json"). -// MatchHeader("X-Transport-Token", "test-token"). -// Reply(201). -// BodyString(`{"id": 1}`) -// -// req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "connectionId": 1}`)) -// if err != nil { -// t.Fatal(err) -// } -// rr := httptest.NewRecorder() -// router.ServeHTTP(rr, req) -// require.Equal(t, http.StatusCreated, rr.Code, -// fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusCreated)) -// -// bytes, err := ioutil.ReadAll(rr.Body) -// if err != nil { -// t.Fatal(err) -// } -// -// var res map[string]interface{} -// -// err = json.Unmarshal(bytes, &res) -// if err != nil { -// t.Fatal(err) -// } -// -// assert.Equal(t, "123123:Qwerty", res["token"]) -// } +func TestRouting_addBotHandler(t *testing.T) { + defer gock.Off() + + ch := v1.Channel{ + Type: "telegram", + Settings: v1.ChannelSettings{ + SpamAllowed: false, + Status: v1.Status{ + Delivered: v1.ChannelFeatureSend, + Read: v1.ChannelFeatureNone, + }, + Text: v1.ChannelSettingsText{ + Creating: v1.ChannelFeatureBoth, + Editing: v1.ChannelFeatureBoth, + Quoting: v1.ChannelFeatureBoth, + Deleting: v1.ChannelFeatureReceive, + }, + Product: v1.Product{ + Creating: v1.ChannelFeatureReceive, + Editing: v1.ChannelFeatureReceive, + }, + Order: v1.Order{ + Creating: v1.ChannelFeatureReceive, + Editing: v1.ChannelFeatureReceive, + }, + }, + } + + outgoing, _ := json.Marshal(&ch) + p := url.Values{"url": {"https://" + config.HTTPServer.Host + "/telegram/123123:Qwerty"}} + + gock.New("https://api.telegram.org"). + Post("/bot123123:Qwerty/getMe"). + Reply(200). + BodyString(`{"ok":true,"result":{"id":123,"is_bot":true,"first_name":"Test","username":"TestBot"}}`) + + gock.New("https://api.telegram.org"). + Post("/bot123123:Qwerty/setWebhook"). + MatchType("url"). + BodyString(p.Encode()). + Reply(201). + BodyString(`{"ok":true}`) + + gock.New("https://api.telegram.org"). + Post("/bot123123:Qwerty/getWebhookInfo"). + Reply(200). + 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"). + Post("/api/transport/v1/channels"). + JSON([]byte(outgoing)). + MatchHeader("Content-Type", "application/json"). + MatchHeader("X-Transport-Token", "test-token"). + Reply(201). + BodyString(`{"id": 1}`) + + req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "connectionId": 1}`)) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + router.ServeHTTP(rr, req) + require.Equal(t, http.StatusCreated, rr.Code, + fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusCreated)) + + bytes, err := ioutil.ReadAll(rr.Body) + if err != nil { + t.Fatal(err) + } + + var res map[string]interface{} + + err = json.Unmarshal(bytes, &res) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, "123123:Qwerty", res["token"]) +} func TestRouting_deleteBotHandler(t *testing.T) { defer gock.Off()