forked from umputun/tg-spam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
354 lines (308 loc) · 9.24 KB
/
main_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"bufio"
"context"
"encoding/json"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/umputun/tg-spam/app/bot"
"github.com/umputun/tg-spam/app/storage"
"github.com/umputun/tg-spam/lib/spamcheck"
)
func TestMakeSpamLogger(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "log")
require.NoError(t, err)
defer os.Remove(file.Name())
db, err := sqlx.Open("sqlite", ":memory:")
require.NoError(t, err)
defer db.Close()
logger, err := makeSpamLogger(file, db)
require.NoError(t, err)
msg := &bot.Message{
From: bot.User{
ID: 123,
DisplayName: "Test User",
Username: "testuser",
},
Text: "Test message\nblah blah \n\n\n",
}
response := &bot.Response{
Text: "spam detected",
CheckResults: []spamcheck.Response{
{Name: "Check1", Spam: true, Details: "Details 1"},
{Name: "Check2", Spam: false, Details: "Details 2"},
},
}
logger.Save(msg, response)
file.Close()
// check that the message is saved to the log file
file, err = os.Open(file.Name())
require.NoError(t, err)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
t.Log(line)
var logEntry map[string]interface{}
err = json.Unmarshal([]byte(line), &logEntry)
require.NoError(t, err)
assert.Equal(t, "Test User", logEntry["display_name"])
assert.Equal(t, "testuser", logEntry["user_name"])
assert.Equal(t, float64(123), logEntry["user_id"]) // json.Unmarshal converts numbers to float64
assert.Equal(t, "Test message blah blah", logEntry["text"])
}
assert.NoError(t, scanner.Err())
// check that the message is saved to the database
savedMsgs := []storage.DetectedSpamInfo{}
err = db.Select(&savedMsgs, "SELECT text, user_id, user_name, timestamp, checks FROM detected_spam")
require.NoError(t, err)
assert.Equal(t, 1, len(savedMsgs))
assert.Equal(t, "Test message blah blah", savedMsgs[0].Text)
assert.Equal(t, "testuser", savedMsgs[0].UserName)
assert.Equal(t, int64(123), savedMsgs[0].UserID)
assert.Equal(t, `[{"name":"Check1","spam":true,"details":"Details 1"},{"name":"Check2","spam":false,"details":"Details 2"}]`,
savedMsgs[0].ChecksJSON)
}
func TestMakeSpamLogWriter(t *testing.T) {
setupLog(true, "super-secret-token")
t.Run("happy path", func(t *testing.T) {
file, err := os.CreateTemp(os.TempDir(), "log")
require.NoError(t, err)
defer os.Remove(file.Name())
var opts options
opts.Logger.Enabled = true
opts.Logger.FileName = file.Name()
opts.Logger.MaxSize = "1M"
opts.Logger.MaxBackups = 1
writer, err := makeSpamLogWriter(opts)
require.NoError(t, err)
_, err = writer.Write([]byte("Test log entry\n"))
assert.NoError(t, err)
err = writer.Close()
assert.NoError(t, err)
file, err = os.Open(file.Name())
require.NoError(t, err)
content, err := io.ReadAll(file)
assert.NoError(t, err)
assert.Equal(t, "Test log entry\n", string(content))
})
t.Run("failed on wrong size", func(t *testing.T) {
var opts options
opts.Logger.Enabled = true
opts.Logger.FileName = "/tmp"
opts.Logger.MaxSize = "1f"
opts.Logger.MaxBackups = 1
writer, err := makeSpamLogWriter(opts)
assert.Error(t, err)
t.Log(err)
assert.Nil(t, writer)
})
t.Run("disabled", func(t *testing.T) {
var opts options
opts.Logger.Enabled = false
opts.Logger.FileName = "/tmp"
opts.Logger.MaxSize = "10M"
opts.Logger.MaxBackups = 1
writer, err := makeSpamLogWriter(opts)
assert.NoError(t, err)
assert.IsType(t, nopWriteCloser{}, writer)
})
}
func Test_makeDetector(t *testing.T) {
t.Run("no options", func(t *testing.T) {
var opts options
res := makeDetector(opts)
assert.NotNil(t, res)
})
t.Run("with first msgs count", func(t *testing.T) {
var opts options
opts.OpenAI.Token = "123"
opts.Files.SamplesDataPath = "/tmp"
opts.Files.DynamicDataPath = "/tmp"
opts.FirstMessagesCount = 10
res := makeDetector(opts)
assert.NotNil(t, res)
assert.Equal(t, 10, res.FirstMessagesCount)
assert.Equal(t, true, res.FirstMessageOnly)
})
t.Run("with first msgs count and paranoid", func(t *testing.T) {
var opts options
opts.OpenAI.Token = "123"
opts.Files.SamplesDataPath = "/tmp"
opts.Files.DynamicDataPath = "/tmp"
opts.FirstMessagesCount = 10
opts.ParanoidMode = true
res := makeDetector(opts)
assert.NotNil(t, res)
assert.Equal(t, 0, res.FirstMessagesCount)
assert.Equal(t, false, res.FirstMessageOnly)
})
}
func Test_makeSpamBot(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
t.Run("no options", func(t *testing.T) {
var opts options
_, err := makeSpamBot(ctx, opts, nil)
assert.Error(t, err)
})
t.Run("with valid options", func(t *testing.T) {
var opts options
tmpDir, err := os.MkdirTemp("", "spambot_main_test")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
_, err = os.Create(filepath.Join(tmpDir, samplesSpamFile))
require.NoError(t, err)
_, err = os.Create(filepath.Join(tmpDir, samplesHamFile))
require.NoError(t, err)
_, err = os.Create(filepath.Join(tmpDir, excludeTokensFile))
require.NoError(t, err)
opts.Files.SamplesDataPath = tmpDir
detector := makeDetector(opts)
res, err := makeSpamBot(ctx, opts, detector)
assert.NoError(t, err)
assert.NotNil(t, res)
})
}
func Test_activateServerOnly(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var opts options
opts.Server.Enabled = true
opts.Server.ListenAddr = ":9988"
opts.Server.AuthPasswd = "auto"
opts.Files.SamplesDataPath = "webapi/testdata"
opts.Files.DynamicDataPath = "webapi/testdata"
done := make(chan struct{})
go func() {
err := execute(ctx, opts)
assert.NoError(t, err)
close(done)
}()
time.Sleep(time.Millisecond * 100)
resp, err := http.Get("http://localhost:9988/ping")
require.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "pong", string(body))
cancel()
<-done
}
func Test_checkVolumeMount(t *testing.T) {
prepEnvAndFileSystem := func(opts *options, envValue string, dynamicDataPath string, notMountedExists bool) func() {
os.Setenv("TGSPAM_IN_DOCKER", envValue)
tempDir, _ := os.MkdirTemp("", "test")
if dynamicDataPath != "" {
os.MkdirAll(filepath.Join(tempDir, dynamicDataPath), os.ModePerm)
}
if notMountedExists {
os.WriteFile(filepath.Join(tempDir, dynamicDataPath, ".not_mounted"), []byte{}, 0o644)
}
if dynamicDataPath == "" {
dynamicDataPath = "dynamic"
}
opts.Files.DynamicDataPath = filepath.Join(tempDir, dynamicDataPath)
return func() {
os.RemoveAll(tempDir)
}
}
tests := []struct {
name string
envValue string
dynamicDataPath string
notMountedExists bool
expectedOk bool
}{
{
name: "not in docker",
envValue: "0",
dynamicDataPath: "",
expectedOk: true,
},
{
name: "in Docker, path mounted, no .not_mounted",
envValue: "1",
dynamicDataPath: "dynamic",
notMountedExists: false,
expectedOk: true,
},
{
name: "in docker, .not_mounted exists",
envValue: "1",
dynamicDataPath: "dynamic",
notMountedExists: true,
expectedOk: false,
},
{
name: "not in docker, .not_mounted exists",
envValue: "0",
dynamicDataPath: "dynamic",
notMountedExists: true,
expectedOk: true,
},
{
name: "in docker, path not mounted",
envValue: "1",
dynamicDataPath: "",
notMountedExists: false,
expectedOk: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
opts := options{}
cleanup := prepEnvAndFileSystem(&opts, tt.envValue, tt.dynamicDataPath, tt.notMountedExists)
defer cleanup()
ok := checkVolumeMount(opts)
assert.Equal(t, tt.expectedOk, ok)
})
}
}
func Test_expandPath(t *testing.T) {
home, err := os.UserHomeDir()
require.NoError(t, err)
currentDir, err := os.Getwd()
require.NoError(t, err)
tests := []struct {
name string
path string
want string
}{
{"Empty Path", "", ""},
{"Home Directory", "~", home},
{"Relative Path", ".", ""},
{"Relative Path with directory", "data", filepath.Join(currentDir, "data")},
{"Absolute Path", "/tmp", "/tmp"},
{"Path with Tilde and Subdirectory", "~/Documents", filepath.Join(home, "Documents")},
{"Path with Multiple Relative Directories", "../parent/child", ""},
{"Path with Special Characters", "data/special @#$/file", ""},
{"Invalid Path", "/some/nonexistent/path", "/some/nonexistent/path"},
{"Home Directory with Trailing Slash", "~/", home},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := expandPath(tt.path)
switch {
case strings.Contains(tt.path, "~"):
assert.Equal(t, filepath.Join(home, tt.path[1:]), got)
case tt.path == ".", strings.HasPrefix(tt.path, ".."), strings.Contains(tt.path, "/"):
// For relative paths, paths starting with "..", and paths with special characters
expected, err := filepath.Abs(tt.path)
require.NoError(t, err)
assert.Equal(t, expected, got)
default:
// For absolute paths and invalid paths
assert.Equal(t, tt.want, got)
}
})
}
}