...

Source file src/github.com/google/go-github/v55/github/activity_star_test.go

Documentation: github.com/google/go-github/v55/github

     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  	"fmt"
    11  	"net/http"
    12  	"strings"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestActivityService_ListStargazers(t *testing.T) {
    20  	client, mux, _, teardown := setup()
    21  	defer teardown()
    22  
    23  	mux.HandleFunc("/repos/o/r/stargazers", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  		testHeader(t, r, "Accept", mediaTypeStarringPreview)
    26  		testFormValues(t, r, values{
    27  			"page": "2",
    28  		})
    29  
    30  		fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","user":{"id":1}}]`)
    31  	})
    32  
    33  	ctx := context.Background()
    34  	stargazers, _, err := client.Activity.ListStargazers(ctx, "o", "r", &ListOptions{Page: 2})
    35  	if err != nil {
    36  		t.Errorf("Activity.ListStargazers returned error: %v", err)
    37  	}
    38  
    39  	want := []*Stargazer{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, User: &User{ID: Int64(1)}}}
    40  	if !cmp.Equal(stargazers, want) {
    41  		t.Errorf("Activity.ListStargazers returned %+v, want %+v", stargazers, want)
    42  	}
    43  
    44  	const methodName = "ListStargazers"
    45  	testBadOptions(t, methodName, func() (err error) {
    46  		_, _, err = client.Activity.ListStargazers(ctx, "\n", "\n", &ListOptions{Page: 2})
    47  		return err
    48  	})
    49  
    50  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    51  		got, resp, err := client.Activity.ListStargazers(ctx, "o", "r", &ListOptions{Page: 2})
    52  		if got != nil {
    53  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    54  		}
    55  		return resp, err
    56  	})
    57  }
    58  
    59  func TestActivityService_ListStarred_authenticatedUser(t *testing.T) {
    60  	client, mux, _, teardown := setup()
    61  	defer teardown()
    62  
    63  	mux.HandleFunc("/user/starred", func(w http.ResponseWriter, r *http.Request) {
    64  		testMethod(t, r, "GET")
    65  		testHeader(t, r, "Accept", strings.Join([]string{mediaTypeStarringPreview, mediaTypeTopicsPreview}, ", "))
    66  		fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","repo":{"id":1}}]`)
    67  	})
    68  
    69  	ctx := context.Background()
    70  	repos, _, err := client.Activity.ListStarred(ctx, "", nil)
    71  	if err != nil {
    72  		t.Errorf("Activity.ListStarred returned error: %v", err)
    73  	}
    74  
    75  	want := []*StarredRepository{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, Repository: &Repository{ID: Int64(1)}}}
    76  	if !cmp.Equal(repos, want) {
    77  		t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want)
    78  	}
    79  
    80  	const methodName = "ListStarred"
    81  	testBadOptions(t, methodName, func() (err error) {
    82  		_, _, err = client.Activity.ListStarred(ctx, "\n", nil)
    83  		return err
    84  	})
    85  
    86  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    87  		got, resp, err := client.Activity.ListStarred(ctx, "", nil)
    88  		if got != nil {
    89  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    90  		}
    91  		return resp, err
    92  	})
    93  }
    94  
    95  func TestActivityService_ListStarred_specifiedUser(t *testing.T) {
    96  	client, mux, _, teardown := setup()
    97  	defer teardown()
    98  
    99  	mux.HandleFunc("/users/u/starred", func(w http.ResponseWriter, r *http.Request) {
   100  		testMethod(t, r, "GET")
   101  		testHeader(t, r, "Accept", strings.Join([]string{mediaTypeStarringPreview, mediaTypeTopicsPreview}, ", "))
   102  		testFormValues(t, r, values{
   103  			"sort":      "created",
   104  			"direction": "asc",
   105  			"page":      "2",
   106  		})
   107  		fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","repo":{"id":2}}]`)
   108  	})
   109  
   110  	opt := &ActivityListStarredOptions{"created", "asc", ListOptions{Page: 2}}
   111  	ctx := context.Background()
   112  	repos, _, err := client.Activity.ListStarred(ctx, "u", opt)
   113  	if err != nil {
   114  		t.Errorf("Activity.ListStarred returned error: %v", err)
   115  	}
   116  
   117  	want := []*StarredRepository{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, Repository: &Repository{ID: Int64(2)}}}
   118  	if !cmp.Equal(repos, want) {
   119  		t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want)
   120  	}
   121  
   122  	const methodName = "ListStarred"
   123  	testBadOptions(t, methodName, func() (err error) {
   124  		_, _, err = client.Activity.ListStarred(ctx, "\n", opt)
   125  		return err
   126  	})
   127  
   128  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   129  		got, resp, err := client.Activity.ListStarred(ctx, "u", opt)
   130  		if got != nil {
   131  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   132  		}
   133  		return resp, err
   134  	})
   135  }
   136  
   137  func TestActivityService_ListStarred_invalidUser(t *testing.T) {
   138  	client, _, _, teardown := setup()
   139  	defer teardown()
   140  
   141  	ctx := context.Background()
   142  	_, _, err := client.Activity.ListStarred(ctx, "%", nil)
   143  	testURLParseError(t, err)
   144  }
   145  
   146  func TestActivityService_IsStarred_hasStar(t *testing.T) {
   147  	client, mux, _, teardown := setup()
   148  	defer teardown()
   149  
   150  	mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
   151  		testMethod(t, r, "GET")
   152  		w.WriteHeader(http.StatusNoContent)
   153  	})
   154  
   155  	ctx := context.Background()
   156  	star, _, err := client.Activity.IsStarred(ctx, "o", "r")
   157  	if err != nil {
   158  		t.Errorf("Activity.IsStarred returned error: %v", err)
   159  	}
   160  	if want := true; star != want {
   161  		t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want)
   162  	}
   163  
   164  	const methodName = "IsStarred"
   165  	testBadOptions(t, methodName, func() (err error) {
   166  		_, _, err = client.Activity.IsStarred(ctx, "\n", "\n")
   167  		return err
   168  	})
   169  
   170  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   171  		got, resp, err := client.Activity.IsStarred(ctx, "o", "r")
   172  		if got {
   173  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
   174  		}
   175  		return resp, err
   176  	})
   177  }
   178  
   179  func TestActivityService_IsStarred_noStar(t *testing.T) {
   180  	client, mux, _, teardown := setup()
   181  	defer teardown()
   182  
   183  	mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
   184  		testMethod(t, r, "GET")
   185  		w.WriteHeader(http.StatusNotFound)
   186  	})
   187  
   188  	ctx := context.Background()
   189  	star, _, err := client.Activity.IsStarred(ctx, "o", "r")
   190  	if err != nil {
   191  		t.Errorf("Activity.IsStarred returned error: %v", err)
   192  	}
   193  	if want := false; star != want {
   194  		t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want)
   195  	}
   196  
   197  	const methodName = "IsStarred"
   198  	testBadOptions(t, methodName, func() (err error) {
   199  		_, _, err = client.Activity.IsStarred(ctx, "\n", "\n")
   200  		return err
   201  	})
   202  
   203  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   204  		got, resp, err := client.Activity.IsStarred(ctx, "o", "r")
   205  		if got {
   206  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got)
   207  		}
   208  		return resp, err
   209  	})
   210  }
   211  
   212  func TestActivityService_IsStarred_invalidID(t *testing.T) {
   213  	client, _, _, teardown := setup()
   214  	defer teardown()
   215  
   216  	ctx := context.Background()
   217  	_, _, err := client.Activity.IsStarred(ctx, "%", "%")
   218  	testURLParseError(t, err)
   219  }
   220  
   221  func TestActivityService_Star(t *testing.T) {
   222  	client, mux, _, teardown := setup()
   223  	defer teardown()
   224  
   225  	mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
   226  		testMethod(t, r, "PUT")
   227  	})
   228  
   229  	ctx := context.Background()
   230  	_, err := client.Activity.Star(ctx, "o", "r")
   231  	if err != nil {
   232  		t.Errorf("Activity.Star returned error: %v", err)
   233  	}
   234  
   235  	const methodName = "Star"
   236  	testBadOptions(t, methodName, func() (err error) {
   237  		_, err = client.Activity.Star(ctx, "\n", "\n")
   238  		return err
   239  	})
   240  
   241  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   242  		return client.Activity.Star(ctx, "o", "r")
   243  	})
   244  }
   245  
   246  func TestActivityService_Star_invalidID(t *testing.T) {
   247  	client, _, _, teardown := setup()
   248  	defer teardown()
   249  
   250  	ctx := context.Background()
   251  	_, err := client.Activity.Star(ctx, "%", "%")
   252  	testURLParseError(t, err)
   253  }
   254  
   255  func TestActivityService_Unstar(t *testing.T) {
   256  	client, mux, _, teardown := setup()
   257  	defer teardown()
   258  
   259  	mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
   260  		testMethod(t, r, "DELETE")
   261  	})
   262  
   263  	ctx := context.Background()
   264  	_, err := client.Activity.Unstar(ctx, "o", "r")
   265  	if err != nil {
   266  		t.Errorf("Activity.Unstar returned error: %v", err)
   267  	}
   268  
   269  	const methodName = "Unstar"
   270  	testBadOptions(t, methodName, func() (err error) {
   271  		_, err = client.Activity.Unstar(ctx, "\n", "\n")
   272  		return err
   273  	})
   274  
   275  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   276  		return client.Activity.Unstar(ctx, "o", "r")
   277  	})
   278  }
   279  
   280  func TestActivityService_Unstar_invalidID(t *testing.T) {
   281  	client, _, _, teardown := setup()
   282  	defer teardown()
   283  
   284  	ctx := context.Background()
   285  	_, err := client.Activity.Unstar(ctx, "%", "%")
   286  	testURLParseError(t, err)
   287  }
   288  
   289  func TestStarredRepository_Marshal(t *testing.T) {
   290  	testJSONMarshal(t, &StarredRepository{}, "{}")
   291  
   292  	u := &StarredRepository{
   293  		StarredAt: &Timestamp{referenceTime},
   294  		Repository: &Repository{
   295  			ID:   Int64(1),
   296  			URL:  String("u"),
   297  			Name: String("n"),
   298  		},
   299  	}
   300  
   301  	want := `{
   302  		"starred_at": ` + referenceTimeStr + `,
   303  		"repo": {
   304  			"id": 1,
   305  			"url": "u",
   306  			"name": "n"
   307  		}
   308  	}`
   309  
   310  	testJSONMarshal(t, u, want)
   311  }
   312  
   313  func TestStargazer_Marshal(t *testing.T) {
   314  	testJSONMarshal(t, &Stargazer{}, "{}")
   315  
   316  	u := &Stargazer{
   317  		StarredAt: &Timestamp{referenceTime},
   318  		User: &User{
   319  			Login:           String("l"),
   320  			ID:              Int64(1),
   321  			URL:             String("u"),
   322  			AvatarURL:       String("a"),
   323  			GravatarID:      String("g"),
   324  			Name:            String("n"),
   325  			Company:         String("c"),
   326  			Blog:            String("b"),
   327  			Location:        String("l"),
   328  			Email:           String("e"),
   329  			Hireable:        Bool(true),
   330  			Bio:             String("b"),
   331  			TwitterUsername: String("t"),
   332  			PublicRepos:     Int(1),
   333  			Followers:       Int(1),
   334  			Following:       Int(1),
   335  			CreatedAt:       &Timestamp{referenceTime},
   336  			SuspendedAt:     &Timestamp{referenceTime},
   337  		},
   338  	}
   339  
   340  	want := `{
   341  		"starred_at": ` + referenceTimeStr + `,
   342  		"user": {
   343  			"login": "l",
   344  			"id": 1,
   345  			"avatar_url": "a",
   346  			"gravatar_id": "g",
   347  			"name": "n",
   348  			"company": "c",
   349  			"blog": "b",
   350  			"location": "l",
   351  			"email": "e",
   352  			"hireable": true,
   353  			"bio": "b",
   354  			"twitter_username": "t",
   355  			"public_repos": 1,
   356  			"followers": 1,
   357  			"following": 1,
   358  			"created_at": ` + referenceTimeStr + `,
   359  			"suspended_at": ` + referenceTimeStr + `,
   360  			"url": "u"
   361  		}
   362  	}`
   363  
   364  	testJSONMarshal(t, u, want)
   365  }
   366  

View as plain text