-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_test.go
More file actions
201 lines (169 loc) · 6.96 KB
/
session_test.go
File metadata and controls
201 lines (169 loc) · 6.96 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package apisession
import (
"context"
"fmt"
"testing"
"time"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
// go test -timeout 30s -run ^TestValidateSession_ValidCall_NoError$ github.com/zeroboo/go-api-session -v
func TestValidateSession_ValidCall_NoError(t *testing.T) {
owner := "user_" + t.Name()
manager := NewRedisSessionManager(redisClient, sessionPrefix, 1000, 10000, 10, 5, false)
sessionId, errNewSession := manager.StartSession(context.TODO(), owner)
assert.Nil(t, errNewSession, "Create new session, no error")
sessionOwners = append(sessionOwners, owner)
fmt.Printf("SessionId: %v\n", sessionId)
session, errGetSession := manager.GetSession(context.TODO(), owner)
assert.Nil(t, errGetSession, "Get session, no error")
now := time.Now()
errValidate := manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now)
assert.Nil(t, errValidate, "Valid owner, no error")
}
// go test -timeout 30s -run ^TestValidateSession_InvalidSessionValue_Error$ github.com/zeroboo/go-api-session
func TestValidateSession_InvalidSessionValue_Error(t *testing.T) {
owner := "user_" + t.Name()
manager := NewRedisSessionManager(redisClient, sessionPrefix, 1000, 10000, 10, 5, false)
sessionId, _ := manager.StartSession(context.TODO(), owner)
sessionOwners = append(sessionOwners, owner)
session, _ := manager.GetSession(context.TODO(), owner)
now := time.Now()
errValidate := manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: "invalid_session_id",
URL: "url1",
}, session, now)
assert.Equal(t, ErrInvalidSession, errValidate, "Invalid owner, error")
log.Infof("Session: %v", sessionId)
}
// go test -timeout 30s -run ^TestValidateSession_TooFast_Error$ github.com/zeroboo/go-api-session
func TestValidateSession_TooFast_Error(t *testing.T) {
owner := "user_" + t.Name()
manager := NewRedisSessionManager(redisClient, sessionPrefix, 1000, 10000, 3, 10, false)
sessionId, _ := manager.StartSession(context.TODO(), owner)
session, _ := manager.GetSession(context.TODO(), owner)
sessionOwners = append(sessionOwners, owner)
now := time.Now()
var errValidate error
errValidate = manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now)
assert.Nil(t, errValidate, "Valid call, no error")
errValidate = manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now)
assert.Equal(t, ErrTooFast, errValidate, "Too fast call, has error")
}
// go test -timeout 30s -run ^TestValidateSession_TooFrequently_Error$ github.com/zeroboo/go-api-session
func TestValidateSession_TooFrequently_Error(t *testing.T) {
owner := "user_" + t.Name()
manager := NewRedisSessionManager(redisClient, sessionPrefix, 1000, 10000, 2, 0, false)
sessionId, _ := manager.StartSession(context.TODO(), owner)
session, _ := manager.GetSession(context.TODO(), owner)
sessionOwners = append(sessionOwners, owner)
now := time.Now()
var errValidate error
errValidate = manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now)
assert.Nil(t, errValidate, "Valid call, no error")
errValidate = manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now)
assert.Equal(t, nil, errValidate, "second call, no error")
errValidate = manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now)
assert.Equal(t, ErrTooMany, errValidate, "third call is too frequently, has error")
}
// go test -timeout 30s -run ^TestValidateSession_NewWindow_Correct$ github.com/zeroboo/go-api-session
func TestValidateSession_NewWindow_Correct(t *testing.T) {
owner := "user_" + t.Name()
interval := int64(10)
manager := NewRedisSessionManager(redisClient, sessionPrefix, 1000, 10000, 2, interval, false)
sessionId, _ := manager.StartSession(context.TODO(), owner)
sessionOwners = append(sessionOwners, owner)
session, _ := manager.GetSession(context.TODO(), owner)
now := time.Now()
var errValidate error
errValidate = manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now)
assert.Nil(t, errValidate, "Valid call, no error")
errValidate = manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now.Add(time.Duration(interval+1)*time.Millisecond))
assert.Equal(t, nil, errValidate, "second call, no error")
errValidate = manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now.Add(time.Duration(2*interval+1)*time.Millisecond))
assert.Equal(t, ErrTooMany, errValidate, "third call is too frequently, has error")
session.SetWindow(session.Window + 1)
errValidate = manager.ValidateAPICall(&APIRequest{
Owner: owner,
SessionId: sessionId,
URL: "url1",
}, session, now.Add(time.Duration(2*interval+1)*time.Millisecond))
assert.Equal(t, nil, errValidate, "new windows, request valid")
}
// go test -timeout 30s -run ^TestGetMapFromSession_Correct$ github.com/zeroboo/go-api-session -v
func TestGetMapFromSession_Correct(t *testing.T) {
session := NewAPISessionWithPayload("user1", map[string]any{})
mapValue := GetPayloadMap[string, int64](session, "key")
assert.Nil(t, mapValue, "Value is nil")
t.Logf("Empty map value: %v", mapValue)
session.SetPayload("key", map[string]int64{"key1": 1, "key2": 2})
mapValue = GetPayloadMap[string, int64](session, "key")
assert.NotNil(t, mapValue, "Value is not nil")
t.Logf("Valid map value: %v", mapValue)
mapValue["key3"] = 3
updatedMapValue := GetPayloadMap[string, int64](session, "key")
assert.NotNil(t, mapValue, "Value is not nil")
t.Logf("Updated map value: %v", updatedMapValue)
//Test getorcreate
map2, create := GetOrCreatePayloadMap[string, int64](session, "key3")
assert.True(t, create, "New value is created")
assert.NotNil(t, map2, "Value is not nil")
t.Logf("GetOrCreatePayloadMap map value: %v", map2)
map2["key4"] = 4
t.Logf("GetOrCreatePayloadMap map value after add: %v", map2)
map2After, created := GetOrCreatePayloadMap[string, int64](session, "key3")
assert.False(t, created, "Key not created")
t.Logf("GetOrCreatePayloadMap: map get after update: %v", map2After)
assert.Equal(t, map2, map2After, "Correct map")
}
// go test -timeout 30s -run ^TestGetSliceFromSession_Correct$ github.com/zeroboo/go-api-session -v
func TestGetSliceFromSession_Correct(t *testing.T) {
session := NewAPISessionWithPayload("user1", map[string]any{
"key": []string{"1"},
})
slice, ok := GetOrCreatePayloadSlice[string](session, "key")
assert.True(t, ok, "Key found")
assert.NotNil(t, slice, "Value is nil")
t.Logf("Empty slice value: %v", slice)
slice, ok = GetPayloadSlice[string](session, "invalidkey")
assert.False(t, ok, "Key found")
assert.Nil(t, slice, "Value is nil")
t.Logf("Empty slice value: %v", slice)
}