-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_storage_test.go
79 lines (56 loc) · 1.7 KB
/
token_storage_test.go
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
package n26api_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/nhatthm/n26api"
"github.com/nhatthm/n26api/pkg/auth"
)
func TestInMemoryTokenStorage_GetMissingKey(t *testing.T) {
t.Parallel()
s := n26api.NewInMemoryTokenStorage()
result, err := s.Get(context.Background(), "key")
assert.Equal(t, auth.OAuthToken{}, result)
assert.NoError(t, err)
}
func TestInMemoryTokenStorage(t *testing.T) {
t.Parallel()
timestamp := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)
key := "key"
originalToken := auth.OAuthToken{
AccessToken: "access",
RefreshToken: "refresh",
ExpiresAt: timestamp,
RefreshExpiresAt: timestamp,
}
s := n26api.NewInMemoryTokenStorage()
err := s.Set(context.Background(), key, originalToken)
require.NoError(t, err)
// Retrieve the token.
token, err := s.Get(context.Background(), key)
require.NoError(t, err)
assert.Equal(t, originalToken, token)
// Modify the token.
newAccessToken := "foobar"
token.AccessToken = auth.Token(newAccessToken)
// Retrieve the token again.
token, err = s.Get(context.Background(), key)
require.NoError(t, err)
assert.NotEqual(t, newAccessToken, token.AccessToken)
assert.Equal(t, originalToken, token)
}
func TestInMemoryTokenStorage_SetError(t *testing.T) {
t.Parallel()
timestamp := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)
originalToken := auth.OAuthToken{
AccessToken: "access",
RefreshToken: "refresh",
ExpiresAt: timestamp,
RefreshExpiresAt: timestamp,
}
s := n26api.NewInMemoryTokenStorage()
err := s.Set(context.Background(), "", originalToken)
assert.Equal(t, n26api.ErrTokenKeyEmpty, err)
}