1
0
mirror of synced 2024-11-22 04:56:06 +03:00

Messages methods & tests (#2)

This commit is contained in:
Alex Lushpai 2018-05-21 17:56:42 +03:00 committed by GitHub
parent 11ed32136e
commit 2b5f24ff75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 144 additions and 58 deletions

View File

@ -110,8 +110,12 @@ func (c *MgClient) UpdateTransportChannel(request Channel) (UpdateResponse, int,
// fmt.Printf("%s\n", data.DeactivatedAt) // fmt.Printf("%s\n", data.DeactivatedAt)
func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, error) { func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, error) {
var resp DeleteResponse var resp DeleteResponse
var buf []byte
data, status, err := c.DeleteRequest(fmt.Sprintf("/transport/channels/%s", strconv.FormatUint(id, 10))) data, status, err := c.DeleteRequest(
fmt.Sprintf("/transport/channels/%s", strconv.FormatUint(id, 10)),
buf,
)
if err != nil { if err != nil {
return resp, status, err return resp, status, err
} }
@ -132,11 +136,10 @@ func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, e
// Example: // Example:
// //
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6") // var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
// snd := SendData{ // msg := SendData{
// SendMessage{ // SendMessage{
// Message{ // Message{
// ExternalID: "23e23e23", // ExternalID: "274628",
// Channel: channelId,
// Type: "text", // Type: "text",
// Text: "hello!", // Text: "hello!",
// }, // },
@ -144,13 +147,13 @@ func (c *MgClient) DeactivateTransportChannel(id uint64) (DeleteResponse, int, e
// }, // },
// User{ // User{
// ExternalID: "8", // ExternalID: "8",
// Nickname: "@octopulus", // Nickname: "@octopus",
// Firstname: "Joe", // Firstname: "Joe",
// }, // },
// channelId, // 10,
// } // }
// //
// data, status, err := client.Messages(snd) // data, status, err := client.Messages(msg)
// //
// if err != nil { // if err != nil {
// fmt.Printf("%v", err) // fmt.Printf("%v", err)
@ -182,32 +185,26 @@ func (c *MgClient) Messages(request SendData) (MessagesResponse, int, error) {
// Example: // Example:
// //
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6") // var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
// snd := SendData{ // msg := UpdateData{
// SendMessage{ // UpdateMessage{
// Message{ // Message{
// ExternalID: "23e23e23", // ExternalID: "274628",
// Channel: channelId,
// Type: "text", // Type: "text",
// Text: "hello!", // Text: "hello hello!",
// }, // },
// time.Now(), // time.Now(),
// }, // },
// User{ // 10,
// ExternalID: "8",
// Nickname: "@octopulus",
// Firstname: "Joe",
// },
// channelId,
// } // }
// //
// data, status, err := client.UpdateMessages(snd) // data, status, err := client.UpdateMessages(msg)
// //
// if err != nil { // if err != nil {
// fmt.Printf("%v", err) // fmt.Printf("%v", err)
// } // }
// //
// fmt.Printf("%s\n", data.MessageID) // fmt.Printf("%s\n", data.MessageID)
func (c *MgClient) UpdateMessages(request UpdateMessage) (MessagesResponse, int, error) { func (c *MgClient) UpdateMessages(request UpdateData) (MessagesResponse, int, error) {
var resp MessagesResponse var resp MessagesResponse
outgoing, _ := json.Marshal(&request) outgoing, _ := json.Marshal(&request)
@ -233,17 +230,28 @@ func (c *MgClient) UpdateMessages(request UpdateMessage) (MessagesResponse, int,
// //
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6") // var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d49bcba99be73bff503ea6")
// //
// data, status, err := client.DeleteMessage("3053450384") // msg := DeleteData{
// Message{
// ExternalID: "274628",
// },
// 10,
// }
//
// data, status, err := client.DeleteMessage(msg)
// //
// if err != nil { // if err != nil {
// fmt.Printf("%v", err) // fmt.Printf("%v", err)
// } // }
// //
// fmt.Printf("%s\n", data.MessageID) // fmt.Printf("%s\n", data.MessageID)
func (c *MgClient) DeleteMessage(id string) (MessagesResponse, int, error) { func (c *MgClient) DeleteMessage(request DeleteData) (MessagesResponse, int, error) {
var resp MessagesResponse var resp MessagesResponse
outgoing, _ := json.Marshal(&request)
data, status, err := c.DeleteRequest(fmt.Sprintf("/transport/messages/%s", id)) data, status, err := c.DeleteRequest(
"/transport/messages/",
[]byte(outgoing),
)
if err != nil { if err != nil {
return resp, status, err return resp, status, err
} }

View File

@ -5,12 +5,14 @@ import (
"os" "os"
"strconv" "strconv"
"testing" "testing"
"time"
) )
var ( var (
mgURL = os.Getenv("MG_URL") mgURL = os.Getenv("MG_URL")
mgToken = os.Getenv("MG_TOKEN") mgToken = os.Getenv("MG_TOKEN")
channelId, _ = strconv.ParseUint(os.Getenv("MG_CHANNEL"), 10, 64) channelId, _ = strconv.ParseUint(os.Getenv("MG_CHANNEL"), 10, 64)
ext = strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
) )
func client() *MgClient { func client() *MgClient {
@ -24,8 +26,6 @@ func TestMgClient_ActivateTransportChannel(t *testing.T) {
Type: "telegram", Type: "telegram",
Events: []string{ Events: []string{
"message_sent", "message_sent",
"message_updated",
"message_deleted",
"message_read", "message_read",
}, },
} }
@ -36,7 +36,7 @@ func TestMgClient_ActivateTransportChannel(t *testing.T) {
t.Errorf("%d %v", status, err) t.Errorf("%d %v", status, err)
} }
t.Logf("%v", data.ChannelID) t.Logf("Activate selected channel: %v", data.ChannelID)
} }
func TestMgClient_ActivateNewTransportChannel(t *testing.T) { func TestMgClient_ActivateNewTransportChannel(t *testing.T) {
@ -57,7 +57,19 @@ func TestMgClient_ActivateNewTransportChannel(t *testing.T) {
t.Errorf("%d %v", status, err) t.Errorf("%d %v", status, err)
} }
t.Logf("%v", data.ChannelID) 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)
} }
func TestMgClient_UpdateTransportChannel(t *testing.T) { func TestMgClient_UpdateTransportChannel(t *testing.T) {
@ -66,6 +78,8 @@ func TestMgClient_UpdateTransportChannel(t *testing.T) {
ID: channelId, ID: channelId,
Events: []string{ Events: []string{
"message_sent", "message_sent",
"message_updated",
"message_deleted",
"message_read", "message_read",
}, },
} }
@ -76,40 +90,25 @@ func TestMgClient_UpdateTransportChannel(t *testing.T) {
t.Errorf("%v", err) t.Errorf("%v", err)
} }
t.Logf("%v", data.ChannelID) t.Logf("Update selected channel: %v", data.ChannelID)
} }
func TestMgClient_DeactivateTransportChannel(t *testing.T) { func TestMgClient_Messages(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("%v", deleteData.ChannelID)
}
/*func TestMgClient_Messages(t *testing.T) {
c := client() c := client()
t.Logf("%v", ext)
snd := SendData{ snd := SendData{
SendMessage{ SendMessage{
Message{ Message{
ExternalID: "23e23e23", ExternalID: ext,
Channel: channelId,
Type: "text", Type: "text",
Text: "hello!", Text: "hello!",
}, },
time.Now(), time.Now(),
}, },
User{ User{
ExternalID: "8", ExternalID: "6",
Nickname: "@octopulus", Nickname: "octopus",
Firstname: "Joe", Firstname: "Joe",
}, },
channelId, channelId,
@ -124,4 +123,73 @@ func TestMgClient_DeactivateTransportChannel(t *testing.T) {
if data.Time.String() == "" { if data.Time.String() == "" {
t.Errorf("%v", err) t.Errorf("%v", err)
} }
}*/
t.Logf("Message %v is sent", data.MessageID)
}
func TestMgClient_UpdateMessages(t *testing.T) {
c := client()
t.Logf("%v", ext)
snd := UpdateData{
UpdateMessage{
Message{
ExternalID: ext,
Type: "text",
Text: "hello hello!",
},
time.Now(),
},
channelId,
}
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,
}
data, status, err := c.DeleteMessage(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_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)
}

View File

@ -59,13 +59,11 @@ func (c *MgClient) PutRequest(url string, parameters []byte) ([]byte, int, error
} }
// DeleteRequest implements DELETE Request // DeleteRequest implements DELETE Request
func (c *MgClient) DeleteRequest(url string) ([]byte, int, error) { func (c *MgClient) DeleteRequest(url string, parameters []byte) ([]byte, int, error) {
var buf []byte
return makeRequest( return makeRequest(
"DELETE", "DELETE",
fmt.Sprintf("%s%s%s", c.URL, prefix, url), fmt.Sprintf("%s%s%s", c.URL, prefix, url),
bytes.NewBuffer(buf), bytes.NewBuffer(parameters),
c, c,
) )
} }

View File

@ -39,7 +39,7 @@ type DeleteResponse struct {
// User struct // User struct
type User struct { type User struct {
ExternalID string `url:"external_id"` ExternalID string `url:"external_id" json:"external_id"`
Nickname string `url:"nickname"` Nickname string `url:"nickname"`
Firstname string `url:"first_name,omitempty"` Firstname string `url:"first_name,omitempty"`
Lastname string `url:"last_name,omitempty"` Lastname string `url:"last_name,omitempty"`
@ -52,9 +52,8 @@ type User struct {
// Message struct // Message struct
type Message struct { type Message struct {
ExternalID string `url:"external_id"` ExternalID string `url:"external_id" json:"external_id"`
Channel uint64 `url:"channel"` Type string `url:"type,omitempty"`
Type string `url:"type"`
Text string `url:"text,omitempty"` Text string `url:"text,omitempty"`
} }
@ -70,14 +69,27 @@ type UpdateMessage struct {
EditedAt time.Time `url:"edited_at,omitempty"` EditedAt time.Time `url:"edited_at,omitempty"`
} }
// SendData struct
type SendData struct { type SendData struct {
Message SendMessage `url:"message"` Message SendMessage `url:"message"`
User User `url:"user"` User User `url:"user"`
Channel uint64 `url:"channel"` Channel uint64 `url:"channel"`
} }
// UpdateData struct
type UpdateData struct {
Message UpdateMessage `url:"message"`
Channel uint64 `url:"channel"`
}
// DeleteData struct
type DeleteData struct {
Message Message `url:"message"`
Channel uint64 `url:"channel"`
}
// MessagesResponse message event response // MessagesResponse message event response
type MessagesResponse struct { type MessagesResponse struct {
MessageID string `json:"message_id"` MessageID int `json:"message_id"`
Time time.Time `json:"time"` Time time.Time `json:"time"`
} }