github.com/google/go-github/v33@v33.0.0/github/activity_star_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  	"fmt"
    11  	"net/http"
    12  	"reflect"
    13  	"strings"
    14  	"testing"
    15  	"time"
    16  )
    17  
    18  func TestActivityService_ListStargazers(t *testing.T) {
    19  	client, mux, _, teardown := setup()
    20  	defer teardown()
    21  
    22  	mux.HandleFunc("/repos/o/r/stargazers", func(w http.ResponseWriter, r *http.Request) {
    23  		testMethod(t, r, "GET")
    24  		testHeader(t, r, "Accept", mediaTypeStarringPreview)
    25  		testFormValues(t, r, values{
    26  			"page": "2",
    27  		})
    28  
    29  		fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","user":{"id":1}}]`)
    30  	})
    31  
    32  	stargazers, _, err := client.Activity.ListStargazers(context.Background(), "o", "r", &ListOptions{Page: 2})
    33  	if err != nil {
    34  		t.Errorf("Activity.ListStargazers returned error: %v", err)
    35  	}
    36  
    37  	want := []*Stargazer{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, User: &User{ID: Int64(1)}}}
    38  	if !reflect.DeepEqual(stargazers, want) {
    39  		t.Errorf("Activity.ListStargazers returned %+v, want %+v", stargazers, want)
    40  	}
    41  }
    42  
    43  func TestActivityService_ListStarred_authenticatedUser(t *testing.T) {
    44  	client, mux, _, teardown := setup()
    45  	defer teardown()
    46  
    47  	mux.HandleFunc("/user/starred", func(w http.ResponseWriter, r *http.Request) {
    48  		testMethod(t, r, "GET")
    49  		testHeader(t, r, "Accept", strings.Join([]string{mediaTypeStarringPreview, mediaTypeTopicsPreview}, ", "))
    50  		fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","repo":{"id":1}}]`)
    51  	})
    52  
    53  	repos, _, err := client.Activity.ListStarred(context.Background(), "", nil)
    54  	if err != nil {
    55  		t.Errorf("Activity.ListStarred returned error: %v", err)
    56  	}
    57  
    58  	want := []*StarredRepository{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, Repository: &Repository{ID: Int64(1)}}}
    59  	if !reflect.DeepEqual(repos, want) {
    60  		t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want)
    61  	}
    62  }
    63  
    64  func TestActivityService_ListStarred_specifiedUser(t *testing.T) {
    65  	client, mux, _, teardown := setup()
    66  	defer teardown()
    67  
    68  	mux.HandleFunc("/users/u/starred", func(w http.ResponseWriter, r *http.Request) {
    69  		testMethod(t, r, "GET")
    70  		testHeader(t, r, "Accept", strings.Join([]string{mediaTypeStarringPreview, mediaTypeTopicsPreview}, ", "))
    71  		testFormValues(t, r, values{
    72  			"sort":      "created",
    73  			"direction": "asc",
    74  			"page":      "2",
    75  		})
    76  		fmt.Fprint(w, `[{"starred_at":"2002-02-10T15:30:00Z","repo":{"id":2}}]`)
    77  	})
    78  
    79  	opt := &ActivityListStarredOptions{"created", "asc", ListOptions{Page: 2}}
    80  	repos, _, err := client.Activity.ListStarred(context.Background(), "u", opt)
    81  	if err != nil {
    82  		t.Errorf("Activity.ListStarred returned error: %v", err)
    83  	}
    84  
    85  	want := []*StarredRepository{{StarredAt: &Timestamp{time.Date(2002, time.February, 10, 15, 30, 0, 0, time.UTC)}, Repository: &Repository{ID: Int64(2)}}}
    86  	if !reflect.DeepEqual(repos, want) {
    87  		t.Errorf("Activity.ListStarred returned %+v, want %+v", repos, want)
    88  	}
    89  }
    90  
    91  func TestActivityService_ListStarred_invalidUser(t *testing.T) {
    92  	client, _, _, teardown := setup()
    93  	defer teardown()
    94  
    95  	_, _, err := client.Activity.ListStarred(context.Background(), "%", nil)
    96  	testURLParseError(t, err)
    97  }
    98  
    99  func TestActivityService_IsStarred_hasStar(t *testing.T) {
   100  	client, mux, _, teardown := setup()
   101  	defer teardown()
   102  
   103  	mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
   104  		testMethod(t, r, "GET")
   105  		w.WriteHeader(http.StatusNoContent)
   106  	})
   107  
   108  	star, _, err := client.Activity.IsStarred(context.Background(), "o", "r")
   109  	if err != nil {
   110  		t.Errorf("Activity.IsStarred returned error: %v", err)
   111  	}
   112  	if want := true; star != want {
   113  		t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want)
   114  	}
   115  }
   116  
   117  func TestActivityService_IsStarred_noStar(t *testing.T) {
   118  	client, mux, _, teardown := setup()
   119  	defer teardown()
   120  
   121  	mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
   122  		testMethod(t, r, "GET")
   123  		w.WriteHeader(http.StatusNotFound)
   124  	})
   125  
   126  	star, _, err := client.Activity.IsStarred(context.Background(), "o", "r")
   127  	if err != nil {
   128  		t.Errorf("Activity.IsStarred returned error: %v", err)
   129  	}
   130  	if want := false; star != want {
   131  		t.Errorf("Activity.IsStarred returned %+v, want %+v", star, want)
   132  	}
   133  }
   134  
   135  func TestActivityService_IsStarred_invalidID(t *testing.T) {
   136  	client, _, _, teardown := setup()
   137  	defer teardown()
   138  
   139  	_, _, err := client.Activity.IsStarred(context.Background(), "%", "%")
   140  	testURLParseError(t, err)
   141  }
   142  
   143  func TestActivityService_Star(t *testing.T) {
   144  	client, mux, _, teardown := setup()
   145  	defer teardown()
   146  
   147  	mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
   148  		testMethod(t, r, "PUT")
   149  	})
   150  
   151  	_, err := client.Activity.Star(context.Background(), "o", "r")
   152  	if err != nil {
   153  		t.Errorf("Activity.Star returned error: %v", err)
   154  	}
   155  }
   156  
   157  func TestActivityService_Star_invalidID(t *testing.T) {
   158  	client, _, _, teardown := setup()
   159  	defer teardown()
   160  
   161  	_, err := client.Activity.Star(context.Background(), "%", "%")
   162  	testURLParseError(t, err)
   163  }
   164  
   165  func TestActivityService_Unstar(t *testing.T) {
   166  	client, mux, _, teardown := setup()
   167  	defer teardown()
   168  
   169  	mux.HandleFunc("/user/starred/o/r", func(w http.ResponseWriter, r *http.Request) {
   170  		testMethod(t, r, "DELETE")
   171  	})
   172  
   173  	_, err := client.Activity.Unstar(context.Background(), "o", "r")
   174  	if err != nil {
   175  		t.Errorf("Activity.Unstar returned error: %v", err)
   176  	}
   177  }
   178  
   179  func TestActivityService_Unstar_invalidID(t *testing.T) {
   180  	client, _, _, teardown := setup()
   181  	defer teardown()
   182  
   183  	_, err := client.Activity.Unstar(context.Background(), "%", "%")
   184  	testURLParseError(t, err)
   185  }