github.com/google/go-github/v49@v49.1.0/github/repos_prereceive_hooks_test.go (about) 1 // Copyright 2018 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 "encoding/json" 11 "fmt" 12 "net/http" 13 "testing" 14 15 "github.com/google/go-cmp/cmp" 16 ) 17 18 func TestRepositoriesService_ListPreReceiveHooks(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/repos/o/r/pre-receive-hooks", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview) 25 testFormValues(t, r, values{"page": "2"}) 26 fmt.Fprint(w, `[{"id":1}, {"id":2}]`) 27 }) 28 29 opt := &ListOptions{Page: 2} 30 31 ctx := context.Background() 32 hooks, _, err := client.Repositories.ListPreReceiveHooks(ctx, "o", "r", opt) 33 if err != nil { 34 t.Errorf("Repositories.ListHooks returned error: %v", err) 35 } 36 37 want := []*PreReceiveHook{{ID: Int64(1)}, {ID: Int64(2)}} 38 if !cmp.Equal(hooks, want) { 39 t.Errorf("Repositories.ListPreReceiveHooks returned %+v, want %+v", hooks, want) 40 } 41 42 const methodName = "ListPreReceiveHooks" 43 testBadOptions(t, methodName, func() (err error) { 44 _, _, err = client.Repositories.ListPreReceiveHooks(ctx, "\n", "\n", opt) 45 return err 46 }) 47 48 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 49 got, resp, err := client.Repositories.ListPreReceiveHooks(ctx, "o", "r", opt) 50 if got != nil { 51 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 52 } 53 return resp, err 54 }) 55 } 56 57 func TestRepositoriesService_ListPreReceiveHooks_invalidOwner(t *testing.T) { 58 client, _, _, teardown := setup() 59 defer teardown() 60 61 ctx := context.Background() 62 _, _, err := client.Repositories.ListPreReceiveHooks(ctx, "%", "%", nil) 63 testURLParseError(t, err) 64 } 65 66 func TestRepositoriesService_GetPreReceiveHook(t *testing.T) { 67 client, mux, _, teardown := setup() 68 defer teardown() 69 70 mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { 71 testMethod(t, r, "GET") 72 testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview) 73 fmt.Fprint(w, `{"id":1}`) 74 }) 75 76 ctx := context.Background() 77 hook, _, err := client.Repositories.GetPreReceiveHook(ctx, "o", "r", 1) 78 if err != nil { 79 t.Errorf("Repositories.GetPreReceiveHook returned error: %v", err) 80 } 81 82 want := &PreReceiveHook{ID: Int64(1)} 83 if !cmp.Equal(hook, want) { 84 t.Errorf("Repositories.GetPreReceiveHook returned %+v, want %+v", hook, want) 85 } 86 87 const methodName = "GetPreReceiveHook" 88 testBadOptions(t, methodName, func() (err error) { 89 _, _, err = client.Repositories.GetPreReceiveHook(ctx, "\n", "\n", -1) 90 return err 91 }) 92 93 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 94 got, resp, err := client.Repositories.GetPreReceiveHook(ctx, "o", "r", 1) 95 if got != nil { 96 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 97 } 98 return resp, err 99 }) 100 } 101 102 func TestRepositoriesService_GetPreReceiveHook_invalidOwner(t *testing.T) { 103 client, _, _, teardown := setup() 104 defer teardown() 105 106 ctx := context.Background() 107 _, _, err := client.Repositories.GetPreReceiveHook(ctx, "%", "%", 1) 108 testURLParseError(t, err) 109 } 110 111 func TestRepositoriesService_UpdatePreReceiveHook(t *testing.T) { 112 client, mux, _, teardown := setup() 113 defer teardown() 114 115 input := &PreReceiveHook{} 116 117 mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { 118 v := new(PreReceiveHook) 119 json.NewDecoder(r.Body).Decode(v) 120 121 testMethod(t, r, "PATCH") 122 if !cmp.Equal(v, input) { 123 t.Errorf("Request body = %+v, want %+v", v, input) 124 } 125 126 fmt.Fprint(w, `{"id":1}`) 127 }) 128 129 ctx := context.Background() 130 hook, _, err := client.Repositories.UpdatePreReceiveHook(ctx, "o", "r", 1, input) 131 if err != nil { 132 t.Errorf("Repositories.UpdatePreReceiveHook returned error: %v", err) 133 } 134 135 want := &PreReceiveHook{ID: Int64(1)} 136 if !cmp.Equal(hook, want) { 137 t.Errorf("Repositories.UpdatePreReceiveHook returned %+v, want %+v", hook, want) 138 } 139 140 const methodName = "UpdatePreReceiveHook" 141 testBadOptions(t, methodName, func() (err error) { 142 _, _, err = client.Repositories.UpdatePreReceiveHook(ctx, "\n", "\n", -1, input) 143 return err 144 }) 145 146 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 147 got, resp, err := client.Repositories.UpdatePreReceiveHook(ctx, "o", "r", 1, input) 148 if got != nil { 149 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 150 } 151 return resp, err 152 }) 153 } 154 155 func TestRepositoriesService_PreReceiveHook_invalidOwner(t *testing.T) { 156 client, _, _, teardown := setup() 157 defer teardown() 158 159 ctx := context.Background() 160 _, _, err := client.Repositories.UpdatePreReceiveHook(ctx, "%", "%", 1, nil) 161 testURLParseError(t, err) 162 } 163 164 func TestRepositoriesService_DeletePreReceiveHook(t *testing.T) { 165 client, mux, _, teardown := setup() 166 defer teardown() 167 168 mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { 169 testMethod(t, r, "DELETE") 170 }) 171 172 ctx := context.Background() 173 _, err := client.Repositories.DeletePreReceiveHook(ctx, "o", "r", 1) 174 if err != nil { 175 t.Errorf("Repositories.DeletePreReceiveHook returned error: %v", err) 176 } 177 178 const methodName = "DeletePreReceiveHook" 179 testBadOptions(t, methodName, func() (err error) { 180 _, err = client.Repositories.DeletePreReceiveHook(ctx, "\n", "\n", -1) 181 return err 182 }) 183 184 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 185 return client.Repositories.DeletePreReceiveHook(ctx, "o", "r", 1) 186 }) 187 } 188 189 func TestRepositoriesService_DeletePreReceiveHook_invalidOwner(t *testing.T) { 190 client, _, _, teardown := setup() 191 defer teardown() 192 193 ctx := context.Background() 194 _, err := client.Repositories.DeletePreReceiveHook(ctx, "%", "%", 1) 195 testURLParseError(t, err) 196 } 197 198 func TestPreReceiveHook_Marshal(t *testing.T) { 199 testJSONMarshal(t, &PreReceiveHook{}, "{}") 200 201 u := &PreReceiveHook{ 202 ID: Int64(1), 203 Name: String("name"), 204 Enforcement: String("e"), 205 ConfigURL: String("curl"), 206 } 207 208 want := `{ 209 "id": 1, 210 "name": "name", 211 "enforcement": "e", 212 "configuration_url": "curl" 213 }` 214 215 testJSONMarshal(t, u, want) 216 }