1
0
mirror of synced 2024-11-22 12:26:02 +03:00

update for order & products, minor fixes

This commit is contained in:
Alex Lushpai 2018-09-19 16:34:13 +03:00
parent 1661e8535d
commit 2b1ee5ca5b
4 changed files with 95 additions and 99 deletions

1
Jenkinsfile vendored
View File

@ -17,7 +17,6 @@ pipeline {
steps { steps {
sh 'cp config_test.yml.dist config_test.yml' sh 'cp config_test.yml.dist config_test.yml'
compose 'up -d --build postgres_test' compose 'up -d --build postgres_test'
compose 'run --rm mg_telegram_test make migrate_test'
} }
} }

View File

@ -19,7 +19,7 @@ test: deps fmt
@echo "==> Running tests" @echo "==> Running tests"
@cd $(ROOT_DIR) && go test ./... -v -cpu 2 @cd $(ROOT_DIR) && go test ./... -v -cpu 2
jenkins_test: deps jenkins_test: migrate_test
@echo "==> Running tests (result in test-report.xml)" @echo "==> Running tests (result in test-report.xml)"
@go get -v -u github.com/jstemmer/go-junit-report @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 @go test ./... -v -cpu 2 -race | /go/bin/go-junit-report -set-exit-code > ./test-report.xml

View File

@ -497,25 +497,23 @@ func mgWebhookHandler(c *gin.Context) {
case "message_sent": case "message_sent":
var mb string var mb string
if msg.Data.Type == v1.MsgTypeProduct { if msg.Data.Type == v1.MsgTypeProduct {
mb = fmt.Sprintf( mb = fmt.Sprintf("%s\n", msg.Data.Product.Name)
"[%s](%s)",
msg.Data.Product.Name,
msg.Data.Product.Url,
)
if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 { if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 {
mb += fmt.Sprintf( mb += fmt.Sprintf(
"\n%v %s", "\n%v %s\n",
msg.Data.Product.Cost.Value, msg.Data.Product.Cost.Value,
currency[strings.ToLower(msg.Data.Product.Cost.Currency)], currency[strings.ToLower(msg.Data.Product.Cost.Currency)],
) )
} }
if msg.Data.Product.Img != "" { if msg.Data.Product.Url != "" {
mb = fmt.Sprintf("\n%s", msg.Data.Product.Img) mb += msg.Data.Product.Url
} else {
mb += msg.Data.Product.Img
} }
} else if msg.Data.Type == v1.MsgTypeOrder { } else if msg.Data.Type == v1.MsgTypeOrder {
mb = "**Заказ" mb = "Заказ"
if msg.Data.Order.Number != "" { if msg.Data.Order.Number != "" {
mb += " " + msg.Data.Order.Number mb += " " + msg.Data.Order.Number
@ -525,7 +523,7 @@ func mgWebhookHandler(c *gin.Context) {
mb += fmt.Sprintf(" (%s)", msg.Data.Order.Date) mb += fmt.Sprintf(" (%s)", msg.Data.Order.Date)
} }
mb += "**\n" mb += "\n"
if len(msg.Data.Order.Items) > 0 { if len(msg.Data.Order.Items) > 0 {
for _, v := range msg.Data.Order.Items { 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)]) 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) m := tgbotapi.NewMessage(cid, mb)
if msg.Data.Type == v1.MsgTypeProduct || msg.Data.Type == v1.MsgTypeOrder {
m.ParseMode = "Markdown"
}
if msg.Data.QuoteExternalID != "" { if msg.Data.QuoteExternalID != "" {
qid, err := strconv.Atoi(msg.Data.QuoteExternalID) qid, err := strconv.Atoi(msg.Data.QuoteExternalID)
if err != nil { if err != nil {

View File

@ -1,7 +1,9 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -13,6 +15,7 @@ import (
"github.com/h2non/gock" "github.com/h2non/gock"
"github.com/retailcrm/mg-transport-api-client-go/v1" "github.com/retailcrm/mg-transport-api-client-go/v1"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
var router *gin.Engine 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)) 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()