github.com/google/go-github/v49@v49.1.0/github/orgs_audit_log_test.go (about) 1 // Copyright 2021 The go-github AUTHORS. All rights reserved. 2 // 3 // `Use` of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 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 // assert query string has lower case params 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 RunAttempt: Int64(1), 211 RunnerGroupID: Int64(1), 212 RunnerGroupName: String("rgn"), 213 RunnerID: Int64(1), 214 RunnerLabels: []string{"s"}, 215 RunnerName: String("rn"), 216 SecretsPassed: []string{"s"}, 217 SourceVersion: String("sv"), 218 StartedAt: &Timestamp{referenceTime}, 219 TargetLogin: String("tl"), 220 TargetVersion: String("tv"), 221 Team: String("t"), 222 Timestamp: &Timestamp{referenceTime}, 223 TransportProtocolName: String("tpn"), 224 TransportProtocol: Int(1), 225 TriggerID: Int64(1), 226 User: String("u"), 227 Visibility: String("v"), 228 WorkflowID: Int64(1), 229 WorkflowRunID: Int64(1), 230 } 231 232 want := `{ 233 "action": "a", 234 "active": false, 235 "active_was": false, 236 "actor": "ac", 237 "blocked_user": "bu", 238 "business": "b", 239 "cancelled_at": ` + referenceTimeStr + `, 240 "completed_at": ` + referenceTimeStr + `, 241 "conclusion": "c", 242 "config": { 243 "url": "s" 244 }, 245 "config_was": { 246 "url": "s" 247 }, 248 "content_type": "ct", 249 "created_at": ` + referenceTimeStr + `, 250 "deploy_key_fingerprint": "dkf", 251 "_document_id": "did", 252 "emoji": "e", 253 "environment_name": "en", 254 "event": "e", 255 "events": [ 256 "s" 257 ], 258 "events_were": [ 259 "s" 260 ], 261 "explanation": "e", 262 "fingerprint": "f", 263 "head_branch": "hb", 264 "head_sha": "hsha", 265 "hook_id": 1, 266 "is_hosted_runner": false, 267 "job_name": "jn", 268 "limited_availability": false, 269 "message": "m", 270 "name": "n", 271 "old_user": "ou", 272 "openssh_public_key": "osshpk", 273 "org": "o", 274 "previous_visibility": "pv", 275 "read_only": "ro", 276 "repo": "r", 277 "repository": "repo", 278 "repository_public": false, 279 "run_attempt": 1, 280 "runner_group_id": 1, 281 "runner_group_name": "rgn", 282 "runner_id": 1, 283 "runner_labels": [ 284 "s" 285 ], 286 "runner_name": "rn", 287 "secrets_passed": [ 288 "s" 289 ], 290 "source_version": "sv", 291 "started_at": ` + referenceTimeStr + `, 292 "target_login": "tl", 293 "target_version": "tv", 294 "team": "t", 295 "@timestamp": ` + referenceTimeStr + `, 296 "transport_protocol_name": "tpn", 297 "transport_protocol": 1, 298 "trigger_id": 1, 299 "user": "u", 300 "visibility": "v", 301 "workflow_id": 1, 302 "workflow_run_id": 1 303 }` 304 305 testJSONMarshal(t, u, want) 306 }