github.com/google/go-github/v33@v33.0.0/github/activity_test.go (about)

     1  // Copyright 2016 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  	"net/http"
    11  	"reflect"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestActivityService_List(t *testing.T) {
    17  	client, mux, _, teardown := setup()
    18  	defer teardown()
    19  
    20  	mux.HandleFunc("/feeds", func(w http.ResponseWriter, r *http.Request) {
    21  		testMethod(t, r, "GET")
    22  
    23  		w.WriteHeader(http.StatusOK)
    24  		w.Write(feedsJSON)
    25  	})
    26  
    27  	ctx := context.Background()
    28  	got, _, err := client.Activity.ListFeeds(ctx)
    29  	if err != nil {
    30  		t.Errorf("Activity.ListFeeds returned error: %v", err)
    31  	}
    32  	if want := wantFeeds; !reflect.DeepEqual(got, want) {
    33  		t.Errorf("Activity.ListFeeds = %+v, want %+v", got, want)
    34  	}
    35  
    36  	// Test s.client.NewRequest failure
    37  	client.BaseURL.Path = ""
    38  	got, resp, err := client.Activity.ListFeeds(ctx)
    39  	if got != nil {
    40  		t.Errorf("client.BaseURL.Path='' ListFeeds = %#v, want nil", got)
    41  	}
    42  	if resp != nil {
    43  		t.Errorf("client.BaseURL.Path='' ListFeeds resp = %#v, want nil", resp)
    44  	}
    45  	if err == nil {
    46  		t.Error("client.BaseURL.Path='' ListFeeds err = nil, want error")
    47  	}
    48  
    49  	// Test s.client.Do failure
    50  	client.BaseURL.Path = "/api-v3/"
    51  	client.rateLimits[0].Reset.Time = time.Now().Add(10 * time.Minute)
    52  	got, resp, err = client.Activity.ListFeeds(ctx)
    53  	if got != nil {
    54  		t.Errorf("rate.Reset.Time > now ListFeeds = %#v, want nil", got)
    55  	}
    56  	if want := http.StatusForbidden; resp == nil || resp.Response.StatusCode != want {
    57  		t.Errorf("rate.Reset.Time > now ListFeeds resp = %#v, want StatusCode=%v", resp.Response, want)
    58  	}
    59  	if err == nil {
    60  		t.Error("rate.Reset.Time > now ListFeeds err = nil, want error")
    61  	}
    62  }
    63  
    64  var feedsJSON = []byte(`{
    65    "timeline_url": "https://github.com/timeline",
    66    "user_url": "https://github.com/{user}",
    67    "current_user_public_url": "https://github.com/defunkt",
    68    "current_user_url": "https://github.com/defunkt.private?token=abc123",
    69    "current_user_actor_url": "https://github.com/defunkt.private.actor?token=abc123",
    70    "current_user_organization_url": "",
    71    "current_user_organization_urls": [
    72      "https://github.com/organizations/github/defunkt.private.atom?token=abc123"
    73    ],
    74    "_links": {
    75      "timeline": {
    76        "href": "https://github.com/timeline",
    77        "type": "application/atom+xml"
    78      },
    79      "user": {
    80        "href": "https://github.com/{user}",
    81        "type": "application/atom+xml"
    82      },
    83      "current_user_public": {
    84        "href": "https://github.com/defunkt",
    85        "type": "application/atom+xml"
    86      },
    87      "current_user": {
    88        "href": "https://github.com/defunkt.private?token=abc123",
    89        "type": "application/atom+xml"
    90      },
    91      "current_user_actor": {
    92        "href": "https://github.com/defunkt.private.actor?token=abc123",
    93        "type": "application/atom+xml"
    94      },
    95      "current_user_organization": {
    96        "href": "",
    97        "type": ""
    98      },
    99      "current_user_organizations": [
   100        {
   101          "href": "https://github.com/organizations/github/defunkt.private.atom?token=abc123",
   102          "type": "application/atom+xml"
   103        }
   104      ]
   105    }
   106  }`)
   107  
   108  var wantFeeds = &Feeds{
   109  	TimelineURL:                String("https://github.com/timeline"),
   110  	UserURL:                    String("https://github.com/{user}"),
   111  	CurrentUserPublicURL:       String("https://github.com/defunkt"),
   112  	CurrentUserURL:             String("https://github.com/defunkt.private?token=abc123"),
   113  	CurrentUserActorURL:        String("https://github.com/defunkt.private.actor?token=abc123"),
   114  	CurrentUserOrganizationURL: String(""),
   115  	CurrentUserOrganizationURLs: []string{
   116  		"https://github.com/organizations/github/defunkt.private.atom?token=abc123",
   117  	},
   118  	Links: &struct {
   119  		Timeline                 *FeedLink   `json:"timeline,omitempty"`
   120  		User                     *FeedLink   `json:"user,omitempty"`
   121  		CurrentUserPublic        *FeedLink   `json:"current_user_public,omitempty"`
   122  		CurrentUser              *FeedLink   `json:"current_user,omitempty"`
   123  		CurrentUserActor         *FeedLink   `json:"current_user_actor,omitempty"`
   124  		CurrentUserOrganization  *FeedLink   `json:"current_user_organization,omitempty"`
   125  		CurrentUserOrganizations []*FeedLink `json:"current_user_organizations,omitempty"`
   126  	}{
   127  		Timeline: &FeedLink{
   128  			HRef: String("https://github.com/timeline"),
   129  			Type: String("application/atom+xml"),
   130  		},
   131  		User: &FeedLink{
   132  			HRef: String("https://github.com/{user}"),
   133  			Type: String("application/atom+xml"),
   134  		},
   135  		CurrentUserPublic: &FeedLink{
   136  			HRef: String("https://github.com/defunkt"),
   137  			Type: String("application/atom+xml"),
   138  		},
   139  		CurrentUser: &FeedLink{
   140  			HRef: String("https://github.com/defunkt.private?token=abc123"),
   141  			Type: String("application/atom+xml"),
   142  		},
   143  		CurrentUserActor: &FeedLink{
   144  			HRef: String("https://github.com/defunkt.private.actor?token=abc123"),
   145  			Type: String("application/atom+xml"),
   146  		},
   147  		CurrentUserOrganization: &FeedLink{
   148  			HRef: String(""),
   149  			Type: String(""),
   150  		},
   151  		CurrentUserOrganizations: []*FeedLink{
   152  			{
   153  				HRef: String("https://github.com/organizations/github/defunkt.private.atom?token=abc123"),
   154  				Type: String("application/atom+xml"),
   155  			},
   156  		},
   157  	},
   158  }