github.com/google/go-github/v70@v70.0.0/github/repos_deployments_test.go (about)

     1  // Copyright 2014 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  	"strings"
    14  	"testing"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestRepositoriesService_ListDeployments(t *testing.T) {
    20  	t.Parallel()
    21  	client, mux, _ := setup(t)
    22  
    23  	mux.HandleFunc("/repos/o/r/deployments", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  		testFormValues(t, r, values{"environment": "test"})
    26  		fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
    27  	})
    28  
    29  	opt := &DeploymentsListOptions{Environment: "test"}
    30  	ctx := context.Background()
    31  	deployments, _, err := client.Repositories.ListDeployments(ctx, "o", "r", opt)
    32  	if err != nil {
    33  		t.Errorf("Repositories.ListDeployments returned error: %v", err)
    34  	}
    35  
    36  	want := []*Deployment{{ID: Ptr(int64(1))}, {ID: Ptr(int64(2))}}
    37  	if !cmp.Equal(deployments, want) {
    38  		t.Errorf("Repositories.ListDeployments returned %+v, want %+v", deployments, want)
    39  	}
    40  
    41  	const methodName = "ListDeployments"
    42  	testBadOptions(t, methodName, func() (err error) {
    43  		_, _, err = client.Repositories.ListDeployments(ctx, "\n", "\n", opt)
    44  		return err
    45  	})
    46  
    47  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    48  		got, resp, err := client.Repositories.ListDeployments(ctx, "o", "r", opt)
    49  		if got != nil {
    50  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    51  		}
    52  		return resp, err
    53  	})
    54  }
    55  
    56  func TestRepositoriesService_GetDeployment(t *testing.T) {
    57  	t.Parallel()
    58  	client, mux, _ := setup(t)
    59  
    60  	mux.HandleFunc("/repos/o/r/deployments/3", func(w http.ResponseWriter, r *http.Request) {
    61  		testMethod(t, r, "GET")
    62  		fmt.Fprint(w, `{"id":3}`)
    63  	})
    64  
    65  	ctx := context.Background()
    66  	deployment, _, err := client.Repositories.GetDeployment(ctx, "o", "r", 3)
    67  	if err != nil {
    68  		t.Errorf("Repositories.GetDeployment returned error: %v", err)
    69  	}
    70  
    71  	want := &Deployment{ID: Ptr(int64(3))}
    72  
    73  	if !cmp.Equal(deployment, want) {
    74  		t.Errorf("Repositories.GetDeployment returned %+v, want %+v", deployment, want)
    75  	}
    76  
    77  	const methodName = "GetDeployment"
    78  	testBadOptions(t, methodName, func() (err error) {
    79  		_, _, err = client.Repositories.GetDeployment(ctx, "\n", "\n", 3)
    80  		return err
    81  	})
    82  
    83  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    84  		got, resp, err := client.Repositories.GetDeployment(ctx, "o", "r", 3)
    85  		if got != nil {
    86  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    87  		}
    88  		return resp, err
    89  	})
    90  }
    91  
    92  func TestRepositoriesService_CreateDeployment(t *testing.T) {
    93  	t.Parallel()
    94  	client, mux, _ := setup(t)
    95  
    96  	input := &DeploymentRequest{Ref: Ptr("1111"), Task: Ptr("deploy"), TransientEnvironment: Ptr(true)}
    97  
    98  	mux.HandleFunc("/repos/o/r/deployments", func(w http.ResponseWriter, r *http.Request) {
    99  		v := new(DeploymentRequest)
   100  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
   101  
   102  		testMethod(t, r, "POST")
   103  		wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   104  		testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
   105  		if !cmp.Equal(v, input) {
   106  			t.Errorf("Request body = %+v, want %+v", v, input)
   107  		}
   108  
   109  		fmt.Fprint(w, `{"ref": "1111", "task": "deploy"}`)
   110  	})
   111  
   112  	ctx := context.Background()
   113  	deployment, _, err := client.Repositories.CreateDeployment(ctx, "o", "r", input)
   114  	if err != nil {
   115  		t.Errorf("Repositories.CreateDeployment returned error: %v", err)
   116  	}
   117  
   118  	want := &Deployment{Ref: Ptr("1111"), Task: Ptr("deploy")}
   119  	if !cmp.Equal(deployment, want) {
   120  		t.Errorf("Repositories.CreateDeployment returned %+v, want %+v", deployment, want)
   121  	}
   122  
   123  	const methodName = "CreateDeployment"
   124  	testBadOptions(t, methodName, func() (err error) {
   125  		_, _, err = client.Repositories.CreateDeployment(ctx, "\n", "\n", input)
   126  		return err
   127  	})
   128  
   129  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   130  		got, resp, err := client.Repositories.CreateDeployment(ctx, "o", "r", input)
   131  		if got != nil {
   132  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   133  		}
   134  		return resp, err
   135  	})
   136  }
   137  
   138  func TestRepositoriesService_DeleteDeployment(t *testing.T) {
   139  	t.Parallel()
   140  	client, mux, _ := setup(t)
   141  
   142  	mux.HandleFunc("/repos/o/r/deployments/1", func(w http.ResponseWriter, r *http.Request) {
   143  		testMethod(t, r, "DELETE")
   144  		w.WriteHeader(http.StatusNoContent)
   145  	})
   146  
   147  	ctx := context.Background()
   148  	resp, err := client.Repositories.DeleteDeployment(ctx, "o", "r", 1)
   149  	if err != nil {
   150  		t.Errorf("Repositories.DeleteDeployment returned error: %v", err)
   151  	}
   152  	if resp.StatusCode != http.StatusNoContent {
   153  		t.Error("Repositories.DeleteDeployment should return a 204 status")
   154  	}
   155  
   156  	resp, err = client.Repositories.DeleteDeployment(ctx, "o", "r", 2)
   157  	if err == nil {
   158  		t.Error("Repositories.DeleteDeployment should return an error")
   159  	}
   160  	if resp.StatusCode != http.StatusNotFound {
   161  		t.Error("Repositories.DeleteDeployment should return a 404 status")
   162  	}
   163  
   164  	const methodName = "DeleteDeployment"
   165  	testBadOptions(t, methodName, func() (err error) {
   166  		_, err = client.Repositories.DeleteDeployment(ctx, "\n", "\n", 1)
   167  		return err
   168  	})
   169  
   170  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   171  		return client.Repositories.DeleteDeployment(ctx, "o", "r", 1)
   172  	})
   173  }
   174  
   175  func TestRepositoriesService_ListDeploymentStatuses(t *testing.T) {
   176  	t.Parallel()
   177  	client, mux, _ := setup(t)
   178  
   179  	wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   180  	mux.HandleFunc("/repos/o/r/deployments/1/statuses", func(w http.ResponseWriter, r *http.Request) {
   181  		testMethod(t, r, "GET")
   182  		testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
   183  		testFormValues(t, r, values{"page": "2"})
   184  		fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
   185  	})
   186  
   187  	opt := &ListOptions{Page: 2}
   188  	ctx := context.Background()
   189  	statuses, _, err := client.Repositories.ListDeploymentStatuses(ctx, "o", "r", 1, opt)
   190  	if err != nil {
   191  		t.Errorf("Repositories.ListDeploymentStatuses returned error: %v", err)
   192  	}
   193  
   194  	want := []*DeploymentStatus{{ID: Ptr(int64(1))}, {ID: Ptr(int64(2))}}
   195  	if !cmp.Equal(statuses, want) {
   196  		t.Errorf("Repositories.ListDeploymentStatuses returned %+v, want %+v", statuses, want)
   197  	}
   198  
   199  	const methodName = "ListDeploymentStatuses"
   200  	testBadOptions(t, methodName, func() (err error) {
   201  		_, _, err = client.Repositories.ListDeploymentStatuses(ctx, "\n", "\n", 1, opt)
   202  		return err
   203  	})
   204  
   205  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   206  		got, resp, err := client.Repositories.ListDeploymentStatuses(ctx, "o", "r", 1, opt)
   207  		if got != nil {
   208  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   209  		}
   210  		return resp, err
   211  	})
   212  }
   213  
   214  func TestRepositoriesService_GetDeploymentStatus(t *testing.T) {
   215  	t.Parallel()
   216  	client, mux, _ := setup(t)
   217  
   218  	wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   219  	mux.HandleFunc("/repos/o/r/deployments/3/statuses/4", func(w http.ResponseWriter, r *http.Request) {
   220  		testMethod(t, r, "GET")
   221  		testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
   222  		fmt.Fprint(w, `{"id":4}`)
   223  	})
   224  
   225  	ctx := context.Background()
   226  	deploymentStatus, _, err := client.Repositories.GetDeploymentStatus(ctx, "o", "r", 3, 4)
   227  	if err != nil {
   228  		t.Errorf("Repositories.GetDeploymentStatus returned error: %v", err)
   229  	}
   230  
   231  	want := &DeploymentStatus{ID: Ptr(int64(4))}
   232  	if !cmp.Equal(deploymentStatus, want) {
   233  		t.Errorf("Repositories.GetDeploymentStatus returned %+v, want %+v", deploymentStatus, want)
   234  	}
   235  
   236  	const methodName = "GetDeploymentStatus"
   237  	testBadOptions(t, methodName, func() (err error) {
   238  		_, _, err = client.Repositories.GetDeploymentStatus(ctx, "\n", "\n", 3, 4)
   239  		return err
   240  	})
   241  
   242  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   243  		got, resp, err := client.Repositories.GetDeploymentStatus(ctx, "o", "r", 3, 4)
   244  		if got != nil {
   245  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   246  		}
   247  		return resp, err
   248  	})
   249  }
   250  
   251  func TestRepositoriesService_CreateDeploymentStatus(t *testing.T) {
   252  	t.Parallel()
   253  	client, mux, _ := setup(t)
   254  
   255  	input := &DeploymentStatusRequest{State: Ptr("inactive"), Description: Ptr("deploy"), AutoInactive: Ptr(false)}
   256  
   257  	mux.HandleFunc("/repos/o/r/deployments/1/statuses", func(w http.ResponseWriter, r *http.Request) {
   258  		v := new(DeploymentStatusRequest)
   259  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
   260  
   261  		testMethod(t, r, "POST")
   262  		wantAcceptHeaders := []string{mediaTypeDeploymentStatusPreview, mediaTypeExpandDeploymentStatusPreview}
   263  		testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", "))
   264  		if !cmp.Equal(v, input) {
   265  			t.Errorf("Request body = %+v, want %+v", v, input)
   266  		}
   267  
   268  		fmt.Fprint(w, `{"state": "inactive", "description": "deploy"}`)
   269  	})
   270  
   271  	ctx := context.Background()
   272  	deploymentStatus, _, err := client.Repositories.CreateDeploymentStatus(ctx, "o", "r", 1, input)
   273  	if err != nil {
   274  		t.Errorf("Repositories.CreateDeploymentStatus returned error: %v", err)
   275  	}
   276  
   277  	want := &DeploymentStatus{State: Ptr("inactive"), Description: Ptr("deploy")}
   278  	if !cmp.Equal(deploymentStatus, want) {
   279  		t.Errorf("Repositories.CreateDeploymentStatus returned %+v, want %+v", deploymentStatus, want)
   280  	}
   281  
   282  	const methodName = "CreateDeploymentStatus"
   283  	testBadOptions(t, methodName, func() (err error) {
   284  		_, _, err = client.Repositories.CreateDeploymentStatus(ctx, "\n", "\n", 1, input)
   285  		return err
   286  	})
   287  
   288  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   289  		got, resp, err := client.Repositories.CreateDeploymentStatus(ctx, "o", "r", 1, input)
   290  		if got != nil {
   291  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   292  		}
   293  		return resp, err
   294  	})
   295  }
   296  
   297  func TestDeploymentStatusRequest_Marshal(t *testing.T) {
   298  	t.Parallel()
   299  	testJSONMarshal(t, &DeploymentStatusRequest{}, "{}")
   300  
   301  	r := &DeploymentStatusRequest{
   302  		State:          Ptr("state"),
   303  		LogURL:         Ptr("logurl"),
   304  		Description:    Ptr("desc"),
   305  		Environment:    Ptr("env"),
   306  		EnvironmentURL: Ptr("eurl"),
   307  		AutoInactive:   Ptr(false),
   308  	}
   309  
   310  	want := `{
   311  		"state": "state",
   312  		"log_url": "logurl",
   313  		"description": "desc",
   314  		"environment": "env",
   315  		"environment_url": "eurl",
   316  		"auto_inactive": false
   317  	}`
   318  
   319  	testJSONMarshal(t, r, want)
   320  }
   321  
   322  func TestDeploymentStatus_Marshal(t *testing.T) {
   323  	t.Parallel()
   324  	testJSONMarshal(t, &DeploymentStatus{}, "{}")
   325  
   326  	r := &DeploymentStatus{
   327  		ID:    Ptr(int64(1)),
   328  		State: Ptr("state"),
   329  		Creator: &User{
   330  			Login:           Ptr("l"),
   331  			ID:              Ptr(int64(1)),
   332  			URL:             Ptr("u"),
   333  			AvatarURL:       Ptr("a"),
   334  			GravatarID:      Ptr("g"),
   335  			Name:            Ptr("n"),
   336  			Company:         Ptr("c"),
   337  			Blog:            Ptr("b"),
   338  			Location:        Ptr("l"),
   339  			Email:           Ptr("e"),
   340  			Hireable:        Ptr(true),
   341  			Bio:             Ptr("b"),
   342  			TwitterUsername: Ptr("t"),
   343  			PublicRepos:     Ptr(1),
   344  			Followers:       Ptr(1),
   345  			Following:       Ptr(1),
   346  			CreatedAt:       &Timestamp{referenceTime},
   347  			SuspendedAt:     &Timestamp{referenceTime},
   348  		},
   349  		Description:    Ptr("desc"),
   350  		Environment:    Ptr("env"),
   351  		NodeID:         Ptr("nid"),
   352  		CreatedAt:      &Timestamp{referenceTime},
   353  		UpdatedAt:      &Timestamp{referenceTime},
   354  		TargetURL:      Ptr("turl"),
   355  		DeploymentURL:  Ptr("durl"),
   356  		RepositoryURL:  Ptr("rurl"),
   357  		EnvironmentURL: Ptr("eurl"),
   358  		LogURL:         Ptr("lurl"),
   359  		URL:            Ptr("url"),
   360  	}
   361  
   362  	want := `{
   363  		"id": 1,
   364  		"state": "state",
   365  		"creator": {
   366  			"login": "l",
   367  			"id": 1,
   368  			"avatar_url": "a",
   369  			"gravatar_id": "g",
   370  			"name": "n",
   371  			"company": "c",
   372  			"blog": "b",
   373  			"location": "l",
   374  			"email": "e",
   375  			"hireable": true,
   376  			"bio": "b",
   377  			"twitter_username": "t",
   378  			"public_repos": 1,
   379  			"followers": 1,
   380  			"following": 1,
   381  			"created_at": ` + referenceTimeStr + `,
   382  			"suspended_at": ` + referenceTimeStr + `,
   383  			"url": "u"
   384  		},
   385  		"description": "desc",
   386  		"environment": "env",
   387  		"node_id": "nid",
   388  		"created_at": ` + referenceTimeStr + `,
   389  		"updated_at": ` + referenceTimeStr + `,
   390  		"target_url": "turl",
   391  		"deployment_url": "durl",
   392  		"repository_url": "rurl",
   393  		"environment_url": "eurl",
   394  		"log_url": "lurl",
   395  		"url": "url"
   396  	}`
   397  
   398  	testJSONMarshal(t, r, want)
   399  }
   400  
   401  func TestDeploymentRequest_Marshal(t *testing.T) {
   402  	t.Parallel()
   403  	testJSONMarshal(t, &DeploymentRequest{}, "{}")
   404  
   405  	r := &DeploymentRequest{
   406  		Ref:                   Ptr("ref"),
   407  		Task:                  Ptr("task"),
   408  		AutoMerge:             Ptr(false),
   409  		RequiredContexts:      &[]string{"s"},
   410  		Payload:               "payload",
   411  		Environment:           Ptr("environment"),
   412  		Description:           Ptr("description"),
   413  		TransientEnvironment:  Ptr(false),
   414  		ProductionEnvironment: Ptr(false),
   415  	}
   416  
   417  	want := `{
   418  		"ref": "ref",
   419  		"task": "task",
   420  		"auto_merge": false,
   421  		"required_contexts": ["s"],
   422  		"payload": "payload",
   423  		"environment": "environment",
   424  		"description": "description",
   425  		"transient_environment": false,
   426  		"production_environment": false
   427  	}`
   428  
   429  	testJSONMarshal(t, r, want)
   430  }
   431  
   432  func TestDeployment_Marshal(t *testing.T) {
   433  	t.Parallel()
   434  	testJSONMarshal(t, &Deployment{}, "{}")
   435  
   436  	str := "s"
   437  	jsonMsg, _ := json.Marshal(str)
   438  
   439  	r := &Deployment{
   440  		URL:         Ptr("url"),
   441  		ID:          Ptr(int64(1)),
   442  		SHA:         Ptr("sha"),
   443  		Ref:         Ptr("ref"),
   444  		Task:        Ptr("task"),
   445  		Payload:     jsonMsg,
   446  		Environment: Ptr("env"),
   447  		Description: Ptr("desc"),
   448  		Creator: &User{
   449  			Login:           Ptr("l"),
   450  			ID:              Ptr(int64(1)),
   451  			URL:             Ptr("u"),
   452  			AvatarURL:       Ptr("a"),
   453  			GravatarID:      Ptr("g"),
   454  			Name:            Ptr("n"),
   455  			Company:         Ptr("c"),
   456  			Blog:            Ptr("b"),
   457  			Location:        Ptr("l"),
   458  			Email:           Ptr("e"),
   459  			Hireable:        Ptr(true),
   460  			Bio:             Ptr("b"),
   461  			TwitterUsername: Ptr("t"),
   462  			PublicRepos:     Ptr(1),
   463  			Followers:       Ptr(1),
   464  			Following:       Ptr(1),
   465  			CreatedAt:       &Timestamp{referenceTime},
   466  			SuspendedAt:     &Timestamp{referenceTime},
   467  		},
   468  		CreatedAt:     &Timestamp{referenceTime},
   469  		UpdatedAt:     &Timestamp{referenceTime},
   470  		StatusesURL:   Ptr("surl"),
   471  		RepositoryURL: Ptr("rurl"),
   472  		NodeID:        Ptr("nid"),
   473  	}
   474  
   475  	want := `{
   476  		"url": "url",
   477  		"id": 1,
   478  		"sha": "sha",
   479  		"ref": "ref",
   480  		"task": "task",
   481  		"payload": "s",
   482  		"environment": "env",
   483  		"description": "desc",
   484  		"creator": {
   485  			"login": "l",
   486  			"id": 1,
   487  			"avatar_url": "a",
   488  			"gravatar_id": "g",
   489  			"name": "n",
   490  			"company": "c",
   491  			"blog": "b",
   492  			"location": "l",
   493  			"email": "e",
   494  			"hireable": true,
   495  			"bio": "b",
   496  			"twitter_username": "t",
   497  			"public_repos": 1,
   498  			"followers": 1,
   499  			"following": 1,
   500  			"created_at": ` + referenceTimeStr + `,
   501  			"suspended_at": ` + referenceTimeStr + `,
   502  			"url": "u"
   503  		},
   504  		"created_at": ` + referenceTimeStr + `,
   505  		"updated_at": ` + referenceTimeStr + `,
   506  		"statuses_url": "surl",
   507  		"repository_url": "rurl",
   508  		"node_id": "nid"
   509  	}`
   510  
   511  	testJSONMarshal(t, r, want)
   512  }