2018-05-17 17:26:18 +03:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
2018-05-21 17:56:42 +03:00
|
|
|
"time"
|
2018-05-17 17:26:18 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mgURL = os.Getenv("MG_URL")
|
|
|
|
mgToken = os.Getenv("MG_TOKEN")
|
2018-11-08 10:15:27 +03:00
|
|
|
channelID, _ = strconv.ParseUint(os.Getenv("MG_CHANNEL"), 10, 64)
|
2018-05-21 17:56:42 +03:00
|
|
|
ext = strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
|
2018-05-17 17:26:18 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func client() *MgClient {
|
2018-12-17 14:34:15 +03:00
|
|
|
c := New(mgURL, mgToken)
|
|
|
|
c.Debug = true
|
|
|
|
|
|
|
|
return c
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:26:18 +03:00
|
|
|
func TestMgClient_ActivateTransportChannel(t *testing.T) {
|
|
|
|
c := client()
|
|
|
|
ch := Channel{
|
2018-11-08 10:15:27 +03:00
|
|
|
ID: channelID,
|
2018-05-17 17:26:18 +03:00
|
|
|
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
|
|
|
},
|
2018-09-19 18:12:37 +03:00
|
|
|
Product: Product{
|
|
|
|
Creating: ChannelFeatureSend,
|
|
|
|
Deleting: ChannelFeatureSend,
|
|
|
|
},
|
|
|
|
Order: Order{
|
|
|
|
Creating: ChannelFeatureBoth,
|
|
|
|
Deleting: ChannelFeatureSend,
|
|
|
|
},
|
2018-11-13 12:52:01 +03:00
|
|
|
Image: ChannelSettingsFilesBase{
|
|
|
|
Creating: ChannelFeatureBoth,
|
|
|
|
},
|
|
|
|
File: ChannelSettingsFilesBase{
|
|
|
|
Creating: ChannelFeatureBoth,
|
|
|
|
},
|
2018-08-16 16:53:29 +03:00
|
|
|
},
|
2018-05-17 17:26:18 +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)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
},
|
2018-09-19 18:12:37 +03:00
|
|
|
Product: Product{
|
|
|
|
Creating: ChannelFeatureSend,
|
|
|
|
Deleting: ChannelFeatureSend,
|
|
|
|
},
|
|
|
|
Order: Order{
|
|
|
|
Creating: ChannelFeatureBoth,
|
|
|
|
Deleting: ChannelFeatureSend,
|
|
|
|
},
|
2018-11-13 12:52:01 +03:00
|
|
|
Image: ChannelSettingsFilesBase{
|
|
|
|
Creating: ChannelFeatureBoth,
|
|
|
|
},
|
|
|
|
File: ChannelSettingsFilesBase{
|
|
|
|
Creating: ChannelFeatureBoth,
|
|
|
|
},
|
2018-08-16 16:53:29 +03:00
|
|
|
},
|
2018-05-17 17:26:18 +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)
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
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)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
},
|
2018-09-19 18:12:37 +03:00
|
|
|
Product: Product{
|
|
|
|
Creating: ChannelFeatureSend,
|
|
|
|
Deleting: ChannelFeatureSend,
|
|
|
|
},
|
|
|
|
Order: Order{
|
|
|
|
Creating: ChannelFeatureBoth,
|
|
|
|
Deleting: ChannelFeatureSend,
|
|
|
|
},
|
2018-11-13 12:52:01 +03:00
|
|
|
Image: ChannelSettingsFilesBase{
|
|
|
|
Creating: ChannelFeatureBoth,
|
|
|
|
},
|
|
|
|
File: ChannelSettingsFilesBase{
|
|
|
|
Creating: ChannelFeatureBoth,
|
|
|
|
},
|
2018-08-16 16:53:29 +03:00
|
|
|
},
|
2018-05-17 17:26:18 +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)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
2018-11-13 12:52:01 +03:00
|
|
|
func TestMgClient_TextMessages(t *testing.T) {
|
2018-05-17 17:26:18 +03:00
|
|
|
c := client()
|
2018-05-21 17:56:42 +03:00
|
|
|
t.Logf("%v", ext)
|
2018-05-17 17:26:18 +03:00
|
|
|
|
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
|
|
|
},
|
2018-07-13 15:15:51 +03:00
|
|
|
User: User{
|
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-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
2018-05-21 17:56:42 +03:00
|
|
|
data, status, err := c.Messages(snd)
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
2018-05-17 17:26:18 +03:00
|
|
|
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)
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
2018-11-13 12:52:01 +03:00
|
|
|
func TestMgClient_ImageMessages(t *testing.T) {
|
|
|
|
c := client()
|
|
|
|
t.Logf("%v", ext)
|
|
|
|
|
|
|
|
uploadFileResponse, st, err := c.UploadFileByURL(UploadFileByUrlRequest{
|
|
|
|
Url: "https://via.placeholder.com/300",
|
|
|
|
})
|
|
|
|
|
|
|
|
if st != http.StatusOK {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
snd := SendData{
|
|
|
|
Message: Message{
|
2018-12-05 17:14:51 +03:00
|
|
|
ExternalID: ext + "file",
|
2018-11-13 12:52:01 +03:00
|
|
|
Type: MsgTypeImage,
|
|
|
|
Items: []Item{{ID: uploadFileResponse.ID}},
|
|
|
|
},
|
|
|
|
User: User{
|
|
|
|
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) {
|
2018-05-17 17:26:18 +03:00
|
|
|
c := client()
|
2018-05-21 17:56:42 +03:00
|
|
|
t.Logf("%v", ext)
|
2018-05-17 17:26:18 +03:00
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
sndU := EditMessageRequest{
|
|
|
|
EditMessageRequestMessage{
|
|
|
|
ExternalID: ext,
|
|
|
|
Text: "hello hello!",
|
2018-05-17 17:26:18 +03:00
|
|
|
},
|
2018-11-08 10:15:27 +03:00
|
|
|
channelID,
|
2018-05-17 17:26:18 +03:00
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
dataU, status, err := c.UpdateMessages(sndU)
|
2018-05-21 17:56:42 +03:00
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
if dataU.Time.String() == "" {
|
2018-05-21 17:56:42 +03:00
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
t.Logf("Message %v updated", dataU.MessageID)
|
2018-05-21 17:56:42 +03:00
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
func TestMgClient_MarkMessageReadAndDelete(t *testing.T) {
|
2018-05-21 17:56:42 +03:00
|
|
|
c := client()
|
|
|
|
t.Logf("%v", ext)
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
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
|
|
|
}
|
2018-05-17 17:26:18 +03:00
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
_, status, err := c.MarkMessageRead(snd)
|
|
|
|
|
2018-05-17 17:26:18 +03:00
|
|
|
if status != http.StatusOK {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
t.Logf("Message ext marked as read")
|
|
|
|
|
|
|
|
sndD := DeleteData{
|
|
|
|
Message{
|
|
|
|
ExternalID: ext,
|
|
|
|
},
|
2018-11-08 10:15:27 +03:00
|
|
|
channelID,
|
2018-10-03 09:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data, status, err := c.DeleteMessage(sndD)
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
2018-05-17 17:26:18 +03:00
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2018-05-21 17:56:42 +03:00
|
|
|
|
2018-10-03 09:48:07 +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)
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:48:07 +03:00
|
|
|
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)
|
|
|
|
|
|
|
|
resp, err := http.Get("https://via.placeholder.com/300")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
data, status, err := c.UploadFile(resp.Body)
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Message %+v is sent", data)
|
|
|
|
}
|