/
audit_log_test.go
137 lines (122 loc) · 4.17 KB
/
audit_log_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
package logs_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/Shivam010/go-audit-log"
"github.com/google/uuid"
)
func RunAuditLogTest(lg logs.AuditLog, t *testing.T) {
noOfLogs := 10
userId := uuid.New().String()
action := "some work or action"
ctx := context.Background()
ctx = context.WithValue(ctx, "userId", userId)
var (
strt time.Time
end time.Time
)
t.Run("Add", func(t *testing.T) {
t.Run("One", func(t *testing.T) {
if err := lg.Add(ctx, action); err != nil {
t.Errorf("error while adding a log: %v", err)
}
})
t.Run("Multiple Add", func(t *testing.T) {
strt = time.Now()
for i := 1; i <= noOfLogs; i++ {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if err := lg.Add(ctx, action); err != nil {
t.Errorf("error while adding a log: %v", err)
}
})
}
time.Sleep(time.Second)
end = time.Now()
})
t.Run("Get", func(t *testing.T) {
t.Run("Logs of userId", func(t *testing.T) {
obj, err := lg.GetLogsOfUser(ctx, userId)
if err != nil {
t.Errorf("error while getting log(s) corresponding to a user %v", err)
}
if obj[0].UserID != userId || obj[0].Action != action {
t.Errorf("error while getting logs of user: log retured contains wrong data")
}
if len(obj) != noOfLogs+1 {
t.Errorf("error while getting logs of user: number of logs added are %v and that of got are %v", noOfLogs, len(obj))
}
})
t.Run("Logs between interval", func(t *testing.T) {
obj, err := lg.GetLogsBetweenInterval(ctx, strt, end, userId)
if err != nil {
t.Errorf("error while getting log(s) corresponding to a user in an interval %v", err)
}
// userid and action check
if obj[0].UserID != userId || obj[0].Action != action {
t.Errorf("error while getting logs of user: log retured contains wrong data")
}
// lenght of log list check
if len(obj) != noOfLogs {
t.Errorf("error while getting logs of user in interval: number of logs added are %v and that of got are %v", noOfLogs, len(obj))
}
})
t.Run("get newUser", func(t *testing.T) {
newUserID := uuid.New().String()
t.Run("Logs without interval", func(t *testing.T) {
obj, err := lg.GetLogsOfUser(ctx, newUserID)
if err != nil {
t.Errorf("error while getting log(s) corresponding to a user %v", err)
}
// length of list should be zero as no log is present for newUser
if len(obj) != 0 {
t.Errorf("error while getting logs of user: no data should be found but some found, %v", obj)
}
})
t.Run("Logs with interval", func(t *testing.T) {
obj, err := lg.GetLogsBetweenInterval(ctx, strt, end, newUserID)
if err != nil {
t.Errorf("error while getting log(s) corresponding to a user in an interval %v", err)
}
// length of list should be zero as no log is present for newUser
if len(obj) != 0 {
t.Errorf("error while getting logs of user in an interval: no data should be found but some found, %v", obj)
}
})
})
t.Run("New time Range", func(t *testing.T) {
strt = time.Now()
time.Sleep(5 * time.Second)
end = time.Now()
obj, err := lg.GetLogsBetweenInterval(ctx, strt, end, userId)
if err != nil {
t.Errorf("error while getting log(s) corresponding to a user in an interval %v", err)
}
// length of list should be zero as no log is present in time range provided
if len(obj) != 0 {
t.Errorf("error while getting logs of user: no data should be found but some found, %v", obj)
}
})
})
})
t.Run("Cancelled", func(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
cancel()
t.Run("Add", func(t *testing.T) {
if err := lg.Add(ctx, action); err == nil {
t.Errorf("Add should return context.Cancelled but got: %v", err)
}
})
t.Run("Get logs of userId", func(t *testing.T) {
if _, err := lg.GetLogsOfUser(ctx, userId); err == nil {
t.Errorf("GetLogsOfUser should return context.Cancelled but got: %v", err)
}
})
t.Run("Get logs in interval", func(t *testing.T) {
if _, err := lg.GetLogsBetweenInterval(ctx, strt, end, userId); err == nil {
t.Errorf("GetLogsBetweenInterval should return context.Cancelled but got: %v", err)
}
})
})
}