Skip to content

Commit

Permalink
Update http auth hook tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Duncan committed Feb 21, 2023
1 parent a0912a3 commit 6f951f5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
17 changes: 15 additions & 2 deletions http_auth_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -57,15 +58,20 @@ func (h *HTTPAuthHook) Provides(b byte) bool {

func (h *HTTPAuthHook) Init(config any) error {
if config == nil {
h.Log.Debug().Msg("nil config")
return nil
return errors.New("nil config")
}

authHookConfig, ok := config.(HTTPAuthHookConfig)
if !ok {
return errors.New("improper config")
}

fmt.Println(validateConfig(authHookConfig))

if !validateConfig(authHookConfig) {
return errors.New("hostname configs failed validation")
}

h.httpclient = NewTransport(authHookConfig.RoundTripper)

h.aclhost = authHookConfig.ACLHost
Expand Down Expand Up @@ -134,3 +140,10 @@ func (h *HTTPAuthHook) makeRequest(requestType, url string, payload any) (*http.

return resp, nil
}

func validateConfig(config HTTPAuthHookConfig) bool {
if config.ACLHost == "" || config.ClientAuthenticationHost == "" {
return false
}
return true
}
33 changes: 25 additions & 8 deletions http_auth_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,20 @@ func TestInit(t *testing.T) {
expectError: false,
},
{
name: "Success - nil config",
name: "Failure - nil config",
config: nil,
expectError: false,
expectError: true,
},
{
name: "Failure - improper config",
config: "",
expectError: true,
},
{
name: "Failure - hostname validation fail",
config: HTTPAuthHookConfig{},
expectError: true,
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -106,7 +111,9 @@ func TestOnACLCheck(t *testing.T) {
{
name: "Success - Proper config",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: true,
mocks: func(ctx context.Context) {
Expand All @@ -119,7 +126,9 @@ func TestOnACLCheck(t *testing.T) {
{
name: "Error - HTTP error",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: false,
mocks: func(ctx context.Context) {
Expand All @@ -129,7 +138,9 @@ func TestOnACLCheck(t *testing.T) {
{
name: "Error - Non 2xx",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: false,
mocks: func(ctx context.Context) {
Expand Down Expand Up @@ -170,7 +181,9 @@ func TestOnConnectAuthenticate(t *testing.T) {
{
name: "Success - Proper config",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: true,
mocks: func(ctx context.Context) {
Expand All @@ -183,7 +196,9 @@ func TestOnConnectAuthenticate(t *testing.T) {
{
name: "Error - HTTP error",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: false,
mocks: func(ctx context.Context) {
Expand All @@ -193,7 +208,9 @@ func TestOnConnectAuthenticate(t *testing.T) {
{
name: "Error - Non 2xx",
config: HTTPAuthHookConfig{
RoundTripper: mockRT,
RoundTripper: mockRT,
ACLHost: "http://aclhost.com",
ClientAuthenticationHost: "http://clientauthenticationhost.com",
},
expectPass: false,
mocks: func(ctx context.Context) {
Expand Down

0 comments on commit 6f951f5

Please sign in to comment.