Debian Codegraph @

github/repos_tags_test.go

raw
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 "encoding/json"
11 "fmt"
12 "net/http"
13 "testing"
14
15 "github.com/google/go-cmp/cmp"
16 )
17
18 func TestRepositoriesService_ListTagProtection(t *testing.T) {
19 client, mux, _, teardown := setup()
20 defer teardown()
21
22 mux.HandleFunc("/repos/o/r/tags/protection", func(w http.ResponseWriter, r *http.Request) {
23 testMethod(t, r, "GET")
24
25 fmt.Fprint(w, `[{"id":1, "pattern":"tag1"},{"id":2, "pattern":"tag2"}]`)
26 })
27
28 ctx := context.Background()
29 tagProtections, _, err := client.Repositories.ListTagProtection(ctx, "o", "r")
30 if err != nil {
31 t.Errorf("Repositories.ListTagProtection returned error: %v", err)
32 }
33
34 want := []*TagProtection{{ID: Int64(1), Pattern: String("tag1")}, {ID: Int64(2), Pattern: String("tag2")}}
35 if !cmp.Equal(tagProtections, want) {
36 t.Errorf("Repositories.ListTagProtection returned %+v, want %+v", tagProtections, want)
37 }
38
39 const methodName = "ListTagProtection"
40 testBadOptions(t, methodName, func() (err error) {
41 _, _, err = client.Repositories.ListTagProtection(ctx, "\n", "\n")
42 return err
43 })
44
45 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
46 got, resp, err := client.Repositories.ListTagProtection(ctx, "o", "r")
47 if got != nil {
48 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
49 }
50 return resp, err
51 })
52 }
53
54 func TestRepositoriesService_ListTagProtection_invalidOwner(t *testing.T) {
55 client, _, _, teardown := setup()
56 defer teardown()
57
58 ctx := context.Background()
59 _, _, err := client.Repositories.ListTagProtection(ctx, "%", "r")
60 testURLParseError(t, err)
61 }
62
63 func TestRepositoriesService_CreateTagProtection(t *testing.T) {
64 client, mux, _, teardown := setup()
65 defer teardown()
66
67 pattern := "tag*"
68
69 mux.HandleFunc("/repos/o/r/tags/protection", func(w http.ResponseWriter, r *http.Request) {
70 v := new(tagProtectionRequest)
71 assertNilError(t, json.NewDecoder(r.Body).Decode(v))
72
73 testMethod(t, r, "POST")
74 want := &tagProtectionRequest{Pattern: "tag*"}
75 if !cmp.Equal(v, want) {
76 t.Errorf("Request body = %+v, want %+v", v, want)
77 }
78
79 fmt.Fprint(w, `{"id":1,"pattern":"tag*"}`)
80 })
81
82 ctx := context.Background()
83 got, _, err := client.Repositories.CreateTagProtection(ctx, "o", "r", pattern)
84 if err != nil {
85 t.Errorf("Repositories.CreateTagProtection returned error: %v", err)
86 }
87
88 want := &TagProtection{ID: Int64(1), Pattern: String("tag*")}
89 if !cmp.Equal(got, want) {
90 t.Errorf("Repositories.CreateTagProtection returned %+v, want %+v", got, want)
91 }
92
93 const methodName = "CreateTagProtection"
94 testBadOptions(t, methodName, func() (err error) {
95 _, _, err = client.Repositories.CreateTagProtection(ctx, "\n", "\n", pattern)
96 return err
97 })
98
99 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
100 got, resp, err := client.Repositories.CreateTagProtection(ctx, "o", "r", pattern)
101 if got != nil {
102 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
103 }
104 return resp, err
105 })
106 }
107
108 func TestRepositoriesService_DeleteTagProtection(t *testing.T) {
109 client, mux, _, teardown := setup()
110 defer teardown()
111
112 mux.HandleFunc("/repos/o/r/tags/protection/1", func(w http.ResponseWriter, r *http.Request) {
113 testMethod(t, r, "DELETE")
114 w.WriteHeader(http.StatusNoContent)
115 })
116
117 ctx := context.Background()
118 _, err := client.Repositories.DeleteTagProtection(ctx, "o", "r", 1)
119 if err != nil {
120 t.Errorf("Repositories.DeleteTagProtection returned error: %v", err)
121 }
122
123 const methodName = "DeleteTagProtection"
124 testBadOptions(t, methodName, func() (err error) {
125 _, err = client.Repositories.DeleteTagProtection(ctx, "\n", "\n", 1)
126 return err
127 })
128
129 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
130 return client.Repositories.DeleteTagProtection(ctx, "o", "r", 1)
131 })
132 }
133
134 func TestTagProtection_Marshal(t *testing.T) {
135 testJSONMarshal(t, &TagProtection{}, "{}")
136
137 u := &TagProtection{
138 ID: Int64(1),
139 Pattern: String("pattern"),
140 }
141
142 want := `{
143 "id": 1,
144 "pattern": "pattern"
145 }`
146
147 testJSONMarshal(t, u, want)
148 }