github.com/google/go-github/v49@v49.1.0/github/repos_statuses_test.go (about) 1 // Copyright 2013 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_ListStatuses(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/repos/o/r/commits/r/statuses", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testFormValues(t, r, values{"page": "2"}) 25 fmt.Fprint(w, `[{"id":1}]`) 26 }) 27 28 opt := &ListOptions{Page: 2} 29 ctx := context.Background() 30 statuses, _, err := client.Repositories.ListStatuses(ctx, "o", "r", "r", opt) 31 if err != nil { 32 t.Errorf("Repositories.ListStatuses returned error: %v", err) 33 } 34 35 want := []*RepoStatus{{ID: Int64(1)}} 36 if !cmp.Equal(statuses, want) { 37 t.Errorf("Repositories.ListStatuses returned %+v, want %+v", statuses, want) 38 } 39 40 const methodName = "ListStatuses" 41 testBadOptions(t, methodName, func() (err error) { 42 _, _, err = client.Repositories.ListStatuses(ctx, "\n", "\n", "\n", opt) 43 return err 44 }) 45 46 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 47 got, resp, err := client.Repositories.ListStatuses(ctx, "o", "r", "r", 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 TestRepositoriesService_ListStatuses_invalidOwner(t *testing.T) { 56 client, _, _, teardown := setup() 57 defer teardown() 58 59 ctx := context.Background() 60 _, _, err := client.Repositories.ListStatuses(ctx, "%", "r", "r", nil) 61 testURLParseError(t, err) 62 } 63 64 func TestRepositoriesService_CreateStatus(t *testing.T) { 65 client, mux, _, teardown := setup() 66 defer teardown() 67 68 input := &RepoStatus{State: String("s"), TargetURL: String("t"), Description: String("d")} 69 70 mux.HandleFunc("/repos/o/r/statuses/r", func(w http.ResponseWriter, r *http.Request) { 71 v := new(RepoStatus) 72 json.NewDecoder(r.Body).Decode(v) 73 74 testMethod(t, r, "POST") 75 if !cmp.Equal(v, input) { 76 t.Errorf("Request body = %+v, want %+v", v, input) 77 } 78 fmt.Fprint(w, `{"id":1}`) 79 }) 80 81 ctx := context.Background() 82 status, _, err := client.Repositories.CreateStatus(ctx, "o", "r", "r", input) 83 if err != nil { 84 t.Errorf("Repositories.CreateStatus returned error: %v", err) 85 } 86 87 want := &RepoStatus{ID: Int64(1)} 88 if !cmp.Equal(status, want) { 89 t.Errorf("Repositories.CreateStatus returned %+v, want %+v", status, want) 90 } 91 92 const methodName = "CreateStatus" 93 testBadOptions(t, methodName, func() (err error) { 94 _, _, err = client.Repositories.CreateStatus(ctx, "\n", "\n", "\n", input) 95 return err 96 }) 97 98 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 99 got, resp, err := client.Repositories.CreateStatus(ctx, "o", "r", "r", input) 100 if got != nil { 101 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 102 } 103 return resp, err 104 }) 105 } 106 107 func TestRepositoriesService_CreateStatus_invalidOwner(t *testing.T) { 108 client, _, _, teardown := setup() 109 defer teardown() 110 111 ctx := context.Background() 112 _, _, err := client.Repositories.CreateStatus(ctx, "%", "r", "r", nil) 113 testURLParseError(t, err) 114 } 115 116 func TestRepositoriesService_GetCombinedStatus(t *testing.T) { 117 client, mux, _, teardown := setup() 118 defer teardown() 119 120 mux.HandleFunc("/repos/o/r/commits/r/status", func(w http.ResponseWriter, r *http.Request) { 121 testMethod(t, r, "GET") 122 testFormValues(t, r, values{"page": "2"}) 123 fmt.Fprint(w, `{"state":"success", "statuses":[{"id":1}]}`) 124 }) 125 126 opt := &ListOptions{Page: 2} 127 ctx := context.Background() 128 status, _, err := client.Repositories.GetCombinedStatus(ctx, "o", "r", "r", opt) 129 if err != nil { 130 t.Errorf("Repositories.GetCombinedStatus returned error: %v", err) 131 } 132 133 want := &CombinedStatus{State: String("success"), Statuses: []*RepoStatus{{ID: Int64(1)}}} 134 if !cmp.Equal(status, want) { 135 t.Errorf("Repositories.GetCombinedStatus returned %+v, want %+v", status, want) 136 } 137 138 const methodName = "GetCombinedStatus" 139 testBadOptions(t, methodName, func() (err error) { 140 _, _, err = client.Repositories.GetCombinedStatus(ctx, "\n", "\n", "\n", opt) 141 return err 142 }) 143 144 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { 145 got, resp, err := client.Repositories.GetCombinedStatus(ctx, "o", "r", "r", opt) 146 if got != nil { 147 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) 148 } 149 return resp, err 150 }) 151 } 152 153 func TestRepoStatus_Marshal(t *testing.T) { 154 testJSONMarshal(t, &RepoStatus{}, "{}") 155 156 u := &RepoStatus{ 157 ID: Int64(1), 158 NodeID: String("nid"), 159 URL: String("url"), 160 State: String("state"), 161 TargetURL: String("turl"), 162 Description: String("desc"), 163 Context: String("ctx"), 164 AvatarURL: String("aurl"), 165 Creator: &User{ID: Int64(1)}, 166 CreatedAt: &referenceTime, 167 UpdatedAt: &referenceTime, 168 } 169 170 want := `{ 171 "id": 1, 172 "node_id": "nid", 173 "url": "url", 174 "state": "state", 175 "target_url": "turl", 176 "description": "desc", 177 "context": "ctx", 178 "avatar_url": "aurl", 179 "creator": { 180 "id": 1 181 }, 182 "created_at": ` + referenceTimeStr + `, 183 "updated_at": ` + referenceTimeStr + ` 184 }` 185 186 testJSONMarshal(t, u, want) 187 } 188 189 func TestCombinedStatus_Marshal(t *testing.T) { 190 testJSONMarshal(t, &CombinedStatus{}, "{}") 191 192 u := &CombinedStatus{ 193 State: String("state"), 194 Name: String("name"), 195 SHA: String("sha"), 196 TotalCount: Int(1), 197 Statuses: []*RepoStatus{ 198 { 199 ID: Int64(1), 200 NodeID: String("nid"), 201 URL: String("url"), 202 State: String("state"), 203 TargetURL: String("turl"), 204 Description: String("desc"), 205 Context: String("ctx"), 206 AvatarURL: String("aurl"), 207 Creator: &User{ID: Int64(1)}, 208 CreatedAt: &referenceTime, 209 UpdatedAt: &referenceTime, 210 }, 211 }, 212 CommitURL: String("curl"), 213 RepositoryURL: String("rurl"), 214 } 215 216 want := `{ 217 "state": "state", 218 "name": "name", 219 "sha": "sha", 220 "total_count": 1, 221 "statuses": [ 222 { 223 "id": 1, 224 "node_id": "nid", 225 "url": "url", 226 "state": "state", 227 "target_url": "turl", 228 "description": "desc", 229 "context": "ctx", 230 "avatar_url": "aurl", 231 "creator": { 232 "id": 1 233 }, 234 "created_at": ` + referenceTimeStr + `, 235 "updated_at": ` + referenceTimeStr + ` 236 } 237 ], 238 "commit_url": "curl", 239 "repository_url": "rurl" 240 }` 241 242 testJSONMarshal(t, u, want) 243 }