-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmask_test.go
More file actions
116 lines (103 loc) · 2.65 KB
/
mask_test.go
File metadata and controls
116 lines (103 loc) · 2.65 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
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
package gin_test
import (
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
engine "github.com/darkit/gin"
"github.com/darkit/gin/pkg/mask"
)
func TestMaskMobile(t *testing.T) {
assert.Equal(t, "138****8000", mask.MaskMobile("13800138000"))
}
func TestMaskEmail(t *testing.T) {
assert.Equal(t, "t***@example.com", mask.MaskEmail("test@example.com"))
}
func TestMaskIDCard(t *testing.T) {
assert.Equal(t, "110***********1234", mask.MaskIDCard("110105194912311234"))
}
func TestMaskBankCard(t *testing.T) {
assert.Equal(t, "6222********1234", mask.MaskBankCard("6222334455661234"))
}
func TestMaskName(t *testing.T) {
assert.Equal(t, "张*", mask.MaskName("张三"))
}
func TestOKMasked_Struct(t *testing.T) {
ctx, w := newTestContext(t, http.MethodGet, "/", "")
ctx.OKMasked(testUser{Mobile: "13800138000"})
if w.Code != http.StatusOK {
t.Fatalf("ok masked status")
}
var resp engine.Response
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode ok masked: %v", err)
}
data, ok := resp.Data.(map[string]any)
if !ok {
t.Fatalf("masked data type")
}
if data["mobile"] != "138****8000" {
t.Fatalf("masked mobile")
}
}
func TestOKMasked_NestedStruct(t *testing.T) {
ctx, w := newTestContext(t, http.MethodGet, "/", "")
payload := nestedUser{
Profile: testUser{
Mobile: "13800138000",
Email: "test@example.com",
},
}
ctx.OKMasked(payload)
var resp engine.Response
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode ok masked: %v", err)
}
data, ok := resp.Data.(map[string]any)
if !ok {
t.Fatalf("masked data type")
}
profile, ok := data["profile"].(map[string]any)
if !ok {
t.Fatalf("masked profile type")
}
if profile["mobile"] != "138****8000" {
t.Fatalf("masked nested mobile")
}
if profile["email"] != "t***@example.com" {
t.Fatalf("masked nested email")
}
}
func TestOKMasked_Slice(t *testing.T) {
ctx, w := newTestContext(t, http.MethodGet, "/", "")
users := []testUser{
{Mobile: "13800138000"},
{Mobile: "13900139000"},
}
ctx.OKMasked(users)
var resp engine.Response
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode ok masked: %v", err)
}
list, ok := resp.Data.([]any)
if !ok {
t.Fatalf("masked list type")
}
if len(list) != 2 {
t.Fatalf("masked list length")
}
first, ok := list[0].(map[string]any)
if !ok {
t.Fatalf("masked list item type")
}
if first["mobile"] != "138****8000" {
t.Fatalf("masked list mobile")
}
}
type testUser struct {
Mobile string `json:"mobile" mask:"mobile"`
Email string `json:"email" mask:"email"`
}
type nestedUser struct {
Profile testUser `json:"profile"`
}