2020-10-08 14:38:33 +02:00
|
|
|
package group
|
2020-09-30 00:33:23 +02:00
|
|
|
|
|
|
|
import (
|
2020-11-29 17:00:12 +01:00
|
|
|
"encoding/json"
|
2021-02-14 19:44:19 +01:00
|
|
|
"fmt"
|
2020-11-29 17:00:12 +01:00
|
|
|
"reflect"
|
2020-09-30 00:33:23 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-02-14 20:09:30 +01:00
|
|
|
func TestGroup(t *testing.T) {
|
|
|
|
groups.groups = nil
|
2021-04-22 17:58:30 +02:00
|
|
|
Add("group", &Description{})
|
|
|
|
Add("group/subgroup", &Description{Public: true})
|
2021-02-14 20:09:30 +01:00
|
|
|
if len(groups.groups) != 2 {
|
|
|
|
t.Errorf("Expected 2, got %v", len(groups.groups))
|
|
|
|
}
|
|
|
|
g := Get("group")
|
|
|
|
g2 := Get("group/subgroup")
|
|
|
|
if g == nil {
|
|
|
|
t.Fatalf("Couldn't get group")
|
|
|
|
}
|
|
|
|
if g2 == nil {
|
|
|
|
t.Fatalf("Couldn't get group/subgroup")
|
|
|
|
}
|
|
|
|
if name := g.Name(); name != "group" {
|
|
|
|
t.Errorf("Name: expected group1, got %v", name)
|
|
|
|
}
|
|
|
|
if locked, _ := g.Locked(); locked {
|
|
|
|
t.Errorf("Locked: expected false, got %v", locked)
|
|
|
|
}
|
2021-05-08 16:09:22 +02:00
|
|
|
api, err := g.API()
|
|
|
|
if err != nil || api == nil {
|
|
|
|
t.Errorf("Couldn't get API: %v", err)
|
2021-02-14 20:09:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if names := GetNames(); len(names) != 2 {
|
|
|
|
t.Errorf("Expected 2, got %v", names)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subs := GetSubGroups("group"); len(subs) != 0 {
|
|
|
|
t.Errorf("Expected [], got %v", subs)
|
|
|
|
}
|
|
|
|
|
2022-09-01 15:38:29 +02:00
|
|
|
if public := GetPublic(""); len(public) != 1 || public[0].Name != "group/subgroup" {
|
2021-05-22 16:58:09 +02:00
|
|
|
t.Errorf("Expected group/subgroup, got %v", public)
|
2021-02-14 20:09:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 19:44:19 +01:00
|
|
|
func TestChatHistory(t *testing.T) {
|
|
|
|
g := Group{
|
2021-04-22 17:58:30 +02:00
|
|
|
description: &Description{},
|
2021-02-14 19:44:19 +01:00
|
|
|
}
|
2023-03-22 17:41:16 +01:00
|
|
|
user := "user"
|
2021-02-14 19:44:19 +01:00
|
|
|
for i := 0; i < 2*maxChatHistory; i++ {
|
2023-03-22 17:41:16 +01:00
|
|
|
g.AddToChatHistory("id", &user, time.Now(), "",
|
2021-02-14 19:44:19 +01:00
|
|
|
fmt.Sprintf("%v", i),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
h := g.GetChatHistory()
|
|
|
|
if len(h) != maxChatHistory {
|
|
|
|
t.Errorf("Expected %v, got %v", maxChatHistory, len(g.history))
|
|
|
|
}
|
|
|
|
for i, s := range h {
|
|
|
|
e := fmt.Sprintf("%v", i+maxChatHistory)
|
|
|
|
if s.Value.(string) != e {
|
|
|
|
t.Errorf("Expected %v, got %v", e, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 23:30:45 +01:00
|
|
|
var descJSON = `
|
2020-11-29 17:00:12 +01:00
|
|
|
{
|
2021-02-06 23:30:45 +01:00
|
|
|
"op": [{"username": "jch","password": "topsecret"}],
|
2020-11-29 17:00:12 +01:00
|
|
|
"max-history-age": 10,
|
|
|
|
"allow-subgroups": true,
|
2021-02-06 23:30:45 +01:00
|
|
|
"presenter": [
|
|
|
|
{"username": "john", "password": "secret"},
|
|
|
|
{"username": "john", "password": "secret2"}
|
|
|
|
],
|
|
|
|
"other": [
|
|
|
|
{"username": "james", "password": "secret3"},
|
|
|
|
{"username": "peter", "password": "secret4"},
|
2020-11-29 17:00:12 +01:00
|
|
|
{}
|
|
|
|
]
|
2021-02-06 23:30:45 +01:00
|
|
|
|
2020-11-29 17:00:12 +01:00
|
|
|
}`
|
|
|
|
|
2021-02-06 23:30:45 +01:00
|
|
|
func TestDescriptionJSON(t *testing.T) {
|
2021-04-22 17:58:30 +02:00
|
|
|
var d Description
|
2021-02-06 23:30:45 +01:00
|
|
|
err := json.Unmarshal([]byte(descJSON), &d)
|
2020-11-29 17:00:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-06 23:30:45 +01:00
|
|
|
dd, err := json.Marshal(d)
|
2020-11-29 17:00:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("marshal: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-04-22 17:58:30 +02:00
|
|
|
var ddd Description
|
2021-02-06 23:30:45 +01:00
|
|
|
err = json.Unmarshal([]byte(dd), &ddd)
|
2020-11-29 17:00:12 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-06 23:30:45 +01:00
|
|
|
if !reflect.DeepEqual(d, ddd) {
|
|
|
|
t.Errorf("Got %v, expected %v", ddd, d)
|
2020-11-29 17:00:12 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-06 23:30:45 +01:00
|
|
|
|
2023-03-22 17:41:16 +01:00
|
|
|
var jch = "jch"
|
|
|
|
var john = "john"
|
|
|
|
var james = "james"
|
|
|
|
var paul = "paul"
|
|
|
|
|
2021-10-27 04:15:44 +02:00
|
|
|
var badClients = []ClientCredentials{
|
2023-03-22 17:41:16 +01:00
|
|
|
{Username: &jch, Password: "foo"},
|
|
|
|
{Username: &john, Password: "foo"},
|
|
|
|
{Username: &james, Password: "foo"},
|
2021-02-06 23:30:45 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 04:15:44 +02:00
|
|
|
type credPerm struct {
|
|
|
|
c ClientCredentials
|
2022-02-19 23:43:44 +01:00
|
|
|
p []string
|
2021-02-06 23:30:45 +01:00
|
|
|
}
|
|
|
|
|
2021-10-27 04:15:44 +02:00
|
|
|
var goodClients = []credPerm{
|
2021-02-06 23:30:45 +01:00
|
|
|
{
|
2023-03-22 17:41:16 +01:00
|
|
|
ClientCredentials{Username: &jch, Password: "topsecret"},
|
2022-02-19 23:43:44 +01:00
|
|
|
[]string{"op", "present"},
|
2021-02-06 23:30:45 +01:00
|
|
|
},
|
|
|
|
{
|
2023-03-22 17:41:16 +01:00
|
|
|
ClientCredentials{Username: &john, Password: "secret"},
|
2022-02-19 23:43:44 +01:00
|
|
|
[]string{"present"},
|
2021-02-06 23:30:45 +01:00
|
|
|
},
|
|
|
|
{
|
2023-03-22 17:41:16 +01:00
|
|
|
ClientCredentials{Username: &john, Password: "secret2"},
|
2022-02-19 23:43:44 +01:00
|
|
|
[]string{"present"},
|
2021-02-06 23:30:45 +01:00
|
|
|
},
|
|
|
|
{
|
2023-03-22 17:41:16 +01:00
|
|
|
ClientCredentials{Username: &james, Password: "secret3"},
|
2022-02-19 23:43:44 +01:00
|
|
|
nil,
|
2021-02-06 23:30:45 +01:00
|
|
|
},
|
|
|
|
{
|
2023-03-22 17:41:16 +01:00
|
|
|
ClientCredentials{Username: &paul, Password: "secret3"},
|
2022-02-19 23:43:44 +01:00
|
|
|
nil,
|
2021-02-06 23:30:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPermissions(t *testing.T) {
|
2022-09-09 14:13:24 +02:00
|
|
|
var g Group
|
|
|
|
err := json.Unmarshal([]byte(descJSON), &g.description)
|
2021-02-06 23:30:45 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range badClients {
|
2023-03-22 17:41:16 +01:00
|
|
|
t.Run("bad "+*c.Username, func(t *testing.T) {
|
2022-09-09 14:13:24 +02:00
|
|
|
_, p, err := g.GetPermission(c)
|
2021-02-06 23:30:45 +01:00
|
|
|
if err != ErrNotAuthorised {
|
|
|
|
t.Errorf("GetPermission %v: %v %v", c, err, p)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cp := range goodClients {
|
2023-03-22 17:41:16 +01:00
|
|
|
t.Run("good "+*cp.c.Username, func(t *testing.T) {
|
2022-09-09 14:13:24 +02:00
|
|
|
u, p, err := g.GetPermission(cp.c)
|
2021-02-06 23:30:45 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("GetPermission %v: %v", cp.c, err)
|
2023-03-22 17:41:16 +01:00
|
|
|
} else if u != *cp.c.Username ||
|
2022-02-20 15:32:18 +01:00
|
|
|
!reflect.DeepEqual(p, cp.p) {
|
|
|
|
t.Errorf("%v: got %v %v, expected %v",
|
|
|
|
cp.c, u, p, cp.p)
|
2021-02-06 23:30:45 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-08-02 15:53:33 +02:00
|
|
|
|
2023-03-22 04:04:22 +01:00
|
|
|
func TestUsernameTaken(t *testing.T) {
|
|
|
|
var g Group
|
|
|
|
err := json.Unmarshal([]byte(descJSON), &g.description)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g.UserExists("") {
|
|
|
|
t.Error("UserExists(\"\") is true, expected false")
|
|
|
|
}
|
|
|
|
if !g.UserExists("john") {
|
|
|
|
t.Error("UserExists(john) is false")
|
|
|
|
}
|
|
|
|
if !g.UserExists("john") {
|
|
|
|
t.Error("UserExists(james) is false")
|
|
|
|
}
|
|
|
|
if g.UserExists("paul") {
|
|
|
|
t.Error("UserExists(paul) is true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 15:53:33 +02:00
|
|
|
func TestFmtpValue(t *testing.T) {
|
|
|
|
type fmtpTest struct {
|
|
|
|
fmtp string
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
fmtpTests := []fmtpTest{
|
|
|
|
{"", "foo", ""},
|
|
|
|
{"profile-id=0", "profile-id", "0"},
|
|
|
|
{"profile-id=0", "foo", ""},
|
|
|
|
{"level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f", "profile-level-id", "42001f"},
|
|
|
|
{"foo=1;bar=2;quux=3", "foo", "1"},
|
|
|
|
{"foo=1;bar=2;quux=3", "bar", "2"},
|
|
|
|
{"foo=1;bar=2;quux=3", "fu", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range fmtpTests {
|
|
|
|
v := fmtpValue(test.fmtp, test.key)
|
|
|
|
if v != test.value {
|
|
|
|
t.Errorf("fmtpValue(%v, %v) = %v, expected %v",
|
|
|
|
test.fmtp, test.key, v, test.value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 00:31:46 +02:00
|
|
|
|
|
|
|
func TestValidGroupName(t *testing.T) {
|
|
|
|
type nameTest struct {
|
|
|
|
name string
|
|
|
|
result bool
|
|
|
|
}
|
|
|
|
tests := []nameTest{
|
|
|
|
{"", false},
|
|
|
|
{"/", false},
|
|
|
|
{"/foo", false},
|
|
|
|
{"foo/", false},
|
|
|
|
{"./foo", false},
|
|
|
|
{"foo/.", false},
|
|
|
|
{"../foo", false},
|
|
|
|
{"foo/..", false},
|
|
|
|
{"foo/./bar", false},
|
|
|
|
{"foo/../bar", false},
|
|
|
|
{"foo", true},
|
|
|
|
{"foo/bar", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
r := validGroupName(test.name)
|
|
|
|
if r != test.result {
|
|
|
|
t.Errorf("Valid %v: got %v, expected %v",
|
|
|
|
test.name, r, test.result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|