mirror of
https://github.com/jech/galene.git
synced 2024-11-22 08:35:57 +01:00
Fix JSON formatting of user descriptions.
This commit is contained in:
parent
695c379a6c
commit
a3f894a31e
2 changed files with 60 additions and 1 deletions
|
@ -116,6 +116,18 @@ type UserDescription struct {
|
||||||
Permissions Permissions `json:"permissions"`
|
Permissions Permissions `json:"permissions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Custom MarshalJSON in order to omit ompty fields
|
||||||
|
func (u UserDescription) MarshalJSON() ([]byte, error) {
|
||||||
|
uu := make(map[string]any, 2)
|
||||||
|
if u.Password.Type != "" {
|
||||||
|
uu["password"] = &u.Password
|
||||||
|
}
|
||||||
|
if u.Permissions.name != "" || u.Permissions.permissions != nil {
|
||||||
|
uu["permissions"] = &u.Permissions
|
||||||
|
}
|
||||||
|
return json.Marshal(uu)
|
||||||
|
}
|
||||||
|
|
||||||
// Description represents a group description together with some metadata
|
// Description represents a group description together with some metadata
|
||||||
// about the JSON file it was deserialised from.
|
// about the JSON file it was deserialised from.
|
||||||
type Description struct {
|
type Description struct {
|
||||||
|
|
|
@ -8,6 +8,53 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestMarshalUserDescription(t *testing.T) {
|
||||||
|
tests := []string{
|
||||||
|
`{}`,
|
||||||
|
`{"permissions":"present"}`,
|
||||||
|
`{"password":"secret"}`,
|
||||||
|
`{"password":"secret","permissions":"present"}`,
|
||||||
|
`{"password":"secret","permissions":["present"]}`,
|
||||||
|
`{"password":{"type":"wildcard"},"permissions":"observe"}`,
|
||||||
|
`{"password":{"type":"wildcard"},"permissions":[]}`,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
var u UserDescription
|
||||||
|
err := json.Unmarshal([]byte(test), &u)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unmarshal %v: %v", t, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
v, err := json.Marshal(u)
|
||||||
|
if err != nil || string(v) != test {
|
||||||
|
t.Errorf("Marshal %v: got %v %v", test, string(v), err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmptyJSON(t *testing.T) {
|
||||||
|
type emptyTest struct {
|
||||||
|
value any
|
||||||
|
result string
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
emptyTests := []emptyTest{
|
||||||
|
{Password{}, "{}", "password"},
|
||||||
|
{Permissions{}, "null", "permissions"},
|
||||||
|
{UserDescription{}, "{}", "user description"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range emptyTests {
|
||||||
|
b, err := json.Marshal(v.value)
|
||||||
|
if err != nil || string(b) != v.result {
|
||||||
|
t.Errorf("Marshal empty %v: %#v %v, expected %#v",
|
||||||
|
v.name, string(b), err, v.result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var descJSON = `
|
var descJSON = `
|
||||||
{
|
{
|
||||||
"max-history-age": 10,
|
"max-history-age": 10,
|
||||||
|
@ -218,7 +265,7 @@ func TestWritableGroups(t *testing.T) {
|
||||||
|
|
||||||
pw := "pw"
|
pw := "pw"
|
||||||
err = SetUserPassword("test", "jch", Password{
|
err = SetUserPassword("test", "jch", Password{
|
||||||
Type: "",
|
Type: "plain",
|
||||||
Key: &pw,
|
Key: &pw,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue