1
0
mirror of synced 2024-12-01 09:26:03 +03:00
mg-transport-api-client-go/v1/client_test.go

521 lines
11 KiB
Go
Raw Normal View History

package v1
import (
"bytes"
"encoding/base64"
"fmt"
"net/http"
"os"
"strconv"
"testing"
2018-05-21 17:56:42 +03:00
"time"
"github.com/stretchr/testify/assert"
)
var (
mgURL = os.Getenv("MG_URL")
mgToken = os.Getenv("MG_TOKEN")
channelID, _ = strconv.ParseUint(os.Getenv("MG_CHANNEL"), 10, 64)
ext = strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
tplCode = fmt.Sprintf("testTemplate_%d", time.Now().UnixNano())
tplChannel uint64 = 0
)
func client() *MgClient {
2018-12-17 14:34:15 +03:00
c := New(mgURL, mgToken)
c.Debug = true
return c
}
func templateChannel(t *testing.T) uint64 {
if tplChannel == 0 {
c := client()
resp, _, err := c.ActivateTransportChannel(Channel{
Type: "telegram",
Name: "@test_channel_templates",
Settings: ChannelSettings{
SpamAllowed: false,
Status: Status{
Delivered: ChannelFeatureBoth,
Read: ChannelFeatureBoth,
},
Text: ChannelSettingsText{
Creating: ChannelFeatureBoth,
Editing: ChannelFeatureBoth,
Quoting: ChannelFeatureBoth,
Deleting: ChannelFeatureBoth,
MaxCharsCount: 5000,
},
Product: Product{
Creating: ChannelFeatureBoth,
Editing: ChannelFeatureBoth,
Deleting: ChannelFeatureBoth,
},
Order: Order{
Creating: ChannelFeatureBoth,
Editing: ChannelFeatureBoth,
Deleting: ChannelFeatureBoth,
},
File: ChannelSettingsFilesBase{
Creating: ChannelFeatureBoth,
Editing: ChannelFeatureBoth,
Quoting: ChannelFeatureBoth,
Deleting: ChannelFeatureBoth,
Max: 1000000,
CommentMaxCharsCount: 128,
},
Image: ChannelSettingsFilesBase{
Creating: ChannelFeatureBoth,
Editing: ChannelFeatureBoth,
Quoting: ChannelFeatureBoth,
Deleting: ChannelFeatureBoth,
},
},
})
if err != nil {
t.FailNow()
}
tplChannel = resp.ChannelID
}
return tplChannel
}
2018-10-01 18:17:03 +03:00
func TestMgClient_TransportChannels(t *testing.T) {
c := client()
data, status, err := c.TransportChannels(Channels{Active: true})
if err != nil {
t.Errorf("%d %v", status, err)
}
t.Logf("Channels found: %v", len(data))
}
func TestMgClient_ActivateTransportChannel(t *testing.T) {
c := client()
ch := Channel{
2018-11-08 10:15:27 +03:00
ID: channelID,
Type: "telegram",
2018-09-10 12:06:57 +03:00
Name: "@my_shopping_bot",
2018-08-16 16:53:29 +03:00
Settings: ChannelSettings{
2018-08-21 13:37:49 +03:00
SpamAllowed: false,
Status: Status{
Delivered: ChannelFeatureNone,
Read: ChannelFeatureReceive,
},
Text: ChannelSettingsText{
2018-12-05 10:57:45 +03:00
Creating: ChannelFeatureBoth,
Editing: ChannelFeatureSend,
Quoting: ChannelFeatureReceive,
Deleting: ChannelFeatureBoth,
MaxCharsCount: 2000,
2018-08-16 16:53:29 +03:00
},
Product: Product{
Creating: ChannelFeatureSend,
Deleting: ChannelFeatureSend,
},
Order: Order{
Creating: ChannelFeatureBoth,
Deleting: ChannelFeatureSend,
},
Image: ChannelSettingsFilesBase{
Creating: ChannelFeatureBoth,
},
File: ChannelSettingsFilesBase{
Creating: ChannelFeatureBoth,
},
2018-08-16 16:53:29 +03:00
},
}
data, status, err := c.ActivateTransportChannel(ch)
if err != nil {
t.Errorf("%d %v", status, err)
}
2018-05-21 17:56:42 +03:00
t.Logf("Activate selected channel: %v", data.ChannelID)
}
func TestMgClient_ActivateNewTransportChannel(t *testing.T) {
c := client()
ch := Channel{
Type: "telegram",
2018-09-10 12:06:57 +03:00
Name: "@my_shopping_bot",
2018-08-16 16:53:29 +03:00
Settings: ChannelSettings{
2018-08-21 13:37:49 +03:00
SpamAllowed: false,
Status: Status{
Delivered: ChannelFeatureNone,
Read: ChannelFeatureBoth,
},
Text: ChannelSettingsText{
Creating: ChannelFeatureBoth,
Editing: ChannelFeatureSend,
Quoting: ChannelFeatureBoth,
Deleting: ChannelFeatureSend,
2018-08-16 16:53:29 +03:00
},
Product: Product{
Creating: ChannelFeatureSend,
Deleting: ChannelFeatureSend,
},
Order: Order{
Creating: ChannelFeatureBoth,
Deleting: ChannelFeatureSend,
},
Image: ChannelSettingsFilesBase{
Creating: ChannelFeatureBoth,
},
File: ChannelSettingsFilesBase{
Creating: ChannelFeatureBoth,
},
2018-08-16 16:53:29 +03:00
},
}
data, status, err := c.ActivateTransportChannel(ch)
if err != nil {
t.Errorf("%d %v", status, err)
}
2018-05-21 17:56:42 +03:00
t.Logf("New channel ID %v", data.ChannelID)
deleteData, status, err := c.DeactivateTransportChannel(data.ChannelID)
if err != nil {
t.Errorf("%d %v", status, err)
}
if deleteData.DeactivatedAt.String() == "" {
2018-05-21 17:56:42 +03:00
t.Errorf("%v", err)
}
t.Logf("Deactivate new channel with ID %v", deleteData.ChannelID)
}
func TestMgClient_UpdateTransportChannel(t *testing.T) {
c := client()
ch := Channel{
2018-11-08 10:15:27 +03:00
ID: channelID,
2018-09-10 12:06:57 +03:00
Name: "@my_shopping_bot_2",
2018-08-16 16:53:29 +03:00
Settings: ChannelSettings{
2018-08-21 13:37:49 +03:00
SpamAllowed: false,
Status: Status{
Delivered: ChannelFeatureNone,
Read: ChannelFeatureBoth,
},
Text: ChannelSettingsText{
Creating: ChannelFeatureBoth,
Editing: ChannelFeatureBoth,
Quoting: ChannelFeatureBoth,
Deleting: ChannelFeatureBoth,
2018-08-16 16:53:29 +03:00
},
Product: Product{
Creating: ChannelFeatureSend,
Deleting: ChannelFeatureSend,
},
Order: Order{
Creating: ChannelFeatureBoth,
Deleting: ChannelFeatureSend,
},
Image: ChannelSettingsFilesBase{
Creating: ChannelFeatureBoth,
},
File: ChannelSettingsFilesBase{
Creating: ChannelFeatureBoth,
},
2018-08-16 16:53:29 +03:00
},
}
data, status, err := c.UpdateTransportChannel(ch)
if status != http.StatusOK {
t.Errorf("%v", err)
}
2018-05-21 17:56:42 +03:00
t.Logf("Update selected channel: %v", data.ChannelID)
}
func TestMgClient_TransportTemplates(t *testing.T) {
c := client()
data, status, err := c.TransportTemplates()
assert.NoError(t, err, fmt.Sprintf("%d %s", status, err))
t.Logf("Templates found: %#v", len(data))
2020-04-08 14:17:21 +03:00
for _, item := range data {
for _, tpl := range item.Template {
if tpl.Type == TemplateItemTypeText {
assert.Empty(t, tpl.VarType)
} else {
assert.Empty(t, tpl.Text)
assert.NotEmpty(t, tpl.VarType)
if _, ok := templateVarAssoc[tpl.VarType]; !ok {
t.Errorf("unknown TemplateVar type %s", tpl.VarType)
}
}
}
}
}
func TestMgClient_ActivateTemplate(t *testing.T) {
c := client()
req := ActivateTemplateRequest{
Code: tplCode,
Name: tplCode,
Type: TemplateTypeText,
Template: []TemplateItem{
{
Type: TemplateItemTypeText,
Text: "Hello ",
},
{
Type: TemplateItemTypeVar,
VarType: TemplateVarFirstName,
},
{
Type: TemplateItemTypeText,
Text: "!",
},
},
}
status, err := c.ActivateTemplate(templateChannel(t), req)
assert.NoError(t, err, fmt.Sprintf("%d %s", status, err))
t.Logf("Activated template with code `%s`", req.Code)
}
func TestMgClient_UpdateTemplate(t *testing.T) {
c := client()
tpl := Template{
Code: tplCode,
ChannelID: templateChannel(t),
Name: "updated name",
Enabled: true,
Type: TemplateTypeText,
Template: []TemplateItem{
{
Type: TemplateItemTypeText,
Text: "Welcome ",
},
{
Type: TemplateItemTypeVar,
VarType: TemplateVarFirstName,
},
{
Type: TemplateItemTypeText,
Text: "!",
},
},
}
status, err := c.UpdateTemplate(tpl)
assert.NoError(t, err, fmt.Sprintf("%d %s", status, err))
templates, status, err := c.TransportTemplates()
assert.NoError(t, err, fmt.Sprintf("%d %s", status, err))
for _, template := range templates {
if template.Code == tpl.Code {
assert.Equal(t, tpl.Name, template.Name)
}
}
}
func TestMgClient_DeactivateTemplate(t *testing.T) {
c := client()
status, err := c.DeactivateTemplate(templateChannel(t), tplCode)
assert.NoError(t, err, fmt.Sprintf("%d %s", status, err))
}
func TestMgClient_TextMessages(t *testing.T) {
c := client()
2018-05-21 17:56:42 +03:00
t.Logf("%v", ext)
2018-05-21 17:56:42 +03:00
snd := SendData{
2018-09-10 11:33:42 +03:00
Message: Message{
ExternalID: ext,
2018-12-05 17:14:51 +03:00
Type: MsgTypeText,
2018-09-10 11:33:42 +03:00
Text: "hello!",
2018-05-21 17:56:42 +03:00
},
Originator: OriginatorCustomer,
Customer: Customer{
2018-05-21 17:56:42 +03:00
ExternalID: "6",
Nickname: "octopus",
Firstname: "Joe",
},
2018-11-08 10:15:27 +03:00
Channel: channelID,
2018-07-13 15:15:51 +03:00
ExternalChatID: "24798237492374",
}
2018-05-21 17:56:42 +03:00
data, status, err := c.Messages(snd)
if status != http.StatusOK {
t.Errorf("%v", err)
}
2018-05-21 17:56:42 +03:00
if data.Time.String() == "" {
t.Errorf("%v", err)
}
t.Logf("Message %v is sent", data.MessageID)
}
func TestMgClient_ImageMessages(t *testing.T) {
c := client()
t.Logf("%v", ext)
uploadFileResponse, st, err := c.UploadFileByURL(UploadFileByUrlRequest{
Url: "https://via.placeholder.com/1",
})
if st != http.StatusOK {
t.Errorf("%v", err)
}
snd := SendData{
Message: Message{
2018-12-05 17:14:51 +03:00
ExternalID: ext + "file",
Type: MsgTypeImage,
Items: []Item{{ID: uploadFileResponse.ID}},
},
Originator: OriginatorCustomer,
Customer: Customer{
ExternalID: "6",
Nickname: "octopus",
Firstname: "Joe",
},
Channel: channelID,
ExternalChatID: "24798237492374",
}
data, status, err := c.Messages(snd)
if status != http.StatusOK {
t.Errorf("%v", err)
}
if data.Time.String() == "" {
t.Errorf("%v", err)
}
t.Logf("Message %v is sent", data.MessageID)
}
2018-05-21 17:56:42 +03:00
func TestMgClient_UpdateMessages(t *testing.T) {
c := client()
2018-05-21 17:56:42 +03:00
t.Logf("%v", ext)
sndU := EditMessageRequest{
EditMessageRequestMessage{
ExternalID: ext,
Text: "hello hello!",
},
2018-11-08 10:15:27 +03:00
channelID,
}
dataU, status, err := c.UpdateMessages(sndU)
2018-05-21 17:56:42 +03:00
if status != http.StatusOK {
t.Errorf("%v", err)
}
if dataU.Time.String() == "" {
2018-05-21 17:56:42 +03:00
t.Errorf("%v", err)
}
t.Logf("Message %v updated", dataU.MessageID)
2018-05-21 17:56:42 +03:00
}
func TestMgClient_MarkMessageReadAndDelete(t *testing.T) {
2018-05-21 17:56:42 +03:00
c := client()
t.Logf("%v", ext)
snd := MarkMessageReadRequest{
MarkMessageReadRequestMessage{
2018-05-21 17:56:42 +03:00
ExternalID: ext,
},
2018-11-08 10:15:27 +03:00
channelID,
2018-05-21 17:56:42 +03:00
}
_, status, err := c.MarkMessageRead(snd)
if status != http.StatusOK {
t.Errorf("%v", err)
}
t.Logf("Message ext marked as read")
sndD := DeleteData{
Message{
ExternalID: ext,
},
2018-11-08 10:15:27 +03:00
channelID,
}
data, status, err := c.DeleteMessage(sndD)
if status != http.StatusOK {
t.Errorf("%v", err)
}
2018-05-21 17:56:42 +03:00
t.Logf("Message %v deleted", data.MessageID)
2018-12-05 17:14:51 +03:00
sndD = DeleteData{
Message{
ExternalID: ext + "file",
},
channelID,
}
data, status, err = c.DeleteMessage(sndD)
if status != http.StatusOK {
t.Errorf("%v", err)
}
t.Logf("Message %v deleted", data.MessageID)
2018-05-21 17:56:42 +03:00
}
func TestMgClient_DeactivateTransportChannel(t *testing.T) {
c := client()
2018-11-08 10:15:27 +03:00
deleteData, status, err := c.DeactivateTransportChannel(channelID)
2018-05-21 17:56:42 +03:00
if err != nil {
t.Errorf("%d %v", status, err)
}
if deleteData.DeactivatedAt.String() == "" {
2018-05-21 17:56:42 +03:00
t.Errorf("%v", err)
}
t.Logf("Deactivate selected channel: %v", deleteData.ChannelID)
}
2018-12-05 17:14:51 +03:00
func TestMgClient_UploadFile(t *testing.T) {
c := client()
t.Logf("%v", ext)
// 1x1 png picture
img := "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII="
binary, err := base64.StdEncoding.DecodeString(img)
2018-12-05 17:14:51 +03:00
if err != nil {
t.Errorf("cannot convert base64 to binary: %s", err)
2018-12-05 17:14:51 +03:00
}
data, status, err := c.UploadFile(bytes.NewReader(binary))
2018-12-05 17:14:51 +03:00
if status != http.StatusOK {
t.Errorf("%v", err)
}
t.Logf("Message %+v is sent", data)
}