mock requests in tests

This commit is contained in:
Akolzin Dmitry 2019-03-07 12:58:09 +03:00
parent 0dc56b83e5
commit 381f96af46
3 changed files with 207 additions and 68 deletions

1
.env.dist Normal file
View File

@ -0,0 +1 @@
MG_DEBUG=false

View File

@ -1,9 +1,15 @@
language: go language: go
env:
global:
- MG_DEBUG=false
go: go:
- '1.8' - '1.8'
- '1.9' - '1.9'
- '1.10' - '1.10'
- '1.11' - '1.11'
before_install: before_install:
- go get -v github.com/google/go-querystring/query - go get -v github.com/google/go-querystring/query
- go get -v github.com/stretchr/testify/assert - go get -v github.com/stretchr/testify/assert

View File

@ -1,6 +1,7 @@
package v1 package v1
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"math/rand" "math/rand"
@ -11,6 +12,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/h2non/gock"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -30,25 +32,37 @@ func TestMain(m *testing.M) {
if err != nil { if err != nil {
log.Fatal("Error loading .env file") log.Fatal("Error loading .env file")
} }
}
os.Exit(m.Run()) os.Exit(m.Run())
}
} }
var ( var (
mgURL = os.Getenv("MG_URL") mgURL = "https://api.example.com"
mgToken = os.Getenv("MG_BOT_TOKEN") mgToken = "test_token"
debug, _ = strconv.ParseBool(os.Getenv("DEBUG"))
) )
func client() *MgClient { func client() *MgClient {
c := New(mgURL, mgToken) c := New(mgURL, mgToken)
if debug != false {
c.Debug = true c.Debug = true
}
return c return c
} }
func TestMgClient_Bots(t *testing.T) { func TestMgClient_Bots(t *testing.T) {
c := client() c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/bots").
Reply(200).
BodyString(`[{"id": 1, "name": "Test Bot", "created_at": "2018-01-01T00:00:00.000000Z", "is_active": true, "is_self": true}]`)
req := BotsRequest{Active: 1} req := BotsRequest{Active: 1}
data, status, err := c.Bots(req) data, status, err := c.Bots(req)
@ -66,6 +80,14 @@ func TestMgClient_Bots(t *testing.T) {
func TestMgClient_Channels(t *testing.T) { func TestMgClient_Channels(t *testing.T) {
c := client() c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/channels").
Reply(200).
BodyString(`[{"id": 1, "type":"test_type", "name": "Test Bot", "created_at": "2018-01-01T00:00:00.000000Z", "is_active": true}]`)
req := ChannelsRequest{Active: 1} req := ChannelsRequest{Active: 1}
data, status, err := c.Channels(req) data, status, err := c.Channels(req)
@ -83,6 +105,14 @@ func TestMgClient_Channels(t *testing.T) {
func TestMgClient_Users(t *testing.T) { func TestMgClient_Users(t *testing.T) {
c := client() c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/users").
Reply(200).
BodyString(`[{"id": 1, "external_id":"1", "username": "Test", "first_name":"Test", "last_name":"Test", "created_at": "2018-01-01T00:00:00.000000Z", "is_active": true, "is_online": true}]`)
req := UsersRequest{Active: 1} req := UsersRequest{Active: 1}
data, status, err := c.Users(req) data, status, err := c.Users(req)
@ -100,6 +130,14 @@ func TestMgClient_Users(t *testing.T) {
func TestMgClient_Customers(t *testing.T) { func TestMgClient_Customers(t *testing.T) {
c := client() c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/customers").
Reply(200).
BodyString(`[{"id": 1,"channel_id": 1, "created_at": "2018-01-01T00:00:00.000000Z"}]`)
req := CustomersRequest{} req := CustomersRequest{}
data, status, err := c.Customers(req) data, status, err := c.Customers(req)
@ -117,6 +155,14 @@ func TestMgClient_Customers(t *testing.T) {
func TestMgClient_Chats(t *testing.T) { func TestMgClient_Chats(t *testing.T) {
c := client() c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/chats").
Reply(200).
BodyString(`[{"id": 1,"customer": {"id": 1, "name": "Test"}, "created_at": "2018-01-01T00:00:00.000000Z"}]`)
req := ChatsRequest{ChannelType: ChannelTypeTelegram} req := ChatsRequest{ChannelType: ChannelTypeTelegram}
data, status, err := c.Chats(req) data, status, err := c.Chats(req)
@ -134,6 +180,14 @@ func TestMgClient_Chats(t *testing.T) {
func TestMgClient_Members(t *testing.T) { func TestMgClient_Members(t *testing.T) {
c := client() c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/members").
Reply(200).
BodyString(`[{"id": 1,"user_id": 1, "chat_id": 1, "created_at": "2018-01-01T00:00:00.000000Z"}]`)
req := MembersRequest{State: ChatMemberStateLeaved} req := MembersRequest{State: ChatMemberStateLeaved}
data, status, err := c.Members(req) data, status, err := c.Members(req)
@ -150,6 +204,14 @@ func TestMgClient_Members(t *testing.T) {
func TestMgClient_Dialogs(t *testing.T) { func TestMgClient_Dialogs(t *testing.T) {
c := client() c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/dialogs").
Reply(200).
BodyString(`[{"id": 1, "chat_id": 1, "created_at": "2018-01-01T00:00:00.000000Z"}]`)
req := DialogsRequest{Active: 0} req := DialogsRequest{Active: 0}
data, status, err := c.Dialogs(req) data, status, err := c.Dialogs(req)
@ -167,9 +229,19 @@ func TestMgClient_Dialogs(t *testing.T) {
func TestMgClient_DialogAssign(t *testing.T) { func TestMgClient_DialogAssign(t *testing.T) {
c := client() c := client()
i, err := strconv.ParseUint(os.Getenv("MG_BOT_DIALOG"), 10, 64)
m, err := strconv.ParseUint(os.Getenv("MG_BOT_USER"), 10, 64) d := 1
req := DialogAssignRequest{DialogID: i, UserID: m} u := 1
req := DialogAssignRequest{DialogID: uint64(d), UserID: uint64(u)}
r, _ := json.Marshal(req)
defer gock.Off()
gock.New(mgURL).
Patch("/api/bot/v1/dialogs/1/assign").
JSON(r).
Reply(400).
BodyString(`{"errors": ["dialog is not the latest in the chat"]}`)
_, status, err := c.DialogAssign(req) _, status, err := c.DialogAssign(req)
@ -179,8 +251,16 @@ func TestMgClient_DialogAssign(t *testing.T) {
func TestMgClient_DialogClose(t *testing.T) { func TestMgClient_DialogClose(t *testing.T) {
c := client() c := client()
i, err := strconv.ParseUint(os.Getenv("MG_BOT_DIALOG"), 10, 64) i := 1
_, status, err := c.DialogClose(i)
defer gock.Off()
gock.New(mgURL).
Delete("/api/bot/v1/dialogs/1/close").
Reply(400).
BodyString(`{"errors": ["dialog #1 not found"]}`)
_, status, err := c.DialogClose(uint64(i))
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, http.StatusBadRequest, status) assert.Equal(t, http.StatusBadRequest, status)
@ -188,6 +268,14 @@ func TestMgClient_DialogClose(t *testing.T) {
func TestMgClient_Messages(t *testing.T) { func TestMgClient_Messages(t *testing.T) {
c := client() c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/messages").
Reply(200).
BodyString(`[{"id": 1, "time": "2018-01-01T00:00:00+03:00", "type": "text", "scope": "public", "chat_id": 1, "is_read": false, "is_edit": false, "status": "received", "created_at": "2018-01-01T00:00:00.000000Z"}]`)
req := MessagesRequest{ChannelType: ChannelTypeTelegram, Scope: MessageScopePublic} req := MessagesRequest{ChannelType: ChannelTypeTelegram, Scope: MessageScopePublic}
data, status, err := c.Messages(req) data, status, err := c.Messages(req)
@ -205,7 +293,8 @@ func TestMgClient_Messages(t *testing.T) {
func TestMgClient_MessageSendText(t *testing.T) { func TestMgClient_MessageSendText(t *testing.T) {
c := client() c := client()
i, err := strconv.ParseUint(os.Getenv("MG_BOT_CHAT"), 10, 64)
i := uint64(1)
message := MessageSendRequest{ message := MessageSendRequest{
Type: MsgTypeText, Type: MsgTypeText,
Scope: "public", Scope: "public",
@ -213,6 +302,14 @@ func TestMgClient_MessageSendText(t *testing.T) {
ChatID: i, ChatID: i,
} }
defer gock.Off()
gock.New(mgURL).
Post("/api/bot/v1/messages").
JSON(message).
Reply(200).
BodyString(`{"message_id": 1, "time": "2018-01-01T00:00:00+03:00"}`)
data, status, err := c.MessageSend(message) data, status, err := c.MessageSend(message)
if err != nil { if err != nil {
t.Errorf("%d %v", status, err) t.Errorf("%d %v", status, err)
@ -225,7 +322,7 @@ func TestMgClient_MessageSendText(t *testing.T) {
func TestMgClient_MessageSendProduct(t *testing.T) { func TestMgClient_MessageSendProduct(t *testing.T) {
c := client() c := client()
msg, _, err := c.MessageSend(MessageSendRequest{ message := MessageSendRequest{
Type: MsgTypeProduct, Type: MsgTypeProduct,
ChatID: 5, ChatID: 5,
Scope: "public", Scope: "public",
@ -243,7 +340,17 @@ func TestMgClient_MessageSendProduct(t *testing.T) {
Value: 1, Value: 1,
}, },
}, },
}) }
defer gock.Off()
gock.New(mgURL).
Post("/api/bot/v1/messages").
JSON(message).
Reply(200).
BodyString(`{"message_id": 1, "time": "2018-01-01T00:00:00+03:00"}`)
msg, _, err := c.MessageSend(message)
if err != nil { if err != nil {
t.Errorf("%v", err) t.Errorf("%v", err)
@ -256,7 +363,7 @@ func TestMgClient_MessageSendProduct(t *testing.T) {
func TestMgClient_MessageSendOrder(t *testing.T) { func TestMgClient_MessageSendOrder(t *testing.T) {
c := client() c := client()
msg, _, err := c.MessageSend(MessageSendRequest{ message := MessageSendRequest{
Type: MsgTypeOrder, Type: MsgTypeOrder,
ChatID: 5, ChatID: 5,
Scope: "public", Scope: "public",
@ -293,7 +400,17 @@ func TestMgClient_MessageSendOrder(t *testing.T) {
}, },
}, },
}, },
}) }
defer gock.Off()
gock.New(mgURL).
Post("/api/bot/v1/messages").
JSON(message).
Reply(200).
BodyString(`{"message_id": 1, "time": "2018-01-01T00:00:00+03:00"}`)
msg, _, err := c.MessageSend(message)
if err != nil { if err != nil {
t.Errorf("%v", err) t.Errorf("%v", err)
@ -311,28 +428,21 @@ func TestMgClient_RandomStringGenerator(t *testing.T) {
func TestMgClient_MessageEdit(t *testing.T) { func TestMgClient_MessageEdit(t *testing.T) {
c := client() c := client()
i, err := strconv.ParseUint(os.Getenv("MG_BOT_CHAT"), 10, 64)
message := MessageSendRequest{
Type: MsgTypeText,
Scope: "public",
Content: "test",
ChatID: i,
}
s, status, err := c.MessageSend(message) message := MessageEditRequest{
if err != nil { ID: uint64(1),
t.Errorf("%d %v", status, err)
}
assert.NoError(t, err)
assert.NotEmpty(t, s.MessageID)
edit := MessageEditRequest{
ID: s.MessageID,
Content: "test", Content: "test",
} }
e, status, err := c.MessageEdit(edit) defer gock.Off()
gock.New(mgURL).
Patch("/api/bot/v1/messages/1").
JSON(message).
Reply(200).
BodyString(`{"message_id": 1, "time": "2018-01-01T00:00:00+03:00"}`)
e, status, err := c.MessageEdit(message)
if err != nil { if err != nil {
t.Errorf("%d %v", status, err) t.Errorf("%d %v", status, err)
} }
@ -342,23 +452,15 @@ func TestMgClient_MessageEdit(t *testing.T) {
func TestMgClient_MessageDelete(t *testing.T) { func TestMgClient_MessageDelete(t *testing.T) {
c := client() c := client()
i, err := strconv.ParseUint(os.Getenv("MG_BOT_CHAT"), 10, 64)
message := MessageSendRequest{
Type: MsgTypeText,
Scope: "public",
Content: "test",
ChatID: i,
}
s, status, err := c.MessageSend(message) defer gock.Off()
if err != nil {
t.Errorf("%d %v", status, err)
}
assert.NoError(t, err) gock.New(mgURL).
assert.NotEmpty(t, s.MessageID) Delete("/api/bot/v1/messages/1").
Reply(200).
BodyString(`{}`)
d, status, err := c.MessageDelete(s.MessageID) d, status, err := c.MessageDelete(1)
if err != nil { if err != nil {
t.Errorf("%d %v", status, err) t.Errorf("%d %v", status, err)
} }
@ -368,7 +470,15 @@ func TestMgClient_MessageDelete(t *testing.T) {
func TestMgClient_Info(t *testing.T) { func TestMgClient_Info(t *testing.T) {
c := client() c := client()
req := InfoRequest{Name: "AWESOME", Avatar: os.Getenv("MG_BOT_LOGO")} req := InfoRequest{Name: "AWESOME", Avatar: "https://test.com/awesome_bot_avatar"}
defer gock.Off()
gock.New(mgURL).
Patch("/api/bot/v1/my/info").
JSON(req).
Reply(200).
BodyString(`{}`)
_, status, err := c.Info(req) _, status, err := c.Info(req)
if err != nil { if err != nil {
@ -380,6 +490,14 @@ func TestMgClient_Info(t *testing.T) {
func TestMgClient_Commands(t *testing.T) { func TestMgClient_Commands(t *testing.T) {
c := client() c := client()
defer gock.Off()
gock.New(mgURL).
Get("/api/bot/v1/my/commands").
Reply(200).
BodyString(`[{"id": 1, "name": "command_name", "description": "Command description", "created_at": "2018-01-01T00:00:00.000000Z"}]`)
req := CommandsRequest{} req := CommandsRequest{}
data, status, err := c.Commands(req) data, status, err := c.Commands(req)
@ -398,10 +516,18 @@ func TestMgClient_Commands(t *testing.T) {
func TestMgClient_CommandEditDelete(t *testing.T) { func TestMgClient_CommandEditDelete(t *testing.T) {
c := client() c := client()
req := CommandEditRequest{ req := CommandEditRequest{
Name: "show_payment_types", Name: "test_command",
Description: "Get available payment types", Description: "Test command",
} }
defer gock.Off()
gock.New(mgURL).
Put("/api/bot/v1/my/commands/test_command").
JSON(req).
Reply(200).
BodyString(`{"id": 1, "name": "test_command", "description": "Test description"}`)
n, status, err := c.CommandEdit(req) n, status, err := c.CommandEdit(req)
if err != nil { if err != nil {
t.Errorf("%d %v", status, err) t.Errorf("%d %v", status, err)
@ -410,6 +536,11 @@ func TestMgClient_CommandEditDelete(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.NotEmpty(t, n.ID) assert.NotEmpty(t, n.ID)
gock.New(mgURL).
Delete("/api/bot/v1/my/commands/test_command").
Reply(200).
BodyString(`{}`)
d, status, err := c.CommandDelete(n.Name) d, status, err := c.CommandDelete(n.Name)
if err != nil { if err != nil {
t.Errorf("%d %v", status, err) t.Errorf("%d %v", status, err)
@ -444,6 +575,12 @@ func TestMgClient_UploadFile(t *testing.T) {
} }
defer resp.Body.Close() defer resp.Body.Close()
defer gock.Off()
gock.New(mgURL).
Post("/api/bot/v1/files/upload").
Reply(200).
BodyString(`{"created_at": "2018-01-01T00:00:00.000000Z", "hash": "hash", "id": "1"}`)
data, status, err := c.UploadFile(resp.Body) data, status, err := c.UploadFile(resp.Body)
@ -454,12 +591,21 @@ func TestMgClient_UploadFile(t *testing.T) {
t.Logf("File %+v is upload", data) t.Logf("File %+v is upload", data)
} }
func TestMgClient_ImageMessages(t *testing.T) { func TestMgClient_UploadFileByUrl(t *testing.T) {
c := client() c := client()
file := UploadFileByUrlRequest{
uploadFileResponse, st, err := c.UploadFileByURL(UploadFileByUrlRequest{
Url: "https://via.placeholder.com/300", Url: "https://via.placeholder.com/300",
}) }
defer gock.Off()
gock.New(mgURL).
Post("/api/bot/v1/files/upload_by_url").
JSON(file).
Reply(200).
BodyString(`{"created_at": "2018-01-01T00:00:00.000000Z", "hash": "hash", "id": "1"}`)
uploadFileResponse, st, err := c.UploadFileByURL(file)
if st != http.StatusOK { if st != http.StatusOK {
t.Errorf("%v", err) t.Errorf("%v", err)
@ -467,21 +613,7 @@ func TestMgClient_ImageMessages(t *testing.T) {
t.Logf("File %+v is upload", uploadFileResponse.ID) t.Logf("File %+v is upload", uploadFileResponse.ID)
i, err := strconv.ParseUint(os.Getenv("MG_BOT_CHAT"), 10, 64)
message := MessageSendRequest{
Type: MsgTypeImage,
Scope: MessageScopePublic,
Items: []Item{{ID: uploadFileResponse.ID}},
ChatID: i,
}
data, status, err := c.MessageSend(message)
if err != nil {
t.Errorf("%d %v", status, err)
}
assert.NoError(t, err) assert.NoError(t, err)
assert.NotEmpty(t, data.MessageID)
} }
func RandStringBytesMaskImprSrc(n int) string { func RandStringBytesMaskImprSrc(n int) string {