2019-04-25 04:52:08 +07:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2019-11-25 03:27:59 +07:00
|
|
|
"time"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-07 18:18:22 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2020-04-05 19:37:21 +07:00
|
|
|
"github.com/authelia/authelia/internal/regulation"
|
|
|
|
"github.com/authelia/authelia/internal/storage"
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/sirupsen/logrus/hooks/test"
|
|
|
|
"github.com/valyala/fasthttp"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/authorization"
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/middlewares"
|
|
|
|
"github.com/authelia/authelia/internal/session"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// MockAutheliaCtx a mock of AutheliaCtx.
|
2019-04-25 04:52:08 +07:00
|
|
|
type MockAutheliaCtx struct {
|
2020-04-21 04:03:38 +07:00
|
|
|
// Logger hook.
|
2019-04-25 04:52:08 +07:00
|
|
|
Hook *test.Hook
|
|
|
|
Ctx *middlewares.AutheliaCtx
|
|
|
|
Ctrl *gomock.Controller
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Providers.
|
2019-04-25 04:52:08 +07:00
|
|
|
UserProviderMock *MockUserProvider
|
2019-11-17 08:05:46 +07:00
|
|
|
StorageProviderMock *storage.MockProvider
|
2019-04-25 04:52:08 +07:00
|
|
|
NotifierMock *MockNotifier
|
|
|
|
|
|
|
|
UserSession *session.UserSession
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
Clock TestingClock
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// TestingClock implementation of clock for tests.
|
2019-11-25 03:27:59 +07:00
|
|
|
type TestingClock struct {
|
|
|
|
now time.Time
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Now return the stored clock.
|
2019-11-25 03:27:59 +07:00
|
|
|
func (dc *TestingClock) Now() time.Time {
|
|
|
|
return dc.now
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// After return a channel receiving the time after duration has elapsed.
|
2019-11-25 03:27:59 +07:00
|
|
|
func (dc *TestingClock) After(d time.Duration) <-chan time.Time {
|
|
|
|
return time.After(d)
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Set set the time of the clock.
|
2019-11-25 03:27:59 +07:00
|
|
|
func (dc *TestingClock) Set(now time.Time) {
|
|
|
|
dc.now = now
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// NewMockAutheliaCtx create an instance of AutheliaCtx mock.
|
2019-04-25 04:52:08 +07:00
|
|
|
func NewMockAutheliaCtx(t *testing.T) *MockAutheliaCtx {
|
|
|
|
mockAuthelia := new(MockAutheliaCtx)
|
2019-11-25 03:27:59 +07:00
|
|
|
mockAuthelia.Clock = TestingClock{}
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2019-11-30 21:33:45 +07:00
|
|
|
datetime, _ := time.Parse("2006-Jan-02", "2013-Feb-03")
|
|
|
|
mockAuthelia.Clock.Set(datetime)
|
|
|
|
|
2020-02-05 04:18:02 +07:00
|
|
|
configuration := schema.Configuration{}
|
2020-04-04 06:11:33 +07:00
|
|
|
configuration.Session.RememberMeDuration = schema.DefaultSessionConfiguration.RememberMeDuration
|
2019-04-25 04:52:08 +07:00
|
|
|
configuration.Session.Name = "authelia_session"
|
|
|
|
configuration.AccessControl.DefaultPolicy = "deny"
|
2020-04-09 08:05:17 +07:00
|
|
|
configuration.AccessControl.Rules = []schema.ACLRule{{
|
2020-04-16 07:18:11 +07:00
|
|
|
Domains: []string{"bypass.example.com"},
|
|
|
|
Policy: "bypass",
|
2020-04-09 08:05:17 +07:00
|
|
|
}, {
|
2020-04-16 07:18:11 +07:00
|
|
|
Domains: []string{"one-factor.example.com"},
|
|
|
|
Policy: "one_factor",
|
2020-04-09 08:05:17 +07:00
|
|
|
}, {
|
2020-04-16 07:18:11 +07:00
|
|
|
Domains: []string{"two-factor.example.com"},
|
|
|
|
Policy: "two_factor",
|
2020-04-09 08:05:17 +07:00
|
|
|
}, {
|
2020-04-16 07:18:11 +07:00
|
|
|
Domains: []string{"deny.example.com"},
|
|
|
|
Policy: "deny",
|
2019-04-25 04:52:08 +07:00
|
|
|
}}
|
|
|
|
|
|
|
|
providers := middlewares.Providers{}
|
|
|
|
|
|
|
|
mockAuthelia.Ctrl = gomock.NewController(t)
|
|
|
|
mockAuthelia.UserProviderMock = NewMockUserProvider(mockAuthelia.Ctrl)
|
|
|
|
providers.UserProvider = mockAuthelia.UserProviderMock
|
|
|
|
|
2019-11-17 08:05:46 +07:00
|
|
|
mockAuthelia.StorageProviderMock = storage.NewMockProvider(mockAuthelia.Ctrl)
|
2019-04-25 04:52:08 +07:00
|
|
|
providers.StorageProvider = mockAuthelia.StorageProviderMock
|
|
|
|
|
|
|
|
mockAuthelia.NotifierMock = NewMockNotifier(mockAuthelia.Ctrl)
|
|
|
|
providers.Notifier = mockAuthelia.NotifierMock
|
|
|
|
|
|
|
|
providers.Authorizer = authorization.NewAuthorizer(
|
2020-02-05 04:18:02 +07:00
|
|
|
configuration.AccessControl)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
providers.SessionProvider = session.NewProvider(
|
|
|
|
configuration.Session)
|
|
|
|
|
2019-11-25 03:27:59 +07:00
|
|
|
providers.Regulator = regulation.NewRegulator(configuration.Regulation, providers.StorageProvider, &mockAuthelia.Clock)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
request := &fasthttp.RequestCtx{}
|
2020-04-21 04:03:38 +07:00
|
|
|
// Set a cookie to identify this client throughout the test.
|
2019-04-25 04:52:08 +07:00
|
|
|
// request.Request.Header.SetCookie("authelia_session", "client_cookie")
|
|
|
|
|
|
|
|
autheliaCtx, _ := middlewares.NewAutheliaCtx(request, configuration, providers)
|
|
|
|
mockAuthelia.Ctx = autheliaCtx
|
|
|
|
|
|
|
|
logger, hook := test.NewNullLogger()
|
|
|
|
mockAuthelia.Hook = hook
|
|
|
|
|
|
|
|
mockAuthelia.Ctx.Logger = logrus.NewEntry(logger)
|
|
|
|
return mockAuthelia
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:03:38 +07:00
|
|
|
// Close close the mock.
|
2019-04-25 04:52:08 +07:00
|
|
|
func (m *MockAutheliaCtx) Close() {
|
|
|
|
m.Hook.Reset()
|
2019-11-25 03:27:59 +07:00
|
|
|
m.Ctrl.Finish()
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assert200KO assert an error response from the service.
|
|
|
|
func (m *MockAutheliaCtx) Assert200KO(t *testing.T, message string) {
|
|
|
|
assert.Equal(t, 200, m.Ctx.Response.StatusCode())
|
|
|
|
assert.Equal(t, fmt.Sprintf("{\"status\":\"KO\",\"message\":\"%s\"}", message), string(m.Ctx.Response.Body()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assert200OK assert a successful response from the service.
|
|
|
|
func (m *MockAutheliaCtx) Assert200OK(t *testing.T, data interface{}) {
|
|
|
|
assert.Equal(t, 200, m.Ctx.Response.StatusCode())
|
|
|
|
response := middlewares.OKResponse{
|
|
|
|
Status: "OK",
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.Marshal(response)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, string(b), string(m.Ctx.Response.Body()))
|
|
|
|
}
|
2019-12-07 18:18:22 +07:00
|
|
|
|
2020-04-21 07:48:24 +07:00
|
|
|
// GetResponseData retrieves a response from the service.
|
2019-12-07 18:18:22 +07:00
|
|
|
func (m *MockAutheliaCtx) GetResponseData(t *testing.T, data interface{}) {
|
|
|
|
okResponse := middlewares.OKResponse{}
|
|
|
|
okResponse.Data = data
|
|
|
|
err := json.Unmarshal(m.Ctx.Response.Body(), &okResponse)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|