-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoupon_test.go
More file actions
65 lines (41 loc) · 1.29 KB
/
Copy pathcoupon_test.go
File metadata and controls
65 lines (41 loc) · 1.29 KB
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
// nolint:wrapcheck
package main
import (
"encoding/json"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCoupon(t *testing.T) {
engine, gormDB := prepareForTest()
coupon := Coupon{}
t.Run("add", func(t *testing.T) {
responseRecorder, err := httpTest[Coupon](engine, http.MethodPost, "/v1/coupons", nil)
require.NoError(t, err)
assert.Equal(t, http.StatusCreated, responseRecorder.Code)
err = json.Unmarshal(responseRecorder.Body.Bytes(), &coupon)
require.NoError(t, err)
assert.NotEmpty(t, coupon.Code)
assert.NotEmpty(t, coupon)
assert.Empty(t, coupon.Mac)
})
t.Run("use", func(t *testing.T) {
user, err := DBFirst[User](gormDB, "")
require.NoError(t, err)
t.Logf("user mac: %s", user.Mac)
coupon.Mac = user.Mac
code := coupon.Code
responseRecorder, err := httpTest(engine, http.MethodPut, "/v1/coupons/use", &coupon)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, responseRecorder.Code)
expire := user.Expire
user, err = DBFirst[User](gormDB, "mac = ?", user.Mac)
require.NoError(t, err)
assert.WithinDuration(t, user.Expire, expire, 366*24*time.Hour)
coupon, err := DBFirst[Coupon](gormDB, "code = ?", code)
assert.Error(t, err)
assert.Empty(t, coupon)
})
}