-
Notifications
You must be signed in to change notification settings - Fork 8
/
repository_test.go
79 lines (74 loc) · 1.54 KB
/
repository_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
package main
import (
"bufio"
"bytes"
"encoding/json"
"io"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)
func TestFileLootRepository(t *testing.T) {
tests := []struct {
name string
infos []*DBLoginInfo
}{
{
name: "OneRecord",
infos: []*DBLoginInfo{
{
Username: "user",
Password: "pass",
Date: time.Date(2021, 1, 1, 1, 0, 0, 0, time.UTC),
SessionID: "abc",
LureURL: "/abc/def",
},
},
},
{
name: "TwoRecords",
infos: []*DBLoginInfo{
{
Username: "user",
Password: "pass",
Date: time.Date(2021, 1, 1, 1, 0, 0, 0, time.UTC),
SessionID: "abc",
LureURL: "/abc/def",
},
{
Username: "user2",
Password: "pass2",
Date: time.Date(2021, 2, 2, 2, 0, 0, 0, time.UTC),
SessionID: "abc2",
LureURL: "/abc/def2",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buff bytes.Buffer
logger := zerolog.New(io.Discard)
repo := NewFileLootRepository(&logger, &buff)
for _, info := range tt.infos {
err := repo.SaveCreds(info)
require.NoError(t, err)
}
result := scanDBLoginInfos(t, &buff)
require.Equal(t, tt.infos, result)
})
}
}
func scanDBLoginInfos(t *testing.T, r io.Reader) []*DBLoginInfo {
t.Helper()
bs := bufio.NewScanner(r)
var result []*DBLoginInfo
for bs.Scan() {
var info DBLoginInfo
err := json.Unmarshal([]byte(bs.Text()), &info)
require.NoError(t, err)
result = append(result, &info)
}
return result
}