github.com/google/go-github/v69@v69.2.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  	t.Parallel()
    20  	client, mux, _ := setup(t)
    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: Ptr(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  	t.Parallel()
    57  	client, _, _ := setup(t)
    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  	t.Parallel()
    66  	client, mux, _ := setup(t)
    67  
    68  	input := &RepoStatus{State: Ptr("s"), TargetURL: Ptr("t"), Description: Ptr("d")}
    69  
    70  	mux.HandleFunc("/repos/o/r/statuses/r", func(w http.ResponseWriter, r *http.Request) {
    71  		v := new(RepoStatus)
    72  		assertNilError(t, 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: Ptr(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  	t.Parallel()
   109  	client, _, _ := setup(t)
   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  	t.Parallel()
   118  	client, mux, _ := setup(t)
   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: Ptr("success"), Statuses: []*RepoStatus{{ID: Ptr(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  	t.Parallel()
   155  	testJSONMarshal(t, &RepoStatus{}, "{}")
   156  
   157  	u := &RepoStatus{
   158  		ID:          Ptr(int64(1)),
   159  		NodeID:      Ptr("nid"),
   160  		URL:         Ptr("url"),
   161  		State:       Ptr("state"),
   162  		TargetURL:   Ptr("turl"),
   163  		Description: Ptr("desc"),
   164  		Context:     Ptr("ctx"),
   165  		AvatarURL:   Ptr("aurl"),
   166  		Creator:     &User{ID: Ptr(int64(1))},
   167  		CreatedAt:   &Timestamp{referenceTime},
   168  		UpdatedAt:   &Timestamp{referenceTime},
   169  	}
   170  
   171  	want := `{
   172  		"id": 1,
   173  		"node_id": "nid",
   174  		"url": "url",
   175  		"state": "state",
   176  		"target_url": "turl",
   177  		"description": "desc",
   178  		"context": "ctx",
   179  		"avatar_url": "aurl",
   180  		"creator": {
   181  			"id": 1
   182  		},
   183  		"created_at": ` + referenceTimeStr + `,
   184  		"updated_at": ` + referenceTimeStr + `
   185  	}`
   186  
   187  	testJSONMarshal(t, u, want)
   188  }
   189  
   190  func TestCombinedStatus_Marshal(t *testing.T) {
   191  	t.Parallel()
   192  	testJSONMarshal(t, &CombinedStatus{}, "{}")
   193  
   194  	u := &CombinedStatus{
   195  		State:      Ptr("state"),
   196  		Name:       Ptr("name"),
   197  		SHA:        Ptr("sha"),
   198  		TotalCount: Ptr(1),
   199  		Statuses: []*RepoStatus{
   200  			{
   201  				ID:          Ptr(int64(1)),
   202  				NodeID:      Ptr("nid"),
   203  				URL:         Ptr("url"),
   204  				State:       Ptr("state"),
   205  				TargetURL:   Ptr("turl"),
   206  				Description: Ptr("desc"),
   207  				Context:     Ptr("ctx"),
   208  				AvatarURL:   Ptr("aurl"),
   209  				Creator:     &User{ID: Ptr(int64(1))},
   210  				CreatedAt:   &Timestamp{referenceTime},
   211  				UpdatedAt:   &Timestamp{referenceTime},
   212  			},
   213  		},
   214  		CommitURL:     Ptr("curl"),
   215  		RepositoryURL: Ptr("rurl"),
   216  	}
   217  
   218  	want := `{
   219  		"state": "state",
   220  		"name": "name",
   221  		"sha": "sha",
   222  		"total_count": 1,
   223  		"statuses": [
   224  			{
   225  				"id": 1,
   226  				"node_id": "nid",
   227  				"url": "url",
   228  				"state": "state",
   229  				"target_url": "turl",
   230  				"description": "desc",
   231  				"context": "ctx",
   232  				"avatar_url": "aurl",
   233  				"creator": {
   234  					"id": 1
   235  				},
   236  				"created_at": ` + referenceTimeStr + `,
   237  				"updated_at": ` + referenceTimeStr + `
   238  			}
   239  		],
   240  		"commit_url": "curl",
   241  		"repository_url": "rurl"
   242  	}`
   243  
   244  	testJSONMarshal(t, u, want)
   245  }