diff options
| author | Brynn Crowley <littlehill723@gmail.com> | 2025-03-08 15:04:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-08 15:04:15 +0000 |
| commit | 1c907929c614779adb963c97776810cdba8ce5f6 (patch) | |
| tree | 746e3f015490252cc8b96af0fe4b1ba894a987bb /internal/mocks | |
| parent | 9241731a4dd5592b4a02b5352c903b4d06b6f4ab (diff) | |
refactor(handlers): add more detailed errors for password-change failures (#8899)
Adds some more helpful log information to the change password feature.
Signed-off-by: Brynn Crowley <littlehill723@gmail.com>
Signed-off-by: James Elliott <james-d-elliott@users.noreply.github.com>
Co-authored-by: James Elliott <james-d-elliott@users.noreply.github.com>
Diffstat (limited to 'internal/mocks')
| -rw-r--r-- | internal/mocks/authelia_ctx.go | 49 |
1 files changed, 43 insertions, 6 deletions
diff --git a/internal/mocks/authelia_ctx.go b/internal/mocks/authelia_ctx.go index b69f03ab2..f1d7202c2 100644 --- a/internal/mocks/authelia_ctx.go +++ b/internal/mocks/authelia_ctx.go @@ -268,16 +268,14 @@ func (m *MockAutheliaCtx) SetRequestBody(t *testing.T, body interface{}) { m.Ctx.Request.SetBody(bodyBytes) } -func (m *MockAutheliaCtx) LogEntryN(i int) *logrus.Entry { +func (m *MockAutheliaCtx) LogEntryN(n int) *logrus.Entry { entries := m.Hook.AllEntries() - n := len(entries) - (1 + i) - - if n < 0 { + if i := len(entries) - (1 + n); i < 0 { return nil + } else { + return entries[i] } - - return entries[n] } // AssertKO assert an error response from the service. @@ -376,6 +374,22 @@ func (m *MockAutheliaCtx) AssertLastLogMessage(t *testing.T, message, err string } } +func (m *MockAutheliaCtx) AssertLogEntryAdvanced(t *testing.T, n int, level logrus.Level, message any, fields map[string]any) { + entry := m.LogEntryN(n) + + require.NotNil(t, entry) + + assert.Equal(t, level, entry.Level) + + AssertIsStringEqualOrRegexp(t, message, entry.Message) + + for field, expected := range fields { + require.Contains(t, entry.Data, field) + + AssertIsStringEqualOrRegexp(t, expected, entry.Data[field]) + } +} + // GetResponseData retrieves a response from the service. func (m *MockAutheliaCtx) GetResponseData(t *testing.T, data interface{}) { okResponse := middlewares.OKResponse{} @@ -391,3 +405,26 @@ func (m *MockAutheliaCtx) GetResponseError(t *testing.T) (errResponse middleware return errResponse } + +func AssertIsStringEqualOrRegexp(t *testing.T, expected any, actual any) { + switch v := expected.(type) { + case string: + switch a := actual.(type) { + case error: + assert.EqualError(t, a, v) + default: + assert.Equal(t, v, a) + } + case *regexp.Regexp: + switch a := actual.(type) { + case error: + assert.Regexp(t, v, a.Error()) + default: + assert.Regexp(t, v, a) + } + case nil: + break + default: + t.Fatal("Expected value must be a string or Regexp") + } +} |
