Skip to content

Commit

Permalink
Write unit tests for join/part/quit handlers (#300)
Browse files Browse the repository at this point in the history
* Started working on interfaces for IRC side

* Started working on implementing interface for IRC client

* Continued working on fulfilling interface

* Finished converting IRC client to interface

* Added one test and added comment to IRCClient

* Added test for when ShowJoinMessage is false

* Added part handler tests

* Added quit handler tests

* Got rid of pesky spaces
  • Loading branch information
Zedjones committed Apr 17, 2020
1 parent 5edf1fc commit f6fd3be
Show file tree
Hide file tree
Showing 8 changed files with 642 additions and 27 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-playground/universal-translator v0.16.0 // indirect
github.com/go-playground/validator v9.29.1+incompatible
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/golang/mock v1.4.3
github.com/joho/godotenv v1.3.0
github.com/leodido/go-urn v1.1.0 // indirect
github.com/lrstanley/girc v0.0.0-20190801035559-4fc93959e1a7
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/go-playground/validator v9.29.1+incompatible h1:KzeWOnoPKd5L5RG0JlVFw
github.com/go-playground/validator v9.29.1+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8=
Expand All @@ -23,5 +25,15 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86JNJe4spst6Ff8MjvPUdPg=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
48 changes: 24 additions & 24 deletions internal/handlers/irc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ Handler specifies a function that handles an IRC event
In this case, we take an IRC client and return a function that
handles an IRC event
*/
type Handler = func(c Client) func(*girc.Client, girc.Event)
type Handler = func(c ClientInterface) func(*girc.Client, girc.Event)

/*
checkBlacklist checks the IRC blacklist for a name, and returns whether
or not the name is in the blacklist
*/
func checkBlacklist(c Client, toCheck string) bool {
for _, name := range c.Settings.IRCBlacklist {
func checkBlacklist(c ClientInterface, toCheck string) bool {
for _, name := range c.IRCSettings().IRCBlacklist {
if strings.EqualFold(toCheck, name) {
return true
}
Expand All @@ -37,13 +37,13 @@ func checkBlacklist(c Client, toCheck string) bool {
connectHandler returns a function to use as the connect handler for girc,
so that the specified channel is joined after the server connection is established
*/
func connectHandler(c Client) func(*girc.Client, girc.Event) {
func connectHandler(c ClientInterface) func(*girc.Client, girc.Event) {
return func(gc *girc.Client, e girc.Event) {
c.logger.LogDebug("connectHandler triggered")
if c.Settings.ChannelKey != "" {
c.Cmd.JoinKey(c.Settings.Channel, c.Settings.ChannelKey)
c.Logger().LogDebug("connectHandler triggered")
if c.IRCSettings().ChannelKey != "" {
c.JoinKey(c.IRCSettings().Channel, c.IRCSettings().ChannelKey)
} else {
c.Cmd.Join(c.Settings.Channel)
c.Join(c.IRCSettings().Channel)
}
}
}
Expand All @@ -52,42 +52,42 @@ func connectHandler(c Client) func(*girc.Client, girc.Event) {
messageHandler handles the PRIVMSG IRC event, which entails both private
and channel messages. However, it only cares about channel messages
*/
func messageHandler(c Client) func(*girc.Client, girc.Event) {
func messageHandler(c ClientInterface) func(*girc.Client, girc.Event) {
return func(gc *girc.Client, e girc.Event) {
c.logger.LogDebug("messageHandler triggered")
c.Logger().LogDebug("messageHandler triggered")
// Only send if user is not in blacklist
if !(checkBlacklist(c, e.Source.Name)) {
formatted := c.Settings.Prefix + e.Source.Name + c.Settings.Suffix + " " + e.Params[1]
formatted := c.IRCSettings().Prefix + e.Source.Name + c.IRCSettings().Suffix + " " + e.Params[1]
if e.IsFromChannel() {
c.sendToTg(formatted)
c.SendToTg(formatted)
}
}
}
}

func joinHandler(c Client) func(*girc.Client, girc.Event) {
func joinHandler(c ClientInterface) func(*girc.Client, girc.Event) {
return func(gc *girc.Client, e girc.Event) {
c.logger.LogDebug("joinHandler triggered")
if c.TelegramSettings != nil && c.TelegramSettings.ShowJoinMessage {
c.sendToTg(fmt.Sprintf(joinFmt, e.Source.Name))
c.Logger().LogDebug("joinHandler triggered")
if c.TgSettings().ShowJoinMessage {
c.SendToTg(fmt.Sprintf(joinFmt, e.Source.Name))
}
}
}

func partHandler(c Client) func(*girc.Client, girc.Event) {
func partHandler(c ClientInterface) func(*girc.Client, girc.Event) {
return func(gc *girc.Client, e girc.Event) {
c.logger.LogDebug("partHandler triggered")
if c.TelegramSettings != nil && c.TelegramSettings.ShowLeaveMessage {
c.sendToTg(fmt.Sprintf(partFmt, e.Source.Name))
c.Logger().LogDebug("partHandler triggered")
if c.TgSettings().ShowLeaveMessage {
c.SendToTg(fmt.Sprintf(partFmt, e.Source.Name))
}
}
}

func quitHandler(c Client) func(*girc.Client, girc.Event) {
func quitHandler(c ClientInterface) func(*girc.Client, girc.Event) {
return func(gc *girc.Client, e girc.Event) {
c.logger.LogDebug("quitHandler triggered")
if c.TelegramSettings != nil && c.TelegramSettings.ShowLeaveMessage {
c.sendToTg(fmt.Sprintf(quitFmt, e.Source.Name, e.Params[0]))
c.Logger().LogDebug("quitHandler triggered")
if c.TgSettings().ShowLeaveMessage {
c.SendToTg(fmt.Sprintf(quitFmt, e.Source.Name, e.Params[0]))
}
}
}
Expand Down
207 changes: 207 additions & 0 deletions internal/handlers/irc/handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package irc

import (
"testing"

gomock "github.com/golang/mock/gomock"
"github.com/lrstanley/girc"
"github.com/ritlug/teleirc/internal"
)

func TestJoinHandler_On(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

tgSettings := internal.TelegramSettings{
ShowJoinMessage: true,
}

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("joinHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Eq("* TEST_NAME joins"))

myHandler := joinHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{
Source: &girc.Source{
Name: "TEST_NAME",
},
})
}

func TestJoinHandler_Off(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

tgSettings := internal.TelegramSettings{
ShowJoinMessage: false,
}

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("joinHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Any()).
MaxTimes(0)

myHandler := joinHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{})
}

func TestPartHandler_On(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

tgSettings := internal.TelegramSettings{
ShowLeaveMessage: true,
}

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("partHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Eq("* TEST_NAME parts"))

myHandler := partHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{
Source: &girc.Source{
Name: "TEST_NAME",
},
})
}

func TestPartHandler_Off(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

tgSettings := internal.TelegramSettings{
ShowLeaveMessage: false,
}

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("partHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Any()).
MaxTimes(0)

myHandler := partHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{})
}

func TestQuitHandler_On(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

tgSettings := internal.TelegramSettings{
ShowLeaveMessage: true,
}

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("quitHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Eq("* TEST_NAME quit (TEST_REASON)"))

myHandler := quitHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{
Source: &girc.Source{
Name: "TEST_NAME",
},
Params: []string{
"TEST_REASON",
},
})
}

func TestQuitHandler_Off(t *testing.T) {
ctrl := gomock.NewController(t)

defer ctrl.Finish()

tgSettings := internal.TelegramSettings{
ShowLeaveMessage: false,
}

mockClient := NewMockClientInterface(ctrl)
mockLogger := internal.NewMockDebugLogger(ctrl)
mockClient.
EXPECT().
Logger().
Return(mockLogger)
mockLogger.
EXPECT().
LogDebug(gomock.Eq("quitHandler triggered"))
mockClient.
EXPECT().
TgSettings().
Return(&tgSettings)
mockClient.
EXPECT().
SendToTg(gomock.Any()).
MaxTimes(0)

myHandler := quitHandler(mockClient)
myHandler(&girc.Client{}, girc.Event{})
}
25 changes: 25 additions & 0 deletions internal/handlers/irc/interfaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package irc

import (
"github.com/lrstanley/girc"
"github.com/ritlug/teleirc/internal"
)

/*
ClientInterface represents an IRC client
*/
type ClientInterface interface {
SendMessage(string)
StartBot(chan<- error, func(string))
Logger() internal.DebugLogger
addHandlers()
SendToTg(string)
IRCSettings() *internal.IRCSettings
TgSettings() *internal.TelegramSettings

AddHandler(string, func(*girc.Client, girc.Event))
ConnectDialer(girc.Dialer) error
Message(string, string)
JoinKey(string, string)
Join(...string)
}
Loading

0 comments on commit f6fd3be

Please sign in to comment.