-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase_test.go
More file actions
132 lines (98 loc) · 4.38 KB
/
database_test.go
File metadata and controls
132 lines (98 loc) · 4.38 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package subscribe
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetDB(t *testing.T) {
t.Parallel()
assertions := assert.New(t)
sub, err := GetDB("")
require.NoError(t, err, "getting an empty db must produce no error")
json, err := sub.StateGetJSON()
assertions.JSONEq(`{"enabledApis":[],"events":{"eventsMap":{}},"subscribers":[]}`, json,
"the initial state must be empty")
require.NoError(t, err, "getting an empty state must produce no error")
}
func TestStateFileLoad(t *testing.T) {
t.Parallel()
assertions := assert.New(t)
testFile := filepath.Join(t.TempDir(), "state.json")
// test with good data.
testJSON := `{"enabledApis":[],"events":{"eventsMap":{}},"subscribers":[{"id":0,"meta":null,"api":` +
`"http","contact":"testUser","events":{"eventsMap":{}},"isAdmin":false,"ignored":false}]}`
require.NoError(t, os.WriteFile(testFile, []byte(testJSON), 0o600), "problem writing test file")
sub, err := GetDB(testFile)
require.NoError(t, err, "there must be no error loading the state file")
json, err := sub.StateGetJSON()
require.NoError(t, err, "there must be no error getting the state data")
assertions.JSONEq(testJSON, json)
// Test missing file.
require.NoError(t, os.RemoveAll(testFile), "problem removing test file")
_, err = GetDB(testFile)
require.NoError(t, err, "there must be no error when the state file is missing")
// #nosec G304 -- test controls this temporary file path.
data, err := os.ReadFile(testFile)
require.NoError(t, err, "error reading test file")
assertions.JSONEq(`{"enabledApis":[],"events":{"eventsMap":{}},"subscribers":[]}`, string(data),
"the initial state file must be empty")
// Test uncreatable file.
_, err = GetDB("/tmp/xxx/yyy/zzz/aaa/bbb/this_file_dont_exist")
require.Error(t, err, "there must be an error when the state cannot be created")
// Test unreadable path (directory).
_, err = GetDB(t.TempDir())
require.Error(t, err, "there must be an error when the state path is not readable as a file")
// Test bad data.
err = os.WriteFile(testFile, []byte("this aint good json}}"), 0o600)
require.NoError(t, err, "problem writing test file")
_, err = GetDB(testFile)
require.Error(t, err, "there must be an error when the state file is corrupt")
}
func TestStateFileSave(t *testing.T) {
t.Parallel()
testFile := filepath.Join(t.TempDir(), "state-save.json")
sub, err := GetDB(testFile)
require.NoError(t, err, "there must be no error creating the initial state file")
require.NoError(t, sub.StateFileSave(), "there must be no error saving the state file")
sub, err = GetDB("")
require.NoError(t, err, "there must be no error when the state file does not exist")
require.NoError(t, sub.StateFileSave(), "there must be no error when the state file does not exist")
}
func TestStateFileRelocate(t *testing.T) {
t.Parallel()
assertions := assert.New(t)
testFile4 := filepath.Join(t.TempDir(), "state-relocate.json")
sub, err := GetDB("")
require.NoError(t, err, "there must be no error when the state file does not exist")
require.NoError(t, sub.StateFileSave(), "there must be no error when the state file does not exist")
err = sub.StateFileRelocate(testFile4)
assertions.Equal(testFile4, sub.stateFile, "the path was not to the new value")
require.NoError(t, err, "there must be no error creating the initial state file")
err = sub.StateFileRelocate(t.TempDir())
require.Error(t, err, "there must be an error trying to write a file as a folder")
assertions.Equal(testFile4, sub.stateFile, "the path was not changed back to the previous value")
}
func TestStateGetJSONMarshalError(t *testing.T) {
t.Parallel()
sub := &Subscribe{
Events: &Events{Map: make(map[string]*Rules)},
Subscribers: make([]*Subscriber, 0),
}
subscriber := sub.CreateSub("marshal", "api", false, false)
subscriber.Meta = map[string]any{"bad": make(chan int)}
_, err := sub.StateGetJSON()
require.Error(t, err, "unsupported meta values should fail marshaling")
}
func TestStateFileSaveMarshalError(t *testing.T) {
t.Parallel()
stateFile := filepath.Join(t.TempDir(), "state-save-marshal.json")
sub, err := GetDB(stateFile)
require.NoError(t, err)
subscriber := sub.CreateSub("marshal", "api", false, false)
subscriber.Meta = map[string]any{"bad": make(chan int)}
err = sub.StateFileSave()
require.Error(t, err)
assert.ErrorContains(t, err, "marshaling json")
}