2021-05-22 13:20:18 +00:00
|
|
|
package promauth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewConfig(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
baseDir string
|
|
|
|
az *Authorization
|
|
|
|
basicAuth *BasicAuthConfig
|
|
|
|
bearerToken string
|
|
|
|
bearerTokenFile string
|
|
|
|
oauth *OAuth2Config
|
|
|
|
tlsConfig *TLSConfig
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2021-05-22 14:59:23 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
expectHeader string
|
2021-05-22 13:20:18 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "OAuth2 config",
|
|
|
|
args: args{
|
|
|
|
oauth: &OAuth2Config{
|
|
|
|
ClientID: "some-id",
|
2021-11-05 12:41:14 +00:00
|
|
|
ClientSecret: NewSecret("some-secret"),
|
2021-05-22 13:20:18 +00:00
|
|
|
TokenURL: "http://localhost:8511",
|
|
|
|
},
|
|
|
|
},
|
2021-05-22 14:59:23 +00:00
|
|
|
expectHeader: "Bearer some-token",
|
2021-05-22 13:20:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OAuth2 config with file",
|
|
|
|
args: args{
|
|
|
|
oauth: &OAuth2Config{
|
|
|
|
ClientID: "some-id",
|
|
|
|
ClientSecretFile: "testdata/test_secretfile.txt",
|
|
|
|
TokenURL: "http://localhost:8511",
|
|
|
|
},
|
|
|
|
},
|
2021-05-22 14:59:23 +00:00
|
|
|
expectHeader: "Bearer some-token",
|
2021-05-22 13:20:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "OAuth2 want err",
|
|
|
|
args: args{
|
|
|
|
oauth: &OAuth2Config{
|
|
|
|
ClientID: "some-id",
|
2021-11-05 12:41:14 +00:00
|
|
|
ClientSecret: NewSecret("some-secret"),
|
2021-05-22 13:20:18 +00:00
|
|
|
ClientSecretFile: "testdata/test_secretfile.txt",
|
|
|
|
TokenURL: "http://localhost:8511",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic Auth config",
|
|
|
|
args: args{
|
|
|
|
basicAuth: &BasicAuthConfig{
|
|
|
|
Username: "user",
|
2021-11-05 12:41:14 +00:00
|
|
|
Password: NewSecret("password"),
|
2021-05-22 13:20:18 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-22 14:59:23 +00:00
|
|
|
expectHeader: "Basic dXNlcjpwYXNzd29yZA==",
|
2021-05-22 13:20:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic Auth config with file",
|
|
|
|
args: args{
|
|
|
|
basicAuth: &BasicAuthConfig{
|
|
|
|
Username: "user",
|
|
|
|
PasswordFile: "testdata/test_secretfile.txt",
|
|
|
|
},
|
|
|
|
},
|
2021-05-22 14:59:23 +00:00
|
|
|
expectHeader: "Basic dXNlcjpzZWNyZXQtY29udGVudA==",
|
2021-05-22 13:20:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "want Authorization",
|
|
|
|
args: args{
|
|
|
|
az: &Authorization{
|
2021-05-22 14:59:23 +00:00
|
|
|
Type: "Bearer",
|
2021-11-05 12:41:14 +00:00
|
|
|
Credentials: NewSecret("Value"),
|
2021-05-22 13:20:18 +00:00
|
|
|
},
|
|
|
|
},
|
2021-05-22 14:59:23 +00:00
|
|
|
expectHeader: "Bearer Value",
|
2021-05-22 13:20:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "token file",
|
|
|
|
args: args{
|
|
|
|
bearerTokenFile: "testdata/test_secretfile.txt",
|
|
|
|
},
|
2021-05-22 14:59:23 +00:00
|
|
|
expectHeader: "Bearer secret-content",
|
2021-05-22 13:20:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "token with tls",
|
|
|
|
args: args{
|
|
|
|
bearerToken: "some-token",
|
|
|
|
tlsConfig: &TLSConfig{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
2021-05-22 14:59:23 +00:00
|
|
|
expectHeader: "Bearer some-token",
|
2021-05-22 13:20:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.args.oauth != nil {
|
|
|
|
r := http.NewServeMux()
|
|
|
|
r.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-05-22 14:59:23 +00:00
|
|
|
w.Write([]byte(`{"access_token":"some-token","token_type": "Bearer"}`))
|
2021-05-22 13:20:18 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
mock := httptest.NewServer(r)
|
|
|
|
tt.args.oauth.TokenURL = mock.URL
|
|
|
|
}
|
|
|
|
got, err := NewConfig(tt.args.baseDir, tt.args.az, tt.args.basicAuth, tt.args.bearerToken, tt.args.bearerTokenFile, tt.args.oauth, tt.args.tlsConfig)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("NewConfig() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != nil {
|
2021-05-22 14:59:23 +00:00
|
|
|
ah := got.GetAuthHeader()
|
|
|
|
if ah != tt.expectHeader {
|
|
|
|
t.Fatalf("unexpected auth header; got %q; want %q", ah, tt.expectHeader)
|
2021-05-22 13:20:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|