1// Copyright 2015 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 6package github 7 8import ( 9 "context" 10 "encoding/json" 11 "fmt" 12 "net/http" 13 "reflect" 14 "testing" 15) 16 17func TestOrganizationsService_ListHooks(t *testing.T) { 18 client, mux, _, teardown := setup() 19 defer teardown() 20 21 mux.HandleFunc("/orgs/o/hooks", func(w http.ResponseWriter, r *http.Request) { 22 testMethod(t, r, "GET") 23 testFormValues(t, r, values{"page": "2"}) 24 fmt.Fprint(w, `[{"id":1}, {"id":2}]`) 25 }) 26 27 opt := &ListOptions{Page: 2} 28 29 hooks, _, err := client.Organizations.ListHooks(context.Background(), "o", opt) 30 if err != nil { 31 t.Errorf("Organizations.ListHooks returned error: %v", err) 32 } 33 34 want := []*Hook{{ID: Int64(1)}, {ID: Int64(2)}} 35 if !reflect.DeepEqual(hooks, want) { 36 t.Errorf("Organizations.ListHooks returned %+v, want %+v", hooks, want) 37 } 38} 39 40func TestOrganizationsService_ListHooks_invalidOrg(t *testing.T) { 41 client, _, _, teardown := setup() 42 defer teardown() 43 44 _, _, err := client.Organizations.ListHooks(context.Background(), "%", nil) 45 testURLParseError(t, err) 46} 47 48func TestOrganizationsService_GetHook(t *testing.T) { 49 client, mux, _, teardown := setup() 50 defer teardown() 51 52 mux.HandleFunc("/orgs/o/hooks/1", func(w http.ResponseWriter, r *http.Request) { 53 testMethod(t, r, "GET") 54 fmt.Fprint(w, `{"id":1}`) 55 }) 56 57 hook, _, err := client.Organizations.GetHook(context.Background(), "o", 1) 58 if err != nil { 59 t.Errorf("Organizations.GetHook returned error: %v", err) 60 } 61 62 want := &Hook{ID: Int64(1)} 63 if !reflect.DeepEqual(hook, want) { 64 t.Errorf("Organizations.GetHook returned %+v, want %+v", hook, want) 65 } 66} 67 68func TestOrganizationsService_GetHook_invalidOrg(t *testing.T) { 69 client, _, _, teardown := setup() 70 defer teardown() 71 72 _, _, err := client.Organizations.GetHook(context.Background(), "%", 1) 73 testURLParseError(t, err) 74} 75 76func TestOrganizationsService_EditHook(t *testing.T) { 77 client, mux, _, teardown := setup() 78 defer teardown() 79 80 input := &Hook{Name: String("t")} 81 82 mux.HandleFunc("/orgs/o/hooks/1", func(w http.ResponseWriter, r *http.Request) { 83 v := new(Hook) 84 json.NewDecoder(r.Body).Decode(v) 85 86 testMethod(t, r, "PATCH") 87 if !reflect.DeepEqual(v, input) { 88 t.Errorf("Request body = %+v, want %+v", v, input) 89 } 90 91 fmt.Fprint(w, `{"id":1}`) 92 }) 93 94 hook, _, err := client.Organizations.EditHook(context.Background(), "o", 1, input) 95 if err != nil { 96 t.Errorf("Organizations.EditHook returned error: %v", err) 97 } 98 99 want := &Hook{ID: Int64(1)} 100 if !reflect.DeepEqual(hook, want) { 101 t.Errorf("Organizations.EditHook returned %+v, want %+v", hook, want) 102 } 103} 104 105func TestOrganizationsService_EditHook_invalidOrg(t *testing.T) { 106 client, _, _, teardown := setup() 107 defer teardown() 108 109 _, _, err := client.Organizations.EditHook(context.Background(), "%", 1, nil) 110 testURLParseError(t, err) 111} 112 113func TestOrganizationsService_PingHook(t *testing.T) { 114 client, mux, _, teardown := setup() 115 defer teardown() 116 117 mux.HandleFunc("/orgs/o/hooks/1/pings", func(w http.ResponseWriter, r *http.Request) { 118 testMethod(t, r, "POST") 119 }) 120 121 _, err := client.Organizations.PingHook(context.Background(), "o", 1) 122 if err != nil { 123 t.Errorf("Organizations.PingHook returned error: %v", err) 124 } 125} 126 127func TestOrganizationsService_DeleteHook(t *testing.T) { 128 client, mux, _, teardown := setup() 129 defer teardown() 130 131 mux.HandleFunc("/orgs/o/hooks/1", func(w http.ResponseWriter, r *http.Request) { 132 testMethod(t, r, "DELETE") 133 }) 134 135 _, err := client.Organizations.DeleteHook(context.Background(), "o", 1) 136 if err != nil { 137 t.Errorf("Organizations.DeleteHook returned error: %v", err) 138 } 139} 140 141func TestOrganizationsService_DeleteHook_invalidOrg(t *testing.T) { 142 client, _, _, teardown := setup() 143 defer teardown() 144 145 _, err := client.Organizations.DeleteHook(context.Background(), "%", 1) 146 testURLParseError(t, err) 147} 148