1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "net/http"
12 "testing"
13 "time"
14
15 "github.com/google/go-cmp/cmp"
16 )
17
18 func TestEnterpriseService_GetAuditLog(t *testing.T) {
19 client, mux, _, teardown := setup()
20 defer teardown()
21
22 mux.HandleFunc("/enterprises/e/audit-log", func(w http.ResponseWriter, r *http.Request) {
23 testMethod(t, r, "GET")
24
25 fmt.Fprint(w, `[
26 {
27 "workflow_id": 123456,
28 "head_branch": "master",
29 "org": "o",
30 "trigger_id": null,
31 "repo": "o/blue-crayon-1",
32 "created_at": 1615077308538,
33 "head_sha": "5acdeadbeef64d1a62388e901e5cdc9358644b37",
34 "conclusion": "success",
35 "actor": "testactor",
36 "completed_at": "2021-03-07T00:35:08.000Z",
37 "@timestamp": 1615077308538,
38 "name": "Code scanning - action",
39 "action": "workflows.completed_workflow_run",
40 "started_at": "2021-03-07T00:33:04.000Z",
41 "event": "schedule",
42 "workflow_run_id": 628312345,
43 "_document_id": "beeZYapIUe-wKg5-beadb33"
44 }
45 ]`)
46 })
47 getOpts := GetAuditLogOptions{
48 Include: String("all"),
49 Phrase: String("action:workflows"),
50 Order: String("asc"),
51 }
52 ctx := context.Background()
53 auditEntries, _, err := client.Enterprise.GetAuditLog(ctx, "e", &getOpts)
54 if err != nil {
55 t.Errorf("Enterprise.GetAuditLog returned error: %v", err)
56 }
57 startedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:33:04.000Z")
58 completedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:35:08.000Z")
59 timestamp := time.Unix(0, 1615077308538*1e6)
60
61 want := []*AuditEntry{
62 {
63 Timestamp: &Timestamp{timestamp},
64 DocumentID: String("beeZYapIUe-wKg5-beadb33"),
65 Action: String("workflows.completed_workflow_run"),
66 Actor: String("testactor"),
67 CompletedAt: &Timestamp{completedAt},
68 Conclusion: String("success"),
69 CreatedAt: &Timestamp{timestamp},
70 Event: String("schedule"),
71 HeadBranch: String("master"),
72 HeadSHA: String("5acdeadbeef64d1a62388e901e5cdc9358644b37"),
73 Name: String("Code scanning - action"),
74 Org: String("o"),
75 Repo: String("o/blue-crayon-1"),
76 StartedAt: &Timestamp{startedAt},
77 WorkflowID: Int64(123456),
78 WorkflowRunID: Int64(628312345),
79 },
80 }
81
82 if !cmp.Equal(auditEntries, want) {
83 t.Errorf("Enterprise.GetAuditLog return \ngot: %+v,\nwant:%+v", auditEntries, want)
84 }
85
86 const methodName = "GetAuditLog"
87 testBadOptions(t, methodName, func() (err error) {
88 _, _, err = client.Enterprise.GetAuditLog(ctx, "\n", &getOpts)
89 return err
90 })
91
92 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
93 got, resp, err := client.Enterprise.GetAuditLog(ctx, "o", &GetAuditLogOptions{})
94 if got != nil {
95 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
96 }
97 return resp, err
98 })
99 }
100
View as plain text