-
Notifications
You must be signed in to change notification settings - Fork 0
/
postUpdate_test.go
88 lines (73 loc) · 2.11 KB
/
postUpdate_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
package pogifyapi
import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
gonanoid "github.com/matoous/go-nanoid"
)
func Test_server_postUpdate(t *testing.T) {
sessionCode, _ := gonanoid.ID(10)
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)
return
}
os.Setenv("REDIS_URI", "redis://"+mr.Addr())
os.Setenv("PUBSUB_SECRET", _pubsubsecret)
router := gin.Default()
Server(router.Group("/"))
t.Run("empty call", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/update", nil)
router.ServeHTTP(w, req)
if w.Code != 400 {
t.Errorf("returned code %v, expected %v", w.Code, 400)
}
})
t.Run("invalid token", func(t *testing.T) {
invalidToken, _ := token.SignedString([]byte("aaaa"))
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/update", nil)
req.Header.Add("X-Session-Token", invalidToken)
router.ServeHTTP(w, req)
if w.Code != 401 {
t.Errorf("returned code %v, expected %v", w.Code, 401)
}
})
t.Run("proper token, empty body", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/update", nil)
req.Header.Add("x-session-token", mockToken)
router.ServeHTTP(w, req)
if w.Code != 500 {
t.Errorf("returned code %v, expected %v", w.Code, 500)
}
})
t.Run("proper token, body", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/update", strings.NewReader("{\"nothing\":\"nothing\"}"))
req.Header.Add("x-session-token", mockToken)
router.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("returned code %v, expected %v", w.Code, 200)
}
})
}