1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "encoding/json"
11 "fmt"
12 "net/http"
13 "testing"
14 "time"
15
16 "github.com/google/go-cmp/cmp"
17 )
18
19 func TestActionsService_ListWorkflows(t *testing.T) {
20 client, mux, _, teardown := setup()
21 defer teardown()
22
23 mux.HandleFunc("/repos/o/r/actions/workflows", func(w http.ResponseWriter, r *http.Request) {
24 testMethod(t, r, "GET")
25 testFormValues(t, r, values{"per_page": "2", "page": "2"})
26 fmt.Fprint(w, `{"total_count":4,"workflows":[{"id":72844,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"id":72845,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`)
27 })
28
29 opts := &ListOptions{Page: 2, PerPage: 2}
30 ctx := context.Background()
31 workflows, _, err := client.Actions.ListWorkflows(ctx, "o", "r", opts)
32 if err != nil {
33 t.Errorf("Actions.ListWorkflows returned error: %v", err)
34 }
35
36 want := &Workflows{
37 TotalCount: Int(4),
38 Workflows: []*Workflow{
39 {ID: Int64(72844), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
40 {ID: Int64(72845), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}},
41 },
42 }
43 if !cmp.Equal(workflows, want) {
44 t.Errorf("Actions.ListWorkflows returned %+v, want %+v", workflows, want)
45 }
46
47 const methodName = "ListWorkflows"
48 testBadOptions(t, methodName, func() (err error) {
49 _, _, err = client.Actions.ListWorkflows(ctx, "\n", "\n", opts)
50 return err
51 })
52
53 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
54 got, resp, err := client.Actions.ListWorkflows(ctx, "o", "r", opts)
55 if got != nil {
56 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
57 }
58 return resp, err
59 })
60 }
61
62 func TestActionsService_GetWorkflowByID(t *testing.T) {
63 client, mux, _, teardown := setup()
64 defer teardown()
65
66 mux.HandleFunc("/repos/o/r/actions/workflows/72844", func(w http.ResponseWriter, r *http.Request) {
67 testMethod(t, r, "GET")
68 fmt.Fprint(w, `{"id":72844,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`)
69 })
70
71 ctx := context.Background()
72 workflow, _, err := client.Actions.GetWorkflowByID(ctx, "o", "r", 72844)
73 if err != nil {
74 t.Errorf("Actions.GetWorkflowByID returned error: %v", err)
75 }
76
77 want := &Workflow{
78 ID: Int64(72844),
79 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
80 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
81 }
82 if !cmp.Equal(workflow, want) {
83 t.Errorf("Actions.GetWorkflowByID returned %+v, want %+v", workflow, want)
84 }
85
86 const methodName = "GetWorkflowByID"
87 testBadOptions(t, methodName, func() (err error) {
88 _, _, err = client.Actions.GetWorkflowByID(ctx, "\n", "\n", -72844)
89 return err
90 })
91
92 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
93 got, resp, err := client.Actions.GetWorkflowByID(ctx, "o", "r", 72844)
94 if got != nil {
95 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
96 }
97 return resp, err
98 })
99 }
100
101 func TestActionsService_GetWorkflowByFileName(t *testing.T) {
102 client, mux, _, teardown := setup()
103 defer teardown()
104
105 mux.HandleFunc("/repos/o/r/actions/workflows/main.yml", func(w http.ResponseWriter, r *http.Request) {
106 testMethod(t, r, "GET")
107 fmt.Fprint(w, `{"id":72844,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`)
108 })
109
110 ctx := context.Background()
111 workflow, _, err := client.Actions.GetWorkflowByFileName(ctx, "o", "r", "main.yml")
112 if err != nil {
113 t.Errorf("Actions.GetWorkflowByFileName returned error: %v", err)
114 }
115
116 want := &Workflow{
117 ID: Int64(72844),
118 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
119 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
120 }
121 if !cmp.Equal(workflow, want) {
122 t.Errorf("Actions.GetWorkflowByFileName returned %+v, want %+v", workflow, want)
123 }
124
125 const methodName = "GetWorkflowByFileName"
126 testBadOptions(t, methodName, func() (err error) {
127 _, _, err = client.Actions.GetWorkflowByFileName(ctx, "\n", "\n", "\n")
128 return err
129 })
130
131 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
132 got, resp, err := client.Actions.GetWorkflowByFileName(ctx, "o", "r", "main.yml")
133 if got != nil {
134 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
135 }
136 return resp, err
137 })
138 }
139
140 func TestActionsService_GetWorkflowUsageByID(t *testing.T) {
141 client, mux, _, teardown := setup()
142 defer teardown()
143
144 mux.HandleFunc("/repos/o/r/actions/workflows/72844/timing", func(w http.ResponseWriter, r *http.Request) {
145 testMethod(t, r, "GET")
146 fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000},"MACOS":{"total_ms":240000},"WINDOWS":{"total_ms":300000}}}`)
147 })
148
149 ctx := context.Background()
150 workflowUsage, _, err := client.Actions.GetWorkflowUsageByID(ctx, "o", "r", 72844)
151 if err != nil {
152 t.Errorf("Actions.GetWorkflowUsageByID returned error: %v", err)
153 }
154
155 want := &WorkflowUsage{
156 Billable: &WorkflowBillMap{
157 "UBUNTU": &WorkflowBill{
158 TotalMS: Int64(180000),
159 },
160 "MACOS": &WorkflowBill{
161 TotalMS: Int64(240000),
162 },
163 "WINDOWS": &WorkflowBill{
164 TotalMS: Int64(300000),
165 },
166 },
167 }
168 if !cmp.Equal(workflowUsage, want) {
169 t.Errorf("Actions.GetWorkflowUsageByID returned %+v, want %+v", workflowUsage, want)
170 }
171
172 const methodName = "GetWorkflowUsageByID"
173 testBadOptions(t, methodName, func() (err error) {
174 _, _, err = client.Actions.GetWorkflowUsageByID(ctx, "\n", "\n", -72844)
175 return err
176 })
177
178 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
179 got, resp, err := client.Actions.GetWorkflowUsageByID(ctx, "o", "r", 72844)
180 if got != nil {
181 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
182 }
183 return resp, err
184 })
185 }
186
187 func TestActionsService_GetWorkflowUsageByFileName(t *testing.T) {
188 client, mux, _, teardown := setup()
189 defer teardown()
190
191 mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/timing", func(w http.ResponseWriter, r *http.Request) {
192 testMethod(t, r, "GET")
193 fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000},"MACOS":{"total_ms":240000},"WINDOWS":{"total_ms":300000}}}`)
194 })
195
196 ctx := context.Background()
197 workflowUsage, _, err := client.Actions.GetWorkflowUsageByFileName(ctx, "o", "r", "main.yml")
198 if err != nil {
199 t.Errorf("Actions.GetWorkflowUsageByFileName returned error: %v", err)
200 }
201
202 want := &WorkflowUsage{
203 Billable: &WorkflowBillMap{
204 "UBUNTU": &WorkflowBill{
205 TotalMS: Int64(180000),
206 },
207 "MACOS": &WorkflowBill{
208 TotalMS: Int64(240000),
209 },
210 "WINDOWS": &WorkflowBill{
211 TotalMS: Int64(300000),
212 },
213 },
214 }
215 if !cmp.Equal(workflowUsage, want) {
216 t.Errorf("Actions.GetWorkflowUsageByFileName returned %+v, want %+v", workflowUsage, want)
217 }
218
219 const methodName = "GetWorkflowUsageByFileName"
220 testBadOptions(t, methodName, func() (err error) {
221 _, _, err = client.Actions.GetWorkflowUsageByFileName(ctx, "\n", "\n", "\n")
222 return err
223 })
224
225 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
226 got, resp, err := client.Actions.GetWorkflowUsageByFileName(ctx, "o", "r", "main.yml")
227 if got != nil {
228 t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
229 }
230 return resp, err
231 })
232 }
233
234 func TestActionsService_CreateWorkflowDispatchEventByID(t *testing.T) {
235 client, mux, _, teardown := setup()
236 defer teardown()
237
238 event := CreateWorkflowDispatchEventRequest{
239 Ref: "d4cfb6e7",
240 Inputs: map[string]interface{}{
241 "key": "value",
242 },
243 }
244 mux.HandleFunc("/repos/o/r/actions/workflows/72844/dispatches", func(w http.ResponseWriter, r *http.Request) {
245 var v CreateWorkflowDispatchEventRequest
246 json.NewDecoder(r.Body).Decode(&v)
247
248 testMethod(t, r, "POST")
249 if !cmp.Equal(v, event) {
250 t.Errorf("Request body = %+v, want %+v", v, event)
251 }
252 })
253
254 ctx := context.Background()
255 _, err := client.Actions.CreateWorkflowDispatchEventByID(ctx, "o", "r", 72844, event)
256 if err != nil {
257 t.Errorf("Actions.CreateWorkflowDispatchEventByID returned error: %v", err)
258 }
259
260
261 client.BaseURL.Path = ""
262 _, err = client.Actions.CreateWorkflowDispatchEventByID(ctx, "o", "r", 72844, event)
263 if err == nil {
264 t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEventByID err = nil, want error")
265 }
266
267 const methodName = "CreateWorkflowDispatchEventByID"
268 testBadOptions(t, methodName, func() (err error) {
269 _, err = client.Actions.CreateWorkflowDispatchEventByID(ctx, "o", "r", 72844, event)
270 return err
271 })
272
273 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
274 return client.Actions.CreateWorkflowDispatchEventByID(ctx, "o", "r", 72844, event)
275 })
276 }
277
278 func TestActionsService_CreateWorkflowDispatchEventByFileName(t *testing.T) {
279 client, mux, _, teardown := setup()
280 defer teardown()
281
282 event := CreateWorkflowDispatchEventRequest{
283 Ref: "d4cfb6e7",
284 Inputs: map[string]interface{}{
285 "key": "value",
286 },
287 }
288 mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/dispatches", func(w http.ResponseWriter, r *http.Request) {
289 var v CreateWorkflowDispatchEventRequest
290 json.NewDecoder(r.Body).Decode(&v)
291
292 testMethod(t, r, "POST")
293 if !cmp.Equal(v, event) {
294 t.Errorf("Request body = %+v, want %+v", v, event)
295 }
296 })
297
298 ctx := context.Background()
299 _, err := client.Actions.CreateWorkflowDispatchEventByFileName(ctx, "o", "r", "main.yml", event)
300 if err != nil {
301 t.Errorf("Actions.CreateWorkflowDispatchEventByFileName returned error: %v", err)
302 }
303
304
305 client.BaseURL.Path = ""
306 _, err = client.Actions.CreateWorkflowDispatchEventByFileName(ctx, "o", "r", "main.yml", event)
307 if err == nil {
308 t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEventByFileName err = nil, want error")
309 }
310
311 const methodName = "CreateWorkflowDispatchEventByFileName"
312 testBadOptions(t, methodName, func() (err error) {
313 _, err = client.Actions.CreateWorkflowDispatchEventByFileName(ctx, "o", "r", "main.yml", event)
314 return err
315 })
316
317 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
318 return client.Actions.CreateWorkflowDispatchEventByFileName(ctx, "o", "r", "main.yml", event)
319 })
320 }
321
322 func TestActionsService_EnableWorkflowByID(t *testing.T) {
323 client, mux, _, teardown := setup()
324 defer teardown()
325
326 mux.HandleFunc("/repos/o/r/actions/workflows/72844/enable", func(w http.ResponseWriter, r *http.Request) {
327 testMethod(t, r, "PUT")
328 if r.Body != http.NoBody {
329 t.Errorf("Request body = %+v, want %+v", r.Body, http.NoBody)
330 }
331 })
332
333 ctx := context.Background()
334 _, err := client.Actions.EnableWorkflowByID(ctx, "o", "r", 72844)
335 if err != nil {
336 t.Errorf("Actions.EnableWorkflowByID returned error: %v", err)
337 }
338
339
340 client.BaseURL.Path = ""
341 _, err = client.Actions.EnableWorkflowByID(ctx, "o", "r", 72844)
342 if err == nil {
343 t.Error("client.BaseURL.Path='' EnableWorkflowByID err = nil, want error")
344 }
345
346 const methodName = "EnableWorkflowByID"
347 testBadOptions(t, methodName, func() (err error) {
348 _, err = client.Actions.EnableWorkflowByID(ctx, "o", "r", 72844)
349 return err
350 })
351
352 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
353 return client.Actions.EnableWorkflowByID(ctx, "o", "r", 72844)
354 })
355 }
356
357 func TestActionsService_EnableWorkflowByFilename(t *testing.T) {
358 client, mux, _, teardown := setup()
359 defer teardown()
360
361 mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/enable", func(w http.ResponseWriter, r *http.Request) {
362 testMethod(t, r, "PUT")
363 if r.Body != http.NoBody {
364 t.Errorf("Request body = %+v, want %+v", r.Body, http.NoBody)
365 }
366 })
367
368 ctx := context.Background()
369 _, err := client.Actions.EnableWorkflowByFileName(ctx, "o", "r", "main.yml")
370 if err != nil {
371 t.Errorf("Actions.EnableWorkflowByFilename returned error: %v", err)
372 }
373
374
375 client.BaseURL.Path = ""
376 _, err = client.Actions.EnableWorkflowByFileName(ctx, "o", "r", "main.yml")
377 if err == nil {
378 t.Error("client.BaseURL.Path='' EnableWorkflowByFilename err = nil, want error")
379 }
380
381 const methodName = "EnableWorkflowByFileName"
382 testBadOptions(t, methodName, func() (err error) {
383 _, err = client.Actions.EnableWorkflowByFileName(ctx, "o", "r", "main.yml")
384 return err
385 })
386
387 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
388 return client.Actions.EnableWorkflowByFileName(ctx, "o", "r", "main.yml")
389 })
390 }
391
392 func TestActionsService_DisableWorkflowByID(t *testing.T) {
393 client, mux, _, teardown := setup()
394 defer teardown()
395
396 mux.HandleFunc("/repos/o/r/actions/workflows/72844/disable", func(w http.ResponseWriter, r *http.Request) {
397 testMethod(t, r, "PUT")
398 if r.Body != http.NoBody {
399 t.Errorf("Request body = %+v, want %+v", r.Body, http.NoBody)
400 }
401 })
402
403 ctx := context.Background()
404 _, err := client.Actions.DisableWorkflowByID(ctx, "o", "r", 72844)
405 if err != nil {
406 t.Errorf("Actions.DisableWorkflowByID returned error: %v", err)
407 }
408
409
410 client.BaseURL.Path = ""
411 _, err = client.Actions.DisableWorkflowByID(ctx, "o", "r", 72844)
412 if err == nil {
413 t.Error("client.BaseURL.Path='' DisableWorkflowByID err = nil, want error")
414 }
415
416 const methodName = "DisableWorkflowByID"
417 testBadOptions(t, methodName, func() (err error) {
418 _, err = client.Actions.DisableWorkflowByID(ctx, "o", "r", 72844)
419 return err
420 })
421
422 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
423 return client.Actions.DisableWorkflowByID(ctx, "o", "r", 72844)
424 })
425 }
426
427 func TestActionsService_DisableWorkflowByFileName(t *testing.T) {
428 client, mux, _, teardown := setup()
429 defer teardown()
430
431 mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/disable", func(w http.ResponseWriter, r *http.Request) {
432 testMethod(t, r, "PUT")
433 if r.Body != http.NoBody {
434 t.Errorf("Request body = %+v, want %+v", r.Body, http.NoBody)
435 }
436 })
437
438 ctx := context.Background()
439 _, err := client.Actions.DisableWorkflowByFileName(ctx, "o", "r", "main.yml")
440 if err != nil {
441 t.Errorf("Actions.DisableWorkflowByFileName returned error: %v", err)
442 }
443
444
445 client.BaseURL.Path = ""
446 _, err = client.Actions.DisableWorkflowByFileName(ctx, "o", "r", "main.yml")
447 if err == nil {
448 t.Error("client.BaseURL.Path='' DisableWorkflowByFileName err = nil, want error")
449 }
450
451 const methodName = "DisableWorkflowByFileName"
452 testBadOptions(t, methodName, func() (err error) {
453 _, err = client.Actions.DisableWorkflowByFileName(ctx, "o", "r", "main.yml")
454 return err
455 })
456
457 testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
458 return client.Actions.DisableWorkflowByFileName(ctx, "o", "r", "main.yml")
459 })
460 }
461
462 func TestWorkflow_Marshal(t *testing.T) {
463 testJSONMarshal(t, &Workflow{}, "{}")
464
465 u := &Workflow{
466 ID: Int64(1),
467 NodeID: String("nid"),
468 Name: String("n"),
469 Path: String("p"),
470 State: String("s"),
471 CreatedAt: &Timestamp{referenceTime},
472 UpdatedAt: &Timestamp{referenceTime},
473 URL: String("u"),
474 HTMLURL: String("h"),
475 BadgeURL: String("b"),
476 }
477
478 want := `{
479 "id": 1,
480 "node_id": "nid",
481 "name": "n",
482 "path": "p",
483 "state": "s",
484 "created_at": ` + referenceTimeStr + `,
485 "updated_at": ` + referenceTimeStr + `,
486 "url": "u",
487 "html_url": "h",
488 "badge_url": "b"
489 }`
490
491 testJSONMarshal(t, u, want)
492 }
493
494 func TestWorkflows_Marshal(t *testing.T) {
495 testJSONMarshal(t, &Workflows{}, "{}")
496
497 u := &Workflows{
498 TotalCount: Int(1),
499 Workflows: []*Workflow{
500 {
501 ID: Int64(1),
502 NodeID: String("nid"),
503 Name: String("n"),
504 Path: String("p"),
505 State: String("s"),
506 CreatedAt: &Timestamp{referenceTime},
507 UpdatedAt: &Timestamp{referenceTime},
508 URL: String("u"),
509 HTMLURL: String("h"),
510 BadgeURL: String("b"),
511 },
512 },
513 }
514
515 want := `{
516 "total_count": 1,
517 "workflows": [{
518 "id": 1,
519 "node_id": "nid",
520 "name": "n",
521 "path": "p",
522 "state": "s",
523 "created_at": ` + referenceTimeStr + `,
524 "updated_at": ` + referenceTimeStr + `,
525 "url": "u",
526 "html_url": "h",
527 "badge_url": "b"
528 }]
529 }`
530
531 testJSONMarshal(t, u, want)
532 }
533
534 func TestWorkflowBill_Marshal(t *testing.T) {
535 testJSONMarshal(t, &WorkflowBill{}, "{}")
536
537 u := &WorkflowBill{
538 TotalMS: Int64(1),
539 }
540
541 want := `{
542 "total_ms": 1
543 }`
544
545 testJSONMarshal(t, u, want)
546 }
547
548 func TestWorkflowBillMap_Marshal(t *testing.T) {
549 testJSONMarshal(t, &WorkflowBillMap{}, "{}")
550
551 u := &WorkflowBillMap{
552 "UBUNTU": &WorkflowBill{
553 TotalMS: Int64(1),
554 },
555 "MACOS": &WorkflowBill{
556 TotalMS: Int64(1),
557 },
558 "WINDOWS": &WorkflowBill{
559 TotalMS: Int64(1),
560 },
561 }
562
563 want := `{
564 "UBUNTU": {
565 "total_ms": 1
566 },
567 "MACOS": {
568 "total_ms": 1
569 },
570 "WINDOWS": {
571 "total_ms": 1
572 }
573 }`
574
575 testJSONMarshal(t, u, want)
576 }
577
578 func TestWorkflowUsage_Marshal(t *testing.T) {
579 testJSONMarshal(t, &WorkflowUsage{}, "{}")
580
581 u := &WorkflowUsage{
582 Billable: &WorkflowBillMap{
583 "UBUNTU": &WorkflowBill{
584 TotalMS: Int64(1),
585 },
586 "MACOS": &WorkflowBill{
587 TotalMS: Int64(1),
588 },
589 "WINDOWS": &WorkflowBill{
590 TotalMS: Int64(1),
591 },
592 },
593 }
594
595 want := `{
596 "billable": {
597 "UBUNTU": {
598 "total_ms": 1
599 },
600 "MACOS": {
601 "total_ms": 1
602 },
603 "WINDOWS": {
604 "total_ms": 1
605 }
606 }
607 }`
608
609 testJSONMarshal(t, u, want)
610 }
611
612 func TestCreateWorkflowDispatchEventRequest_Marshal(t *testing.T) {
613 testJSONMarshal(t, &CreateWorkflowDispatchEventRequest{}, "{}")
614
615 inputs := make(map[string]interface{}, 0)
616 inputs["key"] = "value"
617
618 u := &CreateWorkflowDispatchEventRequest{
619 Ref: "r",
620 Inputs: inputs,
621 }
622
623 want := `{
624 "ref": "r",
625 "inputs": {
626 "key": "value"
627 }
628 }`
629
630 testJSONMarshal(t, u, want)
631 }
632
View as plain text