Skip to content

Commit 504e8cc

Browse files
committed
add test on claims
1 parent 01b9d54 commit 504e8cc

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

claims_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package admin_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"fmt"
8+
9+
admin "github.com/acoshift/go-firebase-admin"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestValidClaims(t *testing.T) {
14+
15+
t.Run("Valid", func(t *testing.T) {
16+
now := time.Now()
17+
claim := &admin.Claims{
18+
IssuedAt: now.Unix(),
19+
ExpiresAt: now.Unix(),
20+
}
21+
22+
err := claim.Valid()
23+
assert.Nil(t, err)
24+
})
25+
26+
t.Run("usedbefore", func(t *testing.T) {
27+
now := time.Now().AddDate(1, 0, 0)
28+
now2 := time.Now()
29+
claim := &admin.Claims{
30+
IssuedAt: now.Unix(),
31+
ExpiresAt: now2.Unix(),
32+
}
33+
34+
err := claim.Valid()
35+
assert.NotNil(t, err)
36+
assert.Equal(t, fmt.Errorf("token used before issued"), err)
37+
})
38+
39+
t.Run("expired", func(t *testing.T) {
40+
now := time.Now()
41+
claim := &admin.Claims{
42+
IssuedAt: now.Unix(),
43+
ExpiresAt: 1500651130,
44+
}
45+
46+
err := claim.Valid()
47+
assert.NotNil(t, err)
48+
assert.Equal(t, fmt.Errorf("token is expired by %v", time.Unix(now.Unix(), 0).Sub(time.Unix(claim.ExpiresAt, 0))), err)
49+
})
50+
}

0 commit comments

Comments
 (0)