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")
|
|
|
|
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 {
|
|
|
|
return New(mgURL, mgToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMgClient_ActivateTransportChannel(t *testing.T) {
|
|
|
|
c := client()
|
|
|
|
ch := Channel{
|
|
|
|
ID: channelId,
|
|
|
|
Type: "telegram",
|
|
|
|
Events: []string{
|
|
|
|
"message_sent",
|
|
|
|
"message_read",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
Events: []string{
|
|
|
|
"message_sent",
|
|
|
|
"message_updated",
|
|
|
|
"message_deleted",
|
|
|
|
"message_read",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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.DectivatedAt.String() == "" {
|
|
|
|
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{
|
|
|
|
ID: channelId,
|
|
|
|
Events: []string{
|
|
|
|
"message_sent",
|
2018-05-21 17:56:42 +03:00
|
|
|
"message_updated",
|
|
|
|
"message_deleted",
|
2018-05-17 17:26:18 +03:00
|
|
|
"message_read",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-21 17:56:42 +03:00
|
|
|
func TestMgClient_Messages(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-07-13 15:15:51 +03:00
|
|
|
Message: SendMessage{
|
2018-05-21 17:56:42 +03:00
|
|
|
Message{
|
|
|
|
ExternalID: ext,
|
|
|
|
Type: "text",
|
|
|
|
Text: "hello!",
|
|
|
|
},
|
|
|
|
time.Now(),
|
|
|
|
},
|
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-07-13 15:15:51 +03:00
|
|
|
Channel: channelId,
|
|
|
|
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-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-05-21 17:56:42 +03:00
|
|
|
snd := UpdateData{
|
|
|
|
UpdateMessage{
|
2018-05-17 17:26:18 +03:00
|
|
|
Message{
|
2018-05-21 17:56:42 +03:00
|
|
|
ExternalID: ext,
|
2018-05-17 17:26:18 +03:00
|
|
|
Type: "text",
|
2018-05-21 17:56:42 +03:00
|
|
|
Text: "hello hello!",
|
2018-05-17 17:26:18 +03:00
|
|
|
},
|
|
|
|
time.Now(),
|
|
|
|
},
|
|
|
|
channelId,
|
|
|
|
}
|
|
|
|
|
2018-05-21 17:56:42 +03:00
|
|
|
data, status, err := c.UpdateMessages(snd)
|
|
|
|
|
|
|
|
if status != http.StatusOK {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Time.String() == "" {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Message %v updated", data.MessageID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMgClient_DeleteMessage(t *testing.T) {
|
|
|
|
c := client()
|
|
|
|
t.Logf("%v", ext)
|
|
|
|
|
|
|
|
snd := DeleteData{
|
|
|
|
Message{
|
|
|
|
ExternalID: ext,
|
|
|
|
},
|
|
|
|
channelId,
|
|
|
|
}
|
2018-05-17 17:26:18 +03:00
|
|
|
|
2018-05-21 17:56:42 +03:00
|
|
|
data, status, err := c.DeleteMessage(snd)
|
2018-05-17 17:26:18 +03:00
|
|
|
if status != http.StatusOK {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Time.String() == "" {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2018-05-21 17:56:42 +03:00
|
|
|
|
|
|
|
t.Logf("Message %v updated", data.MessageID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMgClient_DeactivateTransportChannel(t *testing.T) {
|
|
|
|
c := client()
|
|
|
|
deleteData, status, err := c.DeactivateTransportChannel(channelId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%d %v", status, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deleteData.DectivatedAt.String() == "" {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Deactivate selected channel: %v", deleteData.ChannelID)
|
|
|
|
}
|