4 "github.com/stretchr/testify/assert"
10 func TestHandleSetChatSubject(t *testing.T) {
22 name: "sends chat subject to private chat members",
25 UserName: &[]byte{0x00, 0x01},
27 PrivateChats: map[uint32]*PrivateChat{
30 ClientConn: map[uint16]*ClientConn{
33 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
39 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
46 Clients: map[uint16]*ClientConn{
49 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
55 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
65 Type: []byte{0, 0x6a},
66 ID: []byte{0, 0, 0, 1},
67 ErrorCode: []byte{0, 0, 0, 0},
69 NewField(fieldChatID, []byte{0, 0, 0, 1}),
70 NewField(fieldChatSubject, []byte("Test Subject")),
76 clientID: &[]byte{0, 1},
79 Type: []byte{0, 0x77},
80 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
81 ErrorCode: []byte{0, 0, 0, 0},
83 NewField(fieldChatID, []byte{0, 0, 0, 1}),
84 NewField(fieldChatSubject, []byte("Test Subject")),
88 clientID: &[]byte{0, 2},
91 Type: []byte{0, 0x77},
92 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
93 ErrorCode: []byte{0, 0, 0, 0},
95 NewField(fieldChatID, []byte{0, 0, 0, 1}),
96 NewField(fieldChatSubject, []byte("Test Subject")),
103 for _, tt := range tests {
104 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
106 t.Run(tt.name, func(t *testing.T) {
107 got, err := HandleSetChatSubject(tt.args.cc, tt.args.t)
108 if (err != nil) != tt.wantErr {
109 t.Errorf("HandleSetChatSubject() error = %v, wantErr %v", err, tt.wantErr)
112 if !assert.Equal(t, tt.want, got) {
113 t.Errorf("HandleSetChatSubject() got = %v, want %v", got, tt.want)
119 func TestHandleLeaveChat(t *testing.T) {
131 name: "returns expected transactions",
136 PrivateChats: map[uint32]*PrivateChat{
138 ClientConn: map[uint16]*ClientConn{
141 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
147 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
154 Clients: map[uint16]*ClientConn{
157 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
163 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
170 t: NewTransaction(tranDeleteUser,nil, NewField(fieldChatID, []byte{0, 0, 0, 1})),
174 clientID: &[]byte{0, 1},
177 Type: []byte{0, 0x76},
178 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
179 ErrorCode: []byte{0, 0, 0, 0},
181 NewField(fieldChatID, []byte{0, 0, 0, 1}),
182 NewField(fieldUserID, []byte{0, 2}),
189 for _, tt := range tests {
191 t.Run(tt.name, func(t *testing.T) {
192 got, err := HandleLeaveChat(tt.args.cc, tt.args.t)
193 if (err != nil) != tt.wantErr {
194 t.Errorf("HandleLeaveChat() error = %v, wantErr %v", err, tt.wantErr)
197 if !assert.Equal(t, tt.want, got) {
198 t.Errorf("HandleLeaveChat() got = %v, want %v", got, tt.want)
205 func TestHandleGetUserNameList(t *testing.T) {
217 name: "replies with userlist transaction",
223 Clients: map[uint16]*ClientConn{
227 Flags: &[]byte{0, 3},
228 UserName: &[]byte{0, 4},
234 ID: []byte{0, 0, 0, 1},
240 clientID: &[]byte{1, 1},
244 ID: []byte{0, 0, 0, 1},
245 ErrorCode: []byte{0, 0, 0, 0},
248 fieldUsernameWithInfo,
249 []byte{00, 01, 00, 02, 00, 03, 00, 02, 00, 04},
257 for _, tt := range tests {
258 t.Run(tt.name, func(t *testing.T) {
259 got, err := HandleGetUserNameList(tt.args.cc, tt.args.t)
260 if (err != nil) != tt.wantErr {
261 t.Errorf("HandleGetUserNameList() error = %v, wantErr %v", err, tt.wantErr)
264 if !reflect.DeepEqual(got, tt.want) {
265 t.Errorf("HandleGetUserNameList() got = %v, want %v", got, tt.want)
271 func TestHandleChatSend(t *testing.T) {
283 name: "sends chat msg transaction to all clients",
286 UserName: &[]byte{0x00, 0x01},
288 Clients: map[uint16]*ClientConn{
291 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
297 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
306 NewField(fieldData, []byte("hai")),
312 clientID: &[]byte{0, 1},
315 Type: []byte{0, 0x6a},
316 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
317 ErrorCode: []byte{0, 0, 0, 0},
319 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
323 clientID: &[]byte{0, 2},
326 Type: []byte{0, 0x6a},
327 ID: []byte{0xf0, 0xc5, 0x34, 0x1e}, // Random ID from rand.Seed(1)
328 ErrorCode: []byte{0, 0, 0, 0},
330 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
337 name: "only sends chat msg to clients with accessReadChat permission",
340 UserName: &[]byte{0x00, 0x01},
342 Clients: map[uint16]*ClientConn{
345 Access: &[]byte{255, 255, 255, 255, 255, 255, 255, 255},
351 Access: &[]byte{0, 0, 0, 0, 0, 0, 0, 0},
360 NewField(fieldData, []byte("hai")),
366 clientID: &[]byte{0, 1},
369 Type: []byte{0, 0x6a},
370 ID: []byte{0x9a, 0xcb, 0x04, 0x42}, // Random ID from rand.Seed(1)
371 ErrorCode: []byte{0, 0, 0, 0},
373 NewField(fieldData, []byte{0x0d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x01, 0x3a, 0x20, 0x20, 0x68, 0x61, 0x69}),
380 for _, tt := range tests {
381 rand.Seed(1) // reset seed between tests to make transaction IDs predictable
382 t.Run(tt.name, func(t *testing.T) {
383 got, err := HandleChatSend(tt.args.cc, tt.args.t)
385 if (err != nil) != tt.wantErr {
386 t.Errorf("HandleChatSend() error = %v, wantErr %v", err, tt.wantErr)
389 if !assert.Equal(t, tt.want, got) {
390 t.Errorf("HandleChatSend() got = %v, want %v", got, tt.want)