-
Notifications
You must be signed in to change notification settings - Fork 0
/
setConfig_test.go
99 lines (75 loc) · 2.25 KB
/
setConfig_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
package pogifyapi
import (
"bytes"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
func Test_server_setConfig(t *testing.T) {
sessionCode := "test"
claims := sessionJwtClaims{
sessionCode,
jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour).Unix(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
mockToken, err := token.SignedString([]byte(os.Getenv("JWT_SECRET")))
if err != nil {
t.Fatalf("Error generating token: %v", err)
}
mr, err := miniredis.Run()
defer mr.Close()
if err != nil {
t.Fatalf("MiniRedis error: %s", err)
}
os.Setenv("REDIS_URI", "redis://"+mr.Addr())
router := gin.New()
Server(router.Group("/"))
t.Run("test without token", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/config", nil)
router.ServeHTTP(w, req)
if w.Code != 400 {
t.Errorf("setConfig didn't return 400 without access token, instead: %v", w.Code)
}
})
t.Run("test with token, no body", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/config", nil)
req.Header.Add("X-Session-Token", mockToken)
router.ServeHTTP(w, req)
if w.Code != 400 {
t.Errorf("setConfig didn't return 400, instead: %v", w.Code)
}
})
t.Run("test with invalid token", func(t *testing.T) {
invalidToken, _ := token.SignedString([]byte("aaaa"))
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/config", nil)
req.Header.Add("X-Session-Token", invalidToken)
router.ServeHTTP(w, req)
if w.Code != 401 {
t.Errorf("setConfig didn't return 401 on invalid token, instead: %v", w.Code)
}
})
t.Run("test with body", func(t *testing.T) {
conf := []byte("{\"requestInterval\":100}")
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/config", bytes.NewReader(conf))
req.Header.Add("X-Session-Token", mockToken)
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("setConfig didn't return 401 on invalid token, instead: %v", w.Code)
}
ri := mr.HGet("session:test:config", "RequestInterval")
if ri != "100" {
t.Error("setConfig didn't set config in redis")
}
})
}