summaryrefslogtreecommitdiff
path: root/internal/handlers/handler_firstfactor_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/handlers/handler_firstfactor_test.go')
-rw-r--r--internal/handlers/handler_firstfactor_test.go35
1 files changed, 28 insertions, 7 deletions
diff --git a/internal/handlers/handler_firstfactor_test.go b/internal/handlers/handler_firstfactor_test.go
index fb00a51df..d7a2a1a14 100644
--- a/internal/handlers/handler_firstfactor_test.go
+++ b/internal/handlers/handler_firstfactor_test.go
@@ -6,7 +6,9 @@ import (
"testing"
"github.com/golang/mock/gomock"
+ "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/valyala/fasthttp"
@@ -36,7 +38,7 @@ func (s *FirstFactorSuite) TestShouldFailIfBodyIsNil() {
FirstFactorPOST(nil)(s.mock.Ctx)
// No body.
- assert.Equal(s.T(), "Failed to parse 1FA request body: unable to parse body: unexpected end of JSON input", s.mock.Hook.LastEntry().Message)
+ AssertLogEntryMessageAndError(s.T(), s.mock.Hook.LastEntry(), "Failed to parse 1FA request body", "unable to parse body: unexpected end of JSON input")
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
}
@@ -47,7 +49,7 @@ func (s *FirstFactorSuite) TestShouldFailIfBodyIsInBadFormat() {
}`)
FirstFactorPOST(nil)(s.mock.Ctx)
- assert.Equal(s.T(), "Failed to parse 1FA request body: unable to validate body: password: non zero value required", s.mock.Hook.LastEntry().Message)
+ AssertLogEntryMessageAndError(s.T(), s.mock.Hook.LastEntry(), "Failed to parse 1FA request body", "unable to validate body: password: non zero value required")
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
}
@@ -75,8 +77,8 @@ func (s *FirstFactorSuite) TestShouldFailIfUserProviderCheckPasswordFail() {
}`)
FirstFactorPOST(nil)(s.mock.Ctx)
- assert.Equal(s.T(), "Unsuccessful 1FA authentication attempt by user 'test'", s.mock.Hook.LastEntry().Message)
- assert.EqualError(s.T(), s.mock.Hook.LastEntry().Data["error"].(error), "failed")
+ AssertLogEntryMessageAndError(s.T(), s.mock.Hook.LastEntry(), "Unsuccessful 1FA authentication attempt by user 'test'", "failed")
+
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
}
@@ -155,7 +157,7 @@ func (s *FirstFactorSuite) TestShouldFailIfUserProviderGetDetailsFail() {
}`)
FirstFactorPOST(nil)(s.mock.Ctx)
- assert.Equal(s.T(), "Could not obtain profile details during 1FA authentication for user 'test': failed", s.mock.Hook.LastEntry().Message)
+ AssertLogEntryMessageAndError(s.T(), s.mock.Hook.LastEntry(), "Could not obtain profile details during 1FA authentication for user 'test'", "failed")
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
}
@@ -177,8 +179,7 @@ func (s *FirstFactorSuite) TestShouldFailIfAuthenticationMarkFail() {
}`)
FirstFactorPOST(nil)(s.mock.Ctx)
- assert.Equal(s.T(), "Unable to mark 1FA authentication attempt by user 'test'", s.mock.Hook.LastEntry().Message)
- assert.EqualError(s.T(), s.mock.Hook.LastEntry().Data["error"].(error), "failed")
+ AssertLogEntryMessageAndError(s.T(), s.mock.Hook.LastEntry(), "Unable to mark 1FA authentication attempt by user 'test'", "failed")
s.mock.Assert401KO(s.T(), "Authentication failed. Check your credentials.")
}
@@ -463,3 +464,23 @@ func TestFirstFactorSuite(t *testing.T) {
suite.Run(t, new(FirstFactorSuite))
suite.Run(t, new(FirstFactorRedirectionSuite))
}
+
+func AssertLogEntryMessageAndError(t *testing.T, entry *logrus.Entry, message, err string) {
+ assert.Equal(t, message, entry.Message)
+
+ v, ok := entry.Data["error"]
+
+ if err == "" {
+ assert.False(t, ok)
+ assert.Nil(t, v)
+ } else {
+ assert.True(t, ok)
+ require.NotNil(t, v)
+
+ theErr, ok := v.(error)
+ assert.True(t, ok)
+ require.NotNil(t, theErr)
+
+ assert.EqualError(t, theErr, err)
+ }
+}