Extracted scenario
This commit is contained in:
parent
c02435ccaa
commit
cc2058090c
13 changed files with 235 additions and 173 deletions
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
@ -10,6 +9,9 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/scenario"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gitlab.com/flygrounder/go-mtg-vk/internal/caching"
|
"gitlab.com/flygrounder/go-mtg-vk/internal/caching"
|
||||||
"gitlab.com/flygrounder/go-mtg-vk/internal/vk"
|
"gitlab.com/flygrounder/go-mtg-vk/internal/vk"
|
||||||
|
|
@ -26,17 +28,19 @@ func main() {
|
||||||
var dictMap map[string]string
|
var dictMap map[string]string
|
||||||
_ = json.Unmarshal(dictBytes, &dictMap)
|
_ = json.Unmarshal(dictBytes, &dictMap)
|
||||||
handler := vk.Handler{
|
handler := vk.Handler{
|
||||||
|
Scenario: &scenario.Scenario{
|
||||||
Sender: &vk.ApiSender{
|
Sender: &vk.ApiSender{
|
||||||
Token: os.Getenv("VK_TOKEN"),
|
Token: os.Getenv("VK_TOKEN"),
|
||||||
},
|
},
|
||||||
Logger: log.New(os.Stdout, "", 0),
|
Logger: log.New(os.Stdout, "", 0),
|
||||||
SecretKey: os.Getenv("VK_SECRET_KEY"),
|
|
||||||
GroupId: groupId,
|
|
||||||
ConfirmationString: os.Getenv("VK_CONFIRMATION_STRING"),
|
|
||||||
Cache: caching.NewClient("redis:6379", "", time.Hour*24, 0),
|
Cache: caching.NewClient("redis:6379", "", time.Hour*24, 0),
|
||||||
InfoFetcher: &cardsinfo.Fetcher{
|
InfoFetcher: &cardsinfo.Fetcher{
|
||||||
Dict: dictMap,
|
Dict: dictMap,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
SecretKey: os.Getenv("VK_SECRET_KEY"),
|
||||||
|
GroupId: groupId,
|
||||||
|
ConfirmationString: os.Getenv("VK_CONFIRMATION_STRING"),
|
||||||
}
|
}
|
||||||
|
|
||||||
r.POST("callback/message", handler.HandleMessage)
|
r.POST("callback/message", handler.HandleMessage)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
package caching
|
package caching
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-redis/redis"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CacheClient struct {
|
type CacheClient struct {
|
||||||
89
internal/scenario/scenario.go
Normal file
89
internal/scenario/scenario.go
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
package scenario
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
incorrectMessage = "Некорректная команда"
|
||||||
|
cardNotFoundMessage = "Карта не найдена"
|
||||||
|
pricesUnavailableMessage = "Цены временно недоступны, попробуйте позже"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Scenario struct {
|
||||||
|
Sender Sender
|
||||||
|
Logger *log.Logger
|
||||||
|
InfoFetcher CardInfoFetcher
|
||||||
|
Cache CardCache
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserMessage struct {
|
||||||
|
Body string
|
||||||
|
UserId int64
|
||||||
|
}
|
||||||
|
|
||||||
|
type CardCache interface {
|
||||||
|
Get(cardName string) (string, error)
|
||||||
|
Set(cardName string, message string)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CardInfoFetcher interface {
|
||||||
|
GetFormattedCardPrices(name string) (string, error)
|
||||||
|
GetNameByCardId(set string, number string) string
|
||||||
|
GetOriginalName(name string) string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Sender interface {
|
||||||
|
Send(userId int64, message string)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scenario) HandleSearch(msg *UserMessage) {
|
||||||
|
cardName, err := s.getCardNameByCommand(msg.Body)
|
||||||
|
if err != nil {
|
||||||
|
s.Sender.Send(msg.UserId, incorrectMessage)
|
||||||
|
s.Logger.Printf("[info] Not correct command. Message: %s user input: %s", err.Error(), msg.Body)
|
||||||
|
} else if cardName == "" {
|
||||||
|
s.Sender.Send(msg.UserId, cardNotFoundMessage)
|
||||||
|
s.Logger.Printf("[info] Could not find card. User input: %s", msg.Body)
|
||||||
|
} else {
|
||||||
|
message, err := s.getMessage(cardName)
|
||||||
|
if err != nil {
|
||||||
|
s.Sender.Send(msg.UserId, pricesUnavailableMessage)
|
||||||
|
s.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.Sender.Send(msg.UserId, message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scenario) getMessage(cardName string) (string, error) {
|
||||||
|
val, err := s.Cache.Get(cardName)
|
||||||
|
if err != nil {
|
||||||
|
message, err := s.InfoFetcher.GetFormattedCardPrices(cardName)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
s.Cache.Set(cardName, message)
|
||||||
|
return message, nil
|
||||||
|
}
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scenario) getCardNameByCommand(command string) (string, error) {
|
||||||
|
var name string
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(command, "!s"):
|
||||||
|
split := strings.Split(command, " ")
|
||||||
|
if len(split) < 3 {
|
||||||
|
return "", errors.New("wrong command")
|
||||||
|
}
|
||||||
|
set := split[1]
|
||||||
|
number := split[2]
|
||||||
|
name = s.InfoFetcher.GetNameByCardId(set, number)
|
||||||
|
default:
|
||||||
|
name = s.InfoFetcher.GetOriginalName(command)
|
||||||
|
}
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
82
internal/scenario/scenario_test.go
Normal file
82
internal/scenario/scenario_test.go
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
package scenario
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestScenario_HandleSearch_BadCommand(t *testing.T) {
|
||||||
|
testCtx := GetTestScenarioCtx()
|
||||||
|
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||||
|
Body: "!s",
|
||||||
|
UserId: 1,
|
||||||
|
})
|
||||||
|
assert.Equal(t, []testMessage{
|
||||||
|
{
|
||||||
|
userId: 1,
|
||||||
|
message: incorrectMessage,
|
||||||
|
},
|
||||||
|
}, testCtx.Sender.sent)
|
||||||
|
assert.True(t, strings.Contains(testCtx.LogBuf.String(), "[info]"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScenario_HandleSearch_GoodCommand(t *testing.T) {
|
||||||
|
testCtx := GetTestScenarioCtx()
|
||||||
|
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||||
|
Body: "!s grn 228",
|
||||||
|
UserId: 1,
|
||||||
|
})
|
||||||
|
assert.Equal(t, []testMessage{
|
||||||
|
{
|
||||||
|
userId: 1,
|
||||||
|
message: "good",
|
||||||
|
},
|
||||||
|
}, testCtx.Sender.sent)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScenario_HandleSearch_NotFoundCard(t *testing.T) {
|
||||||
|
testCtx := GetTestScenarioCtx()
|
||||||
|
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||||
|
Body: "absolutely_random_card",
|
||||||
|
UserId: 1,
|
||||||
|
})
|
||||||
|
assert.Equal(t, []testMessage{
|
||||||
|
{
|
||||||
|
userId: 1,
|
||||||
|
message: cardNotFoundMessage,
|
||||||
|
},
|
||||||
|
}, testCtx.Sender.sent)
|
||||||
|
assert.True(t, strings.Contains(testCtx.LogBuf.String(), "[info]"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestScenario_HandleSearch_BadCard(t *testing.T) {
|
||||||
|
testCtx := GetTestScenarioCtx()
|
||||||
|
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||||
|
Body: "bad",
|
||||||
|
UserId: 1,
|
||||||
|
})
|
||||||
|
assert.Equal(t, []testMessage{
|
||||||
|
{
|
||||||
|
userId: 1,
|
||||||
|
message: pricesUnavailableMessage,
|
||||||
|
},
|
||||||
|
}, testCtx.Sender.sent)
|
||||||
|
assert.True(t, strings.Contains(testCtx.LogBuf.String(), "[error]"))
|
||||||
|
}
|
||||||
|
func TestScenario_HandleSearch_Uncached(t *testing.T) {
|
||||||
|
testCtx := GetTestScenarioCtx()
|
||||||
|
testCtx.Scenario.HandleSearch(&UserMessage{
|
||||||
|
Body: "uncached",
|
||||||
|
UserId: 1,
|
||||||
|
})
|
||||||
|
assert.Equal(t, []testMessage{
|
||||||
|
{
|
||||||
|
userId: 1,
|
||||||
|
message: "uncached",
|
||||||
|
},
|
||||||
|
}, testCtx.Sender.sent)
|
||||||
|
msg, _ := testCtx.Scenario.Cache.Get("uncached")
|
||||||
|
assert.Equal(t, "uncached", msg)
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package vk
|
package scenario
|
||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
31
internal/scenario/test_ctx.go
Normal file
31
internal/scenario/test_ctx.go
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
package scenario
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TestScenarioCtx struct {
|
||||||
|
Scenario *Scenario
|
||||||
|
Sender *testSender
|
||||||
|
LogBuf *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTestScenarioCtx() TestScenarioCtx {
|
||||||
|
sender := &testSender{}
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
return TestScenarioCtx{
|
||||||
|
LogBuf: buf,
|
||||||
|
Scenario: &Scenario{
|
||||||
|
Sender: sender,
|
||||||
|
Logger: log.New(buf, "", 0),
|
||||||
|
InfoFetcher: &testInfoFetcher{},
|
||||||
|
Cache: &testCache{
|
||||||
|
table: map[string]string{
|
||||||
|
"good": "good",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Sender: sender,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package vk
|
package scenario
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
package vk
|
package scenario
|
||||||
|
|
||||||
type testSender struct {
|
type testSender struct {
|
||||||
sent []testMessage
|
sent []testMessage
|
||||||
|
|
@ -9,7 +9,7 @@ type testMessage struct {
|
||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *testSender) send(userId int64, message string) {
|
func (s *testSender) Send(userId int64, message string) {
|
||||||
s.sent = append(s.sent, testMessage{
|
s.sent = append(s.sent, testMessage{
|
||||||
userId: userId,
|
userId: userId,
|
||||||
message: message,
|
message: message,
|
||||||
|
|
@ -1,23 +1,18 @@
|
||||||
package vk
|
package vk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/scenario"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
Sender sender
|
Scenario *scenario.Scenario
|
||||||
Logger *log.Logger
|
|
||||||
SecretKey string
|
SecretKey string
|
||||||
GroupId int64
|
GroupId int64
|
||||||
ConfirmationString string
|
ConfirmationString string
|
||||||
DictPath string
|
DictPath string
|
||||||
Cache cardCache
|
|
||||||
InfoFetcher cardInfoFetcher
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type cardInfoFetcher interface {
|
type cardInfoFetcher interface {
|
||||||
|
|
@ -59,62 +54,16 @@ func (h *Handler) HandleMessage(c *gin.Context) {
|
||||||
case "confirmation":
|
case "confirmation":
|
||||||
h.handleConfirmation(c, &req)
|
h.handleConfirmation(c, &req)
|
||||||
case "message_new":
|
case "message_new":
|
||||||
go h.handleSearch(&req)
|
go h.Scenario.HandleSearch(&scenario.UserMessage{
|
||||||
|
Body: req.Object.Body,
|
||||||
|
UserId: req.Object.UserId,
|
||||||
|
})
|
||||||
c.String(http.StatusOK, "ok")
|
c.String(http.StatusOK, "ok")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) handleSearch(req *messageRequest) {
|
|
||||||
cardName, err := h.getCardNameByCommand(req.Object.Body)
|
|
||||||
if err != nil {
|
|
||||||
h.Sender.send(req.Object.UserId, incorrectMessage)
|
|
||||||
h.Logger.Printf("[info] Not correct command. Message: %s user input: %s", err.Error(), req.Object.Body)
|
|
||||||
} else if cardName == "" {
|
|
||||||
h.Sender.send(req.Object.UserId, cardNotFoundMessage)
|
|
||||||
h.Logger.Printf("[info] Could not find card. User input: %s", req.Object.Body)
|
|
||||||
} else {
|
|
||||||
message, err := h.getMessage(cardName)
|
|
||||||
if err != nil {
|
|
||||||
h.Sender.send(req.Object.UserId, pricesUnavailableMessage)
|
|
||||||
h.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
h.Sender.send(req.Object.UserId, message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) handleConfirmation(c *gin.Context, req *messageRequest) {
|
func (h *Handler) handleConfirmation(c *gin.Context, req *messageRequest) {
|
||||||
if (req.Type == "confirmation") && (req.GroupId == h.GroupId) {
|
if (req.Type == "confirmation") && (req.GroupId == h.GroupId) {
|
||||||
c.String(http.StatusOK, h.ConfirmationString)
|
c.String(http.StatusOK, h.ConfirmationString)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) getMessage(cardName string) (string, error) {
|
|
||||||
val, err := h.Cache.Get(cardName)
|
|
||||||
if err != nil {
|
|
||||||
message, err := h.InfoFetcher.GetFormattedCardPrices(cardName)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
h.Cache.Set(cardName, message)
|
|
||||||
return message, nil
|
|
||||||
}
|
|
||||||
return val, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) getCardNameByCommand(command string) (string, error) {
|
|
||||||
var name string
|
|
||||||
switch {
|
|
||||||
case strings.HasPrefix(command, "!s"):
|
|
||||||
split := strings.Split(command, " ")
|
|
||||||
if len(split) < 3 {
|
|
||||||
return "", errors.New("wrong command")
|
|
||||||
}
|
|
||||||
set := split[1]
|
|
||||||
number := split[2]
|
|
||||||
name = h.InfoFetcher.GetNameByCardId(set, number)
|
|
||||||
default:
|
|
||||||
name = h.InfoFetcher.GetOriginalName(command)
|
|
||||||
}
|
|
||||||
return name, nil
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
package vk
|
package vk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHandler_HandleMessage_Confirm(t *testing.T) {
|
func TestHandler_HandleMessage_Confirm(t *testing.T) {
|
||||||
|
|
@ -36,86 +36,3 @@ func TestHandler_HandleMessage_NoSecretKey(t *testing.T) {
|
||||||
assert.Equal(t, "", testCtx.recorder.Body.String())
|
assert.Equal(t, "", testCtx.recorder.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandler_handleSearch_BadCommand(t *testing.T) {
|
|
||||||
testCtx := getTestHandlerCtx()
|
|
||||||
testCtx.handler.handleSearch(&messageRequest{
|
|
||||||
Object: userMessage{
|
|
||||||
Body: "!s",
|
|
||||||
UserId: 1,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
assert.Equal(t, []testMessage{
|
|
||||||
{
|
|
||||||
userId: 1,
|
|
||||||
message: incorrectMessage,
|
|
||||||
},
|
|
||||||
}, testCtx.sender.sent)
|
|
||||||
assert.True(t, strings.Contains(testCtx.logBuf.String(), "[info]"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandler_handleSearch_GoodCommand(t *testing.T) {
|
|
||||||
testCtx := getTestHandlerCtx()
|
|
||||||
testCtx.handler.handleSearch(&messageRequest{
|
|
||||||
Object: userMessage{
|
|
||||||
Body: "!s grn 228",
|
|
||||||
UserId: 1,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
assert.Equal(t, []testMessage{
|
|
||||||
{
|
|
||||||
userId: 1,
|
|
||||||
message: "good",
|
|
||||||
},
|
|
||||||
}, testCtx.sender.sent)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandler_handleSearch_NotFoundCard(t *testing.T) {
|
|
||||||
testCtx := getTestHandlerCtx()
|
|
||||||
testCtx.handler.handleSearch(&messageRequest{
|
|
||||||
Object: userMessage{
|
|
||||||
Body: "absolutely_random_card",
|
|
||||||
UserId: 1,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
assert.Equal(t, []testMessage{
|
|
||||||
{
|
|
||||||
userId: 1,
|
|
||||||
message: cardNotFoundMessage,
|
|
||||||
},
|
|
||||||
}, testCtx.sender.sent)
|
|
||||||
assert.True(t, strings.Contains(testCtx.logBuf.String(), "[info]"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandler_handleSearch_BadCard(t *testing.T) {
|
|
||||||
testCtx := getTestHandlerCtx()
|
|
||||||
testCtx.handler.handleSearch(&messageRequest{
|
|
||||||
Object: userMessage{
|
|
||||||
Body: "bad",
|
|
||||||
UserId: 1,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
assert.Equal(t, []testMessage{
|
|
||||||
{
|
|
||||||
userId: 1,
|
|
||||||
message: pricesUnavailableMessage,
|
|
||||||
},
|
|
||||||
}, testCtx.sender.sent)
|
|
||||||
assert.True(t, strings.Contains(testCtx.logBuf.String(), "[error]"))
|
|
||||||
}
|
|
||||||
func TestHandler_handleSearch_Uncached(t *testing.T) {
|
|
||||||
testCtx := getTestHandlerCtx()
|
|
||||||
testCtx.handler.handleSearch(&messageRequest{
|
|
||||||
Object: userMessage{
|
|
||||||
Body: "uncached",
|
|
||||||
UserId: 1,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
assert.Equal(t, []testMessage{
|
|
||||||
{
|
|
||||||
userId: 1,
|
|
||||||
message: "uncached",
|
|
||||||
},
|
|
||||||
}, testCtx.sender.sent)
|
|
||||||
msg, _ := testCtx.handler.Cache.Get("uncached")
|
|
||||||
assert.Equal(t, "uncached", msg)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ type errorResponse struct {
|
||||||
ErrorMsg string `json:"error_msg"`
|
ErrorMsg string `json:"error_msg"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ApiSender) send(userId int64, message string) {
|
func (s *ApiSender) Send(userId int64, message string) {
|
||||||
randomId := rand.Int63()
|
randomId := rand.Int63()
|
||||||
params := []string{
|
params := []string{
|
||||||
"access_token=" + s.Token,
|
"access_token=" + s.Token,
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ func TestApiSender_Send_OK(t *testing.T) {
|
||||||
).ParamPresent("random_id").Reply(http.StatusOK)
|
).ParamPresent("random_id").Reply(http.StatusOK)
|
||||||
|
|
||||||
sender := ApiSender{Token: "token"}
|
sender := ApiSender{Token: "token"}
|
||||||
sender.send(1, "msg")
|
sender.Send(1, "msg")
|
||||||
assert.False(t, gock.HasUnmatchedRequest())
|
assert.False(t, gock.HasUnmatchedRequest())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,7 +39,7 @@ func TestApiSender_Send_NotOK(t *testing.T) {
|
||||||
Token: "token",
|
Token: "token",
|
||||||
Logger: log.New(b, "", 0),
|
Logger: log.New(b, "", 0),
|
||||||
}
|
}
|
||||||
sender.send(1, "msg")
|
sender.Send(1, "msg")
|
||||||
assert.True(t, strings.Contains(b.String(), "[error]"))
|
assert.True(t, strings.Contains(b.String(), "[error]"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,6 +60,6 @@ func TestApiSender_Send_ErrorCode(t *testing.T) {
|
||||||
Token: "token",
|
Token: "token",
|
||||||
Logger: log.New(b, "", 0),
|
Logger: log.New(b, "", 0),
|
||||||
}
|
}
|
||||||
sender.send(1, "msg")
|
sender.Send(1, "msg")
|
||||||
assert.True(t, strings.Contains(b.String(), "[error]"))
|
assert.True(t, strings.Contains(b.String(), "[error]"))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,37 +3,26 @@ package vk
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"log"
|
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"gitlab.com/flygrounder/go-mtg-vk/internal/scenario"
|
||||||
)
|
)
|
||||||
|
|
||||||
type testCtx struct {
|
type testCtx struct {
|
||||||
handler *Handler
|
handler *Handler
|
||||||
recorder *httptest.ResponseRecorder
|
recorder *httptest.ResponseRecorder
|
||||||
sender *testSender
|
|
||||||
logBuf *bytes.Buffer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTestHandlerCtx() testCtx {
|
func getTestHandlerCtx() testCtx {
|
||||||
sender := &testSender{}
|
s := scenario.GetTestScenarioCtx()
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
return testCtx{
|
return testCtx{
|
||||||
logBuf: buf,
|
|
||||||
handler: &Handler{
|
handler: &Handler{
|
||||||
SecretKey: "sec",
|
SecretKey: "sec",
|
||||||
GroupId: 10,
|
GroupId: 10,
|
||||||
ConfirmationString: "con",
|
ConfirmationString: "con",
|
||||||
Sender: sender,
|
Scenario: s.Scenario,
|
||||||
Logger: log.New(buf, "", 0),
|
|
||||||
InfoFetcher: &testInfoFetcher{},
|
|
||||||
Cache: &testCache{
|
|
||||||
table: map[string]string{
|
|
||||||
"good": "good",
|
|
||||||
},
|
},
|
||||||
},
|
|
||||||
},
|
|
||||||
sender: sender,
|
|
||||||
recorder: httptest.NewRecorder(),
|
recorder: httptest.NewRecorder(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue