aboutsummaryrefslogtreecommitdiff
path: root/clientapi/routing/server_notices_test.go
blob: 2fac072cdc1b311d052ef5adb702dfe822bca91d (plain)
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
package routing

import (
	"testing"
)

func Test_sendServerNoticeRequest_validate(t *testing.T) {
	type fields struct {
		UserID  string `json:"user_id,omitempty"`
		Content struct {
			MsgType string `json:"msgtype,omitempty"`
			Body    string `json:"body,omitempty"`
		} `json:"content,omitempty"`
		Type     string `json:"type,omitempty"`
		StateKey string `json:"state_key,omitempty"`
	}

	content := struct {
		MsgType string `json:"msgtype,omitempty"`
		Body    string `json:"body,omitempty"`
	}{
		MsgType: "m.text",
		Body:    "Hello world!",
	}

	tests := []struct {
		name   string
		fields fields
		wantOk bool
	}{
		{
			name:   "empty request",
			fields: fields{},
		},
		{
			name: "msgtype empty",
			fields: fields{
				UserID: "@alice:localhost",
				Content: struct {
					MsgType string `json:"msgtype,omitempty"`
					Body    string `json:"body,omitempty"`
				}{
					Body: "Hello world!",
				},
			},
		},
		{
			name: "msg body empty",
			fields: fields{
				UserID: "@alice:localhost",
			},
		},
		{
			name: "statekey empty",
			fields: fields{
				UserID:  "@alice:localhost",
				Content: content,
			},
			wantOk: true,
		},
		{
			name: "type empty",
			fields: fields{
				UserID:  "@alice:localhost",
				Content: content,
			},
			wantOk: true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			r := sendServerNoticeRequest{
				UserID:   tt.fields.UserID,
				Content:  tt.fields.Content,
				Type:     tt.fields.Type,
				StateKey: tt.fields.StateKey,
			}
			if gotOk := r.valid(); gotOk != tt.wantOk {
				t.Errorf("valid() = %v, want %v", gotOk, tt.wantOk)
			}
		})
	}
}