github.com/google/go-github/v33@v33.0.0/github/code-scanning_test.go (about)

     1  // Copyright 2020 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  	"reflect"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func TestActionsService_Alert_ID(t *testing.T) {
    18  	// Test: nil Alert ID == 0
    19  	var a *Alert
    20  	id := a.ID()
    21  	var want int64
    22  	if id != want {
    23  		t.Errorf("Alert.ID error returned %+v, want %+v", id, want)
    24  	}
    25  
    26  	// Test: Valid HTMLURL
    27  	a = &Alert{
    28  		HTMLURL: String("https://github.com/o/r/security/code-scanning/88"),
    29  	}
    30  	id = a.ID()
    31  	want = 88
    32  	if !reflect.DeepEqual(id, want) {
    33  		t.Errorf("Alert.ID error returned %+v, want %+v", id, want)
    34  	}
    35  
    36  	// Test: HTMLURL is nil
    37  	a = &Alert{}
    38  	id = a.ID()
    39  	want = 0
    40  	if !reflect.DeepEqual(id, want) {
    41  		t.Errorf("Alert.ID error returned %+v, want %+v", id, want)
    42  	}
    43  
    44  	// Test: ID can't be parsed as an int
    45  	a = &Alert{
    46  		HTMLURL: String("https://github.com/o/r/security/code-scanning/bad88"),
    47  	}
    48  	id = a.ID()
    49  	want = 0
    50  	if !reflect.DeepEqual(id, want) {
    51  		t.Errorf("Alert.ID error returned %+v, want %+v", id, want)
    52  	}
    53  }
    54  
    55  func TestActionsService_ListAlertsForRepo(t *testing.T) {
    56  	client, mux, _, teardown := setup()
    57  	defer teardown()
    58  
    59  	mux.HandleFunc("/repos/o/r/code-scanning/alerts", func(w http.ResponseWriter, r *http.Request) {
    60  		testMethod(t, r, "GET")
    61  		testFormValues(t, r, values{"state": "open", "ref": "heads/master"})
    62  		fmt.Fprint(w, `[{
    63  				"rule_id":"js/trivial-conditional",
    64  				"rule_severity":"warning",
    65  				"rule_description":"Useless conditional",
    66  				"tool":"CodeQL",
    67  				"created_at":"2020-05-06T12:00:00Z",
    68  				"open":true,
    69  				"closed_by":null,
    70  				"closed_at":null,
    71  				"url":"https://api.github.com/repos/o/r/code-scanning/alerts/25",
    72  				"html_url":"https://github.com/o/r/security/code-scanning/25"
    73  				},
    74  				{
    75  				"rule_id":"js/useless-expression",
    76  				"rule_severity":"warning",
    77  				"rule_description":"Expression has no effect",
    78  				"tool":"CodeQL",
    79  				"created_at":"2020-05-06T12:00:00Z",
    80  				"open":true,
    81  				"closed_by":null,
    82  				"closed_at":null,
    83  				"url":"https://api.github.com/repos/o/r/code-scanning/alerts/88",
    84  				"html_url":"https://github.com/o/r/security/code-scanning/88"
    85  				}]`)
    86  	})
    87  
    88  	opts := &AlertListOptions{State: "open", Ref: "heads/master"}
    89  	alerts, _, err := client.CodeScanning.ListAlertsForRepo(context.Background(), "o", "r", opts)
    90  	if err != nil {
    91  		t.Errorf("CodeScanning.ListAlertsForRepo returned error: %v", err)
    92  	}
    93  
    94  	date := Timestamp{time.Date(2020, time.May, 06, 12, 00, 00, 0, time.UTC)}
    95  	want := []*Alert{
    96  		{
    97  			RuleID:          String("js/trivial-conditional"),
    98  			RuleSeverity:    String("warning"),
    99  			RuleDescription: String("Useless conditional"),
   100  			Tool:            String("CodeQL"),
   101  			CreatedAt:       &date,
   102  			Open:            Bool(true),
   103  			ClosedBy:        nil,
   104  			ClosedAt:        nil,
   105  			URL:             String("https://api.github.com/repos/o/r/code-scanning/alerts/25"),
   106  			HTMLURL:         String("https://github.com/o/r/security/code-scanning/25"),
   107  		},
   108  		{
   109  			RuleID:          String("js/useless-expression"),
   110  			RuleSeverity:    String("warning"),
   111  			RuleDescription: String("Expression has no effect"),
   112  			Tool:            String("CodeQL"),
   113  			CreatedAt:       &date,
   114  			Open:            Bool(true),
   115  			ClosedBy:        nil,
   116  			ClosedAt:        nil,
   117  			URL:             String("https://api.github.com/repos/o/r/code-scanning/alerts/88"),
   118  			HTMLURL:         String("https://github.com/o/r/security/code-scanning/88"),
   119  		},
   120  	}
   121  	if !reflect.DeepEqual(alerts, want) {
   122  		t.Errorf("CodeScanning.ListAlertsForRepo returned %+v, want %+v", alerts, want)
   123  	}
   124  }
   125  
   126  func TestActionsService_GetAlert(t *testing.T) {
   127  	client, mux, _, teardown := setup()
   128  	defer teardown()
   129  
   130  	mux.HandleFunc("/repos/o/r/code-scanning/alerts/88", func(w http.ResponseWriter, r *http.Request) {
   131  		testMethod(t, r, "GET")
   132  		fmt.Fprint(w, `{"rule_id":"js/useless-expression",
   133  				"rule_severity":"warning",
   134  				"rule_description":"Expression has no effect",
   135  				"tool":"CodeQL",
   136  				"created_at":"2019-01-02T15:04:05Z",
   137  				"open":true,
   138  				"closed_by":null,
   139  				"closed_at":null,
   140  				"url":"https://api.github.com/repos/o/r/code-scanning/alerts/88",
   141  				"html_url":"https://github.com/o/r/security/code-scanning/88"}`)
   142  	})
   143  
   144  	alert, _, err := client.CodeScanning.GetAlert(context.Background(), "o", "r", 88)
   145  	if err != nil {
   146  		t.Errorf("CodeScanning.GetAlert returned error: %v", err)
   147  	}
   148  
   149  	date := Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}
   150  	want := &Alert{
   151  		RuleID:          String("js/useless-expression"),
   152  		RuleSeverity:    String("warning"),
   153  		RuleDescription: String("Expression has no effect"),
   154  		Tool:            String("CodeQL"),
   155  		CreatedAt:       &date,
   156  		Open:            Bool(true),
   157  		ClosedBy:        nil,
   158  		ClosedAt:        nil,
   159  		URL:             String("https://api.github.com/repos/o/r/code-scanning/alerts/88"),
   160  		HTMLURL:         String("https://github.com/o/r/security/code-scanning/88"),
   161  	}
   162  	if !reflect.DeepEqual(alert, want) {
   163  		t.Errorf("CodeScanning.GetAlert returned %+v, want %+v", alert, want)
   164  	}
   165  }