Skip to content

Commit

Permalink
fix: handle the zero cases for oidc.Time
Browse files Browse the repository at this point in the history
  • Loading branch information
muhlemmer committed Mar 20, 2023
1 parent 890a7f3 commit 115813e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/oidc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,16 @@ func NewEncoder() *schema.Encoder {
type Time int64

func (ts Time) AsTime() time.Time {
if ts == 0 {
return time.Time{}
}
return time.Unix(int64(ts), 0)
}

func FromTime(tt time.Time) Time {
if tt.IsZero() {
return 0
}
return Time(tt.Unix())
}

Expand Down
51 changes: 51 additions & 0 deletions pkg/oidc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/gorilla/schema"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -467,6 +468,56 @@ func TestNewEncoder(t *testing.T) {
assert.Equal(t, a, b)
}

func TestTime_AsTime(t *testing.T) {
tests := []struct {
name string
ts Time
want time.Time
}{
{
name: "unset",
ts: 0,
want: time.Time{},
},
{
name: "set",
ts: 1,
want: time.Unix(1, 0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.ts.AsTime()
assert.Equal(t, tt.want, got)
})
}
}

func TestTime_FromTime(t *testing.T) {
tests := []struct {
name string
tt time.Time
want Time
}{
{
name: "zero",
tt: time.Time{},
want: 0,
},
{
name: "set",
tt: time.Unix(1, 0),
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := FromTime(tt.tt)
assert.Equal(t, tt.want, got)
})
}
}

func TestTime_UnmarshalJSON(t *testing.T) {
type dst struct {
UpdatedAt Time `json:"updated_at"`
Expand Down

0 comments on commit 115813e

Please sign in to comment.