summaryrefslogtreecommitdiff
path: root/internal/handlers/handler_verify_test.go
diff options
context:
space:
mode:
authorJames Elliott <james-d-elliott@users.noreply.github.com>2022-07-08 12:32:43 +1000
committerGitHub <noreply@github.com>2022-07-08 12:32:43 +1000
commit24f5caed9740f71a107af0974436eb70b572512b (patch)
tree6c3cfe51b17c75c62625584546e43314f0f50e3f /internal/handlers/handler_verify_test.go
parenta6fb7cc4eed64652d67351342ed58749a4302ed9 (diff)
refactor: factorize verify handler (#3662)
This factorizes a few sections of the /api/verify handler and improves both the code flow and error output of the section of code.
Diffstat (limited to 'internal/handlers/handler_verify_test.go')
-rw-r--r--internal/handlers/handler_verify_test.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/internal/handlers/handler_verify_test.go b/internal/handlers/handler_verify_test.go
index e7f99a8e7..66eb23dda 100644
--- a/internal/handlers/handler_verify_test.go
+++ b/internal/handlers/handler_verify_test.go
@@ -1314,3 +1314,67 @@ func TestShouldNotRedirectRequestsForBypassACLWhenInactiveForTooLong(t *testing.
assert.Equal(t, fasthttp.StatusUnauthorized, mock.Ctx.Response.StatusCode())
assert.Nil(t, mock.Ctx.Response.Header.Peek("Location"))
}
+
+func TestIsSessionInactiveTooLong(t *testing.T) {
+ testCases := []struct {
+ name string
+ have *session.UserSession
+ now time.Time
+ inactivity time.Duration
+ expected bool
+ }{
+ {
+ name: "ShouldNotBeInactiveTooLong",
+ have: &session.UserSession{Username: "john", LastActivity: 1656994960},
+ now: time.Unix(1656994970, 0),
+ inactivity: time.Second * 90,
+ expected: false,
+ },
+ {
+ name: "ShouldNotBeInactiveTooLongIfAnonymous",
+ have: &session.UserSession{Username: "", LastActivity: 1656994960},
+ now: time.Unix(1656994990, 0),
+ inactivity: time.Second * 20,
+ expected: false,
+ },
+ {
+ name: "ShouldNotBeInactiveTooLongIfRemembered",
+ have: &session.UserSession{Username: "john", LastActivity: 1656994960, KeepMeLoggedIn: true},
+ now: time.Unix(1656994990, 0),
+ inactivity: time.Second * 20,
+ expected: false,
+ },
+ {
+ name: "ShouldNotBeInactiveTooLongIfDisabled",
+ have: &session.UserSession{Username: "john", LastActivity: 1656994960},
+ now: time.Unix(1656994990, 0),
+ inactivity: time.Second * 0,
+ expected: false,
+ },
+ {
+ name: "ShouldBeInactiveTooLong",
+ have: &session.UserSession{Username: "john", LastActivity: 1656994960},
+ now: time.Unix(4656994990, 0),
+ inactivity: time.Second * 1,
+ expected: true,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ ctx := mocks.NewMockAutheliaCtx(t)
+
+ defer ctx.Close()
+
+ ctx.Ctx.Configuration.Session.Inactivity = tc.inactivity
+ ctx.Ctx.Providers.SessionProvider = session.NewProvider(ctx.Ctx.Configuration.Session, nil)
+
+ ctx.Clock.Set(tc.now)
+ ctx.Ctx.Clock = &ctx.Clock
+
+ actual := isSessionInactiveTooLong(ctx.Ctx, tc.have, tc.have.Username == "")
+
+ assert.Equal(t, tc.expected, actual)
+ })
+ }
+}