forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access_audit_log_test.go
93 lines (77 loc) · 2.42 KB
/
access_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
package cloudflare
import (
"context"
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestAccessAuditLogs(t *testing.T) {
setup()
defer teardown()
handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
{
"user_email": "[email protected]",
"ip_address": "198.51.100.1",
"app_uid": "df7e2w5f-02b7-4d9d-af26-8d1988fca630",
"app_domain": "test.example.com/admin",
"action": "login",
"connection": "saml",
"allowed": false,
"created_at": "2014-01-01T05:20:00.12345Z",
"ray_id": "187d944c61940c77"
}
]
}
`)
}
mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/access/logs/access-requests", handler)
createdAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
want := []AccessAuditLogRecord{{
UserEmail: "[email protected]",
IPAddress: "198.51.100.1",
AppUID: "df7e2w5f-02b7-4d9d-af26-8d1988fca630",
AppDomain: "test.example.com/admin",
Action: "login",
Connection: "saml",
Allowed: false,
CreatedAt: &createdAt,
RayID: "187d944c61940c77",
}}
actual, err := client.AccessAuditLogs(context.Background(), "01a7362d577a6c3019a474fd6f485823", AccessAuditLogFilterOptions{})
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}
func TestAccessAuditLogsEncodeAllParametersDefined(t *testing.T) {
since, _ := time.Parse(time.RFC3339, "2020-07-01T00:00:00Z")
until, _ := time.Parse(time.RFC3339, "2020-07-02T00:00:00Z")
opts := AccessAuditLogFilterOptions{
Direction: "desc",
Since: &since,
Until: &until,
Limit: 10,
}
assert.Equal(t, "direction=desc&limit=10&since=2020-07-01T00%3A00%3A00Z&until=2020-07-02T00%3A00%3A00Z", opts.Encode())
}
func TestAccessAuditLogsEncodeOnlyDatesDefined(t *testing.T) {
since, _ := time.Parse(time.RFC3339, "2020-07-01T00:00:00Z")
until, _ := time.Parse(time.RFC3339, "2020-07-02T00:00:00Z")
opts := AccessAuditLogFilterOptions{
Since: &since,
Until: &until,
}
assert.Equal(t, "since=2020-07-01T00%3A00%3A00Z&until=2020-07-02T00%3A00%3A00Z", opts.Encode())
}
func TestAccessAuditLogsEncodeEmptyValues(t *testing.T) {
opts := AccessAuditLogFilterOptions{}
assert.Equal(t, "", opts.Encode())
}