github.com/google/go-github/v66@v66.0.0/github/issues_events_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  	"fmt"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/google/go-cmp/cmp"
    15  )
    16  
    17  func TestIssuesService_ListIssueEvents(t *testing.T) {
    18  	t.Parallel()
    19  	client, mux, _ := setup(t)
    20  
    21  	mux.HandleFunc("/repos/o/r/issues/1/events", func(w http.ResponseWriter, r *http.Request) {
    22  		testMethod(t, r, "GET")
    23  		testHeader(t, r, "Accept", mediaTypeProjectCardDetailsPreview)
    24  		testFormValues(t, r, values{
    25  			"page":     "1",
    26  			"per_page": "2",
    27  		})
    28  		fmt.Fprint(w, `[{"id":1}]`)
    29  	})
    30  
    31  	opt := &ListOptions{Page: 1, PerPage: 2}
    32  	ctx := context.Background()
    33  	events, _, err := client.Issues.ListIssueEvents(ctx, "o", "r", 1, opt)
    34  	if err != nil {
    35  		t.Errorf("Issues.ListIssueEvents returned error: %v", err)
    36  	}
    37  
    38  	want := []*IssueEvent{{ID: Int64(1)}}
    39  	if !cmp.Equal(events, want) {
    40  		t.Errorf("Issues.ListIssueEvents returned %+v, want %+v", events, want)
    41  	}
    42  
    43  	const methodName = "ListIssueEvents"
    44  	testBadOptions(t, methodName, func() (err error) {
    45  		_, _, err = client.Issues.ListIssueEvents(ctx, "\n", "\n", -1, &ListOptions{})
    46  		return err
    47  	})
    48  
    49  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    50  		got, resp, err := client.Issues.ListIssueEvents(ctx, "o", "r", 1, nil)
    51  		if got != nil {
    52  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    53  		}
    54  		return resp, err
    55  	})
    56  }
    57  
    58  func TestIssuesService_ListRepositoryEvents(t *testing.T) {
    59  	t.Parallel()
    60  	client, mux, _ := setup(t)
    61  
    62  	mux.HandleFunc("/repos/o/r/issues/events", func(w http.ResponseWriter, r *http.Request) {
    63  		testMethod(t, r, "GET")
    64  		testFormValues(t, r, values{
    65  			"page":     "1",
    66  			"per_page": "2",
    67  		})
    68  		fmt.Fprint(w, `[{"id":1}]`)
    69  	})
    70  
    71  	opt := &ListOptions{Page: 1, PerPage: 2}
    72  	ctx := context.Background()
    73  	events, _, err := client.Issues.ListRepositoryEvents(ctx, "o", "r", opt)
    74  	if err != nil {
    75  		t.Errorf("Issues.ListRepositoryEvents returned error: %v", err)
    76  	}
    77  
    78  	want := []*IssueEvent{{ID: Int64(1)}}
    79  	if !cmp.Equal(events, want) {
    80  		t.Errorf("Issues.ListRepositoryEvents returned %+v, want %+v", events, want)
    81  	}
    82  
    83  	const methodName = "ListRepositoryEvents"
    84  	testBadOptions(t, methodName, func() (err error) {
    85  		_, _, err = client.Issues.ListRepositoryEvents(ctx, "\n", "\n", &ListOptions{})
    86  		return err
    87  	})
    88  
    89  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    90  		got, resp, err := client.Issues.ListRepositoryEvents(ctx, "o", "r", nil)
    91  		if got != nil {
    92  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    93  		}
    94  		return resp, err
    95  	})
    96  }
    97  
    98  func TestIssuesService_GetEvent(t *testing.T) {
    99  	t.Parallel()
   100  	client, mux, _ := setup(t)
   101  
   102  	mux.HandleFunc("/repos/o/r/issues/events/1", func(w http.ResponseWriter, r *http.Request) {
   103  		testMethod(t, r, "GET")
   104  		fmt.Fprint(w, `{"id":1}`)
   105  	})
   106  
   107  	ctx := context.Background()
   108  	event, _, err := client.Issues.GetEvent(ctx, "o", "r", 1)
   109  	if err != nil {
   110  		t.Errorf("Issues.GetEvent returned error: %v", err)
   111  	}
   112  
   113  	want := &IssueEvent{ID: Int64(1)}
   114  	if !cmp.Equal(event, want) {
   115  		t.Errorf("Issues.GetEvent returned %+v, want %+v", event, want)
   116  	}
   117  
   118  	const methodName = "GetEvent"
   119  	testBadOptions(t, methodName, func() (err error) {
   120  		_, _, err = client.Issues.GetEvent(ctx, "\n", "\n", -1)
   121  		return err
   122  	})
   123  
   124  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   125  		got, resp, err := client.Issues.GetEvent(ctx, "o", "r", 1)
   126  		if got != nil {
   127  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   128  		}
   129  		return resp, err
   130  	})
   131  }
   132  
   133  func TestRename_Marshal(t *testing.T) {
   134  	t.Parallel()
   135  	testJSONMarshal(t, &Rename{}, "{}")
   136  
   137  	u := &Rename{
   138  		From: String("from"),
   139  		To:   String("to"),
   140  	}
   141  
   142  	want := `{
   143  		"from": "from",
   144  		"to": "to"
   145  	}`
   146  
   147  	testJSONMarshal(t, u, want)
   148  }
   149  
   150  func TestDismissedReview_Marshal(t *testing.T) {
   151  	t.Parallel()
   152  	testJSONMarshal(t, &DismissedReview{}, "{}")
   153  
   154  	u := &DismissedReview{
   155  		State:             String("state"),
   156  		ReviewID:          Int64(1),
   157  		DismissalMessage:  String("dm"),
   158  		DismissalCommitID: String("dcid"),
   159  	}
   160  
   161  	want := `{
   162  		"state": "state",
   163  		"review_id": 1,
   164  		"dismissal_message": "dm",
   165  		"dismissal_commit_id": "dcid"
   166  	}`
   167  
   168  	testJSONMarshal(t, u, want)
   169  }
   170  
   171  func TestIssueEvent_Marshal(t *testing.T) {
   172  	t.Parallel()
   173  	testJSONMarshal(t, &IssueEvent{}, "{}")
   174  
   175  	u := &IssueEvent{
   176  		ID:  Int64(1),
   177  		URL: String("url"),
   178  		Actor: &User{
   179  			Login:           String("l"),
   180  			ID:              Int64(1),
   181  			URL:             String("u"),
   182  			AvatarURL:       String("a"),
   183  			GravatarID:      String("g"),
   184  			Name:            String("n"),
   185  			Company:         String("c"),
   186  			Blog:            String("b"),
   187  			Location:        String("l"),
   188  			Email:           String("e"),
   189  			Hireable:        Bool(true),
   190  			Bio:             String("b"),
   191  			TwitterUsername: String("t"),
   192  			PublicRepos:     Int(1),
   193  			Followers:       Int(1),
   194  			Following:       Int(1),
   195  			CreatedAt:       &Timestamp{referenceTime},
   196  			SuspendedAt:     &Timestamp{referenceTime},
   197  		},
   198  		Event:     String("event"),
   199  		CreatedAt: &Timestamp{referenceTime},
   200  		Issue:     &Issue{ID: Int64(1)},
   201  		Assignee: &User{
   202  			Login:           String("l"),
   203  			ID:              Int64(1),
   204  			URL:             String("u"),
   205  			AvatarURL:       String("a"),
   206  			GravatarID:      String("g"),
   207  			Name:            String("n"),
   208  			Company:         String("c"),
   209  			Blog:            String("b"),
   210  			Location:        String("l"),
   211  			Email:           String("e"),
   212  			Hireable:        Bool(true),
   213  			Bio:             String("b"),
   214  			TwitterUsername: String("t"),
   215  			PublicRepos:     Int(1),
   216  			Followers:       Int(1),
   217  			Following:       Int(1),
   218  			CreatedAt:       &Timestamp{referenceTime},
   219  			SuspendedAt:     &Timestamp{referenceTime},
   220  		},
   221  		Assigner: &User{
   222  			Login:           String("l"),
   223  			ID:              Int64(1),
   224  			URL:             String("u"),
   225  			AvatarURL:       String("a"),
   226  			GravatarID:      String("g"),
   227  			Name:            String("n"),
   228  			Company:         String("c"),
   229  			Blog:            String("b"),
   230  			Location:        String("l"),
   231  			Email:           String("e"),
   232  			Hireable:        Bool(true),
   233  			Bio:             String("b"),
   234  			TwitterUsername: String("t"),
   235  			PublicRepos:     Int(1),
   236  			Followers:       Int(1),
   237  			Following:       Int(1),
   238  			CreatedAt:       &Timestamp{referenceTime},
   239  			SuspendedAt:     &Timestamp{referenceTime},
   240  		},
   241  		CommitID:  String("cid"),
   242  		Milestone: &Milestone{ID: Int64(1)},
   243  		Label:     &Label{ID: Int64(1)},
   244  		Rename: &Rename{
   245  			From: String("from"),
   246  			To:   String("to"),
   247  		},
   248  		LockReason:  String("lr"),
   249  		ProjectCard: &ProjectCard{ID: Int64(1)},
   250  		DismissedReview: &DismissedReview{
   251  			State:             String("state"),
   252  			ReviewID:          Int64(1),
   253  			DismissalMessage:  String("dm"),
   254  			DismissalCommitID: String("dcid"),
   255  		},
   256  		RequestedReviewer: &User{
   257  			Login:           String("l"),
   258  			ID:              Int64(1),
   259  			URL:             String("u"),
   260  			AvatarURL:       String("a"),
   261  			GravatarID:      String("g"),
   262  			Name:            String("n"),
   263  			Company:         String("c"),
   264  			Blog:            String("b"),
   265  			Location:        String("l"),
   266  			Email:           String("e"),
   267  			Hireable:        Bool(true),
   268  			Bio:             String("b"),
   269  			TwitterUsername: String("t"),
   270  			PublicRepos:     Int(1),
   271  			Followers:       Int(1),
   272  			Following:       Int(1),
   273  			CreatedAt:       &Timestamp{referenceTime},
   274  			SuspendedAt:     &Timestamp{referenceTime},
   275  		},
   276  		RequestedTeam: &Team{
   277  			ID:              Int64(1),
   278  			NodeID:          String("n"),
   279  			Name:            String("n"),
   280  			Description:     String("d"),
   281  			URL:             String("u"),
   282  			Slug:            String("s"),
   283  			Permission:      String("p"),
   284  			Privacy:         String("p"),
   285  			MembersCount:    Int(1),
   286  			ReposCount:      Int(1),
   287  			MembersURL:      String("m"),
   288  			RepositoriesURL: String("r"),
   289  			Organization: &Organization{
   290  				Login:     String("l"),
   291  				ID:        Int64(1),
   292  				NodeID:    String("n"),
   293  				AvatarURL: String("a"),
   294  				HTMLURL:   String("h"),
   295  				Name:      String("n"),
   296  				Company:   String("c"),
   297  				Blog:      String("b"),
   298  				Location:  String("l"),
   299  				Email:     String("e"),
   300  			},
   301  			Parent: &Team{
   302  				ID:           Int64(1),
   303  				NodeID:       String("n"),
   304  				Name:         String("n"),
   305  				Description:  String("d"),
   306  				URL:          String("u"),
   307  				Slug:         String("s"),
   308  				Permission:   String("p"),
   309  				Privacy:      String("p"),
   310  				MembersCount: Int(1),
   311  				ReposCount:   Int(1),
   312  			},
   313  			LDAPDN: String("l"),
   314  		},
   315  		PerformedViaGithubApp: &App{
   316  			ID:     Int64(1),
   317  			NodeID: String("n"),
   318  			Owner: &User{
   319  				Login:     String("l"),
   320  				ID:        Int64(1),
   321  				NodeID:    String("n"),
   322  				URL:       String("u"),
   323  				ReposURL:  String("r"),
   324  				EventsURL: String("e"),
   325  				AvatarURL: String("a"),
   326  			},
   327  			Name:        String("n"),
   328  			Description: String("d"),
   329  			HTMLURL:     String("h"),
   330  			ExternalURL: String("u"),
   331  		},
   332  		ReviewRequester: &User{
   333  			Login:           String("l"),
   334  			ID:              Int64(1),
   335  			URL:             String("u"),
   336  			AvatarURL:       String("a"),
   337  			GravatarID:      String("g"),
   338  			Name:            String("n"),
   339  			Company:         String("c"),
   340  			Blog:            String("b"),
   341  			Location:        String("l"),
   342  			Email:           String("e"),
   343  			Hireable:        Bool(true),
   344  			Bio:             String("b"),
   345  			TwitterUsername: String("t"),
   346  			PublicRepos:     Int(1),
   347  			Followers:       Int(1),
   348  			Following:       Int(1),
   349  			CreatedAt:       &Timestamp{referenceTime},
   350  			SuspendedAt:     &Timestamp{referenceTime},
   351  		},
   352  	}
   353  
   354  	want := `{
   355  		"id": 1,
   356  		"url": "url",
   357  		"actor": {
   358  			"login": "l",
   359  			"id": 1,
   360  			"avatar_url": "a",
   361  			"gravatar_id": "g",
   362  			"name": "n",
   363  			"company": "c",
   364  			"blog": "b",
   365  			"location": "l",
   366  			"email": "e",
   367  			"hireable": true,
   368  			"bio": "b",
   369  			"twitter_username": "t",
   370  			"public_repos": 1,
   371  			"followers": 1,
   372  			"following": 1,
   373  			"created_at": ` + referenceTimeStr + `,
   374  			"suspended_at": ` + referenceTimeStr + `,
   375  			"url": "u"
   376  		},
   377  		"event": "event",
   378  		"created_at": ` + referenceTimeStr + `,
   379  		"issue": {
   380  			"id": 1
   381  		},
   382  		"assignee": {
   383  			"login": "l",
   384  			"id": 1,
   385  			"avatar_url": "a",
   386  			"gravatar_id": "g",
   387  			"name": "n",
   388  			"company": "c",
   389  			"blog": "b",
   390  			"location": "l",
   391  			"email": "e",
   392  			"hireable": true,
   393  			"bio": "b",
   394  			"twitter_username": "t",
   395  			"public_repos": 1,
   396  			"followers": 1,
   397  			"following": 1,
   398  			"created_at": ` + referenceTimeStr + `,
   399  			"suspended_at": ` + referenceTimeStr + `,
   400  			"url": "u"
   401  		},
   402  		"assigner": {
   403  			"login": "l",
   404  			"id": 1,
   405  			"avatar_url": "a",
   406  			"gravatar_id": "g",
   407  			"name": "n",
   408  			"company": "c",
   409  			"blog": "b",
   410  			"location": "l",
   411  			"email": "e",
   412  			"hireable": true,
   413  			"bio": "b",
   414  			"twitter_username": "t",
   415  			"public_repos": 1,
   416  			"followers": 1,
   417  			"following": 1,
   418  			"created_at": ` + referenceTimeStr + `,
   419  			"suspended_at": ` + referenceTimeStr + `,
   420  			"url": "u"
   421  		},
   422  		"commit_id": "cid",
   423  		"milestone": {
   424  			"id": 1
   425  		},
   426  		"label": {
   427  			"id": 1
   428  		},
   429  		"rename": {
   430  			"from": "from",
   431  			"to": "to"
   432  		},
   433  		"lock_reason": "lr",
   434  		"project_card": {
   435  			"id": 1
   436  		},
   437  		"dismissed_review": {
   438  			"state": "state",
   439  			"review_id": 1,
   440  			"dismissal_message": "dm",
   441  			"dismissal_commit_id": "dcid"
   442  		},
   443  		"requested_reviewer": {
   444  			"login": "l",
   445  			"id": 1,
   446  			"avatar_url": "a",
   447  			"gravatar_id": "g",
   448  			"name": "n",
   449  			"company": "c",
   450  			"blog": "b",
   451  			"location": "l",
   452  			"email": "e",
   453  			"hireable": true,
   454  			"bio": "b",
   455  			"twitter_username": "t",
   456  			"public_repos": 1,
   457  			"followers": 1,
   458  			"following": 1,
   459  			"created_at": ` + referenceTimeStr + `,
   460  			"suspended_at": ` + referenceTimeStr + `,
   461  			"url": "u"
   462  		},
   463  		"requested_team": {
   464  			"id": 1,
   465  			"node_id": "n",
   466  			"name": "n",
   467  			"description": "d",
   468  			"url": "u",
   469  			"slug": "s",
   470  			"permission": "p",
   471  			"privacy": "p",
   472  			"members_count": 1,
   473  			"repos_count": 1,
   474  			"members_url": "m",
   475  			"repositories_url": "r",
   476  			"organization": {
   477  				"login": "l",
   478  				"id": 1,
   479  				"node_id": "n",
   480  				"avatar_url": "a",
   481  				"html_url": "h",
   482  				"name": "n",
   483  				"company": "c",
   484  				"blog": "b",
   485  				"location": "l",
   486  				"email": "e"
   487  			},
   488  			"parent": {
   489  				"id": 1,
   490  				"node_id": "n",
   491  				"name": "n",
   492  				"description": "d",
   493  				"url": "u",
   494  				"slug": "s",
   495  				"permission": "p",
   496  				"privacy": "p",
   497  				"members_count": 1,
   498  				"repos_count": 1
   499  			},
   500  			"ldap_dn": "l"
   501  		},
   502  		"performed_via_github_app": {
   503  			"id": 1,
   504  			"node_id": "n",
   505  			"owner": {
   506  				"login": "l",
   507  				"id": 1,
   508  				"node_id": "n",
   509  				"url": "u",
   510  				"repos_url": "r",
   511  				"events_url": "e",
   512  				"avatar_url": "a"
   513  			},
   514  			"name": "n",
   515  			"description": "d",
   516  			"html_url": "h",
   517  			"external_url": "u"
   518  		},
   519  		"review_requester": {
   520  			"login": "l",
   521  			"id": 1,
   522  			"avatar_url": "a",
   523  			"gravatar_id": "g",
   524  			"name": "n",
   525  			"company": "c",
   526  			"blog": "b",
   527  			"location": "l",
   528  			"email": "e",
   529  			"hireable": true,
   530  			"bio": "b",
   531  			"twitter_username": "t",
   532  			"public_repos": 1,
   533  			"followers": 1,
   534  			"following": 1,
   535  			"created_at": ` + referenceTimeStr + `,
   536  			"suspended_at": ` + referenceTimeStr + `,
   537  			"url": "u"
   538  		}
   539  	}`
   540  
   541  	testJSONMarshal(t, u, want)
   542  }