github.com/google/go-github/v49@v49.1.0/github/dependabot_alerts_test.go (about) 1 // Copyright 2022 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 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestDependabotService_ListRepoAlerts(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 mux.HandleFunc("/repos/o/r/dependabot/alerts", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 testFormValues(t, r, values{"state": "open"}) 24 fmt.Fprint(w, `[{"number":1,"state":"open"},{"number":42,"state":"fixed"}]`) 25 }) 26 27 opts := &ListAlertsOptions{State: String("open")} 28 ctx := context.Background() 29 alerts, _, err := client.Dependabot.ListRepoAlerts(ctx, "o", "r", opts) 30 if err != nil { 31 t.Errorf("Dependabot.ListRepoAlerts returned error: %v", err) 32 } 33 34 want := []*DependabotAlert{ 35 {Number: Int(1), State: String("open")}, 36 {Number: Int(42), State: String("fixed")}, 37 } 38 if !cmp.Equal(alerts, want) { 39 t.Errorf("Dependabot.ListRepoAlerts returned %+v, want %+v", alerts, want) 40 } 41 42 const methodName = "ListRepoAlerts" 43 testBadOptions(t, methodName, func() (err error) { 44 _, _, err = client.Dependabot.ListRepoAlerts(ctx, "\n", "\n", opts) 45 return err 46 }) 47 48 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 49 got, resp, err := client.Dependabot.ListRepoAlerts(ctx, "o", "r", opts) 50 if got != nil { 51 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 52 } 53 return resp, err 54 }) 55 } 56 57 func TestDependabotService_GetRepoAlert(t *testing.T) { 58 client, mux, _, teardown := setup() 59 defer teardown() 60 61 mux.HandleFunc("/repos/o/r/dependabot/alerts/42", func(w http.ResponseWriter, r *http.Request) { 62 testMethod(t, r, "GET") 63 fmt.Fprint(w, `{"number":42,"state":"fixed"}`) 64 }) 65 66 ctx := context.Background() 67 alert, _, err := client.Dependabot.GetRepoAlert(ctx, "o", "r", 42) 68 if err != nil { 69 t.Errorf("Dependabot.GetRepoAlert returned error: %v", err) 70 } 71 72 want := &DependabotAlert{ 73 Number: Int(42), 74 State: String("fixed"), 75 } 76 if !cmp.Equal(alert, want) { 77 t.Errorf("Dependabot.GetRepoAlert returned %+v, want %+v", alert, want) 78 } 79 80 const methodName = "GetRepoAlert" 81 testBadOptions(t, methodName, func() (err error) { 82 _, _, err = client.Dependabot.GetRepoAlert(ctx, "\n", "\n", 0) 83 return err 84 }) 85 86 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 87 got, resp, err := client.Dependabot.GetRepoAlert(ctx, "o", "r", 42) 88 if got != nil { 89 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 90 } 91 return resp, err 92 }) 93 } 94 95 func TestDependabotService_ListOrgAlerts(t *testing.T) { 96 client, mux, _, teardown := setup() 97 defer teardown() 98 99 mux.HandleFunc("/orgs/o/dependabot/alerts", func(w http.ResponseWriter, r *http.Request) { 100 testMethod(t, r, "GET") 101 testFormValues(t, r, values{"state": "open"}) 102 fmt.Fprint(w, `[{"number":1,"state":"open"},{"number":42,"state":"fixed"}]`) 103 }) 104 105 opts := &ListAlertsOptions{State: String("open")} 106 ctx := context.Background() 107 alerts, _, err := client.Dependabot.ListOrgAlerts(ctx, "o", opts) 108 if err != nil { 109 t.Errorf("Dependabot.ListOrgAlerts returned error: %v", err) 110 } 111 112 want := []*DependabotAlert{ 113 {Number: Int(1), State: String("open")}, 114 {Number: Int(42), State: String("fixed")}, 115 } 116 if !cmp.Equal(alerts, want) { 117 t.Errorf("Dependabot.ListOrgAlerts returned %+v, want %+v", alerts, want) 118 } 119 120 const methodName = "ListOrgAlerts" 121 testBadOptions(t, methodName, func() (err error) { 122 _, _, err = client.Dependabot.ListOrgAlerts(ctx, "\n", opts) 123 return err 124 }) 125 126 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 127 got, resp, err := client.Dependabot.ListOrgAlerts(ctx, "o", opts) 128 if got != nil { 129 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 130 } 131 return resp, err 132 }) 133 }