github.com/google/go-github/v33@v33.0.0/github/actions_artifacts_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  	"fmt"
    11  	"net/http"
    12  	"net/url"
    13  	"reflect"
    14  	"testing"
    15  )
    16  
    17  func TestActionsService_ListArtifacts(t *testing.T) {
    18  	client, mux, _, teardown := setup()
    19  	defer teardown()
    20  
    21  	mux.HandleFunc("/repos/o/r/actions/artifacts", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testFormValues(t, r, values{"page": "2"})
    24  		fmt.Fprint(w,
    25  			`{
    26  				"total_count":1, 
    27  				"artifacts":[{"id":1}]
    28  			}`,
    29  		)
    30  	})
    31  
    32  	opts := &ListOptions{Page: 2}
    33  	artifacts, _, err := client.Actions.ListArtifacts(context.Background(), "o", "r", opts)
    34  	if err != nil {
    35  		t.Errorf("Actions.ListArtifacts returned error: %v", err)
    36  	}
    37  
    38  	want := &ArtifactList{TotalCount: Int64(1), Artifacts: []*Artifact{{ID: Int64(1)}}}
    39  	if !reflect.DeepEqual(artifacts, want) {
    40  		t.Errorf("Actions.ListArtifacts returned %+v, want %+v", artifacts, want)
    41  	}
    42  }
    43  
    44  func TestActionsService_ListArtifacts_invalidOwner(t *testing.T) {
    45  	client, _, _, teardown := setup()
    46  	defer teardown()
    47  
    48  	_, _, err := client.Actions.ListArtifacts(context.Background(), "%", "r", nil)
    49  	testURLParseError(t, err)
    50  }
    51  
    52  func TestActionsService_ListArtifacts_invalidRepo(t *testing.T) {
    53  	client, _, _, teardown := setup()
    54  	defer teardown()
    55  
    56  	_, _, err := client.Actions.ListArtifacts(context.Background(), "o", "%", nil)
    57  	testURLParseError(t, err)
    58  }
    59  
    60  func TestActionsService_ListArtifacts_notFound(t *testing.T) {
    61  	client, mux, _, teardown := setup()
    62  	defer teardown()
    63  
    64  	mux.HandleFunc("/repos/o/r/actions/artifacts", func(w http.ResponseWriter, r *http.Request) {
    65  		testMethod(t, r, "GET")
    66  		w.WriteHeader(http.StatusNotFound)
    67  	})
    68  
    69  	artifacts, resp, err := client.Actions.ListArtifacts(context.Background(), "o", "r", nil)
    70  	if err == nil {
    71  		t.Errorf("Expected HTTP 404 response")
    72  	}
    73  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
    74  		t.Errorf("Actions.ListArtifacts return status %d, want %d", got, want)
    75  	}
    76  	if artifacts != nil {
    77  		t.Errorf("Actions.ListArtifacts return %+v, want nil", artifacts)
    78  	}
    79  }
    80  
    81  func TestActionsService_ListWorkflowRunArtifacts(t *testing.T) {
    82  	client, mux, _, teardown := setup()
    83  	defer teardown()
    84  
    85  	mux.HandleFunc("/repos/o/r/actions/runs/1/artifacts", func(w http.ResponseWriter, r *http.Request) {
    86  		testMethod(t, r, "GET")
    87  		testFormValues(t, r, values{"page": "2"})
    88  		fmt.Fprint(w,
    89  			`{
    90  				"total_count":1, 
    91  				"artifacts":[{"id":1}]
    92  			}`,
    93  		)
    94  	})
    95  
    96  	opts := &ListOptions{Page: 2}
    97  	artifacts, _, err := client.Actions.ListWorkflowRunArtifacts(context.Background(), "o", "r", 1, opts)
    98  	if err != nil {
    99  		t.Errorf("Actions.ListWorkflowRunArtifacts returned error: %v", err)
   100  	}
   101  
   102  	want := &ArtifactList{TotalCount: Int64(1), Artifacts: []*Artifact{{ID: Int64(1)}}}
   103  	if !reflect.DeepEqual(artifacts, want) {
   104  		t.Errorf("Actions.ListWorkflowRunArtifacts returned %+v, want %+v", artifacts, want)
   105  	}
   106  }
   107  
   108  func TestActionsService_ListWorkflowRunArtifacts_invalidOwner(t *testing.T) {
   109  	client, _, _, teardown := setup()
   110  	defer teardown()
   111  
   112  	_, _, err := client.Actions.ListWorkflowRunArtifacts(context.Background(), "%", "r", 1, nil)
   113  	testURLParseError(t, err)
   114  }
   115  
   116  func TestActionsService_ListWorkflowRunArtifacts_invalidRepo(t *testing.T) {
   117  	client, _, _, teardown := setup()
   118  	defer teardown()
   119  
   120  	_, _, err := client.Actions.ListWorkflowRunArtifacts(context.Background(), "o", "%", 1, nil)
   121  	testURLParseError(t, err)
   122  }
   123  
   124  func TestActionsService_ListWorkflowRunArtifacts_notFound(t *testing.T) {
   125  	client, mux, _, teardown := setup()
   126  	defer teardown()
   127  
   128  	mux.HandleFunc("/repos/o/r/actions/runs/1/artifacts", func(w http.ResponseWriter, r *http.Request) {
   129  		testMethod(t, r, "GET")
   130  		w.WriteHeader(http.StatusNotFound)
   131  	})
   132  
   133  	artifacts, resp, err := client.Actions.ListWorkflowRunArtifacts(context.Background(), "o", "r", 1, nil)
   134  	if err == nil {
   135  		t.Errorf("Expected HTTP 404 response")
   136  	}
   137  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
   138  		t.Errorf("Actions.ListWorkflowRunArtifacts return status %d, want %d", got, want)
   139  	}
   140  	if artifacts != nil {
   141  		t.Errorf("Actions.ListWorkflowRunArtifacts return %+v, want nil", artifacts)
   142  	}
   143  }
   144  
   145  func TestActionsService_GetArtifact(t *testing.T) {
   146  	client, mux, _, teardown := setup()
   147  	defer teardown()
   148  
   149  	mux.HandleFunc("/repos/o/r/actions/artifacts/1", func(w http.ResponseWriter, r *http.Request) {
   150  		testMethod(t, r, "GET")
   151  		fmt.Fprint(w, `{
   152  			"id":1,
   153  			"node_id":"xyz",
   154  			"name":"a",
   155  			"size_in_bytes":5,
   156  			"archive_download_url":"u"
   157  		}`)
   158  	})
   159  
   160  	artifact, _, err := client.Actions.GetArtifact(context.Background(), "o", "r", 1)
   161  	if err != nil {
   162  		t.Errorf("Actions.GetArtifact returned error: %v", err)
   163  	}
   164  
   165  	want := &Artifact{
   166  		ID:                 Int64(1),
   167  		NodeID:             String("xyz"),
   168  		Name:               String("a"),
   169  		SizeInBytes:        Int64(5),
   170  		ArchiveDownloadURL: String("u"),
   171  	}
   172  	if !reflect.DeepEqual(artifact, want) {
   173  		t.Errorf("Actions.GetArtifact returned %+v, want %+v", artifact, want)
   174  	}
   175  }
   176  
   177  func TestActionsService_GetArtifact_invalidOwner(t *testing.T) {
   178  	client, _, _, teardown := setup()
   179  	defer teardown()
   180  
   181  	_, _, err := client.Actions.GetArtifact(context.Background(), "%", "r", 1)
   182  	testURLParseError(t, err)
   183  }
   184  
   185  func TestActionsService_GetArtifact_invalidRepo(t *testing.T) {
   186  	client, _, _, teardown := setup()
   187  	defer teardown()
   188  
   189  	_, _, err := client.Actions.GetArtifact(context.Background(), "o", "%", 1)
   190  	testURLParseError(t, err)
   191  }
   192  
   193  func TestActionsService_GetArtifact_notFound(t *testing.T) {
   194  	client, mux, _, teardown := setup()
   195  	defer teardown()
   196  
   197  	mux.HandleFunc("/repos/o/r/actions/artifacts/1", func(w http.ResponseWriter, r *http.Request) {
   198  		testMethod(t, r, "GET")
   199  		w.WriteHeader(http.StatusNotFound)
   200  	})
   201  
   202  	artifact, resp, err := client.Actions.GetArtifact(context.Background(), "o", "r", 1)
   203  	if err == nil {
   204  		t.Errorf("Expected HTTP 404 response")
   205  	}
   206  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
   207  		t.Errorf("Actions.GetArtifact return status %d, want %d", got, want)
   208  	}
   209  	if artifact != nil {
   210  		t.Errorf("Actions.GetArtifact return %+v, want nil", artifact)
   211  	}
   212  }
   213  
   214  func TestActionsSerivice_DownloadArtifact(t *testing.T) {
   215  	client, mux, _, teardown := setup()
   216  	defer teardown()
   217  
   218  	mux.HandleFunc("/repos/o/r/actions/artifacts/1/zip", func(w http.ResponseWriter, r *http.Request) {
   219  		testMethod(t, r, "GET")
   220  		http.Redirect(w, r, "https://github.com/artifact", http.StatusFound)
   221  	})
   222  
   223  	url, resp, err := client.Actions.DownloadArtifact(context.Background(), "o", "r", 1, true)
   224  	if err != nil {
   225  		t.Errorf("Actions.DownloadArtifact returned error: %v", err)
   226  	}
   227  	if resp.StatusCode != http.StatusFound {
   228  		t.Errorf("Actions.DownloadArtifact returned status: %d, want %d", resp.StatusCode, http.StatusFound)
   229  	}
   230  
   231  	want := "https://github.com/artifact"
   232  	if url.String() != want {
   233  		t.Errorf("Actions.DownloadArtifact returned %+v, want %+v", url.String(), want)
   234  	}
   235  }
   236  
   237  func TestActionsService_DownloadArtifact_invalidOwner(t *testing.T) {
   238  	client, _, _, teardown := setup()
   239  	defer teardown()
   240  
   241  	_, _, err := client.Actions.DownloadArtifact(context.Background(), "%", "r", 1, true)
   242  	testURLParseError(t, err)
   243  }
   244  
   245  func TestActionsService_DownloadArtifact_invalidRepo(t *testing.T) {
   246  	client, _, _, teardown := setup()
   247  	defer teardown()
   248  
   249  	_, _, err := client.Actions.DownloadArtifact(context.Background(), "o", "%", 1, true)
   250  	testURLParseError(t, err)
   251  }
   252  
   253  func TestActionsService_DownloadArtifact_StatusMovedPermanently_dontFollowRedirects(t *testing.T) {
   254  	client, mux, _, teardown := setup()
   255  	defer teardown()
   256  
   257  	mux.HandleFunc("/repos/o/r/actions/artifacts/1/zip", func(w http.ResponseWriter, r *http.Request) {
   258  		testMethod(t, r, "GET")
   259  		http.Redirect(w, r, "https://github.com/artifact", http.StatusMovedPermanently)
   260  	})
   261  
   262  	_, resp, _ := client.Actions.DownloadArtifact(context.Background(), "o", "r", 1, false)
   263  	if resp.StatusCode != http.StatusMovedPermanently {
   264  		t.Errorf("Actions.DownloadArtifact return status %d, want %d", resp.StatusCode, http.StatusMovedPermanently)
   265  	}
   266  }
   267  
   268  func TestActionsService_DownloadArtifact_StatusMovedPermanently_followRedirects(t *testing.T) {
   269  	client, mux, serverURL, teardown := setup()
   270  	defer teardown()
   271  
   272  	mux.HandleFunc("/repos/o/r/actions/artifacts/1/zip", func(w http.ResponseWriter, r *http.Request) {
   273  		testMethod(t, r, "GET")
   274  		redirectURL, _ := url.Parse(serverURL + baseURLPath + "/redirect")
   275  		http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently)
   276  	})
   277  	mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
   278  		testMethod(t, r, "GET")
   279  		http.Redirect(w, r, "http://github.com/artifact", http.StatusFound)
   280  	})
   281  
   282  	url, resp, err := client.Actions.DownloadArtifact(context.Background(), "o", "r", 1, true)
   283  	if err != nil {
   284  		t.Errorf("Actions.DownloadArtifact return error: %v", err)
   285  	}
   286  	if resp.StatusCode != http.StatusFound {
   287  		t.Errorf("Actions.DownloadArtifact return status %d, want %d", resp.StatusCode, http.StatusFound)
   288  	}
   289  	want := "http://github.com/artifact"
   290  	if url.String() != want {
   291  		t.Errorf("Actions.DownloadArtifact returned %+v, want %+v", url.String(), want)
   292  	}
   293  }
   294  
   295  func TestActionsService_DeleteArtifact(t *testing.T) {
   296  	client, mux, _, teardown := setup()
   297  	defer teardown()
   298  
   299  	mux.HandleFunc("/repos/o/r/actions/artifacts/1", func(w http.ResponseWriter, r *http.Request) {
   300  		testMethod(t, r, "DELETE")
   301  	})
   302  
   303  	_, err := client.Actions.DeleteArtifact(context.Background(), "o", "r", 1)
   304  	if err != nil {
   305  		t.Errorf("Actions.DeleteArtifact return error: %v", err)
   306  	}
   307  }
   308  
   309  func TestActionsService_DeleteArtifact_invalidOwner(t *testing.T) {
   310  	client, _, _, teardown := setup()
   311  	defer teardown()
   312  
   313  	_, err := client.Actions.DeleteArtifact(context.Background(), "%", "r", 1)
   314  	testURLParseError(t, err)
   315  }
   316  
   317  func TestActionsService_DeleteArtifact_invalidRepo(t *testing.T) {
   318  	client, _, _, teardown := setup()
   319  	defer teardown()
   320  
   321  	_, err := client.Actions.DeleteArtifact(context.Background(), "o", "%", 1)
   322  	testURLParseError(t, err)
   323  }
   324  
   325  func TestActionsService_DeleteArtifact_notFound(t *testing.T) {
   326  	client, mux, _, teardown := setup()
   327  	defer teardown()
   328  
   329  	mux.HandleFunc("/repos/o/r/actions/artifacts/1", func(w http.ResponseWriter, r *http.Request) {
   330  		testMethod(t, r, "DELETE")
   331  		w.WriteHeader(http.StatusNotFound)
   332  	})
   333  
   334  	resp, err := client.Actions.DeleteArtifact(context.Background(), "o", "r", 1)
   335  	if err == nil {
   336  		t.Errorf("Expected HTTP 404 response")
   337  	}
   338  	if got, want := resp.Response.StatusCode, http.StatusNotFound; got != want {
   339  		t.Errorf("Actions.DeleteArtifact return status %d, want %d", got, want)
   340  	}
   341  }