-
Notifications
You must be signed in to change notification settings - Fork 0
/
claimSession_test.go
167 lines (140 loc) · 4.69 KB
/
claimSession_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 pogifyapi
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/gin-gonic/gin"
)
func Test_server_claimSession(t *testing.T) {
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.Default()
Server(router.Group("/"))
// var key1 string
t.Run("Test /session/claim returns 400 on empty request", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/claim", strings.NewReader(""))
router.ServeHTTP(w, req)
if expect := http.StatusBadRequest; w.Code != expect {
t.Errorf("expected error with: %v but got %v instead", expect, w.Code)
}
})
t.Run("Test /session/claim returns 400 on bind error", func(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/session/claim", strings.NewReader("{\"nothing\":\"nothing\"}"))
router.ServeHTTP(w, req)
if expect := http.StatusBadRequest; w.Code != expect {
t.Errorf("expected error with: %v but got %v instead", expect, w.Code)
}
})
t.Run("Test /session/claim", func(t *testing.T) {
w1 := httptest.NewRecorder()
req1, _ := http.NewRequest("GET", "/session/issue", strings.NewReader("{\"nothing\":\"nothing\"}"))
router.ServeHTTP(w1, req1)
p := struct {
SessionID string `json:"sessionId" binding:"required"`
Issued int64 `json:"issued" binding:"required"`
Checksum string `json:"checksum" binding:"required"`
Difficulty int `json:"difficulty" binding:"required"`
}{}
if e := json.Unmarshal(w1.Body.Bytes(), &p); e != nil {
t.Error(e)
return
}
p.Issued = time.Now().Unix()
idIss := fmt.Sprintf("%v.%v", p.SessionID, strconv.FormatInt(p.Issued, 10))
cs := sha256.Sum256([]byte(idIss + os.Getenv("POW_SECRET")))
s, h := findSolution(idIss, p.Difficulty)
sol := struct {
SessionID string `json:"sessionId" binding:"required"`
Issued int64 `json:"issued" binding:"required"`
Checksum string `json:"checksum" binding:"required"`
Solution string `json:"solution" binding:"required"`
Hash string `json:"hash" binding:"required"`
}{
SessionID: p.SessionID,
Issued: p.Issued,
Checksum: hex.EncodeToString(cs[:]),
Solution: s,
Hash: h,
}
jsonSol, _ := json.Marshal(sol)
w2 := httptest.NewRecorder()
req2, _ := http.NewRequest("POST", "/session/claim", bytes.NewReader(jsonSol))
router.ServeHTTP(w2, req2)
session := struct {
ExpiresIn int `json:"expiresIn" binding:"required"`
RT string `json:"refreshToken" binding:"required"`
Session string `json:"session" binding:"required"`
Token string `json:"token" binding:"required"`
}{}
if err := json.Unmarshal(w2.Body.Bytes(), &session); err != nil {
t.Error(err)
}
})
t.Run("Test already claimed session /session/claim", func(t *testing.T) {
w1 := httptest.NewRecorder()
req1, _ := http.NewRequest("GET", "/session/issue", strings.NewReader("{\"nothing\":\"nothing\"}"))
router.ServeHTTP(w1, req1)
p := struct {
SessionID string `json:"sessionId" binding:"required"`
Issued int64 `json:"issued" binding:"required"`
Checksum string `json:"checksum" binding:"required"`
Difficulty int `json:"difficulty" binding:"required"`
}{}
if e := json.Unmarshal(w1.Body.Bytes(), &p); e != nil {
t.Error(e)
return
}
p.Issued = time.Now().Unix()
idIss := fmt.Sprintf("%v.%v", p.SessionID, strconv.FormatInt(p.Issued, 10))
cs := sha256.Sum256([]byte(idIss + os.Getenv("POW_SECRET")))
s, h := findSolution(idIss, p.Difficulty)
sol := struct {
SessionID string `json:"sessionId" binding:"required"`
Issued int64 `json:"issued" binding:"required"`
Checksum string `json:"checksum" binding:"required"`
Solution string `json:"solution" binding:"required"`
Hash string `json:"hash" binding:"required"`
}{
SessionID: p.SessionID,
Issued: p.Issued,
Checksum: hex.EncodeToString(cs[:]),
Solution: s,
Hash: h,
}
jsonSol, _ := json.Marshal(sol)
w2 := httptest.NewRecorder()
req2, _ := http.NewRequest("POST", "/session/claim", bytes.NewReader(jsonSol))
router.ServeHTTP(w2, req2)
if expect := http.StatusGone; w2.Code != expect {
t.Errorf("got unexpected response code %v, expected %v", w2.Code, expect)
}
})
}
func findSolution(nonce string, difficulty int) (s string, hash string) {
s = "0"
prefix := strings.Repeat("0", difficulty)
for {
h := sha256.Sum256([]byte(s + nonce))
hash = hex.EncodeToString(h[:])
if strings.HasPrefix(hash, prefix) {
return
}
s += "0"
}
}