mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* AuthN: Add functionallity to test if auth client should be used * AuthN: Add bolierplate client for api keys and register it * AuthN: Add tests for api key client * Inject service * AuthN: Update client names * ContextHandler: Set authn service * AuthN: Implement authentication for api key client * ContextHandler: Use authn service for api keys if flag is enabled * AuthN: refactor authentication method to return additional value to indicate if client could perform authentication * update prefixes * Add namespaced id to identity * AuthN: Expand the Identity struct to include required fields from signed in user * Add error for disabled service account * Add function to write error response based on errutil.Error * Add error to log * Return errors based on errutil.Error * pass error * update log message * Fix namespaced ids * Add tests * Lint
78 lines
1.7 KiB
Go
78 lines
1.7 KiB
Go
package authnimpl
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
|
"github.com/grafana/grafana/pkg/services/authn"
|
|
"github.com/grafana/grafana/pkg/services/authn/authntest"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
func TestService_Authenticate(t *testing.T) {
|
|
type TestCase struct {
|
|
desc string
|
|
clientName string
|
|
expectedOK bool
|
|
expectedErr error
|
|
}
|
|
|
|
tests := []TestCase{
|
|
{
|
|
desc: "should succeed with authentication for configured client",
|
|
clientName: "fake",
|
|
expectedOK: true,
|
|
},
|
|
{
|
|
desc: "should return false when client is not configured",
|
|
clientName: "gitlab",
|
|
expectedOK: false,
|
|
},
|
|
{
|
|
desc: "should return true and error when client could be used but failed to authenticate",
|
|
clientName: "fake",
|
|
expectedOK: true,
|
|
expectedErr: errors.New("some error"),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
svc := setupTests(t, func(svc *Service) {
|
|
svc.clients["fake"] = &authntest.FakeClient{
|
|
ExpectedErr: tt.expectedErr,
|
|
ExpectedTest: tt.expectedOK,
|
|
}
|
|
})
|
|
|
|
_, ok, err := svc.Authenticate(context.Background(), tt.clientName, &authn.Request{})
|
|
assert.Equal(t, tt.expectedOK, ok)
|
|
if tt.expectedErr != nil {
|
|
assert.Error(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func setupTests(t *testing.T, opts ...func(svc *Service)) *Service {
|
|
t.Helper()
|
|
|
|
s := &Service{
|
|
log: log.NewNopLogger(),
|
|
cfg: setting.NewCfg(),
|
|
clients: map[string]authn.Client{},
|
|
tracer: tracing.InitializeTracerForTest(),
|
|
}
|
|
|
|
for _, o := range opts {
|
|
o(s)
|
|
}
|
|
|
|
return s
|
|
}
|