Repository navigation
Expand file tree
/
Copy pathtoken_test.go
More file actions
111 lines (88 loc) · 2.78 KB
/
Copy pathtoken_test.go
File metadata and controls
111 lines (88 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: Apache-2.0
package gouncer_test
import (
"bytes"
"errors"
"testing"
"time"
"github.com/google/uuid"
"github.com/gopherium/gouncer"
)
func TestNewTokenIssuesAHashedSecret(t *testing.T) {
t.Parallel()
userID := uuid.Must(uuid.NewV7())
tok, err := gouncer.NewToken(userID, gouncer.PurposeInvite, 7*24*time.Hour)
if err != nil {
t.Fatalf("NewToken() error = %v, want nil", err)
}
if tok.Token == "" {
t.Error("token = empty, want a fresh secret")
}
if !bytes.Equal(tok.TokenHash, gouncer.HashToken(tok.Token)) {
t.Error("token hash does not match the hashed secret")
}
if tok.UserID != userID {
t.Errorf("user id = %v, want %v", tok.UserID, userID)
}
if tok.Purpose != gouncer.PurposeInvite {
t.Errorf("purpose = %q, want %q", tok.Purpose, gouncer.PurposeInvite)
}
if tok.CreatedAt.Location() != time.UTC {
t.Errorf("created_at location = %v, want UTC", tok.CreatedAt.Location())
}
if want := tok.CreatedAt.Add(7 * 24 * time.Hour); !tok.ExpiresAt.Equal(want) {
t.Errorf("expires_at = %v, want created_at plus the lifetime %v", tok.ExpiresAt, want)
}
}
func TestNewTokenSecretsAreUnique(t *testing.T) {
t.Parallel()
userID := uuid.Must(uuid.NewV7())
first, err := gouncer.NewToken(userID, gouncer.PurposeReset, time.Hour)
if err != nil {
t.Fatalf("NewToken() error = %v, want nil", err)
}
second, err := gouncer.NewToken(userID, gouncer.PurposeReset, time.Hour)
if err != nil {
t.Fatalf("NewToken() error = %v, want nil", err)
}
if first.Token == second.Token {
t.Error("two tokens share a secret, want unique secrets")
}
}
func TestNewTokenRejectsAnEmptyPurpose(t *testing.T) {
t.Parallel()
_, err := gouncer.NewToken(uuid.Must(uuid.NewV7()), "", time.Hour)
if !errors.Is(err, gouncer.ErrEmptyPurpose) {
t.Errorf("NewToken() error = %v, want ErrEmptyPurpose", err)
}
}
func TestNewTokenRejectsANilUserID(t *testing.T) {
t.Parallel()
_, err := gouncer.NewToken(uuid.Nil, gouncer.PurposeInvite, time.Hour)
if !errors.Is(err, gouncer.ErrNilUserID) {
t.Errorf("NewToken() error = %v, want ErrNilUserID", err)
}
}
func TestNewTokenRejectsANonPositiveLifetime(t *testing.T) {
t.Parallel()
for name, ttl := range map[string]time.Duration{"zero": 0, "negative": -time.Hour} {
t.Run(name, func(t *testing.T) {
t.Parallel()
_, err := gouncer.NewToken(uuid.Must(uuid.NewV7()), gouncer.PurposeInvite, ttl)
if !errors.Is(err, gouncer.ErrTokenLifetime) {
t.Errorf("NewToken() error = %v, want ErrTokenLifetime", err)
}
})
}
}
func TestTokenPurposesAreDistinct(t *testing.T) {
t.Parallel()
purposes := map[gouncer.TokenPurpose]bool{
gouncer.PurposeInvite: true,
gouncer.PurposeReset: true,
gouncer.PurposeConfirm: true,
}
if len(purposes) != 3 {
t.Errorf("purposes = %d distinct values, want 3", len(purposes))
}
}