-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
364 lines (304 loc) · 11 KB
/
service_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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package main
import (
"bytes"
"context"
"encoding/json"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestGetCodeWithTimeout(t *testing.T) {
// Setup database connection for tests
var err error
db, err = connectToDB() // Ensuring db is set globally as it might be used elsewhere
if err != nil {
t.Fatalf("Unable to connect to database: %v\n", err)
}
defer db.Close()
// Generate valid UUIDs for testing
// This is based on the seed data
validBatchID := "11111111-1111-1111-1111-111111111111"
validClientID := "217be7c8-679c-4e08-bffc-db3451bdcdbf"
validCustomerID := uuid.New().String()
req := Request{
BatchID: validBatchID,
ClientID: validClientID,
CustomerID: validCustomerID,
}
t.Run("Successful Code Assignment", func(t *testing.T) {
code, err := getCode(context.Background(), req)
assert.Nil(t, err, "Expected no error")
assert.NotEmpty(t, code, "Expected code to be returned")
})
t.Run("No Batch Found", func(t *testing.T) {
req := Request{
BatchID: uuid.New().String(),
ClientID: validClientID,
CustomerID: validCustomerID,
}
code, err := getCode(context.Background(), req)
assert.Equal(t, ErrNoBatchFound, err, "Expected no batch was found error")
assert.Empty(t, code, "Expected no code to be returned")
})
t.Run("No Code Found when all are used in the batch", func(t *testing.T) {
req := Request{
BatchID: "33333333-3333-3333-3333-333333333333",
ClientID: validClientID,
CustomerID: validCustomerID,
}
code, err := getCode(context.Background(), req)
assert.Equal(t, ErrNoCodeFound, err, "Expected no code was found error")
assert.Empty(t, code, "Expected no code to be returned")
})
t.Run("Responds when the batch is expired", func(t *testing.T) {
req := Request{
BatchID: "44444444-4444-4444-4444-444444444444",
ClientID: validClientID,
CustomerID: validCustomerID,
}
code, err := getCode(context.Background(), req)
assert.Equal(t, ErrBatchExpired, err, "Expected batch expired error")
assert.Empty(t, code, "Expected no code to be returned")
})
t.Run("Invalid BatchID", func(t *testing.T) {
req := Request{
BatchID: "invalid-uuid",
ClientID: validClientID,
CustomerID: validCustomerID,
}
code, err := getCode(context.Background(), req)
assert.NotNil(t, err, "Expected error for invalid BatchID")
assert.Contains(t, err.Error(), "invalid batch_id format")
assert.Empty(t, code, "Expected no code to be returned")
})
t.Run("Invalid ClientID", func(t *testing.T) {
req := Request{
BatchID: validBatchID,
ClientID: "invalid-uuid",
CustomerID: validCustomerID,
}
code, err := getCode(context.Background(), req)
assert.NotNil(t, err, "Expected error for invalid ClientID")
assert.Contains(t, err.Error(), "invalid client_id format")
assert.Empty(t, code, "Expected no code to be returned")
})
t.Run("Invalid CustomerID", func(t *testing.T) {
req := Request{
BatchID: validBatchID,
ClientID: validClientID,
CustomerID: "invalid-uuid",
}
code, err := getCode(context.Background(), req)
assert.NotNil(t, err, "Expected error for invalid CustomerID")
assert.Contains(t, err.Error(), "invalid customer_id format")
assert.Empty(t, code, "Expected no code to be returned")
})
}
func TestGetCodeHandler_InvalidJSON(t *testing.T) {
tests := []struct {
name string
request string
errorMsg string
}{
{
name: "Invalid BatchID",
request: `{
"batchid": "invalid-batch-id",
"clientid": "217be7c8-679c-4e08-bffc-db3451bdcdbf",
"customerid": "fba9230a-a521-430e-aaf8-8aefbf588071"
}`,
errorMsg: "invalid batch_id format",
},
{
name: "Invalid ClientID",
request: `{
"batchid": "c6ffca2e-603b-4b14-a39d-9e37b6f1d63b",
"clientid": "invalid-client-id",
"customerid": "fba9230a-a521-430e-aaf8-8aefbf588071"
}`,
errorMsg: "invalid client_id format",
},
{
name: "Invalid CustomerID",
request: `{
"batchid": "c6ffca2e-603b-4b14-a39d-9e37b6f1d63b",
"clientid": "217be7c8-679c-4e08-bffc-db3451bdcdbf",
"customerid": "invalid-customer-id"
}`,
errorMsg: "invalid customer_id format",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("POST", "/api/v1/code/redeem", bytes.NewBuffer([]byte(tt.request)))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router := gin.Default()
router.POST("/api/v1/code/redeem", getCodeHandler)
router.ServeHTTP(w, req)
assert.Equal(t, 400, w.Code)
assert.Contains(t, w.Body.String(), tt.errorMsg)
})
}
}
func TestGetBatchesHandler(t *testing.T) {
// Setup database connection for tests
var err error
db, err = connectToDB() // Ensuring db is set globally as it might be used elsewhere
if err != nil {
t.Fatalf("Unable to connect to database: %v\n", err)
}
defer db.Close()
// Create a new gin router
router := gin.Default()
router.GET("/api/v1/batches", getBatchesHandler)
t.Run("Fetch batches successfully", func(t *testing.T) {
// Create a new request
req, _ := http.NewRequest("GET", "/api/v1/batches", nil)
w := httptest.NewRecorder()
// Serve the request
router.ServeHTTP(w, req)
// Assert the response code
assert.Equal(t, 200, w.Code)
// Parse the response body
var response []Batch
err = json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
// Assert that we got some batches
assert.NotEmpty(t, response)
// Check the structure of the first batch
firstBatch := response[0]
assert.NotEmpty(t, firstBatch.ID)
assert.NotEmpty(t, firstBatch.Name)
assert.NotNil(t, firstBatch.Rules)
assert.NotNil(t, firstBatch.Expired)
})
}
func TestUploadCodesHandler(t *testing.T) {
// Setup database connection for tests
var err error
db, err = connectToDB() // Ensuring db is set globally as it might be used elsewhere
if err != nil {
t.Fatalf("Unable to connect to database: %v\n", err)
}
defer db.Close()
// Create a new gin router
router := gin.Default()
router.POST("/api/v1/codes/upload", uploadCodesHandler)
t.Run("Successful upload", func(t *testing.T) {
// Create a new multipart writer
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
// Add the batch name and rules
_ = writer.WriteField("batch_name", "Test Batch")
_ = writer.WriteField("rules", `{"maxpercustomer": 2, "timelimit": 7}`)
// Create the file part
part, _ := writer.CreateFormFile("file", "test.csv")
_, _ = part.Write([]byte("client_id,batch_id,code\n217be7c8-679c-4e08-bffc-db3451bdcdbf,11111111-1111-1111-1111-111111111111,TESTCODE123"))
writer.Close()
// Create a new request
req, _ := http.NewRequest("POST", "/api/v1/codes/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
// Serve the request
router.ServeHTTP(w, req)
// Assert the response code
assert.Equal(t, 200, w.Code)
// Parse the response body
var response map[string]string
err = json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
// Check the response message
assert.Equal(t, "Codes uploaded successfully", response["message"])
// Check if the record is in the database
var count int
err = db.QueryRow(context.Background(), "SELECT COUNT(*) FROM codes WHERE code = $1", "TESTCODE123").Scan(&count)
assert.NoError(t, err)
assert.Equal(t, 1, count)
})
t.Run("Incorrectly formatted CSV", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
_ = writer.WriteField("batch_name", "Test Batch")
part, _ := writer.CreateFormFile("file", "test.csv")
_, _ = part.Write([]byte("invalid,csv,format"))
writer.Close()
req, _ := http.NewRequest("POST", "/api/v1/codes/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, 500, w.Code)
assert.Contains(t, w.Body.String(), "Failed to upload codes")
})
t.Run("CSV missing required columns", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
_ = writer.WriteField("batch_name", "Test Batch")
part, _ := writer.CreateFormFile("file", "test.csv")
_, _ = part.Write([]byte("client_id,code\n217be7c8-679c-4e08-bffc-db3451bdcdbf,TESTCODE123"))
writer.Close()
req, _ := http.NewRequest("POST", "/api/v1/codes/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, 500, w.Code)
assert.Contains(t, w.Body.String(), "Failed to upload codes")
})
t.Run("No batch name provided", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", "test.csv")
_, _ = part.Write([]byte("client_id,batch_id,code\n217be7c8-679c-4e08-bffc-db3451bdcdbf,11111111-1111-1111-1111-111111111111,TESTCODE123"))
writer.Close()
req, _ := http.NewRequest("POST", "/api/v1/codes/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, 400, w.Code)
assert.Contains(t, w.Body.String(), "Batch name is required")
})
t.Run("No rules provided", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
_ = writer.WriteField("batch_name", "Test Batch")
part, _ := writer.CreateFormFile("file", "test.csv")
_, _ = part.Write([]byte("client_id,batch_id,code\n217be7c8-679c-4e08-bffc-db3451bdcdbf,11111111-1111-1111-1111-111111111111,TESTCODE123"))
writer.Close()
req, _ := http.NewRequest("POST", "/api/v1/codes/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
assert.Contains(t, w.Body.String(), "Codes uploaded successfully")
})
t.Run("No CSV provided", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
_ = writer.WriteField("batch_name", "Test Batch")
writer.Close()
req, _ := http.NewRequest("POST", "/api/v1/codes/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, 400, w.Code)
assert.Contains(t, w.Body.String(), "No CSV file provided")
})
t.Run("File is not a CSV", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
_ = writer.WriteField("batch_name", "Test Batch")
part, _ := writer.CreateFormFile("file", "test.txt")
_, _ = part.Write([]byte("This is not a CSV file"))
writer.Close()
req, _ := http.NewRequest("POST", "/api/v1/codes/upload", body)
req.Header.Set("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, 400, w.Code)
assert.Contains(t, w.Body.String(), "File must be a CSV")
})
}