Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moar tests #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ language: go
# Specify Go versions to test (".x" suffix means latest minor version, "tip" is
# the latest development version)
go:
# - "1.9.x"
# - "1.10.x"
- "1.9.x"
- "1.10.x"
- "1.11.x"
- tip

Expand All @@ -25,8 +25,7 @@ matrix:
# Install all dependencies (including dependencies of tests) without installing
# binaries.
install:
-
# - go get -d -t -v ./...
- ./internal/scripts/travis-install.sh

# Check if gofmt or go vet report any errors, run all tests with the race
# detector enabled, then build all examples to make sure they still compile.
Expand Down
110 changes: 20 additions & 90 deletions commands/command.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
// Package commands provides command handling capable of ingesting transport.Events
// and responding over the provided transport wire.
// If the userDB is set permissions on a command will be honored otherwise
// if no userDB is set permissions will be ignored entirely (this could possibly
// use improvement).
package commands // import "github.com/justanotherorganization/justanotherbotkit/commands"
package commands

import (
"context"
"fmt"
"strings"

"github.com/justanotherorganization/justanotherbotkit/transport"
"github.com/justanotherorganization/justanotherbotkit/users"
"github.com/pkg/errors"
)

type (
Expand All @@ -39,42 +32,41 @@ type (
// It attempts to match the event against any children commands first
// allowing a base Command to act as a command tree.
func (c *Command) Execute(ev *transport.Event) error {
if c.Disabled {
if ev == nil ||
ev.GetBody() == "" ||
c.Disabled {
return nil
}

if c.UserDB != nil && len(c.Perms) > 0 {
ok, err := _hasPerms(c, ev.Origin.Sender.ID)
if err != nil {
return err
}
ok, err := checkPerms(c, ev)
if err != nil {
return err
}

if !ok {
return nil
}
if !ok {
return nil
}

fields := strings.Fields(ev.Body)
fields := strings.Fields(ev.GetBody())

if strings.Compare("help", fields[0]) == 0 {
ev.Body = strings.Join(fields[1:], " ")
return c.help(ev)
}

_c := c.match(ev)
if _c == nil {
if _c == nil ||
_c.Disabled {
return nil
}

if _c.UserDB != nil && len(_c.Perms) > 0 {
ok, err := _hasPerms(c, ev.Origin.Sender.ID)
if err != nil {
return err
}
ok, err = checkPerms(_c, ev)
if err != nil {
return err
}

if !ok {
return nil
}
if !ok {
return nil
}

if _c.ExecFunc == nil {
Expand Down Expand Up @@ -114,69 +106,7 @@ func (c *Command) help(ev *transport.Event) error {
}

return ev.SendMessage(
ev.Origin.ID,
ev.GetOrigin().GetID(),
fmt.Sprintf("%s:\n%s\n", _c.Use, _c.Long),
)
}

func (c *Command) match(ev *transport.Event) *Command {
if ev.Body == "" {
return c
}

fields := strings.Fields(ev.Body)
if len(fields) == 0 {
return c
}

var cmd *Command
for _, _c := range c.children {
if _isCommand(_c, fields[0]) {
ev.Body = strings.Join(fields[1:], " ")
cmd = _c
break
}
}

if cmd == nil {
cmd = c
}

return cmd
}

func _isCommand(c *Command, s string) bool {
if strings.Compare(c.Use, s) == 0 {
return true
}

for _, a := range c.Aliases {
if strings.Compare(a, s) == 0 {
return true
}
}

return false
}

func _hasPerms(c *Command, id string) (bool, error) {
u, err := c.UserDB.GetUser(context.Background(), id)
if err != nil {
return false, errors.Wrap(err, "UserDB.GetUser")
}

for _, p := range u.GetPermissions() {
// Root users can do all the things!!!
if p == "root" {
return true, nil
}

for _, _p := range c.Perms {
if p == _p {
return true, nil
}
}
}

return false, nil
}
39 changes: 39 additions & 0 deletions commands/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package commands_test

import (
"testing"

. "github.com/justanotherorganization/justanotherbotkit/commands"
"github.com/justanotherorganization/justanotherbotkit/internal/test"
"github.com/justanotherorganization/justanotherbotkit/transport"
"github.com/justanotherorganization/justanotherbotkit/transport/proto"
)

func TestSingleCommand(t *testing.T) {
tt := test.TableTest{
Cases: []*test.TableTestCase{
// Disabled commands should return nil.
// TODO: test that nothing was sent over the transport in response.
&test.TableTestCase{
Val: &Command{Disabled: true},
Exp: nil,
},
// Perms are set but no database is present (perms will be un-checked).
// TODO: confirm that the command sent someting over the transport in response.
&test.TableTestCase{
Val: &Command{Perms: []string{"foo"}},
Exp: nil,
},
},
F: func(v interface{}) interface{} {
cmd := v.(*Command)
return cmd.Execute(&transport.Event{
BaseEvent: &pb.BaseEvent{
Body: "test",
},
})
},
}

tt.Run(t)
}
7 changes: 7 additions & 0 deletions commands/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Package commands provides command handling capable of ingesting transport.Events
// and responding over the provided transport wire.
//
// If the userDB is set permissions on a command will be honored otherwise
// if no userDB is set permissions will be ignored entirely (this could possibly
// use improvement).
package commands // import "github.com/justanotherorganization/justanotherbotkit/commands"
3 changes: 2 additions & 1 deletion commands/go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module github.com/justanotherorganization/justanotherbotkit/commands

require (
github.com/justanotherorganization/justanotherbotkit/transport v0.0.2
github.com/justanotherorganization/justanotherbotkit/internal v0.0.2
github.com/justanotherorganization/justanotherbotkit/transport v0.0.3-pre1
github.com/justanotherorganization/justanotherbotkit/users v0.0.1
github.com/pkg/errors v0.8.0
)
6 changes: 4 additions & 2 deletions commands/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/justanotherorganization/justanotherbotkit/internal v0.0.2 h1:lj5ClN0JpGuOOwDTw2NHzmMJVM8VlDvN2veHVcfOBRk=
github.com/justanotherorganization/justanotherbotkit/internal v0.0.2/go.mod h1:Ky6Plt9xEtoSkl64huLSkq0lq1xYKBxI58Wb45mucV8=
github.com/justanotherorganization/justanotherbotkit/proto v0.0.1 h1:N2hHF03EYoOfKXFlU4Sx97laA1fGu5K7K0ZjgbaGjF8=
github.com/justanotherorganization/justanotherbotkit/proto v0.0.1/go.mod h1:r2hwUKNIK21pZ+e1KuoNWCRDJIpotmDrwS02cMKZu6c=
github.com/justanotherorganization/justanotherbotkit/transport v0.0.2 h1:ooS6/mfTebytKcyzOvGSYr/Ay0K3uxegSIrEumrCqxw=
github.com/justanotherorganization/justanotherbotkit/transport v0.0.2/go.mod h1:gbF/m3czwTohZm7mRNbG/E7ycOmsSNMThrdMC+DuOjg=
github.com/justanotherorganization/justanotherbotkit/transport v0.0.3-pre1 h1:kgnhaJeOZ2Y64/Dv9XLQFGA5AMLGcrsN80YBHeJcZ88=
github.com/justanotherorganization/justanotherbotkit/transport v0.0.3-pre1/go.mod h1:gbF/m3czwTohZm7mRNbG/E7ycOmsSNMThrdMC+DuOjg=
github.com/justanotherorganization/justanotherbotkit/users v0.0.1 h1:g96VVlYIYV5kRYfG2eLBQa5Cptg1WaKu2Xv8eftOz6M=
github.com/justanotherorganization/justanotherbotkit/users v0.0.1/go.mod h1:pSY/4vMnSHd4qjLQzLFe3WyGhpVYhNibLQuZTjkIpE4=
github.com/lusis/go-slackbot v0.0.0-20180109053408-401027ccfef5/go.mod h1:c2mYKRyMb1BPkO5St0c/ps62L4S0W2NAkaTXj9qEI+0=
Expand Down
47 changes: 47 additions & 0 deletions commands/match.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package commands

import (
"strings"

"github.com/justanotherorganization/justanotherbotkit/transport"
)

func (c *Command) match(ev *transport.Event) *Command {
if ev.GetBody() == "" {
return c
}

fields := strings.Fields(ev.GetBody())
if len(fields) == 0 {
return c
}

var cmd *Command
for _, _c := range c.children {
if isMatch(_c, fields[0]) {
ev.Body = strings.Join(fields[1:], " ")
cmd = _c
break
}
}

if cmd == nil {
cmd = c
}

return cmd
}

func isMatch(c *Command, s string) bool {
if strings.Compare(c.Use, s) == 0 {
return true
}

for _, a := range c.Aliases {
if strings.Compare(a, s) == 0 {
return true
}
}

return false
}
76 changes: 76 additions & 0 deletions commands/match_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package commands

import (
"testing"

"github.com/justanotherorganization/justanotherbotkit/internal/test"
"github.com/justanotherorganization/justanotherbotkit/transport"
)

func TestMatch(t *testing.T) {
type _mt struct {
cmd *Command
ev *transport.Event
}

tt := test.TableTest{
Cases: []*test.TableTestCase{
&test.TableTestCase{
Val: &_mt{cmd: &Command{}, ev: &transport.Event{}},
Exp: &Command{},
},
},
F: func(v interface{}) interface{} {
mt := v.(*_mt)
return mt.cmd.match(mt.ev)
},
}

tt.Run(t)
}

func TestIsMatch(t *testing.T) {
type _mt struct {
cmd *Command
s string
}

tt := test.TableTest{
Cases: []*test.TableTestCase{
&test.TableTestCase{
Val: &_mt{cmd: &Command{Use: ""}, s: ""},
Exp: true,
},
&test.TableTestCase{
Val: &_mt{cmd: &Command{Use: "foo"}, s: ""},
Exp: false,
},
&test.TableTestCase{
Val: &_mt{cmd: &Command{Use: "foo"}, s: "bar"},
Exp: false,
},
&test.TableTestCase{
Val: &_mt{cmd: &Command{Use: "foo"}, s: "foo"},
Exp: true,
},
&test.TableTestCase{
Val: &_mt{cmd: &Command{Use: "foo", Aliases: []string{"bar"}}, s: "bar"},
Exp: true,
},
&test.TableTestCase{
Val: &_mt{cmd: &Command{Use: "foo", Aliases: []string{"bar", "foobar"}}, s: "bar"},
Exp: true,
},
&test.TableTestCase{
Val: &_mt{cmd: &Command{Use: "foo", Aliases: []string{"bar", "foobar"}}, s: "foobar"},
Exp: true,
},
},
F: func(v interface{}) interface{} {
mt := v.(*_mt)
return isMatch(mt.cmd, mt.s)
},
}

tt.Run(t)
}
Loading