2019-12-07 18:18:22 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2021-11-23 16:45:38 +07:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2019-12-07 18:18:22 +07:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-16 08:47:31 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-07 18:18:22 +07:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-05-01 13:56:42 +07:00
|
|
|
|
2022-03-28 08:26:30 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/mocks"
|
2022-03-06 12:47:40 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/model"
|
2019-12-07 18:18:22 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
type FetchSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
mock *mocks.MockAutheliaCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FetchSuite) SetupTest() {
|
|
|
|
s.mock = mocks.NewMockAutheliaCtx(s.T())
|
2020-01-06 06:03:16 +07:00
|
|
|
// Set the initial user session.
|
2019-12-07 18:18:22 +07:00
|
|
|
userSession := s.mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2019-12-07 18:18:22 +07:00
|
|
|
userSession.AuthenticationLevel = 1
|
2020-12-16 08:47:31 +07:00
|
|
|
err := s.mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(s.T(), err)
|
2019-12-07 18:18:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FetchSuite) TearDownTest() {
|
|
|
|
s.mock.Close()
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
type expectedResponse struct {
|
2022-03-06 12:47:40 +07:00
|
|
|
db model.UserInfo
|
|
|
|
api *model.UserInfo
|
2021-11-23 16:45:38 +07:00
|
|
|
err error
|
2019-12-07 18:18:22 +07:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:26:30 +07:00
|
|
|
type expectedResponseAlt struct {
|
|
|
|
description string
|
|
|
|
|
|
|
|
db model.UserInfo
|
|
|
|
api *model.UserInfo
|
|
|
|
loadErr error
|
|
|
|
saveErr error
|
|
|
|
config *schema.Configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserInfoEndpoint_SetCorrectMethod(t *testing.T) {
|
2021-11-23 16:45:38 +07:00
|
|
|
expectedResponses := []expectedResponse{
|
|
|
|
{
|
2022-03-06 12:47:40 +07:00
|
|
|
db: model.UserInfo{
|
2021-11-23 16:45:38 +07:00
|
|
|
Method: "totp",
|
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
{
|
2022-03-06 12:47:40 +07:00
|
|
|
db: model.UserInfo{
|
2022-03-03 18:20:43 +07:00
|
|
|
Method: "webauthn",
|
|
|
|
HasWebauthn: true,
|
|
|
|
HasTOTP: true,
|
2021-11-23 16:45:38 +07:00
|
|
|
},
|
|
|
|
err: nil,
|
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2022-03-06 12:47:40 +07:00
|
|
|
db: model.UserInfo{
|
2022-03-03 18:20:43 +07:00
|
|
|
Method: "webauthn",
|
|
|
|
HasWebauthn: true,
|
|
|
|
HasTOTP: false,
|
2021-11-23 16:45:38 +07:00
|
|
|
},
|
|
|
|
err: nil,
|
2019-12-07 18:18:22 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2022-03-06 12:47:40 +07:00
|
|
|
db: model.UserInfo{
|
2022-03-03 18:20:43 +07:00
|
|
|
Method: "mobile_push",
|
|
|
|
HasWebauthn: false,
|
|
|
|
HasTOTP: false,
|
2021-11-23 16:45:38 +07:00
|
|
|
},
|
|
|
|
err: nil,
|
2019-12-07 18:18:22 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2022-03-06 12:47:40 +07:00
|
|
|
db: model.UserInfo{},
|
2021-11-23 16:45:38 +07:00
|
|
|
err: sql.ErrNoRows,
|
2019-12-07 18:18:22 +07:00
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2022-03-06 12:47:40 +07:00
|
|
|
db: model.UserInfo{},
|
2021-11-23 16:45:38 +07:00
|
|
|
err: errors.New("invalid thing"),
|
2019-12-08 00:51:47 +07:00
|
|
|
},
|
2019-12-07 18:18:22 +07:00
|
|
|
}
|
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
for _, resp := range expectedResponses {
|
|
|
|
if resp.api == nil {
|
|
|
|
resp.api = &resp.db
|
|
|
|
}
|
|
|
|
|
2019-12-08 00:51:47 +07:00
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
2022-03-28 08:26:30 +07:00
|
|
|
|
2020-01-06 06:03:16 +07:00
|
|
|
// Set the initial user session.
|
2019-12-08 00:51:47 +07:00
|
|
|
userSession := mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2019-12-08 00:51:47 +07:00
|
|
|
userSession.AuthenticationLevel = 1
|
2020-12-16 08:47:31 +07:00
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
2019-12-08 00:51:47 +07:00
|
|
|
|
2021-12-01 19:11:29 +07:00
|
|
|
mock.StorageMock.
|
2021-11-23 16:45:38 +07:00
|
|
|
EXPECT().
|
|
|
|
LoadUserInfo(mock.Ctx, gomock.Eq("john")).
|
|
|
|
Return(resp.db, resp.err)
|
|
|
|
|
2022-03-28 08:26:30 +07:00
|
|
|
UserInfoGET(mock.Ctx)
|
2019-12-07 18:18:22 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
if resp.err == nil {
|
|
|
|
t.Run("expected status code", func(t *testing.T) {
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
})
|
2019-12-07 18:18:22 +07:00
|
|
|
|
2022-03-06 12:47:40 +07:00
|
|
|
actualPreferences := model.UserInfo{}
|
2019-12-07 18:18:22 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
mock.GetResponseData(t, &actualPreferences)
|
2019-12-07 18:18:22 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
t.Run("expected method", func(t *testing.T) {
|
|
|
|
assert.Equal(t, resp.api.Method, actualPreferences.Method)
|
|
|
|
})
|
2019-12-07 18:18:22 +07:00
|
|
|
|
2022-03-03 18:20:43 +07:00
|
|
|
t.Run("registered webauthn", func(t *testing.T) {
|
|
|
|
assert.Equal(t, resp.api.HasWebauthn, actualPreferences.HasWebauthn)
|
2021-11-23 16:45:38 +07:00
|
|
|
})
|
2019-12-07 18:18:22 +07:00
|
|
|
|
2021-11-23 16:45:38 +07:00
|
|
|
t.Run("registered totp", func(t *testing.T) {
|
|
|
|
assert.Equal(t, resp.api.HasTOTP, actualPreferences.HasTOTP)
|
|
|
|
})
|
2022-03-28 08:26:30 +07:00
|
|
|
|
|
|
|
t.Run("registered duo", func(t *testing.T) {
|
|
|
|
assert.Equal(t, resp.api.HasDuo, actualPreferences.HasDuo)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
t.Run("expected status code", func(t *testing.T) {
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
})
|
|
|
|
|
|
|
|
errResponse := mock.GetResponseError(t)
|
|
|
|
|
|
|
|
assert.Equal(t, "KO", errResponse.Status)
|
|
|
|
assert.Equal(t, "Operation failed.", errResponse.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
mock.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserInfoEndpoint_SetDefaultMethod(t *testing.T) {
|
|
|
|
expectedResponses := []expectedResponseAlt{
|
|
|
|
{
|
|
|
|
description: "should set method to totp by default even when user doesn't have totp configured and no preferred method",
|
|
|
|
db: model.UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
HasDuo: false,
|
|
|
|
},
|
|
|
|
api: &model.UserInfo{
|
|
|
|
Method: "totp",
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
HasDuo: false,
|
|
|
|
},
|
2022-04-16 06:34:26 +07:00
|
|
|
config: &schema.Configuration{},
|
2022-03-28 08:26:30 +07:00
|
|
|
loadErr: nil,
|
|
|
|
saveErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "should set method to duo by default when user has duo configured and no preferred method",
|
|
|
|
db: model.UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
HasDuo: true,
|
|
|
|
},
|
|
|
|
api: &model.UserInfo{
|
|
|
|
Method: "mobile_push",
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
HasDuo: true,
|
|
|
|
},
|
2022-04-16 06:34:26 +07:00
|
|
|
config: &schema.Configuration{},
|
2022-03-28 08:26:30 +07:00
|
|
|
loadErr: nil,
|
|
|
|
saveErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "should set method to totp by default when user has duo configured and no preferred method but duo is not enabled",
|
|
|
|
db: model.UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
HasDuo: true,
|
|
|
|
},
|
|
|
|
api: &model.UserInfo{
|
|
|
|
Method: "totp",
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
HasDuo: true,
|
|
|
|
},
|
2022-04-16 06:34:26 +07:00
|
|
|
config: &schema.Configuration{DuoAPI: schema.DuoAPIConfiguration{Disable: true}},
|
2022-03-28 08:26:30 +07:00
|
|
|
loadErr: nil,
|
|
|
|
saveErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "should set method to duo by default when user has duo configured and no preferred method",
|
|
|
|
db: model.UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
HasDuo: true,
|
|
|
|
},
|
|
|
|
api: &model.UserInfo{
|
|
|
|
Method: "webauthn",
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
HasDuo: true,
|
|
|
|
},
|
|
|
|
config: &schema.Configuration{
|
|
|
|
TOTP: schema.TOTPConfiguration{
|
|
|
|
Disable: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
loadErr: nil,
|
|
|
|
saveErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "should default new users to totp if all enabled",
|
|
|
|
db: model.UserInfo{
|
|
|
|
Method: "",
|
|
|
|
HasTOTP: false,
|
|
|
|
HasWebauthn: false,
|
|
|
|
HasDuo: false,
|
|
|
|
},
|
|
|
|
api: &model.UserInfo{
|
|
|
|
Method: "totp",
|
|
|
|
HasTOTP: true,
|
|
|
|
HasWebauthn: true,
|
|
|
|
HasDuo: true,
|
|
|
|
},
|
2022-04-16 06:34:26 +07:00
|
|
|
config: &schema.Configuration{},
|
2022-03-28 08:26:30 +07:00
|
|
|
loadErr: nil,
|
|
|
|
saveErr: errors.New("could not save"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, resp := range expectedResponses {
|
2022-04-16 06:34:26 +07:00
|
|
|
t.Run(resp.description, func(t *testing.T) {
|
|
|
|
if resp.api == nil {
|
|
|
|
resp.api = &resp.db
|
|
|
|
}
|
|
|
|
|
|
|
|
mock := mocks.NewMockAutheliaCtx(t)
|
|
|
|
|
|
|
|
if resp.config != nil {
|
|
|
|
mock.Ctx.Configuration = *resp.config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the initial user session.
|
|
|
|
userSession := mock.Ctx.GetSession()
|
|
|
|
userSession.Username = testUsername
|
|
|
|
userSession.AuthenticationLevel = 1
|
|
|
|
err := mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if resp.db.Method == "" {
|
|
|
|
gomock.InOrder(
|
|
|
|
mock.StorageMock.
|
|
|
|
EXPECT().
|
|
|
|
LoadPreferred2FAMethod(mock.Ctx, gomock.Eq("john")).
|
|
|
|
Return("", sql.ErrNoRows),
|
|
|
|
mock.StorageMock.
|
|
|
|
EXPECT().
|
|
|
|
SavePreferred2FAMethod(mock.Ctx, gomock.Eq("john"), gomock.Eq("")).
|
|
|
|
Return(resp.saveErr),
|
|
|
|
mock.StorageMock.
|
|
|
|
EXPECT().
|
|
|
|
LoadUserInfo(mock.Ctx, gomock.Eq("john")).
|
|
|
|
Return(resp.db, nil),
|
|
|
|
mock.StorageMock.EXPECT().
|
|
|
|
SavePreferred2FAMethod(mock.Ctx, gomock.Eq("john"), gomock.Eq(resp.api.Method)).
|
|
|
|
Return(resp.saveErr),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
gomock.InOrder(
|
|
|
|
mock.StorageMock.
|
|
|
|
EXPECT().
|
|
|
|
LoadPreferred2FAMethod(mock.Ctx, gomock.Eq("john")).
|
|
|
|
Return(resp.db.Method, nil),
|
|
|
|
mock.StorageMock.
|
|
|
|
EXPECT().
|
|
|
|
LoadUserInfo(mock.Ctx, gomock.Eq("john")).
|
|
|
|
Return(resp.db, nil),
|
|
|
|
mock.StorageMock.EXPECT().
|
|
|
|
SavePreferred2FAMethod(mock.Ctx, gomock.Eq("john"), gomock.Eq(resp.api.Method)).
|
|
|
|
Return(resp.saveErr),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
UserInfoPOST(mock.Ctx)
|
|
|
|
|
|
|
|
if resp.loadErr == nil && resp.saveErr == nil {
|
|
|
|
t.Run(fmt.Sprintf("%s/%s", resp.description, "expected status code"), func(t *testing.T) {
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
})
|
|
|
|
|
|
|
|
actualPreferences := model.UserInfo{}
|
|
|
|
|
|
|
|
mock.GetResponseData(t, &actualPreferences)
|
|
|
|
|
|
|
|
t.Run("expected method", func(t *testing.T) {
|
|
|
|
assert.Equal(t, resp.api.Method, actualPreferences.Method)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("registered webauthn", func(t *testing.T) {
|
|
|
|
assert.Equal(t, resp.api.HasWebauthn, actualPreferences.HasWebauthn)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("registered totp", func(t *testing.T) {
|
|
|
|
assert.Equal(t, resp.api.HasTOTP, actualPreferences.HasTOTP)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("registered duo", func(t *testing.T) {
|
|
|
|
assert.Equal(t, resp.api.HasDuo, actualPreferences.HasDuo)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
t.Run("expected status code", func(t *testing.T) {
|
|
|
|
assert.Equal(t, 200, mock.Ctx.Response.StatusCode())
|
|
|
|
})
|
|
|
|
|
|
|
|
errResponse := mock.GetResponseError(t)
|
|
|
|
|
|
|
|
assert.Equal(t, "KO", errResponse.Status)
|
|
|
|
assert.Equal(t, "Operation failed.", errResponse.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
mock.Close()
|
|
|
|
})
|
2021-11-23 16:45:38 +07:00
|
|
|
}
|
2019-12-07 18:18:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FetchSuite) TestShouldReturnError500WhenStorageFailsToLoad() {
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.EXPECT().
|
2021-11-23 16:45:38 +07:00
|
|
|
LoadUserInfo(s.mock.Ctx, gomock.Eq("john")).
|
2022-03-06 12:47:40 +07:00
|
|
|
Return(model.UserInfo{}, fmt.Errorf("failure"))
|
2019-12-08 00:51:47 +07:00
|
|
|
|
2022-03-28 08:26:30 +07:00
|
|
|
UserInfoGET(s.mock.Ctx)
|
2019-12-07 18:18:22 +07:00
|
|
|
|
|
|
|
s.mock.Assert200KO(s.T(), "Operation failed.")
|
2021-11-23 16:45:38 +07:00
|
|
|
assert.Equal(s.T(), "unable to load user information: failure", s.mock.Hook.LastEntry().Message)
|
2019-12-07 18:18:22 +07:00
|
|
|
assert.Equal(s.T(), logrus.ErrorLevel, s.mock.Hook.LastEntry().Level)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &FetchSuite{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type SaveSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
mock *mocks.MockAutheliaCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SaveSuite) SetupTest() {
|
|
|
|
s.mock = mocks.NewMockAutheliaCtx(s.T())
|
2020-01-06 06:03:16 +07:00
|
|
|
// Set the initial user session.
|
2019-12-07 18:18:22 +07:00
|
|
|
userSession := s.mock.Ctx.GetSession()
|
2020-05-02 23:20:40 +07:00
|
|
|
userSession.Username = testUsername
|
2019-12-07 18:18:22 +07:00
|
|
|
userSession.AuthenticationLevel = 1
|
2020-12-16 08:47:31 +07:00
|
|
|
err := s.mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(s.T(), err)
|
2019-12-07 18:18:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SaveSuite) TearDownTest() {
|
|
|
|
s.mock.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SaveSuite) TestShouldReturnError500WhenNoBodyProvided() {
|
|
|
|
s.mock.Ctx.Request.SetBody(nil)
|
2022-04-08 11:13:47 +07:00
|
|
|
MethodPreferencePOST(s.mock.Ctx)
|
2019-12-07 18:18:22 +07:00
|
|
|
|
|
|
|
s.mock.Assert200KO(s.T(), "Operation failed.")
|
2021-11-29 10:09:14 +07:00
|
|
|
assert.Equal(s.T(), "unable to parse body: unexpected end of JSON input", s.mock.Hook.LastEntry().Message)
|
2019-12-07 18:18:22 +07:00
|
|
|
assert.Equal(s.T(), logrus.ErrorLevel, s.mock.Hook.LastEntry().Level)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SaveSuite) TestShouldReturnError500WhenMalformedBodyProvided() {
|
|
|
|
s.mock.Ctx.Request.SetBody([]byte("{\"method\":\"abc\""))
|
2022-04-08 11:13:47 +07:00
|
|
|
MethodPreferencePOST(s.mock.Ctx)
|
2019-12-07 18:18:22 +07:00
|
|
|
|
|
|
|
s.mock.Assert200KO(s.T(), "Operation failed.")
|
2021-11-29 10:09:14 +07:00
|
|
|
assert.Equal(s.T(), "unable to parse body: unexpected end of JSON input", s.mock.Hook.LastEntry().Message)
|
2019-12-07 18:18:22 +07:00
|
|
|
assert.Equal(s.T(), logrus.ErrorLevel, s.mock.Hook.LastEntry().Level)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SaveSuite) TestShouldReturnError500WhenBadBodyProvided() {
|
|
|
|
s.mock.Ctx.Request.SetBody([]byte("{\"weird_key\":\"abc\"}"))
|
2022-04-08 11:13:47 +07:00
|
|
|
MethodPreferencePOST(s.mock.Ctx)
|
2019-12-07 18:18:22 +07:00
|
|
|
|
|
|
|
s.mock.Assert200KO(s.T(), "Operation failed.")
|
2021-11-29 10:09:14 +07:00
|
|
|
assert.Equal(s.T(), "unable to validate body: method: non zero value required", s.mock.Hook.LastEntry().Message)
|
2019-12-07 18:18:22 +07:00
|
|
|
assert.Equal(s.T(), logrus.ErrorLevel, s.mock.Hook.LastEntry().Level)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SaveSuite) TestShouldReturnError500WhenBadMethodProvided() {
|
|
|
|
s.mock.Ctx.Request.SetBody([]byte("{\"method\":\"abc\"}"))
|
2022-04-08 11:13:47 +07:00
|
|
|
MethodPreferencePOST(s.mock.Ctx)
|
2019-12-07 18:18:22 +07:00
|
|
|
|
|
|
|
s.mock.Assert200KO(s.T(), "Operation failed.")
|
2022-04-16 06:34:26 +07:00
|
|
|
assert.Equal(s.T(), "unknown or unavailable method 'abc', it should be one of totp, webauthn, mobile_push", s.mock.Hook.LastEntry().Message)
|
2019-12-07 18:18:22 +07:00
|
|
|
assert.Equal(s.T(), logrus.ErrorLevel, s.mock.Hook.LastEntry().Level)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SaveSuite) TestShouldReturnError500WhenDatabaseFailsToSave() {
|
2022-03-03 18:20:43 +07:00
|
|
|
s.mock.Ctx.Request.SetBody([]byte("{\"method\":\"webauthn\"}"))
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.EXPECT().
|
2022-03-03 18:20:43 +07:00
|
|
|
SavePreferred2FAMethod(s.mock.Ctx, gomock.Eq("john"), gomock.Eq("webauthn")).
|
2019-12-07 18:18:22 +07:00
|
|
|
Return(fmt.Errorf("Failure"))
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
MethodPreferencePOST(s.mock.Ctx)
|
2019-12-07 18:18:22 +07:00
|
|
|
|
|
|
|
s.mock.Assert200KO(s.T(), "Operation failed.")
|
2021-09-17 12:53:40 +07:00
|
|
|
assert.Equal(s.T(), "unable to save new preferred 2FA method: Failure", s.mock.Hook.LastEntry().Message)
|
2019-12-07 18:18:22 +07:00
|
|
|
assert.Equal(s.T(), logrus.ErrorLevel, s.mock.Hook.LastEntry().Level)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SaveSuite) TestShouldReturn200WhenMethodIsSuccessfullySaved() {
|
2022-03-03 18:20:43 +07:00
|
|
|
s.mock.Ctx.Request.SetBody([]byte("{\"method\":\"webauthn\"}"))
|
2021-12-01 19:11:29 +07:00
|
|
|
s.mock.StorageMock.EXPECT().
|
2022-03-03 18:20:43 +07:00
|
|
|
SavePreferred2FAMethod(s.mock.Ctx, gomock.Eq("john"), gomock.Eq("webauthn")).
|
2019-12-07 18:18:22 +07:00
|
|
|
Return(nil)
|
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
MethodPreferencePOST(s.mock.Ctx)
|
2019-12-07 18:18:22 +07:00
|
|
|
|
|
|
|
assert.Equal(s.T(), 200, s.mock.Ctx.Response.StatusCode())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &SaveSuite{})
|
|
|
|
}
|