github.com/google/go-github/v66@v66.0.0/github/enterprise_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  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestEnterpriseService_GetAuditLog(t *testing.T) {
    17  	t.Parallel()
    18  	client, mux, _ := setup(t)
    19  
    20  	mux.HandleFunc("/enterprises/e/audit-log", func(w http.ResponseWriter, r *http.Request) {
    21  		testMethod(t, r, "GET")
    22  
    23  		fmt.Fprint(w, `[
    24  		{
    25          "workflow_id": 123456,
    26          "head_branch": "master",
    27          "org": "o",
    28          "trigger_id": null,
    29          "repo": "o/blue-crayon-1",
    30          "created_at": 1615077308538,
    31          "head_sha": "5acdeadbeef64d1a62388e901e5cdc9358644b37",
    32          "conclusion": "success",
    33          "actor": "testactor",
    34          "completed_at": "2021-03-07T00:35:08.000Z",
    35          "@timestamp": 1615077308538,
    36          "name": "Code scanning - action",
    37          "action": "workflows.completed_workflow_run",
    38          "started_at": "2021-03-07T00:33:04.000Z",
    39          "event": "schedule",
    40          "workflow_run_id": 628312345,
    41          "_document_id": "beeZYapIUe-wKg5-beadb33"
    42  		}
    43  		]`)
    44  	})
    45  	getOpts := GetAuditLogOptions{
    46  		Include: String("all"),
    47  		Phrase:  String("action:workflows"),
    48  		Order:   String("asc"),
    49  	}
    50  	ctx := context.Background()
    51  	auditEntries, _, err := client.Enterprise.GetAuditLog(ctx, "e", &getOpts)
    52  	if err != nil {
    53  		t.Errorf("Enterprise.GetAuditLog returned error: %v", err)
    54  	}
    55  	timestamp := time.Unix(0, 1615077308538*1e6)
    56  	want := []*AuditEntry{
    57  		{
    58  			Timestamp:  &Timestamp{timestamp},
    59  			DocumentID: String("beeZYapIUe-wKg5-beadb33"),
    60  			Action:     String("workflows.completed_workflow_run"),
    61  			Actor:      String("testactor"),
    62  			CreatedAt:  &Timestamp{timestamp},
    63  			Org:        String("o"),
    64  			AdditionalFields: map[string]interface{}{
    65  				"completed_at":    "2021-03-07T00:35:08.000Z",
    66  				"conclusion":      "success",
    67  				"event":           "schedule",
    68  				"head_branch":     "master",
    69  				"head_sha":        "5acdeadbeef64d1a62388e901e5cdc9358644b37",
    70  				"name":            "Code scanning - action",
    71  				"repo":            "o/blue-crayon-1",
    72  				"started_at":      "2021-03-07T00:33:04.000Z",
    73  				"workflow_id":     float64(123456),
    74  				"workflow_run_id": float64(628312345),
    75  			},
    76  		},
    77  	}
    78  
    79  	assertNoDiff(t, want, auditEntries)
    80  
    81  	const methodName = "GetAuditLog"
    82  	testBadOptions(t, methodName, func() (err error) {
    83  		_, _, err = client.Enterprise.GetAuditLog(ctx, "\n", &getOpts)
    84  		return err
    85  	})
    86  
    87  	testNewRequestAndDoFailureCategory(t, methodName, client, AuditLogCategory, func() (*Response, error) {
    88  		got, resp, err := client.Enterprise.GetAuditLog(ctx, "o", &GetAuditLogOptions{})
    89  		if got != nil {
    90  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    91  		}
    92  		return resp, err
    93  	})
    94  }