2019-04-24 21:52:08 +00:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2019-11-24 20:27:59 +00:00
|
|
|
"time"
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/sirupsen/logrus/hooks/test"
|
2020-05-01 06:56:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-24 21:52:08 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/authorization"
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/middlewares"
|
2023-01-07 00:19:41 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/random"
|
2021-08-11 01:04:35 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/regulation"
|
|
|
|
"github.com/authelia/authelia/v4/internal/session"
|
2022-07-18 00:56:09 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/templates"
|
2022-12-21 10:31:21 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// MockAutheliaCtx a mock of AutheliaCtx.
|
2019-04-24 21:52:08 +00:00
|
|
|
type MockAutheliaCtx struct {
|
2020-04-20 21:03:38 +00:00
|
|
|
// Logger hook.
|
2019-04-24 21:52:08 +00:00
|
|
|
Hook *test.Hook
|
|
|
|
Ctx *middlewares.AutheliaCtx
|
|
|
|
Ctrl *gomock.Controller
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// Providers.
|
2021-12-01 12:11:29 +00:00
|
|
|
UserProviderMock *MockUserProvider
|
|
|
|
StorageMock *MockStorage
|
|
|
|
NotifierMock *MockNotifier
|
|
|
|
TOTPMock *MockTOTP
|
2023-01-07 00:19:41 +00:00
|
|
|
RandomMock *MockRandom
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
UserSession *session.UserSession
|
2019-11-24 20:27:59 +00:00
|
|
|
|
2022-12-21 10:31:21 +00:00
|
|
|
Clock utils.TestingClock
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// NewMockAutheliaCtx create an instance of AutheliaCtx mock.
|
2019-04-24 21:52:08 +00:00
|
|
|
func NewMockAutheliaCtx(t *testing.T) *MockAutheliaCtx {
|
|
|
|
mockAuthelia := new(MockAutheliaCtx)
|
2022-12-21 10:31:21 +00:00
|
|
|
mockAuthelia.Clock = utils.TestingClock{}
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2019-11-30 14:33:45 +00:00
|
|
|
datetime, _ := time.Parse("2006-Jan-02", "2013-Feb-03")
|
|
|
|
mockAuthelia.Clock.Set(datetime)
|
|
|
|
|
2022-08-07 11:13:56 +00:00
|
|
|
config := schema.Configuration{}
|
2023-01-12 10:57:44 +00:00
|
|
|
config.Session.Cookies = []schema.SessionCookieConfiguration{
|
|
|
|
{
|
|
|
|
SessionCookieCommonConfiguration: schema.SessionCookieCommonConfiguration{
|
|
|
|
Name: "authelia_session",
|
|
|
|
Domain: "example.com",
|
|
|
|
RememberMe: schema.DefaultSessionConfiguration.RememberMe,
|
|
|
|
Expiration: schema.DefaultSessionConfiguration.Expiration,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
SessionCookieCommonConfiguration: schema.SessionCookieCommonConfiguration{
|
|
|
|
Name: "authelia_session",
|
|
|
|
Domain: "example2.com",
|
|
|
|
RememberMe: schema.DefaultSessionConfiguration.RememberMe,
|
|
|
|
Expiration: schema.DefaultSessionConfiguration.Expiration,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-07 11:13:56 +00:00
|
|
|
config.AccessControl.DefaultPolicy = "deny"
|
|
|
|
config.AccessControl.Rules = []schema.ACLRule{{
|
2020-04-16 00:18:11 +00:00
|
|
|
Domains: []string{"bypass.example.com"},
|
|
|
|
Policy: "bypass",
|
2020-04-09 01:05:17 +00:00
|
|
|
}, {
|
2020-04-16 00:18:11 +00:00
|
|
|
Domains: []string{"one-factor.example.com"},
|
|
|
|
Policy: "one_factor",
|
2020-04-09 01:05:17 +00:00
|
|
|
}, {
|
2020-04-16 00:18:11 +00:00
|
|
|
Domains: []string{"two-factor.example.com"},
|
|
|
|
Policy: "two_factor",
|
2020-04-09 01:05:17 +00:00
|
|
|
}, {
|
2020-04-16 00:18:11 +00:00
|
|
|
Domains: []string{"deny.example.com"},
|
|
|
|
Policy: "deny",
|
2020-05-04 19:39:25 +00:00
|
|
|
}, {
|
|
|
|
Domains: []string{"admin.example.com"},
|
|
|
|
Policy: "two_factor",
|
2020-06-25 08:22:42 +00:00
|
|
|
Subjects: [][]string{{"group:admin"}},
|
2020-05-04 19:39:25 +00:00
|
|
|
}, {
|
|
|
|
Domains: []string{"grafana.example.com"},
|
|
|
|
Policy: "two_factor",
|
2020-06-25 08:22:42 +00:00
|
|
|
Subjects: [][]string{{"group:grafana"}},
|
2019-04-24 21:52:08 +00:00
|
|
|
}}
|
|
|
|
|
|
|
|
providers := middlewares.Providers{}
|
|
|
|
|
|
|
|
mockAuthelia.Ctrl = gomock.NewController(t)
|
|
|
|
mockAuthelia.UserProviderMock = NewMockUserProvider(mockAuthelia.Ctrl)
|
|
|
|
providers.UserProvider = mockAuthelia.UserProviderMock
|
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
mockAuthelia.StorageMock = NewMockStorage(mockAuthelia.Ctrl)
|
|
|
|
providers.StorageProvider = mockAuthelia.StorageMock
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
mockAuthelia.NotifierMock = NewMockNotifier(mockAuthelia.Ctrl)
|
|
|
|
providers.Notifier = mockAuthelia.NotifierMock
|
|
|
|
|
|
|
|
providers.Authorizer = authorization.NewAuthorizer(
|
2022-08-07 11:13:56 +00:00
|
|
|
&config)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
providers.SessionProvider = session.NewProvider(
|
2022-08-07 11:13:56 +00:00
|
|
|
config.Session, nil)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2022-08-07 11:13:56 +00:00
|
|
|
providers.Regulator = regulation.NewRegulator(config.Regulation, providers.StorageProvider, &mockAuthelia.Clock)
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2021-12-01 12:11:29 +00:00
|
|
|
mockAuthelia.TOTPMock = NewMockTOTP(mockAuthelia.Ctrl)
|
|
|
|
providers.TOTP = mockAuthelia.TOTPMock
|
|
|
|
|
2023-01-07 00:19:41 +00:00
|
|
|
mockAuthelia.RandomMock = NewMockRandom(mockAuthelia.Ctrl)
|
|
|
|
|
|
|
|
providers.Random = random.NewMathematical()
|
|
|
|
|
2022-07-18 00:56:09 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if providers.Templates, err = templates.New(templates.Config{}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
request := &fasthttp.RequestCtx{}
|
2020-04-20 21:03:38 +00:00
|
|
|
// Set a cookie to identify this client throughout the test.
|
2022-01-31 05:25:15 +00:00
|
|
|
// request.Request.Header.SetCookie("authelia_session", "client_cookie").
|
2019-04-24 21:52:08 +00:00
|
|
|
|
2023-01-12 10:57:44 +00:00
|
|
|
// Set X-Forwarded-Host for compatibility with multi-root-domain implementation.
|
|
|
|
request.Request.Header.Set("X-Forwarded-Host", "example.com")
|
|
|
|
|
2022-08-07 11:13:56 +00:00
|
|
|
ctx := middlewares.NewAutheliaCtx(request, config, providers)
|
2022-06-10 01:34:43 +00:00
|
|
|
mockAuthelia.Ctx = ctx
|
2019-04-24 21:52:08 +00:00
|
|
|
|
|
|
|
logger, hook := test.NewNullLogger()
|
|
|
|
mockAuthelia.Hook = hook
|
|
|
|
|
|
|
|
mockAuthelia.Ctx.Logger = logrus.NewEntry(logger)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return mockAuthelia
|
|
|
|
}
|
|
|
|
|
2021-08-02 06:15:38 +00:00
|
|
|
// NewMockAutheliaCtxWithUserSession create an instance of AutheliaCtx mock with predefined user session.
|
|
|
|
func NewMockAutheliaCtxWithUserSession(t *testing.T, userSession session.UserSession) *MockAutheliaCtx {
|
|
|
|
mock := NewMockAutheliaCtx(t)
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return mock
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:03:38 +00:00
|
|
|
// Close close the mock.
|
2019-04-24 21:52:08 +00:00
|
|
|
func (m *MockAutheliaCtx) Close() {
|
|
|
|
m.Hook.Reset()
|
2019-11-24 20:27:59 +00:00
|
|
|
m.Ctrl.Finish()
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 06:15:38 +00:00
|
|
|
// SetRequestBody set the request body from a struct with json tags.
|
|
|
|
func (m *MockAutheliaCtx) SetRequestBody(t *testing.T, body interface{}) {
|
|
|
|
bodyBytes, err := json.Marshal(body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
m.Ctx.Request.SetBody(bodyBytes)
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:27:38 +00:00
|
|
|
// Assert401KO assert an error response from the service.
|
|
|
|
func (m *MockAutheliaCtx) Assert401KO(t *testing.T, message string) {
|
|
|
|
assert.Equal(t, 401, m.Ctx.Response.StatusCode())
|
|
|
|
assert.Equal(t, fmt.Sprintf("{\"status\":\"KO\",\"message\":\"%s\"}", message), string(m.Ctx.Response.Body()))
|
|
|
|
}
|
|
|
|
|
2019-04-24 21:52:08 +00: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())
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
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 11:18:22 +00:00
|
|
|
|
2020-04-21 00:48:24 +00:00
|
|
|
// GetResponseData retrieves a response from the service.
|
2019-12-07 11:18:22 +00: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)
|
|
|
|
}
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
// GetResponseError retrieves an error response from the service.
|
|
|
|
func (m *MockAutheliaCtx) GetResponseError(t *testing.T) (errResponse middlewares.ErrorResponse) {
|
|
|
|
err := json.Unmarshal(m.Ctx.Response.Body(), &errResponse)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return errResponse
|
|
|
|
}
|