Skip to content

Commit

Permalink
Better testing of WS
Browse files Browse the repository at this point in the history
  • Loading branch information
tsawler committed Mar 12, 2021
1 parent eb9e67a commit b1a1941
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/web/setup-app.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func setupApp() (*string, error) {
log.Println("Host", fmt.Sprintf("%s:%s", *pusherHost, *pusherPort))
log.Println("Secure", *pusherSecure)

app.WsClient = wsClient
app.WsClient = &wsClient
monitorMap := make(map[int]cron.EntryID)
app.MonitorMap = monitorMap

Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package config

import (
"github.com/alexedwards/scs/v2"
"github.com/pusher/pusher-http-go"
"github.com/robfig/cron/v3"
"github.com/tsawler/vigilate/internal/channeldata"
"github.com/tsawler/vigilate/internal/driver"
"github.com/tsawler/vigilate/internal/models"
"html/template"
)

Expand All @@ -18,7 +18,7 @@ type AppConfig struct {
MonitorMap map[int]cron.EntryID
PreferenceMap map[string]string
Scheduler *cron.Cron
WsClient pusher.Client
WsClient models.WSClient
PusherSecret string
TemplateCache map[string]*template.Template
MailQueue chan channeldata.MailJob
Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func TestLoginScreen(t *testing.T) {
}

func TestDBRepo_PusherAuth(t *testing.T) {
// IMPORTANT!!!
// ipe, or whatever pusher service you are using, must be running for this test to pass!
// Now that we have a wrapper for our websocket client, pusher (or ipe)
// does not have to be running. This is much better.
postedData := url.Values{
"socket_id": {"471281528.421564659"},
"channel_name": {"private-channel-1"},
Expand Down
69 changes: 67 additions & 2 deletions internal/handlers/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ func TestMain(m *testing.M) {
app.PreferenceMap = preferenceMap

// create pusher client
wsClient := pusher.Client{
dws := dummyWs{
AppID: "1",
Secret: "123abc",
Key: "abc123",
Secure: false,
Host: "localhost:4001",
}

app.WsClient = wsClient
app.WsClient = &dws

monitorMap := make(map[int]cron.EntryID)
app.MonitorMap = monitorMap
Expand Down Expand Up @@ -92,3 +92,68 @@ func NewTestHandlers(a *config.AppConfig) *DBRepo {
DB: dbrepo.NewTestingRepo(a),
}
}

// dummyWs is a type that satisfies the pusher.Client interface
type dummyWs struct {
AppID string
Key string
Secret string
Host string // host or host:port pair
Secure bool // true for HTTPS
Cluster string
HTTPClient *http.Client
EncryptionMasterKey string // deprecated
EncryptionMasterKeyBase64 string // for E2E
validatedEncryptionMasterKey *[]byte // parsed key for use
}

func (c *dummyWs) Trigger(channel string, eventName string, data interface{}) error {
return nil
}

func (c *dummyWs) TriggerMulti(channels []string, eventName string, data interface{}) error {
return nil
}

func (c *dummyWs) TriggerExclusive(channel string, eventName string, data interface{}, socketID string) error {
return nil
}

func (c *dummyWs) TriggerMultiExclusive(channels []string, eventName string, data interface{}, socketID string) error {
return nil
}

func (c *dummyWs) TriggerBatch(batch []pusher.Event) error {
return nil
}

func (c *dummyWs) Channels(additionalQueries map[string]string) (*pusher.ChannelsList, error) {
var cl pusher.ChannelsList
return &cl, nil
}

func (c *dummyWs) Channel(name string, additionalQueries map[string]string) (*pusher.Channel, error) {
var cl pusher.Channel
return &cl, nil
}

func (c *dummyWs) GetChannelUsers(name string) (*pusher.Users, error) {
var cl pusher.Users
return &cl, nil
}

func (c *dummyWs) AuthenticatePrivateChannel(params []byte) (response []byte, err error) {
return []byte("Hello"), nil
}

func (c *dummyWs) AuthenticatePresenceChannel(params []byte, member pusher.MemberData) (response []byte, err error) {
jStr := `
{"auth":"abc123:b75c9f83f1a1dbe7d6933316348039c6270b27a416286385a0fed98529cf46d1","channel_data":"{\"user_id\":\"1\",\"user_info\":{\"id\":\"1\",\"name\":\"Admin\"}}"}
`
return []byte(jStr), nil
}

func (c *dummyWs) Webhook(header http.Header, body []byte) (*pusher.Webhook, error) {
var wh pusher.Webhook
return &wh, nil
}
17 changes: 17 additions & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package models

import (
"errors"
"github.com/pusher/pusher-http-go"
"github.com/robfig/cron/v3"
"net/http"
"time"
)

Expand Down Expand Up @@ -108,3 +110,18 @@ type Event struct {
CreatedAt time.Time
UpdatedAt time.Time
}

// WSClient is a wrapper for pusher.Client
type WSClient interface {
Trigger(channel string, eventName string, data interface{}) error
TriggerMulti(channels []string, eventName string, data interface{}) error
TriggerExclusive(channel string, eventName string, data interface{}, socketID string) error
TriggerMultiExclusive(channels []string, eventName string, data interface{}, socketID string) error
TriggerBatch(batch []pusher.Event) error
Channels(additionalQueries map[string]string) (*pusher.ChannelsList, error)
Channel(name string, additionalQueries map[string]string) (*pusher.Channel, error)
GetChannelUsers(name string) (*pusher.Users, error)
AuthenticatePrivateChannel(params []byte) (response []byte, err error)
AuthenticatePresenceChannel(params []byte, member pusher.MemberData) (response []byte, err error)
Webhook(header http.Header, body []byte) (*pusher.Webhook, error)
}

0 comments on commit b1a1941

Please sign in to comment.