-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
responses_test.go
113 lines (101 loc) · 3.11 KB
/
responses_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
// Copyright (c) 2022 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package mautrix_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/crypto/canonicaljson"
)
const sampleData = `{
"capabilities": {
"m.room_versions": {
"default": "9",
"available": {
"1": "stable",
"2": "stable",
"3": "stable",
"4": "stable",
"5": "stable",
"6": "stable",
"org.matrix.msc2176": "unstable",
"7": "stable",
"8": "stable",
"9": "stable",
"org.matrix.msc2716v3": "unstable",
"org.matrix.msc3787": "unstable",
"10": "stable"
}
},
"m.change_password": {
"enabled": true
},
"m.set_displayname": {
"enabled": true
},
"m.3pid_changes": {
"enabled": false
},
"fi.mau.custom_field": {
"🐈️": true
}
}
}`
var sampleObject = mautrix.RespCapabilities{
RoomVersions: &mautrix.CapRoomVersions{
Default: "9",
Available: map[string]mautrix.CapRoomVersionStability{
"1": "stable",
"2": "stable",
"3": "stable",
"4": "stable",
"5": "stable",
"6": "stable",
"org.matrix.msc2176": "unstable",
"7": "stable",
"8": "stable",
"9": "stable",
"org.matrix.msc2716v3": "unstable",
"org.matrix.msc3787": "unstable",
"10": "stable",
},
},
ChangePassword: &mautrix.CapBooleanTrue{Enabled: true},
SetDisplayname: &mautrix.CapBooleanTrue{Enabled: true},
ThreePIDChanges: &mautrix.CapBooleanTrue{Enabled: false},
Custom: map[string]interface{}{
"fi.mau.custom_field": map[string]interface{}{
"🐈️": true,
},
},
}
func TestRespCapabilities_UnmarshalJSON(t *testing.T) {
var caps mautrix.RespCapabilities
err := json.Unmarshal([]byte(sampleData), &caps)
require.NoError(t, err)
fmt.Println(caps)
require.NotNil(t, caps.RoomVersions)
assert.Equal(t, "9", caps.RoomVersions.Default)
assert.True(t, caps.RoomVersions.IsStable("10"))
// Omitted capabilities still support IsEnabled(), and this one defaults to true
assert.Nil(t, caps.SetAvatarURL)
assert.True(t, caps.SetAvatarURL.IsEnabled())
assert.True(t, caps.SetDisplayname.IsEnabled())
assert.False(t, caps.ThreePIDChanges.IsEnabled())
assert.Contains(t, caps.Custom, "fi.mau.custom_field")
assert.NotContains(t, caps.Custom, "m.room_versions")
}
func TestRespCapabilities_MarshalJSON(t *testing.T) {
data, err := json.Marshal(&sampleObject)
require.NoError(t, err)
marshaledString := string(canonicaljson.CanonicalJSONAssumeValid(data))
origString := string(canonicaljson.CanonicalJSONAssumeValid([]byte(sampleData)))
assert.Equal(t, marshaledString, origString)
assert.Len(t, sampleObject.Custom, 1)
}