Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #193 from secrethub/fix/session-refresh
Browse files Browse the repository at this point in the history
Fix session expiration check
  • Loading branch information
jpcoenen authored Jun 15, 2020
2 parents abed833 + 6541c49 commit 3410150
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/secrethub/credentials/sessions/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ type expireTime time.Time

// NeedsRefresh returns true when the session is about to expire and should be refreshed.
func (t expireTime) NeedsRefresh() bool {
return time.Time(t).After(time.Now().Add(expirationMargin))
return time.Now().After(time.Time(t).Add(-expirationMargin))
}
34 changes: 34 additions & 0 deletions pkg/secrethub/credentials/sessions/session_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sessions

import (
"testing"
"time"

"github.com/secrethub/secrethub-go/internals/assert"
)

func TestExpireTimeNeedsRefresh(t *testing.T) {
cases := map[string]struct {
in time.Time
expected bool
}{
"not expired": {
in: time.Now().Add(time.Minute),
expected: false,
},
"in margin": {
in: time.Now().Add(time.Second * 10),
expected: true,
},
"past": {
in: time.Now().Add(-time.Second * 10),
expected: true,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
assert.Equal(t, expireTime(tc.in).NeedsRefresh(), tc.expected)
})
}
}

0 comments on commit 3410150

Please sign in to comment.