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 {
sh 'cp config_test.yml.dist config_test.yml'
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"
@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

View File

@ -497,25 +497,23 @@ 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 != "" {
mb += " " + msg.Data.Order.Number
@ -525,7 +523,7 @@ 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)])
@ -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 {

View File

@ -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()