3cff8ccd64
* Rewrite the code handling basic auth to make it easier to extend for other types of auth. * The behaviour of the existing code is maintained. * No changes to how basic auth is configured from a user's perspective. https://gitlab.com/hectorjsmith/fail2ban-prometheus-exporter/-/merge_requests/89
36 lines
820 B
Go
36 lines
820 B
Go
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")
|
|
}
|
|
}
|