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

templates list data type fix, WIP tests

This commit is contained in:
Pavel 2020-04-08 13:11:46 +03:00
parent ef0905e3ac
commit 99d9784f73
4 changed files with 197 additions and 50 deletions

View File

@ -1,11 +1,14 @@
language: go
env:
- GO111MODULE=on
go:
- '1.11'
- '1.12'
- '1.13'
- '1.14'
env:
- GO111MODULE=on
before_install:
- go mod tidy
script: go test -v ./...
script:
- go test ./... -v -cpu 2 -timeout 2m -race -cover -coverprofile=coverage.txt -covermode=atomic
after_success:
- bash <(curl -s https://codecov.io/bash)

View File

@ -27,40 +27,6 @@ func NewWithClient(url string, token string, client *http.Client) *MgClient {
}
}
// TransportChannels returns channels list
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// data, status, err := client.TransportChannels(Channels{Active: true})
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("Status: %v, Channels found: %v", status, len(data))
func (c *MgClient) TransportChannels(request Channels) ([]ChannelListItem, int, error) {
var resp []ChannelListItem
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/channels?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// TransportTemplates returns templates list
//
// Example:
@ -74,8 +40,8 @@ func (c *MgClient) TransportChannels(request Channels) ([]ChannelListItem, int,
// }
//
// fmt.Printf("Status: %v, Templates found: %v", status, len(data))
func (c *MgClient) TransportTemplates() ([]TemplateItem, int, error) {
var resp []TemplateItem
func (c *MgClient) TransportTemplates() ([]Template, int, error) {
var resp []Template
data, status, err := c.GetRequest("/templates", []byte{})
if err != nil {
@ -207,6 +173,40 @@ func (c *MgClient) DeactivateTemplate(channelID uint64, templateCode string) (in
return status, err
}
// TransportChannels returns channels list
//
// Example:
//
// var client = v1.New("https://token.url", "cb8ccf05e38a47543ad8477d4999be73bff503ea6")
//
// data, status, err := client.TransportChannels(Channels{Active: true})
//
// if err != nil {
// fmt.Printf("%v", err)
// }
//
// fmt.Printf("Status: %v, Channels found: %v", status, len(data))
func (c *MgClient) TransportChannels(request Channels) ([]ChannelListItem, int, error) {
var resp []ChannelListItem
var b []byte
outgoing, _ := query.Values(request)
data, status, err := c.GetRequest(fmt.Sprintf("/channels?%s", outgoing.Encode()), b)
if err != nil {
return resp, status, err
}
if e := json.Unmarshal(data, &resp); e != nil {
return resp, status, e
}
if status > http.StatusCreated || status < http.StatusOK {
return resp, status, c.Error(data)
}
return resp, status, err
}
// ActivateTransportChannel implement channel activation
//
// Example:

View File

@ -1,11 +1,16 @@
package v1
import (
"bytes"
"encoding/base64"
"fmt"
"net/http"
"os"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
@ -13,6 +18,8 @@ var (
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 {
@ -22,6 +29,62 @@ func client() *MgClient {
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
}
func TestMgClient_TransportChannels(t *testing.T) {
c := client()
@ -177,6 +240,87 @@ func TestMgClient_UpdateTransportChannel(t *testing.T) {
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))
t.Logf("%#v", data)
}
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()
t.Logf("%v", ext)
@ -215,7 +359,7 @@ func TestMgClient_ImageMessages(t *testing.T) {
t.Logf("%v", ext)
uploadFileResponse, st, err := c.UploadFileByURL(UploadFileByUrlRequest{
Url: "https://via.placeholder.com/300",
Url: "https://via.placeholder.com/1",
})
if st != http.StatusOK {
@ -345,14 +489,14 @@ func TestMgClient_UploadFile(t *testing.T) {
c := client()
t.Logf("%v", ext)
resp, err := http.Get("https://via.placeholder.com/300")
// 1x1 png picture
img := "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/TQBcNTh/AAAAAXRSTlPM0jRW/QAAAApJREFUeJxjYgAAAAYAAzY3fKgAAAAASUVORK5CYII="
binary, err := base64.StdEncoding.DecodeString(img)
if err != nil {
t.Errorf("%v", err)
t.Errorf("cannot convert base64 to binary: %s", err)
}
defer resp.Body.Close()
data, status, err := c.UploadFile(resp.Body)
data, status, err := c.UploadFile(bytes.NewReader(binary))
if status != http.StatusOK {
t.Errorf("%v", err)

View File

@ -38,7 +38,7 @@ var templateVarAssoc = map[string]interface{}{
// Template struct
type Template struct {
Code string `json:"code"`
ChannelID int64 `json:"channel_id,omitempty"`
ChannelID uint64 `json:"channel_id,omitempty"`
Name string `json:"name"`
Enabled bool `json:"enabled,omitempty"`
Type string `json:"type"`