Skip to content

Commit

Permalink
Merge pull request #2 from dgduncan/dev
Browse files Browse the repository at this point in the history
merge dev to master
  • Loading branch information
dgduncan authored Feb 21, 2023
2 parents a3969e3 + a0912a3 commit 1051160
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 33 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
push:
branches: [ "main", "dev" ]
pull_request:
branches: [ "main", "dev ]
branches: [ "main", "dev" ]

jobs:

Expand All @@ -26,3 +26,22 @@ jobs:

- name: Test
run: go test -v ./...

- name: Vet
run: go vet ./...
- name: Coverage
run: go test -v -covermode=count -coverprofile=coverage.out ./...

coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Coverage
run: go test -v -covermode=count -coverprofile=coverage.out ./...
- name: Convert coverage.out to coverage.lcov
uses: jandelgado/[email protected]
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
vendor

coverage.out
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ go 1.19

require (
github.com/golang/mock v1.6.0
github.com/mochi-co/mqtt/v2 v2.2.1
github.com/mochi-co/mqtt/v2 v2.2.2
github.com/rs/zerolog v1.29.0
github.com/stretchr/testify v1.8.1
)

Expand All @@ -15,7 +16,6 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/xid v1.4.0 // indirect
github.com/rs/zerolog v1.28.0 // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZb
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mochi-co/mqtt/v2 v2.2.1 h1:8GtksYNfkAB14SjZCwWAo2TFWQMKy38LGYVdE5bzDms=
github.com/mochi-co/mqtt/v2 v2.2.1/go.mod h1:KzMgWoPd31AtOSAsQKBjEWY0klMKmNlX1dO+bv+DeAo=
github.com/mochi-co/mqtt/v2 v2.2.2 h1:s1izxNecs0Zfmpo89g1EulKcIlxFzFbR6B2tgdVWVbg=
github.com/mochi-co/mqtt/v2 v2.2.2/go.mod h1:KzMgWoPd31AtOSAsQKBjEWY0klMKmNlX1dO+bv+DeAo=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down
172 changes: 172 additions & 0 deletions http_auth_hook_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package mochicloudhooks

import (
"context"
"errors"
"net/http"
"testing"

gomock "github.com/golang/mock/gomock"
"github.com/mochi-co/mqtt/v2"
"github.com/mochi-co/mqtt/v2/packets"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -46,3 +52,169 @@ func TestProvides(t *testing.T) {
})
}
}

func TestInit(t *testing.T) {
authHook := new(HTTPAuthHook)
authHook.Log = &zerolog.Logger{}

tests := []struct {
name string
config any
expectError bool
}{
{
name: "Success - Proper config",
config: HTTPAuthHookConfig{},
expectError: false,
},
{
name: "Success - nil config",
config: nil,
expectError: false,
},
{
name: "Failure - improper config",
config: "",
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

err := authHook.Init(tt.config)
if tt.expectError {
require.Error(t, err)
}

})
}
}

func TestOnACLCheck(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockRT := NewMockRoundTripper(ctrl)

tests := []struct {
name string
config any
mocks func(ctx context.Context)

expectPass bool
}{
{
name: "Success - Proper config",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
},
expectPass: true,
mocks: func(ctx context.Context) {
mockRT.EXPECT().RoundTrip(gomock.Any()).Return(&http.Response{
StatusCode: http.StatusOK,
}, nil)

},
},
{
name: "Error - HTTP error",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
},
expectPass: false,
mocks: func(ctx context.Context) {
mockRT.EXPECT().RoundTrip(gomock.Any()).Return(nil, errors.New("Oh Crap"))
},
},
{
name: "Error - Non 2xx",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
},
expectPass: false,
mocks: func(ctx context.Context) {
mockRT.EXPECT().RoundTrip(gomock.Any()).Return(&http.Response{
StatusCode: http.StatusTeapot,
}, nil)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
tt.mocks(ctx)

authHook := new(HTTPAuthHook)
authHook.Log = &zerolog.Logger{}
authHook.Init(tt.config)

success := authHook.OnACLCheck(&mqtt.Client{}, "/topic", false)
require.Equal(t, tt.expectPass, success)
})
}
}

func TestOnConnectAuthenticate(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockRT := NewMockRoundTripper(ctrl)

tests := []struct {
name string
config any
mocks func(ctx context.Context)

expectPass bool
}{
{
name: "Success - Proper config",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
},
expectPass: true,
mocks: func(ctx context.Context) {
mockRT.EXPECT().RoundTrip(gomock.Any()).Return(&http.Response{
StatusCode: http.StatusOK,
}, nil)

},
},
{
name: "Error - HTTP error",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
},
expectPass: false,
mocks: func(ctx context.Context) {
mockRT.EXPECT().RoundTrip(gomock.Any()).Return(nil, errors.New("Oh Crap"))
},
},
{
name: "Error - Non 2xx",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
},
expectPass: false,
mocks: func(ctx context.Context) {
mockRT.EXPECT().RoundTrip(gomock.Any()).Return(&http.Response{
StatusCode: http.StatusTeapot,
}, nil)
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
tt.mocks(ctx)

authHook := new(HTTPAuthHook)
authHook.Log = &zerolog.Logger{}
authHook.Init(tt.config)

success := authHook.OnConnectAuthenticate(&mqtt.Client{}, packets.Packet{})
require.Equal(t, tt.expectPass, success)
})
}
}
50 changes: 50 additions & 0 deletions mock_roundtripper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 0 additions & 26 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

// func TestNewRoundTrip(t *testing.T) {

// ctrl := gomock.NewController(t)
// defer ctrl.Finish()

// tests := []struct {
// name string
// expectErr bool
// }{
// {
// name: "Success - Golden Path",
// expectErr: false,
// },
// }

// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {

// req, _ := http.NewRequest("GET", "http://example.com", nil)
// nt := NewTransport(http.DefaultTransport)

// require.Nil(t, err)
// })
// }
// }

func TestRoundTrip(t *testing.T) {

ctrl := gomock.NewController(t)
Expand Down

0 comments on commit 1051160

Please sign in to comment.