-
Notifications
You must be signed in to change notification settings - Fork 13
/
htgroup_test.go
86 lines (77 loc) · 3.13 KB
/
htgroup_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
package htpasswd
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var contents = `users: user1 user2 user3
admins: user1
`
var contents2 = `users: user1 user2 user3
admins: user1 user2
`
func TestGroups(t *testing.T) {
// create temp file and write "contents" into it
var f, err = os.CreateTemp("", "gogroups")
filename := f.Name()
assert.NoError(t, err)
if _, err := f.WriteString(contents); err != nil {
t.Fatalf("Failed to write temporary file: %s", err.Error())
}
f.Close()
// Read the file and check the contents
htGroup, err := NewGroups(filename, nil)
assert.NoError(t, err)
assert.True(t, htGroup.IsUserInGroup("user1", "users"))
assert.True(t, htGroup.IsUserInGroup("user1", "admins"))
assert.True(t, htGroup.IsUserInGroup("user2", "users"))
assert.False(t, htGroup.IsUserInGroup("user2", "admin"))
assert.False(t, htGroup.IsUserInGroup("unknownuser", "users"))
assert.False(t, htGroup.IsUserInGroup("user1", "unknowngroup"))
assert.False(t, htGroup.IsUserInGroup("unknownuser", "unknowngroup"))
assert.Len(t, htGroup.GetUserGroups("user1"), 2)
assert.Len(t, htGroup.GetUserGroups("user2"), 1)
assert.Len(t, htGroup.GetUserGroups("user3"), 1)
assert.Len(t, htGroup.GetUserGroups("unknownuser"), 0)
// Replace temp file with another one (contents2)
os.Remove(filename)
f2, errCreate := os.Create(filename)
if errCreate != nil {
t.Fatalf("Failed to create temporary file: %s", errCreate.Error())
}
if _, err := f2.WriteString(contents2); err != nil {
t.Fatalf("Failed to write temporary file: %s", err.Error())
}
f2.Close()
defer os.Remove(filename)
// Reread the file and check the contents again, user2 should now be member of admins too.
reloadError := htGroup.ReloadGroups(nil)
assert.NoError(t, reloadError)
assert.True(t, htGroup.IsUserInGroup("user1", "users"))
assert.True(t, htGroup.IsUserInGroup("user1", "admins"))
assert.True(t, htGroup.IsUserInGroup("user2", "users"))
assert.True(t, htGroup.IsUserInGroup("user2", "admins"))
assert.False(t, htGroup.IsUserInGroup("unknownuser", "users"))
assert.False(t, htGroup.IsUserInGroup("user1", "unknowngroup"))
assert.False(t, htGroup.IsUserInGroup("unknownuser", "unknowngroup"))
assert.Len(t, htGroup.GetUserGroups("user1"), 2)
assert.Len(t, htGroup.GetUserGroups("user2"), 2)
assert.Len(t, htGroup.GetUserGroups("user3"), 1)
assert.Len(t, htGroup.GetUserGroups("unknownuser"), 0)
// Test load from reader as well
r := strings.NewReader(contents2)
htGroup, err = NewGroupsFromReader(r, nil)
assert.NoError(t, err)
assert.True(t, htGroup.IsUserInGroup("user1", "users"))
assert.True(t, htGroup.IsUserInGroup("user1", "admins"))
assert.True(t, htGroup.IsUserInGroup("user2", "users"))
assert.True(t, htGroup.IsUserInGroup("user2", "admins"))
assert.False(t, htGroup.IsUserInGroup("unknownuser", "users"))
assert.False(t, htGroup.IsUserInGroup("user1", "unknowngroup"))
assert.False(t, htGroup.IsUserInGroup("unknownuser", "unknowngroup"))
assert.Len(t, htGroup.GetUserGroups("user1"), 2)
assert.Len(t, htGroup.GetUserGroups("user2"), 2)
assert.Len(t, htGroup.GetUserGroups("user3"), 1)
assert.Len(t, htGroup.GetUserGroups("unknownuser"), 0)
}