1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "net/http"
12 "strings"
13 "testing"
14 "time"
15
16 "github.com/google/go-cmp/cmp"
17 )
18
19 func TestOrganizationService_GetAuditLog(t *testing.T) {
20 client, mux, _, teardown := setup()
21 defer teardown()
22
23 mux.HandleFunc("/orgs/o/audit-log", func(w http.ResponseWriter, r *http.Request) {
24 testMethod(t, r, "GET")
25
26 fmt.Fprint(w, `[
27 {
28 "active": true,
29 "workflow_id": 123456,
30 "head_branch": "master",
31 "org": "o",
32 "trigger_id": null,
33 "repo": "o/blue-crayon-1",
34 "created_at": 1615077308538,
35 "head_sha": "5acdeadbeef64d1a62388e901e5cdc9358644b37",
36 "conclusion": "success",
37 "actor": "testactor",
38 "completed_at": "2021-03-07T00:35:08.000Z",
39 "@timestamp": 1615077308538,
40 "name": "Code scanning - action",
41 "action": "workflows.completed_workflow_run",
42 "started_at": "2021-03-07T00:33:04.000Z",
43 "event": "schedule",
44 "workflow_run_id": 628312345,
45 "_document_id": "beeZYapIUe-wKg5-beadb33",
46 "config": {
47 "content_type": "json",
48 "insecure_ssl": "0",
49 "url": "https://example.com/deadbeef-new-hook"
50 },
51 "events": ["code_scanning_alert"]
52 }]`)
53 })
54 ctx := context.Background()
55 getOpts := GetAuditLogOptions{
56 Include: String("all"),
57 Phrase: String("action:workflows"),
58 Order: String("asc"),
59 }
60
61 auditEntries, resp, err := client.Organizations.GetAuditLog(ctx, "o", &getOpts)
62 if err != nil {
63 t.Errorf("Organizations.GetAuditLog returned error: %v", err)
64 }
65 startedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:33:04.000Z")
66 completedAt, _ := time.Parse(time.RFC3339, "2021-03-07T00:35:08.000Z")
67 timestamp := time.Unix(0, 1615077308538*1e6)
68
69 want := []*AuditEntry{
70 {
71 Timestamp: &Timestamp{timestamp},
72 DocumentID: String("beeZYapIUe-wKg5-beadb33"),
73 Action: String("workflows.completed_workflow_run"),
74 Actor: String("testactor"),
75 Active: Bool(true),
76 CompletedAt: &Timestamp{completedAt},
77 Conclusion: String("success"),
78 CreatedAt: &Timestamp{timestamp},
79 Event: String("schedule"),
80 HeadBranch: String("master"),
81 HeadSHA: String("5acdeadbeef64d1a62388e901e5cdc9358644b37"),
82 Name: String("Code scanning - action"),
83 Org: String("o"),
84 Repo: String("o/blue-crayon-1"),
85 StartedAt: &Timestamp{startedAt},
86 WorkflowID: Int64(123456),
87 WorkflowRunID: Int64(628312345),
88 Events: []string{"code_scanning_alert"},
89 Config: &HookConfig{
90 ContentType: String("json"),
91 InsecureSSL: String("0"),
92 URL: String("https://example.com/deadbeef-new-hook"),
93 },
94 },
95 }
96
97 if !cmp.Equal(auditEntries, want) {
98 t.Errorf("Organizations.GetAuditLog return \ngot: %+v,\nwant:%+v", auditEntries, want)
99 }
100
101
102 requestedQuery := resp.Request.URL.RawQuery
103 if !strings.Contains(requestedQuery, "phrase") {
104 t.Errorf("Organizations.GetAuditLog query string \ngot: %+v,\nwant:%+v", requestedQuery, "phrase")
105 }
106
107 const methodName = "GetAuditLog"
108 testBadOptions(t, methodName, func() (err error) {
109 _, _, err = client.Organizations.GetAuditLog(ctx, "\n", &getOpts)
110 return err
111 })
112
113 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
114 got, resp, err := client.Organizations.GetAuditLog(ctx, "o", &GetAuditLogOptions{})
115 if got != nil {
116 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
117 }
118 return resp, err
119 })
120 }
121
122 func TestGetAuditLogOptions_Marshal(t *testing.T) {
123 testJSONMarshal(t, &GetAuditLogOptions{}, "{}")
124
125 u := &GetAuditLogOptions{
126 Phrase: String("p"),
127 Include: String("i"),
128 Order: String("o"),
129 ListCursorOptions: ListCursorOptions{
130 Page: "p",
131 PerPage: 1,
132 After: "a",
133 Before: "b",
134 },
135 }
136
137 want := `{
138 "phrase": "p",
139 "include": "i",
140 "order": "o",
141 "Page": "p",
142 "PerPage": 1,
143 "After": "a",
144 "Before": "b"
145 }`
146
147 testJSONMarshal(t, u, want)
148 }
149
150 func TestHookConfig_Marshal(t *testing.T) {
151 testJSONMarshal(t, &HookConfig{}, "{}")
152
153 u := &HookConfig{
154 ContentType: String("ct"),
155 InsecureSSL: String("ct"),
156 URL: String("url"),
157 }
158
159 want := `{
160 "content_type": "ct",
161 "insecure_ssl": "ct",
162 "url": "url"
163 }`
164
165 testJSONMarshal(t, u, want)
166 }
167
168 func TestAuditEntry_Marshal(t *testing.T) {
169 testJSONMarshal(t, &AuditEntry{}, "{}")
170
171 u := &AuditEntry{
172 Action: String("a"),
173 Active: Bool(false),
174 ActiveWas: Bool(false),
175 Actor: String("ac"),
176 BlockedUser: String("bu"),
177 Business: String("b"),
178 CancelledAt: &Timestamp{referenceTime},
179 CompletedAt: &Timestamp{referenceTime},
180 Conclusion: String("c"),
181 Config: &HookConfig{URL: String("s")},
182 ConfigWas: &HookConfig{URL: String("s")},
183 ContentType: String("ct"),
184 CreatedAt: &Timestamp{referenceTime},
185 DeployKeyFingerprint: String("dkf"),
186 DocumentID: String("did"),
187 Emoji: String("e"),
188 EnvironmentName: String("en"),
189 Event: String("e"),
190 Events: []string{"s"},
191 EventsWere: []string{"s"},
192 Explanation: String("e"),
193 Fingerprint: String("f"),
194 HeadBranch: String("hb"),
195 HeadSHA: String("hsha"),
196 HookID: Int64(1),
197 IsHostedRunner: Bool(false),
198 JobName: String("jn"),
199 LimitedAvailability: Bool(false),
200 Message: String("m"),
201 Name: String("n"),
202 OldUser: String("ou"),
203 OpenSSHPublicKey: String("osshpk"),
204 Org: String("o"),
205 PreviousVisibility: String("pv"),
206 ReadOnly: String("ro"),
207 Repo: String("r"),
208 Repository: String("repo"),
209 RepositoryPublic: Bool(false),
210 RunnerGroupID: Int64(1),
211 RunnerGroupName: String("rgn"),
212 RunnerID: Int64(1),
213 RunnerLabels: []string{"s"},
214 RunnerName: String("rn"),
215 SecretsPassed: []string{"s"},
216 SourceVersion: String("sv"),
217 StartedAt: &Timestamp{referenceTime},
218 TargetLogin: String("tl"),
219 TargetVersion: String("tv"),
220 Team: String("t"),
221 Timestamp: &Timestamp{referenceTime},
222 TransportProtocolName: String("tpn"),
223 TransportProtocol: Int(1),
224 TriggerID: Int64(1),
225 User: String("u"),
226 Visibility: String("v"),
227 WorkflowID: Int64(1),
228 WorkflowRunID: Int64(1),
229 }
230
231 want := `{
232 "action": "a",
233 "active": false,
234 "active_was": false,
235 "actor": "ac",
236 "blocked_user": "bu",
237 "business": "b",
238 "cancelled_at": ` + referenceTimeStr + `,
239 "completed_at": ` + referenceTimeStr + `,
240 "conclusion": "c",
241 "config": {
242 "url": "s"
243 },
244 "config_was": {
245 "url": "s"
246 },
247 "content_type": "ct",
248 "created_at": ` + referenceTimeStr + `,
249 "deploy_key_fingerprint": "dkf",
250 "_document_id": "did",
251 "emoji": "e",
252 "environment_name": "en",
253 "event": "e",
254 "events": [
255 "s"
256 ],
257 "events_were": [
258 "s"
259 ],
260 "explanation": "e",
261 "fingerprint": "f",
262 "head_branch": "hb",
263 "head_sha": "hsha",
264 "hook_id": 1,
265 "is_hosted_runner": false,
266 "job_name": "jn",
267 "limited_availability": false,
268 "message": "m",
269 "name": "n",
270 "old_user": "ou",
271 "openssh_public_key": "osshpk",
272 "org": "o",
273 "previous_visibility": "pv",
274 "read_only": "ro",
275 "repo": "r",
276 "repository": "repo",
277 "repository_public": false,
278 "runner_group_id": 1,
279 "runner_group_name": "rgn",
280 "runner_id": 1,
281 "runner_labels": [
282 "s"
283 ],
284 "runner_name": "rn",
285 "secrets_passed": [
286 "s"
287 ],
288 "source_version": "sv",
289 "started_at": ` + referenceTimeStr + `,
290 "target_login": "tl",
291 "target_version": "tv",
292 "team": "t",
293 "@timestamp": ` + referenceTimeStr + `,
294 "transport_protocol_name": "tpn",
295 "transport_protocol": 1,
296 "trigger_id": 1,
297 "user": "u",
298 "visibility": "v",
299 "workflow_id": 1,
300 "workflow_run_id": 1
301 }`
302
303 testJSONMarshal(t, u, want)
304 }
305
View as plain text