2023-01-03 03:23:38 -06:00
|
|
|
package clients
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2024-08-13 03:18:28 -05:00
|
|
|
"github.com/grafana/authlib/claims"
|
2023-01-30 02:34:18 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-01-03 03:23:38 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/authn"
|
2023-01-09 09:40:29 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/authn/authntest"
|
2023-01-03 03:23:38 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasic_Authenticate(t *testing.T) {
|
|
|
|
type TestCase struct {
|
2023-01-09 09:40:29 -06:00
|
|
|
desc string
|
|
|
|
req *authn.Request
|
2023-01-17 02:11:45 -06:00
|
|
|
client authn.PasswordClient
|
2023-01-09 09:40:29 -06:00
|
|
|
expectedErr error
|
|
|
|
expectedIdentity *authn.Identity
|
2023-01-03 03:23:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []TestCase{
|
|
|
|
{
|
2023-01-09 09:40:29 -06:00
|
|
|
desc: "should success when password client return identity",
|
|
|
|
req: &authn.Request{HTTPRequest: &http.Request{Header: map[string][]string{authorizationHeaderName: {encodeBasicAuth("user", "password")}}}},
|
2024-08-13 03:18:28 -05:00
|
|
|
client: authntest.FakePasswordClient{ExpectedIdentity: &authn.Identity{ID: "1", Type: claims.TypeUser}},
|
|
|
|
expectedIdentity: &authn.Identity{ID: "1", Type: claims.TypeUser},
|
2023-01-03 03:23:38 -06:00
|
|
|
},
|
|
|
|
{
|
2023-01-17 02:11:45 -06:00
|
|
|
desc: "should fail when basic auth header could not be decoded",
|
|
|
|
req: &authn.Request{HTTPRequest: &http.Request{Header: map[string][]string{authorizationHeaderName: {}}}},
|
|
|
|
expectedErr: errDecodingBasicAuthHeader,
|
2023-01-03 03:23:38 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
2023-01-17 02:11:45 -06:00
|
|
|
c := ProvideBasic(tt.client)
|
2023-01-03 03:23:38 -06:00
|
|
|
|
|
|
|
identity, err := c.Authenticate(context.Background(), tt.req)
|
|
|
|
if tt.expectedErr != nil {
|
|
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
|
|
assert.Nil(t, identity)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, *tt.expectedIdentity, *identity)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBasic_Test(t *testing.T) {
|
|
|
|
type TestCase struct {
|
2023-01-17 02:11:45 -06:00
|
|
|
desc string
|
|
|
|
req *authn.Request
|
|
|
|
expected bool
|
2023-01-03 03:23:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []TestCase{
|
|
|
|
{
|
|
|
|
desc: "should succeed when authorization header is set with basic prefix",
|
|
|
|
req: &authn.Request{
|
|
|
|
HTTPRequest: &http.Request{
|
|
|
|
Header: map[string][]string{
|
|
|
|
authorizationHeaderName: {encodeBasicAuth("user", "password")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should fail when no http request is passed",
|
|
|
|
req: &authn.Request{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should fail when no http authorization header is set in http request",
|
|
|
|
req: &authn.Request{
|
|
|
|
HTTPRequest: &http.Request{Header: map[string][]string{}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "should fail when authorization header is set but without basic prefix",
|
|
|
|
req: &authn.Request{
|
|
|
|
HTTPRequest: &http.Request{Header: map[string][]string{authorizationHeaderName: {"something"}}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.desc, func(t *testing.T) {
|
2023-01-17 02:11:45 -06:00
|
|
|
c := ProvideBasic(authntest.FakePasswordClient{})
|
2023-01-03 03:23:38 -06:00
|
|
|
assert.Equal(t, tt.expected, c.Test(context.Background(), tt.req))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|