github.com/google/go-github/v33@v33.0.0/github/actions_workflows_test.go (about)

     1  // Copyright 2020 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  	"time"
    16  )
    17  
    18  func TestActionsService_ListWorkflows(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	mux.HandleFunc("/repos/o/r/actions/workflows", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  		testFormValues(t, r, values{"per_page": "2", "page": "2"})
    25  		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"}]}`)
    26  	})
    27  
    28  	opts := &ListOptions{Page: 2, PerPage: 2}
    29  	workflows, _, err := client.Actions.ListWorkflows(context.Background(), "o", "r", opts)
    30  	if err != nil {
    31  		t.Errorf("Actions.ListWorkflows returned error: %v", err)
    32  	}
    33  
    34  	want := &Workflows{
    35  		TotalCount: Int(4),
    36  		Workflows: []*Workflow{
    37  			{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)}},
    38  			{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)}},
    39  		},
    40  	}
    41  	if !reflect.DeepEqual(workflows, want) {
    42  		t.Errorf("Actions.ListWorkflows returned %+v, want %+v", workflows, want)
    43  	}
    44  }
    45  
    46  func TestActionsService_GetWorkflowByID(t *testing.T) {
    47  	client, mux, _, teardown := setup()
    48  	defer teardown()
    49  
    50  	mux.HandleFunc("/repos/o/r/actions/workflows/72844", func(w http.ResponseWriter, r *http.Request) {
    51  		testMethod(t, r, "GET")
    52  		fmt.Fprint(w, `{"id":72844,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`)
    53  	})
    54  
    55  	workflow, _, err := client.Actions.GetWorkflowByID(context.Background(), "o", "r", 72844)
    56  	if err != nil {
    57  		t.Errorf("Actions.GetWorkflowByID returned error: %v", err)
    58  	}
    59  
    60  	want := &Workflow{
    61  		ID:        Int64(72844),
    62  		CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
    63  		UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
    64  	}
    65  	if !reflect.DeepEqual(workflow, want) {
    66  		t.Errorf("Actions.GetWorkflowByID returned %+v, want %+v", workflow, want)
    67  	}
    68  }
    69  
    70  func TestActionsService_GetWorkflowByFileName(t *testing.T) {
    71  	client, mux, _, teardown := setup()
    72  	defer teardown()
    73  
    74  	mux.HandleFunc("/repos/o/r/actions/workflows/main.yml", func(w http.ResponseWriter, r *http.Request) {
    75  		testMethod(t, r, "GET")
    76  		fmt.Fprint(w, `{"id":72844,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`)
    77  	})
    78  
    79  	workflow, _, err := client.Actions.GetWorkflowByFileName(context.Background(), "o", "r", "main.yml")
    80  	if err != nil {
    81  		t.Errorf("Actions.GetWorkflowByFileName returned error: %v", err)
    82  	}
    83  
    84  	want := &Workflow{
    85  		ID:        Int64(72844),
    86  		CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)},
    87  		UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)},
    88  	}
    89  	if !reflect.DeepEqual(workflow, want) {
    90  		t.Errorf("Actions.GetWorkflowByFileName returned %+v, want %+v", workflow, want)
    91  	}
    92  }
    93  
    94  func TestActionsService_GetWorkflowUsageByID(t *testing.T) {
    95  	client, mux, _, teardown := setup()
    96  	defer teardown()
    97  
    98  	mux.HandleFunc("/repos/o/r/actions/workflows/72844/timing", func(w http.ResponseWriter, r *http.Request) {
    99  		testMethod(t, r, "GET")
   100  		fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000},"MACOS":{"total_ms":240000},"WINDOWS":{"total_ms":300000}}}`)
   101  	})
   102  
   103  	workflowUsage, _, err := client.Actions.GetWorkflowUsageByID(context.Background(), "o", "r", 72844)
   104  	if err != nil {
   105  		t.Errorf("Actions.GetWorkflowUsageByID returned error: %v", err)
   106  	}
   107  
   108  	want := &WorkflowUsage{
   109  		Billable: &WorkflowEnvironment{
   110  			Ubuntu: &WorkflowBill{
   111  				TotalMS: Int64(180000),
   112  			},
   113  			MacOS: &WorkflowBill{
   114  				TotalMS: Int64(240000),
   115  			},
   116  			Windows: &WorkflowBill{
   117  				TotalMS: Int64(300000),
   118  			},
   119  		},
   120  	}
   121  	if !reflect.DeepEqual(workflowUsage, want) {
   122  		t.Errorf("Actions.GetWorkflowUsageByID returned %+v, want %+v", workflowUsage, want)
   123  	}
   124  }
   125  
   126  func TestActionsService_GetWorkflowUsageByFileName(t *testing.T) {
   127  	client, mux, _, teardown := setup()
   128  	defer teardown()
   129  
   130  	mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/timing", func(w http.ResponseWriter, r *http.Request) {
   131  		testMethod(t, r, "GET")
   132  		fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000},"MACOS":{"total_ms":240000},"WINDOWS":{"total_ms":300000}}}`)
   133  	})
   134  
   135  	workflowUsage, _, err := client.Actions.GetWorkflowUsageByFileName(context.Background(), "o", "r", "main.yml")
   136  	if err != nil {
   137  		t.Errorf("Actions.GetWorkflowUsageByFileName returned error: %v", err)
   138  	}
   139  
   140  	want := &WorkflowUsage{
   141  		Billable: &WorkflowEnvironment{
   142  			Ubuntu: &WorkflowBill{
   143  				TotalMS: Int64(180000),
   144  			},
   145  			MacOS: &WorkflowBill{
   146  				TotalMS: Int64(240000),
   147  			},
   148  			Windows: &WorkflowBill{
   149  				TotalMS: Int64(300000),
   150  			},
   151  		},
   152  	}
   153  	if !reflect.DeepEqual(workflowUsage, want) {
   154  		t.Errorf("Actions.GetWorkflowUsageByFileName returned %+v, want %+v", workflowUsage, want)
   155  	}
   156  }
   157  
   158  func TestActionsService_CreateWorkflowDispatchEventByID(t *testing.T) {
   159  	client, mux, _, teardown := setup()
   160  	defer teardown()
   161  
   162  	event := CreateWorkflowDispatchEventRequest{
   163  		Ref: "d4cfb6e7",
   164  		Inputs: map[string]interface{}{
   165  			"key": "value",
   166  		},
   167  	}
   168  	mux.HandleFunc("/repos/o/r/actions/workflows/72844/dispatches", func(w http.ResponseWriter, r *http.Request) {
   169  		var v CreateWorkflowDispatchEventRequest
   170  		json.NewDecoder(r.Body).Decode(&v)
   171  
   172  		testMethod(t, r, "POST")
   173  		if !reflect.DeepEqual(v, event) {
   174  			t.Errorf("Request body = %+v, want %+v", v, event)
   175  		}
   176  	})
   177  
   178  	_, err := client.Actions.CreateWorkflowDispatchEventByID(context.Background(), "o", "r", 72844, event)
   179  	if err != nil {
   180  		t.Errorf("Actions.CreateWorkflowDispatchEventByID returned error: %v", err)
   181  	}
   182  
   183  	// Test s.client.NewRequest failure
   184  	client.BaseURL.Path = ""
   185  	_, err = client.Actions.CreateWorkflowDispatchEventByID(context.Background(), "o", "r", 72844, event)
   186  	if err == nil {
   187  		t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEventByID err = nil, want error")
   188  	}
   189  }
   190  
   191  func TestActionsService_CreateWorkflowDispatchEventByFileName(t *testing.T) {
   192  	client, mux, _, teardown := setup()
   193  	defer teardown()
   194  
   195  	event := CreateWorkflowDispatchEventRequest{
   196  		Ref: "d4cfb6e7",
   197  		Inputs: map[string]interface{}{
   198  			"key": "value",
   199  		},
   200  	}
   201  	mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/dispatches", func(w http.ResponseWriter, r *http.Request) {
   202  		var v CreateWorkflowDispatchEventRequest
   203  		json.NewDecoder(r.Body).Decode(&v)
   204  
   205  		testMethod(t, r, "POST")
   206  		if !reflect.DeepEqual(v, event) {
   207  			t.Errorf("Request body = %+v, want %+v", v, event)
   208  		}
   209  	})
   210  
   211  	_, err := client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), "o", "r", "main.yml", event)
   212  	if err != nil {
   213  		t.Errorf("Actions.CreateWorkflowDispatchEventByFileName returned error: %v", err)
   214  	}
   215  
   216  	// Test s.client.NewRequest failure
   217  	client.BaseURL.Path = ""
   218  	_, err = client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), "o", "r", "main.yml", event)
   219  	if err == nil {
   220  		t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEventByFileName err = nil, want error")
   221  	}
   222  }
   223  
   224  func TestActionsService_EnableWorkflowByID(t *testing.T) {
   225  	client, mux, _, teardown := setup()
   226  	defer teardown()
   227  
   228  	mux.HandleFunc("/repos/o/r/actions/workflows/72844/enable", func(w http.ResponseWriter, r *http.Request) {
   229  		testMethod(t, r, "PUT")
   230  		if r.Body != http.NoBody {
   231  			t.Errorf("Request body = %+v, want %+v", r.Body, http.NoBody)
   232  		}
   233  	})
   234  
   235  	_, err := client.Actions.EnableWorkflowByID(context.Background(), "o", "r", 72844)
   236  	if err != nil {
   237  		t.Errorf("Actions.EnableWorkflowByID returned error: %v", err)
   238  	}
   239  
   240  	// Test s.client.NewRequest failure
   241  	client.BaseURL.Path = ""
   242  	_, err = client.Actions.EnableWorkflowByID(context.Background(), "o", "r", 72844)
   243  	if err == nil {
   244  		t.Error("client.BaseURL.Path='' EnableWorkflowByID err = nil, want error")
   245  	}
   246  }
   247  
   248  func TestActionsService_EnableWorkflowByFilename(t *testing.T) {
   249  	client, mux, _, teardown := setup()
   250  	defer teardown()
   251  
   252  	mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/enable", func(w http.ResponseWriter, r *http.Request) {
   253  		testMethod(t, r, "PUT")
   254  		if r.Body != http.NoBody {
   255  			t.Errorf("Request body = %+v, want %+v", r.Body, http.NoBody)
   256  		}
   257  	})
   258  
   259  	_, err := client.Actions.EnableWorkflowByFileName(context.Background(), "o", "r", "main.yml")
   260  	if err != nil {
   261  		t.Errorf("Actions.EnableWorkflowByFilename returned error: %v", err)
   262  	}
   263  
   264  	// Test s.client.NewRequest failure
   265  	client.BaseURL.Path = ""
   266  	_, err = client.Actions.EnableWorkflowByFileName(context.Background(), "o", "r", "main.yml")
   267  	if err == nil {
   268  		t.Error("client.BaseURL.Path='' EnableWorkflowByFilename err = nil, want error")
   269  	}
   270  }
   271  
   272  func TestActionsService_DisableWorkflowByID(t *testing.T) {
   273  	client, mux, _, teardown := setup()
   274  	defer teardown()
   275  
   276  	mux.HandleFunc("/repos/o/r/actions/workflows/72844/disable", func(w http.ResponseWriter, r *http.Request) {
   277  		testMethod(t, r, "PUT")
   278  		if r.Body != http.NoBody {
   279  			t.Errorf("Request body = %+v, want %+v", r.Body, http.NoBody)
   280  		}
   281  	})
   282  
   283  	_, err := client.Actions.DisableWorkflowByID(context.Background(), "o", "r", 72844)
   284  	if err != nil {
   285  		t.Errorf("Actions.DisableWorkflowByID returned error: %v", err)
   286  	}
   287  
   288  	// Test s.client.NewRequest failure
   289  	client.BaseURL.Path = ""
   290  	_, err = client.Actions.DisableWorkflowByID(context.Background(), "o", "r", 72844)
   291  	if err == nil {
   292  		t.Error("client.BaseURL.Path='' DisableWorkflowByID err = nil, want error")
   293  	}
   294  }
   295  
   296  func TestActionsService_DisableWorkflowByFileName(t *testing.T) {
   297  	client, mux, _, teardown := setup()
   298  	defer teardown()
   299  
   300  	mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/disable", func(w http.ResponseWriter, r *http.Request) {
   301  		testMethod(t, r, "PUT")
   302  		if r.Body != http.NoBody {
   303  			t.Errorf("Request body = %+v, want %+v", r.Body, http.NoBody)
   304  		}
   305  	})
   306  
   307  	_, err := client.Actions.DisableWorkflowByFileName(context.Background(), "o", "r", "main.yml")
   308  	if err != nil {
   309  		t.Errorf("Actions.DisableWorkflowByFileName returned error: %v", err)
   310  	}
   311  
   312  	// Test s.client.NewRequest failure
   313  	client.BaseURL.Path = ""
   314  	_, err = client.Actions.DisableWorkflowByFileName(context.Background(), "o", "r", "main.yml")
   315  	if err == nil {
   316  		t.Error("client.BaseURL.Path='' DisableWorkflowByFileName err = nil, want error")
   317  	}
   318  }