Files
gocryptotrader/communications/slack/slack_test.go
Scott ccfcdf26aa Engine: Protocol Features, coverage, types, BTC markets websocket (#368)
* Attempts to update orderbook so it doesn't need to sort

* Reverts the ws ob stuff. Gets rid of sorting because it happens later. Adds some exchange features

* update existing feature lists. Expands list definition to match my emotions

* Adds bithumb bitmex and bitstamp. adds a couple more types

* Features for you, features for me, features for bittrex, btcmarkets, btse, coinbasepro, coinut, exmo, gateio and gemini

* Features for hitbtc, huobi, itbit, kraken, lakebtc, lbank, localbitcoins, okcoin, okex, poloniex, yobit, zb

* Who can forget good old alphapoint?

* Adds btcmarksets websocket :glitch_crab: fixes alphapoint features

* Adds extra data not in the documentation :/

* Replaces websocket features by using protocol features. However, it breaks it due to import cycles. I'm not sure what I'll do just yet

* Removes import cycle via duplicate structs.

* Increases coverage of config with `TestCheckCurrencyConfigValues`. Moves all currency pair package types into their own files or places it at the bottom of files if necessary

* Increase coverage in code.go

* One way of determining a test has failed, is when to it fails. Removed redundant explanation

* Increases code coverage of conversion

* Lint fixes

* Fixes orderbook tests

* Re-adds sorting because its important to still have the internal pre-processed orderbook to be representative of a real orderbook

* Secret lints that did not show up via Windows linting

* Adds protocol package to contain exchange features

* Fixes protocol implementation

* Fixes ws tests

* Addresses the following: Removes st-st-stutters in config types, changes GetAvailableForexProviders -> GetSupportedForexProviders, removes errors from tests where error is nil, removes orderbook setup when not necessary, removes import newlines, removes false bools from declaration, changes should of to should have

* imports and casing

* Fixes two more nil error checks
2019-10-22 10:56:20 +11:00

364 lines
9.4 KiB
Go

package slack
import (
"testing"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/communications/base"
"github.com/thrasher-corp/gocryptotrader/config"
)
const (
verificationToken = ""
)
var s Slack
type group struct {
ID string `json:"id"`
Name string `json:"name"`
IsGroup bool `json:"is_group"`
Created int64 `json:"created"`
Creator string `json:"creator"`
IsArchived bool `json:"is_archived"`
NameNormalised string `json:"name_normalised"`
IsMPIM bool `json:"is_mpim"`
HasPins bool `json:"has_pins"`
IsOpen bool `json:"is_open"`
LastRead string `json:"last_read"`
Members []string `json:"members"`
Topic struct {
Value string `json:"value"`
Creator string `json:"creator"`
LastSet int64 `json:"last_set"`
} `json:"topic"`
Purpose struct {
Value string `json:"value"`
Creator string `json:"creator"`
LastSet int64 `json:"last_set"`
} `json:"purpose"`
}
func TestSetup(t *testing.T) {
cfg := config.GetConfig()
err := cfg.LoadConfig("../../testdata/configtest.json", true)
if err != nil {
t.Fatal(err)
}
commsCfg := cfg.GetCommunicationsConfig()
s.Setup(&commsCfg)
s.Verbose = true
}
func TestConnect(t *testing.T) {
err := s.Connect()
if err == nil {
t.Error("slack Connect() error cannot be nil")
}
}
func TestPushEvent(t *testing.T) {
t.Parallel()
err := s.PushEvent(base.Event{})
if err == nil {
t.Error("slack PushEvent() error cannot be nil")
}
}
func TestBuildURL(t *testing.T) {
t.Parallel()
v := s.BuildURL("lol123")
if v != "https://slack.com/api/rtm.start?token=lol123" {
t.Error("slack BuildURL() error")
}
}
func TestGetChannelsString(t *testing.T) {
s.Details.Channels = append(s.Details.Channels, struct {
Created int `json:"created"`
Creator string `json:"creator"`
HasPins bool `json:"has_pins"`
ID string `json:"id"`
IsArchived bool `json:"is_archived"`
IsChannel bool `json:"is_channel"`
IsGeneral bool `json:"is_general"`
IsMember bool `json:"is_member"`
IsOrgShared bool `json:"is_org_shared"`
IsShared bool `json:"is_shared"`
Name string `json:"name"`
NameNormalized string `json:"name_normalized"`
PreviousNames []string `json:"previous_names"`
}{
NameNormalized: "General",
})
chans := s.GetChannelsString()
testpassed := false
for i := range chans {
if chans[i] == "General" {
testpassed = true
}
}
if !testpassed {
t.Error("slack GetChannelsString() error")
}
}
func TestGetUsernameByID(t *testing.T) {
username := s.GetUsernameByID("1337")
if username != "" {
t.Error("slack GetUsernameByID() error")
}
s.Details.Users = append(s.Details.Users, struct {
Deleted bool `json:"deleted"`
ID string `json:"id"`
IsBot bool `json:"is_bot"`
Name string `json:"name"`
Presence string `json:"presence"`
Profile struct {
AvatarHash string `json:"avatar_hash"`
Email string `json:"email"`
Fields interface{} `json:"fields"`
FirstName string `json:"first_name"`
Image192 string `json:"image_192"`
Image24 string `json:"image_24"`
Image32 string `json:"image_32"`
Image48 string `json:"image_48"`
Image512 string `json:"image_512"`
Image72 string `json:"image_72"`
LastName string `json:"last_name"`
RealName string `json:"real_name"`
RealNameNormalized string `json:"real_name_normalized"`
} `json:"profile"`
TeamID string `json:"team_id"`
Updated int `json:"updated"`
}{
ID: "1337",
Name: "cranktakular",
})
username = s.GetUsernameByID("1337")
if username != "cranktakular" {
t.Error("slack GetUsernameByID() error")
}
}
func TestGetIDByName(t *testing.T) {
id, err := s.GetIDByName("batman")
if err == nil || id != "" {
t.Error("slack GetIDByName() error")
}
s.Details.Groups = append(s.Details.Groups, group{
Name: "this is a group",
ID: "210314",
})
id, err = s.GetIDByName("this is a group")
if err != nil || id != "210314" {
t.Errorf("slack GetIDByName() Expected '210314' Actual '%s' Error: %s",
id, err)
}
}
func TestGetGroupIDByName(t *testing.T) {
id, err := s.GetGroupIDByName("batman")
if err == nil || id != "" {
t.Error("slack GetGroupIDByName() error")
}
s.Details.Groups = append(s.Details.Groups, group{
Name: "another group",
ID: "11223344",
})
id, err = s.GetGroupIDByName("another group")
if err != nil || id != "11223344" {
t.Errorf("slack GetGroupIDByName() Expected '11223344' Actual '%s' Error: %s",
id, err)
}
}
func TestGetChannelIDByName(t *testing.T) {
id, err := s.GetChannelIDByName("1337")
if err == nil || id != "" {
t.Error("slack GetChannelIDByName() error")
}
s.Details.Channels = append(s.Details.Channels, struct {
Created int `json:"created"`
Creator string `json:"creator"`
HasPins bool `json:"has_pins"`
ID string `json:"id"`
IsArchived bool `json:"is_archived"`
IsChannel bool `json:"is_channel"`
IsGeneral bool `json:"is_general"`
IsMember bool `json:"is_member"`
IsOrgShared bool `json:"is_org_shared"`
IsShared bool `json:"is_shared"`
Name string `json:"name"`
NameNormalized string `json:"name_normalized"`
PreviousNames []string `json:"previous_names"`
}{
ID: "2048",
Name: "Slack Test",
})
id, err = s.GetChannelIDByName("Slack Test")
if err != nil || id != "2048" {
t.Errorf("slack GetChannelIDByName() Expected '2048' Actual '%s' Error: %s",
id, err)
}
}
func TestGetUsersInGroup(t *testing.T) {
username := s.GetUsersInGroup("supergroup")
if len(username) != 0 {
t.Error("slack GetUsersInGroup() error")
}
s.Details.Groups = append(s.Details.Groups, group{
Name: "three guys",
ID: "3",
Members: []string{"Guy one", "Guy two", "Guy three"},
})
username = s.GetUsersInGroup("three guys")
if len(username) != 3 {
t.Errorf("slack GetUsersInGroup() Expected '3' Actual '%s'",
username)
}
}
func TestNewConnection(t *testing.T) {
err := s.NewConnection()
if err == nil {
t.Error("slack NewConnection() error")
}
}
func TestWebsocketConnect(t *testing.T) {
err := s.WebsocketConnect()
if err == nil {
t.Error("slack WebsocketConnect() error")
}
}
func TestHandlePresenceChange(t *testing.T) {
var pres PresenceChange
pres.User = "1337"
pres.Presence = "Present"
err := s.handlePresenceChange([]byte(`{"malformedjson}`))
if err == nil {
t.Error("slack handlePresenceChange(), unmarshalled malformed json")
}
data, _ := common.JSONEncode(pres)
err = s.handlePresenceChange(data)
if err != nil {
t.Errorf("slack handlePresenceChange() Error: %s", err)
}
}
func TestHandleMessageResponse(t *testing.T) {
var data WebsocketResponse
data.ReplyTo = 1
err := s.handleMessageResponse(nil, data)
if err.Error() != "reply to is != 0" {
t.Errorf("slack handleMessageResponse(), Incorrect Error: %s",
err)
}
data.ReplyTo = 0
err = s.handleMessageResponse([]byte(`{"malformedjson}`), data)
if err == nil {
t.Error("slack handleMessageResponse(), unmarshalled malformed json")
}
var msg Message
msg.User = "1337"
msg.Text = "Hello World!"
resp, _ := common.JSONEncode(msg)
err = s.handleMessageResponse(resp, data)
if err != nil {
t.Error("slack HandleMessage(), Sent message through nil websocket")
}
msg.Text = "!notacommand"
resp, _ = common.JSONEncode(msg)
err = s.handleMessageResponse(resp, data)
if err == nil {
t.Errorf("slack handleMessageResponse() Expected error")
}
}
func TestHandleErrorResponse(t *testing.T) {
var data WebsocketResponse
err := s.handleErrorResponse(data)
if err == nil {
t.Error("slack handleErrorResponse() Ignored strange input")
}
data.Error.Msg = "Socket URL has expired"
err = s.handleErrorResponse(data)
if err == nil {
t.Error("slack handleErrorResponse() Didn't error on nil websocket")
}
}
func TestHandleHelloResponse(t *testing.T) {
s.handleHelloResponse()
}
func TestHandleReconnectResponse(t *testing.T) {
err := s.handleReconnectResponse([]byte(`{"malformedjson}`))
if err == nil {
t.Error("slack handleReconnectResponse(), unmarshalled malformed json")
}
var testURL struct {
URL string `json:"url"`
}
testURL.URL = "https://www.thrasher.io"
data, _ := common.JSONEncode(testURL)
err = s.handleReconnectResponse(data)
if err != nil || s.ReconnectURL != "https://www.thrasher.io" {
t.Errorf("slack handleReconnectResponse() Expected 'https://www.thrasher.io' Actual '%s' Error: %s",
s.ReconnectURL, err)
}
}
func TestWebsocketSend(t *testing.T) {
err := s.WebsocketSend("test", "Hello World!")
if err == nil {
t.Error("slack WebsocketSend(), Sent message through nil websocket")
}
}
func TestHandleMessage(t *testing.T) {
msg := &Message{}
err := s.HandleMessage(msg)
if err == nil {
t.Error("slack HandleMessage(), Sent message through nil websocket")
}
msg.Text = cmdStatus
err = s.HandleMessage(msg)
if err == nil {
t.Error("slack HandleMessage(), Sent message through nil websocket")
}
msg.Text = cmdHelp
err = s.HandleMessage(msg)
if err == nil {
t.Error("slack HandleMessage(), Sent message through nil websocket")
}
}