-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathgh_test.go
173 lines (137 loc) · 5.3 KB
/
gh_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
168
169
170
171
172
173
package main
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/google/go-github/v64/github"
"github.com/redis/go-redis/v9"
)
// Mock function for exchangeCodeForToken
func mockExchangeCodeForToken(ctx context.Context, secret, clientID, code string) (*github.User, error) {
login := "mock_login"
if code == "valid" {
return &github.User{Login: &login}, nil
}
return nil, errors.New("invalid code")
}
func TestGitHubMiddleware(t *testing.T) {
cooldown := 2 * time.Minute
exchangeCodeForUser = mockExchangeCodeForToken
var tenGnots int64 = 10000000
claimBody := fmt.Sprintf(`{"amount": "%dugnot"}`, tenGnots)
t.Run("request without code", func(t *testing.T) {
middleware := getGithubMiddleware("mockClientID", "mockSecret", getCooldownLimiter(t, cooldown, 0))
req := httptest.NewRequest("GET", "http://localhost?code=", bytes.NewBufferString(claimBody))
rec := httptest.NewRecorder()
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("Expected status BadRequest, got %d", rec.Code)
}
})
t.Run("request invalid code", func(t *testing.T) {
middleware := getGithubMiddleware("mockClientID", "mockSecret", getCooldownLimiter(t, cooldown, 0))
req := httptest.NewRequest("GET", "http://localhost?code=invalid", bytes.NewBufferString(claimBody))
rec := httptest.NewRecorder()
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("Expected status BadRequest, got %d", rec.Code)
}
})
t.Run("Invalid amount", func(t *testing.T) {
claimBody := `{"amount": 100000}`
middleware := getGithubMiddleware("mockClientID", "mockSecret", getCooldownLimiter(t, cooldown, 0))
req := httptest.NewRequest("GET", "http://localhost?code=valid", bytes.NewBufferString(claimBody))
rec := httptest.NewRecorder()
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("Expected status OK, got %d", rec.Code)
}
})
t.Run("OK", func(t *testing.T) {
middleware := getGithubMiddleware("mockClientID", "mockSecret", getCooldownLimiter(t, cooldown, 0))
req := httptest.NewRequest("GET", "http://localhost?code=valid", bytes.NewBufferString(claimBody))
rec := httptest.NewRecorder()
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("Expected status OK, got %d", rec.Code)
}
})
t.Run("Cooldown active", func(t *testing.T) {
middleware := getGithubMiddleware("mockClientID", "mockSecret", getCooldownLimiter(t, cooldown, 0))
req := httptest.NewRequest("GET", "http://localhost?code=valid", bytes.NewBufferString(claimBody))
rec := httptest.NewRecorder()
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("Expected status OK, got %d", rec.Code)
}
req = httptest.NewRequest("GET", "http://localhost?code=valid", bytes.NewBufferString(claimBody))
rec = httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusTooManyRequests {
t.Errorf("Expected status TooManyRequest, got %d", rec.Code)
}
})
t.Run("User exceeded lifetime limit", func(t *testing.T) {
cooldown = time.Millisecond
// Max lifetime amount is 20 Gnots so we should be able to make 2 claims
middleware := getGithubMiddleware("mockClientID", "mockSecret", getCooldownLimiter(t, cooldown, tenGnots*2))
req := httptest.NewRequest("GET", "http://localhost?code=valid", bytes.NewBufferString(claimBody))
rec := httptest.NewRecorder()
handler := middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
handler.ServeHTTP(rec, req)
// First claim ok
if rec.Code != http.StatusOK {
t.Errorf("Expected status OK, got %d", rec.Code)
}
// Wait 2 times the cooldown
time.Sleep(2 * cooldown)
req = httptest.NewRequest("GET", "http://localhost?code=valid", bytes.NewBufferString(claimBody))
rec = httptest.NewRecorder()
handler.ServeHTTP(rec, req)
// Second claim should also be ok
if rec.Code != http.StatusOK {
t.Errorf("Expected status OK, got %d", rec.Code)
}
// Third one should fail
time.Sleep(2 * cooldown)
req = httptest.NewRequest("GET", "http://localhost?code=valid", bytes.NewBufferString(claimBody))
rec = httptest.NewRecorder()
handler.ServeHTTP(rec, req)
// third claim should fail
if rec.Code != http.StatusTooManyRequests {
t.Errorf("Expected status OK, got %d", rec.Code)
}
})
}
func getCooldownLimiter(t *testing.T, duration time.Duration, maxlifeTimeAmount int64) *CooldownLimiter {
t.Helper()
redisServer := miniredis.RunT(t)
rdb := redis.NewClient(&redis.Options{
Addr: redisServer.Addr(),
})
limiter := NewCooldownLimiter(duration, rdb, maxlifeTimeAmount)
return limiter
}