add tests for empty auth
This commit is contained in:
parent
ebae219a47
commit
a654eb942a
14
auth/empty.go
Normal file
14
auth/empty.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func NewEmptyAuthProvider() AuthProvider {
|
||||||
|
return &emptyAuthProvider{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type emptyAuthProvider struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *emptyAuthProvider) IsAllowed(request *http.Request) bool {
|
||||||
|
return true
|
||||||
|
}
|
36
auth/empty_test.go
Normal file
36
auth/empty_test.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_GIVEN_EmptyAuth_WHEN_CallingIsAllowedWithoutAuth_THEN_TrueReturned(t *testing.T) {
|
||||||
|
// assemble
|
||||||
|
request := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
|
||||||
|
provider := NewEmptyAuthProvider()
|
||||||
|
|
||||||
|
// act
|
||||||
|
response := provider.IsAllowed(request)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
if !response {
|
||||||
|
t.Errorf("expected request to be allowed, but failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_GIVEN_EmptyAuth_WHEN_CallingIsAllowedWithAuth_THEN_TrueReturned(t *testing.T) {
|
||||||
|
// assemble
|
||||||
|
request := httptest.NewRequest(http.MethodGet, "http://example.com", nil)
|
||||||
|
request.SetBasicAuth("user", "pass")
|
||||||
|
provider := NewEmptyAuthProvider()
|
||||||
|
|
||||||
|
// act
|
||||||
|
response := provider.IsAllowed(request)
|
||||||
|
|
||||||
|
// assert
|
||||||
|
if !response {
|
||||||
|
t.Errorf("expected request to be allowed, but failed")
|
||||||
|
}
|
||||||
|
}
|
@ -7,28 +7,3 @@ import (
|
|||||||
type AuthProvider interface {
|
type AuthProvider interface {
|
||||||
IsAllowed(*http.Request) bool
|
IsAllowed(*http.Request) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEmptyAuthProvider() AuthProvider {
|
|
||||||
return &emptyAuthProvider{}
|
|
||||||
}
|
|
||||||
|
|
||||||
type emptyAuthProvider struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *emptyAuthProvider) IsAllowed(request *http.Request) bool {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
type compositeAuthProvider struct {
|
|
||||||
providers []AuthProvider
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *compositeAuthProvider) IsAllowed(request *http.Request) bool {
|
|
||||||
for i := 0; i < len(p.providers); i++ {
|
|
||||||
provider := p.providers[i]
|
|
||||||
if provider.IsAllowed(request) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user