-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth_manager_test.go
167 lines (137 loc) · 4.1 KB
/
auth_manager_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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package auth_manager_test
import (
"context"
"fmt"
"log"
"os"
"testing"
"time"
auth_manager "github.com/tahadostifam/go-auth-manager"
"github.com/go-redis/redis/v8"
"github.com/google/uuid"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
func getRedisTestInstance(callback func(_redisClient *redis.Client)) {
dockerContainerEnvVariables := []string{}
err := os.Setenv("ENV", "test")
if err != nil {
log.Fatalf("Could not set the environment variable to test: %s", err)
}
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not construct pool: %s", err)
}
var client *redis.Client
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "redis",
Tag: "latest",
Env: dockerContainerEnvVariables,
})
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
// Kill the container
// defer func() {
// if err = pool.Purge(resource); err != nil {
// log.Fatalf("Could not purge resource: %s", err)
// }
// }()
err = pool.Retry(func() error {
ipAddr := resource.Container.NetworkSettings.IPAddress + ":6379"
fmt.Printf("Docker redis container network ip address: %s\n", ipAddr)
client = redis.NewClient(&redis.Options{
Addr: ipAddr,
DB: 0,
})
if err != nil {
return err
}
return nil
})
if err != nil {
log.Fatalf("Could not connect to Redis: %s", err)
}
callback(client)
}
var redisClient *redis.Client
type AuthManagerTestSuite struct {
suite.Suite
authManager auth_manager.AuthManager
}
func TestMain(m *testing.M) {
getRedisTestInstance(func(_redisClient *redis.Client) {
redisClient = _redisClient
m.Run()
})
}
func (s *AuthManagerTestSuite) SetupSuite() {
s.authManager = auth_manager.NewAuthManager(redisClient, auth_manager.AuthManagerOpts{
PrivateKey: "private-key",
})
}
func (s *AuthManagerTestSuite) Test_GenerateAndDecodeToken() {
// Generate
ctx := context.TODO()
tokenType := auth_manager.VerifyEmail
expiration := time.Minute * 2
payload := &auth_manager.TokenPayload{
UUID: uuid.NewString(),
TokenType: tokenType,
CreatedAt: time.Now(),
}
token, err := s.authManager.GeneratePlainToken(ctx, tokenType, payload, expiration)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), token)
// Decode
decoded, err := s.authManager.DecodePlainToken(ctx, token, tokenType)
require.NoError(s.T(), err)
require.Equal(s.T(), decoded.UUID, payload.UUID)
require.Equal(s.T(), decoded.TokenType, payload.TokenType)
require.NotEmpty(s.T(), decoded.CreatedAt)
}
func (s *AuthManagerTestSuite) Test_GenerateAndDecodeAccessToken() {
// Generate
ctx := context.TODO()
uuid := uuid.NewString()
expiration := time.Minute * 10
token, err := s.authManager.GenerateAccessToken(ctx, uuid, expiration)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), token)
// Decode
decoded, err := s.authManager.DecodeAccessToken(ctx, token)
require.NoError(s.T(), err)
require.Equal(s.T(), decoded.Payload.UUID, uuid)
require.Equal(s.T(), decoded.Payload.TokenType, auth_manager.AccessToken)
require.NotEmpty(s.T(), decoded.Payload.CreatedAt)
}
func (s *AuthManagerTestSuite) Test_RefreshToken() {
// Generate
ctx := context.TODO()
uuid := uuid.NewString()
expiration := time.Minute * 2
payload := &auth_manager.RefreshTokenPayload{
IPAddress: "ip-address",
UserAgent: "user-agent",
LoggedInAt: time.Duration(time.Now().UnixMilli()),
}
token, err := s.authManager.GenerateRefreshToken(ctx, uuid, payload, expiration)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), token)
// Decode
decoded, err := s.authManager.DecodeRefreshToken(ctx, uuid, token)
require.NoError(s.T(), err)
require.Equal(s.T(), decoded.IPAddress, payload.IPAddress)
require.Equal(s.T(), decoded.UserAgent, payload.UserAgent)
require.NotEmpty(s.T(), decoded.LoggedInAt)
// Remove
err = s.authManager.RemoveRefreshToken(ctx, uuid, token)
require.NoError(s.T(), err)
// Terminates
err = s.authManager.TerminateRefreshTokens(ctx, uuid)
require.NoError(s.T(), err)
}
func TestAuthManagerTestSuite(t *testing.T) {
suite.Run(t, new(AuthManagerTestSuite))
}