github.com/google/go-github/v49@v49.1.0/github/orgs_hooks_deliveries_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 14 "github.com/google/go-cmp/cmp" 15 ) 16 17 func TestOrganizationsService_ListHookDeliveries(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 mux.HandleFunc("/orgs/o/hooks/1/deliveries", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 testFormValues(t, r, values{"cursor": "v1_12077215967"}) 24 fmt.Fprint(w, `[{"id":1}, {"id":2}]`) 25 }) 26 27 opt := &ListCursorOptions{Cursor: "v1_12077215967"} 28 29 ctx := context.Background() 30 hooks, _, err := client.Organizations.ListHookDeliveries(ctx, "o", 1, opt) 31 if err != nil { 32 t.Errorf("Organizations.ListHookDeliveries returned error: %v", err) 33 } 34 35 want := []*HookDelivery{{ID: Int64(1)}, {ID: Int64(2)}} 36 if d := cmp.Diff(hooks, want); d != "" { 37 t.Errorf("Organizations.ListHooks want (-), got (+):\n%s", d) 38 } 39 40 const methodName = "ListHookDeliveries" 41 testBadOptions(t, methodName, func() (err error) { 42 _, _, err = client.Organizations.ListHookDeliveries(ctx, "\n", -1, opt) 43 return err 44 }) 45 46 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 47 got, resp, err := client.Organizations.ListHookDeliveries(ctx, "o", 1, opt) 48 if got != nil { 49 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 50 } 51 return resp, err 52 }) 53 } 54 55 func TestOrganizationsService_ListHookDeliveries_invalidOwner(t *testing.T) { 56 client, _, _, teardown := setup() 57 defer teardown() 58 59 ctx := context.Background() 60 _, _, err := client.Organizations.ListHookDeliveries(ctx, "%", 1, nil) 61 testURLParseError(t, err) 62 } 63 64 func TestOrganizationsService_GetHookDelivery(t *testing.T) { 65 client, mux, _, teardown := setup() 66 defer teardown() 67 68 mux.HandleFunc("/orgs/o/hooks/1/deliveries/1", func(w http.ResponseWriter, r *http.Request) { 69 testMethod(t, r, "GET") 70 fmt.Fprint(w, `{"id":1}`) 71 }) 72 73 ctx := context.Background() 74 hook, _, err := client.Organizations.GetHookDelivery(ctx, "o", 1, 1) 75 if err != nil { 76 t.Errorf("Organizations.GetHookDelivery returned error: %v", err) 77 } 78 79 want := &HookDelivery{ID: Int64(1)} 80 if !cmp.Equal(hook, want) { 81 t.Errorf("Organizations.GetHookDelivery returned %+v, want %+v", hook, want) 82 } 83 84 const methodName = "GetHookDelivery" 85 testBadOptions(t, methodName, func() (err error) { 86 _, _, err = client.Organizations.GetHookDelivery(ctx, "\n", -1, -1) 87 return err 88 }) 89 90 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 91 got, resp, err := client.Organizations.GetHookDelivery(ctx, "o", 1, 1) 92 if got != nil { 93 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 94 } 95 return resp, err 96 }) 97 } 98 99 func TestOrganizationsService_GetHookDelivery_invalidOwner(t *testing.T) { 100 client, _, _, teardown := setup() 101 defer teardown() 102 103 ctx := context.Background() 104 _, _, err := client.Organizations.GetHookDelivery(ctx, "%", 1, 1) 105 testURLParseError(t, err) 106 } 107 108 func TestOrganizationsService_RedeliverHookDelivery(t *testing.T) { 109 client, mux, _, teardown := setup() 110 defer teardown() 111 112 mux.HandleFunc("/orgs/o/hooks/1/deliveries/1/attempts", func(w http.ResponseWriter, r *http.Request) { 113 testMethod(t, r, "POST") 114 fmt.Fprint(w, `{"id":1}`) 115 }) 116 117 ctx := context.Background() 118 hook, _, err := client.Organizations.RedeliverHookDelivery(ctx, "o", 1, 1) 119 if err != nil { 120 t.Errorf("Organizations.RedeliverHookDelivery returned error: %v", err) 121 } 122 123 want := &HookDelivery{ID: Int64(1)} 124 if !cmp.Equal(hook, want) { 125 t.Errorf("Organizations.RedeliverHookDelivery returned %+v, want %+v", hook, want) 126 } 127 128 const methodName = "Rede;overHookDelivery" 129 testBadOptions(t, methodName, func() (err error) { 130 _, _, err = client.Organizations.RedeliverHookDelivery(ctx, "\n", -1, -1) 131 return err 132 }) 133 134 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 135 got, resp, err := client.Organizations.RedeliverHookDelivery(ctx, "o", 1, 1) 136 if got != nil { 137 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 138 } 139 return resp, err 140 }) 141 } 142 143 func TestOrganizationsService_RedeliverHookDelivery_invalidOwner(t *testing.T) { 144 client, _, _, teardown := setup() 145 defer teardown() 146 147 ctx := context.Background() 148 _, _, err := client.Organizations.RedeliverHookDelivery(ctx, "%", 1, 1) 149 testURLParseError(t, err) 150 }