summaryrefslogtreecommitdiff
path: root/internal/handlers/handler_user_info_test.go
diff options
context:
space:
mode:
authorJames Elliott <james-d-elliott@users.noreply.github.com>2022-04-16 09:34:26 +1000
committerGitHub <noreply@github.com>2022-04-16 09:34:26 +1000
commit4710de33a4f1702ddac62e09aea8962e57009bde (patch)
tree50b4a9544b6753b3007ef8a27b7f84f27508f53f /internal/handlers/handler_user_info_test.go
parentc9568caf4d1d11c7d6fff524083c53d4a6bf9297 (diff)
refactor(configuration): remove ptr for duoapi and notifier (#3200)
This adds to the ongoing effort to remove all pointers to structs in the configuration without breaking backwards compatibility.
Diffstat (limited to 'internal/handlers/handler_user_info_test.go')
-rw-r--r--internal/handlers/handler_user_info_test.go198
1 files changed, 96 insertions, 102 deletions
diff --git a/internal/handlers/handler_user_info_test.go b/internal/handlers/handler_user_info_test.go
index e4d0fa659..47bc9a6b8 100644
--- a/internal/handlers/handler_user_info_test.go
+++ b/internal/handlers/handler_user_info_test.go
@@ -101,8 +101,6 @@ func TestUserInfoEndpoint_SetCorrectMethod(t *testing.T) {
mock := mocks.NewMockAutheliaCtx(t)
- mock.Ctx.Configuration.DuoAPI = &schema.DuoAPIConfiguration{}
-
// Set the initial user session.
userSession := mock.Ctx.GetSession()
userSession.Username = testUsername
@@ -172,9 +170,7 @@ func TestUserInfoEndpoint_SetDefaultMethod(t *testing.T) {
HasWebauthn: false,
HasDuo: false,
},
- config: &schema.Configuration{
- DuoAPI: &schema.DuoAPIConfiguration{},
- },
+ config: &schema.Configuration{},
loadErr: nil,
saveErr: nil,
},
@@ -192,9 +188,7 @@ func TestUserInfoEndpoint_SetDefaultMethod(t *testing.T) {
HasWebauthn: false,
HasDuo: true,
},
- config: &schema.Configuration{
- DuoAPI: &schema.DuoAPIConfiguration{},
- },
+ config: &schema.Configuration{},
loadErr: nil,
saveErr: nil,
},
@@ -212,6 +206,7 @@ func TestUserInfoEndpoint_SetDefaultMethod(t *testing.T) {
HasWebauthn: false,
HasDuo: true,
},
+ config: &schema.Configuration{DuoAPI: schema.DuoAPIConfiguration{Disable: true}},
loadErr: nil,
saveErr: nil,
},
@@ -233,7 +228,6 @@ func TestUserInfoEndpoint_SetDefaultMethod(t *testing.T) {
TOTP: schema.TOTPConfiguration{
Disable: true,
},
- DuoAPI: &schema.DuoAPIConfiguration{},
},
loadErr: nil,
saveErr: nil,
@@ -252,104 +246,104 @@ func TestUserInfoEndpoint_SetDefaultMethod(t *testing.T) {
HasWebauthn: true,
HasDuo: true,
},
- config: &schema.Configuration{
- DuoAPI: &schema.DuoAPIConfiguration{},
- },
+ config: &schema.Configuration{},
loadErr: nil,
saveErr: errors.New("could not save"),
},
}
for _, resp := range expectedResponses {
- 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(fmt.Sprintf("%s/%s", resp.description, "expected method"), func(t *testing.T) {
- assert.Equal(t, resp.api.Method, actualPreferences.Method)
- })
-
- t.Run(fmt.Sprintf("%s/%s", resp.description, "registered webauthn"), func(t *testing.T) {
- assert.Equal(t, resp.api.HasWebauthn, actualPreferences.HasWebauthn)
- })
-
- t.Run(fmt.Sprintf("%s/%s", resp.description, "registered totp"), func(t *testing.T) {
- assert.Equal(t, resp.api.HasTOTP, actualPreferences.HasTOTP)
- })
-
- t.Run(fmt.Sprintf("%s/%s", resp.description, "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()
+ 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()
+ })
}
}
@@ -420,7 +414,7 @@ func (s *SaveSuite) TestShouldReturnError500WhenBadMethodProvided() {
MethodPreferencePOST(s.mock.Ctx)
s.mock.Assert200KO(s.T(), "Operation failed.")
- assert.Equal(s.T(), "unknown or unavailable method 'abc', it should be one of totp, webauthn", s.mock.Hook.LastEntry().Message)
+ assert.Equal(s.T(), "unknown or unavailable method 'abc', it should be one of totp, webauthn, mobile_push", s.mock.Hook.LastEntry().Message)
assert.Equal(s.T(), logrus.ErrorLevel, s.mock.Hook.LastEntry().Level)
}