|
| 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 | + "reflect" |
| 14 | + "testing" |
| 15 | +) |
| 16 | + |
| 17 | +func TestRepositoriesService_ListPreReceiveHooks(t *testing.T) { |
| 18 | + client, mux, _, teardown := setup() |
| 19 | + defer teardown() |
| 20 | + |
| 21 | + mux.HandleFunc("/repos/o/r/pre-receive-hooks", func(w http.ResponseWriter, r *http.Request) { |
| 22 | + testMethod(t, r, "GET") |
| 23 | + testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview) |
| 24 | + testFormValues(t, r, values{"page": "2"}) |
| 25 | + fmt.Fprint(w, `[{"id":1}, {"id":2}]`) |
| 26 | + }) |
| 27 | + |
| 28 | + opt := &ListOptions{Page: 2} |
| 29 | + |
| 30 | + hooks, _, err := client.Repositories.ListPreReceiveHooks(context.Background(), "o", "r", opt) |
| 31 | + if err != nil { |
| 32 | + t.Errorf("Repositories.ListHooks returned error: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + want := []*PreReceiveHook{{ID: Int64(1)}, {ID: Int64(2)}} |
| 36 | + if !reflect.DeepEqual(hooks, want) { |
| 37 | + t.Errorf("Repositories.ListPreReceiveHooks returned %+v, want %+v", hooks, want) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestRepositoriesService_ListPreReceiveHooks_invalidOwner(t *testing.T) { |
| 42 | + client, _, _, teardown := setup() |
| 43 | + defer teardown() |
| 44 | + |
| 45 | + _, _, err := client.Repositories.ListPreReceiveHooks(context.Background(), "%", "%", nil) |
| 46 | + testURLParseError(t, err) |
| 47 | +} |
| 48 | + |
| 49 | +func TestRepositoriesService_GetPreReceiveHook(t *testing.T) { |
| 50 | + client, mux, _, teardown := setup() |
| 51 | + defer teardown() |
| 52 | + |
| 53 | + mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { |
| 54 | + testMethod(t, r, "GET") |
| 55 | + testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview) |
| 56 | + fmt.Fprint(w, `{"id":1}`) |
| 57 | + }) |
| 58 | + |
| 59 | + hook, _, err := client.Repositories.GetPreReceiveHook(context.Background(), "o", "r", 1) |
| 60 | + if err != nil { |
| 61 | + t.Errorf("Repositories.GetPreReceiveHook returned error: %v", err) |
| 62 | + } |
| 63 | + |
| 64 | + want := &PreReceiveHook{ID: Int64(1)} |
| 65 | + if !reflect.DeepEqual(hook, want) { |
| 66 | + t.Errorf("Repositories.GetPreReceiveHook returned %+v, want %+v", hook, want) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestRepositoriesService_GetPreReceiveHook_invalidOwner(t *testing.T) { |
| 71 | + client, _, _, teardown := setup() |
| 72 | + defer teardown() |
| 73 | + |
| 74 | + _, _, err := client.Repositories.GetPreReceiveHook(context.Background(), "%", "%", 1) |
| 75 | + testURLParseError(t, err) |
| 76 | +} |
| 77 | + |
| 78 | +func TestRepositoriesService_UpdatePreReceiveHook(t *testing.T) { |
| 79 | + client, mux, _, teardown := setup() |
| 80 | + defer teardown() |
| 81 | + |
| 82 | + input := &PreReceiveHook{Name: String("t")} |
| 83 | + |
| 84 | + mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { |
| 85 | + v := new(PreReceiveHook) |
| 86 | + json.NewDecoder(r.Body).Decode(v) |
| 87 | + |
| 88 | + testMethod(t, r, "PATCH") |
| 89 | + if !reflect.DeepEqual(v, input) { |
| 90 | + t.Errorf("Request body = %+v, want %+v", v, input) |
| 91 | + } |
| 92 | + |
| 93 | + fmt.Fprint(w, `{"id":1}`) |
| 94 | + }) |
| 95 | + |
| 96 | + hook, _, err := client.Repositories.UpdatePreReceiveHook(context.Background(), "o", "r", 1, input) |
| 97 | + if err != nil { |
| 98 | + t.Errorf("Repositories.UpdatePreReceiveHook returned error: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + want := &PreReceiveHook{ID: Int64(1)} |
| 102 | + if !reflect.DeepEqual(hook, want) { |
| 103 | + t.Errorf("Repositories.UpdatePreReceiveHook returned %+v, want %+v", hook, want) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestRepositoriesService_PreReceiveHook_invalidOwner(t *testing.T) { |
| 108 | + client, _, _, teardown := setup() |
| 109 | + defer teardown() |
| 110 | + |
| 111 | + _, _, err := client.Repositories.UpdatePreReceiveHook(context.Background(), "%", "%", 1, nil) |
| 112 | + testURLParseError(t, err) |
| 113 | +} |
| 114 | + |
| 115 | +func TestRepositoriesService_DeletePreReceiveHook(t *testing.T) { |
| 116 | + client, mux, _, teardown := setup() |
| 117 | + defer teardown() |
| 118 | + |
| 119 | + mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) { |
| 120 | + testMethod(t, r, "DELETE") |
| 121 | + }) |
| 122 | + |
| 123 | + _, err := client.Repositories.DeletePreReceiveHook(context.Background(), "o", "r", 1) |
| 124 | + if err != nil { |
| 125 | + t.Errorf("Repositories.DeletePreReceiveHook returned error: %v", err) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestRepositoriesService_DeletePreReceiveHook_invalidOwner(t *testing.T) { |
| 130 | + client, _, _, teardown := setup() |
| 131 | + defer teardown() |
| 132 | + |
| 133 | + _, err := client.Repositories.DeletePreReceiveHook(context.Background(), "%", "%", 1) |
| 134 | + testURLParseError(t, err) |
| 135 | +} |
0 commit comments