github.com/google/go-github/v66@v66.0.0/github/activity_notifications_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  	"encoding/json"
    11  	"fmt"
    12  	"net/http"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/google/go-cmp/cmp"
    17  )
    18  
    19  func TestActivityService_ListNotification(t *testing.T) {
    20  	t.Parallel()
    21  	client, mux, _ := setup(t)
    22  
    23  	mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) {
    24  		testMethod(t, r, "GET")
    25  		testFormValues(t, r, values{
    26  			"all":           "true",
    27  			"participating": "true",
    28  			"since":         "2006-01-02T15:04:05Z",
    29  			"before":        "2007-03-04T15:04:05Z",
    30  		})
    31  
    32  		fmt.Fprint(w, `[{"id":"1", "subject":{"title":"t"}}]`)
    33  	})
    34  
    35  	opt := &NotificationListOptions{
    36  		All:           true,
    37  		Participating: true,
    38  		Since:         time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC),
    39  		Before:        time.Date(2007, time.March, 04, 15, 04, 05, 0, time.UTC),
    40  	}
    41  	ctx := context.Background()
    42  	notifications, _, err := client.Activity.ListNotifications(ctx, opt)
    43  	if err != nil {
    44  		t.Errorf("Activity.ListNotifications returned error: %v", err)
    45  	}
    46  
    47  	want := []*Notification{{ID: String("1"), Subject: &NotificationSubject{Title: String("t")}}}
    48  	if !cmp.Equal(notifications, want) {
    49  		t.Errorf("Activity.ListNotifications returned %+v, want %+v", notifications, want)
    50  	}
    51  
    52  	const methodName = "ListNotifications"
    53  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    54  		got, resp, err := client.Activity.ListNotifications(ctx, opt)
    55  		if got != nil {
    56  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    57  		}
    58  		return resp, err
    59  	})
    60  }
    61  
    62  func TestActivityService_ListRepositoryNotifications(t *testing.T) {
    63  	t.Parallel()
    64  	client, mux, _ := setup(t)
    65  
    66  	mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) {
    67  		testMethod(t, r, "GET")
    68  		fmt.Fprint(w, `[{"id":"1"}]`)
    69  	})
    70  
    71  	ctx := context.Background()
    72  	notifications, _, err := client.Activity.ListRepositoryNotifications(ctx, "o", "r", nil)
    73  	if err != nil {
    74  		t.Errorf("Activity.ListRepositoryNotifications returned error: %v", err)
    75  	}
    76  
    77  	want := []*Notification{{ID: String("1")}}
    78  	if !cmp.Equal(notifications, want) {
    79  		t.Errorf("Activity.ListRepositoryNotifications returned %+v, want %+v", notifications, want)
    80  	}
    81  
    82  	const methodName = "ListRepositoryNotifications"
    83  	testBadOptions(t, methodName, func() (err error) {
    84  		_, _, err = client.Activity.ListRepositoryNotifications(ctx, "\n", "\n", &NotificationListOptions{})
    85  		return err
    86  	})
    87  
    88  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
    89  		got, resp, err := client.Activity.ListRepositoryNotifications(ctx, "o", "r", nil)
    90  		if got != nil {
    91  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
    92  		}
    93  		return resp, err
    94  	})
    95  }
    96  
    97  func TestActivityService_MarkNotificationsRead(t *testing.T) {
    98  	t.Parallel()
    99  	client, mux, _ := setup(t)
   100  
   101  	mux.HandleFunc("/notifications", func(w http.ResponseWriter, r *http.Request) {
   102  		testMethod(t, r, "PUT")
   103  		testHeader(t, r, "Content-Type", "application/json")
   104  		testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n")
   105  
   106  		w.WriteHeader(http.StatusResetContent)
   107  	})
   108  
   109  	ctx := context.Background()
   110  	_, err := client.Activity.MarkNotificationsRead(ctx, Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)})
   111  	if err != nil {
   112  		t.Errorf("Activity.MarkNotificationsRead returned error: %v", err)
   113  	}
   114  
   115  	const methodName = "MarkNotificationsRead"
   116  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   117  		return client.Activity.MarkNotificationsRead(ctx, Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)})
   118  	})
   119  }
   120  
   121  func TestActivityService_MarkRepositoryNotificationsRead(t *testing.T) {
   122  	t.Parallel()
   123  	client, mux, _ := setup(t)
   124  
   125  	mux.HandleFunc("/repos/o/r/notifications", func(w http.ResponseWriter, r *http.Request) {
   126  		testMethod(t, r, "PUT")
   127  		testHeader(t, r, "Content-Type", "application/json")
   128  		testBody(t, r, `{"last_read_at":"2006-01-02T15:04:05Z"}`+"\n")
   129  
   130  		w.WriteHeader(http.StatusResetContent)
   131  	})
   132  
   133  	ctx := context.Background()
   134  	_, err := client.Activity.MarkRepositoryNotificationsRead(ctx, "o", "r", Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)})
   135  	if err != nil {
   136  		t.Errorf("Activity.MarkRepositoryNotificationsRead returned error: %v", err)
   137  	}
   138  
   139  	const methodName = "MarkRepositoryNotificationsRead"
   140  	testBadOptions(t, methodName, func() (err error) {
   141  		_, err = client.Activity.MarkRepositoryNotificationsRead(ctx, "\n", "\n", Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)})
   142  		return err
   143  	})
   144  
   145  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   146  		return client.Activity.MarkRepositoryNotificationsRead(ctx, "o", "r", Timestamp{time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)})
   147  	})
   148  }
   149  
   150  func TestActivityService_GetThread(t *testing.T) {
   151  	t.Parallel()
   152  	client, mux, _ := setup(t)
   153  
   154  	mux.HandleFunc("/notifications/threads/1", func(w http.ResponseWriter, r *http.Request) {
   155  		testMethod(t, r, "GET")
   156  		fmt.Fprint(w, `{"id":"1"}`)
   157  	})
   158  
   159  	ctx := context.Background()
   160  	notification, _, err := client.Activity.GetThread(ctx, "1")
   161  	if err != nil {
   162  		t.Errorf("Activity.GetThread returned error: %v", err)
   163  	}
   164  
   165  	want := &Notification{ID: String("1")}
   166  	if !cmp.Equal(notification, want) {
   167  		t.Errorf("Activity.GetThread returned %+v, want %+v", notification, want)
   168  	}
   169  
   170  	const methodName = "GetThread"
   171  	testBadOptions(t, methodName, func() (err error) {
   172  		_, _, err = client.Activity.GetThread(ctx, "\n")
   173  		return err
   174  	})
   175  
   176  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   177  		got, resp, err := client.Activity.GetThread(ctx, "1")
   178  		if got != nil {
   179  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   180  		}
   181  		return resp, err
   182  	})
   183  }
   184  
   185  func TestActivityService_MarkThreadRead(t *testing.T) {
   186  	t.Parallel()
   187  	client, mux, _ := setup(t)
   188  
   189  	mux.HandleFunc("/notifications/threads/1", func(w http.ResponseWriter, r *http.Request) {
   190  		testMethod(t, r, "PATCH")
   191  		w.WriteHeader(http.StatusResetContent)
   192  	})
   193  
   194  	ctx := context.Background()
   195  	_, err := client.Activity.MarkThreadRead(ctx, "1")
   196  	if err != nil {
   197  		t.Errorf("Activity.MarkThreadRead returned error: %v", err)
   198  	}
   199  
   200  	const methodName = "MarkThreadRead"
   201  	testBadOptions(t, methodName, func() (err error) {
   202  		_, err = client.Activity.MarkThreadRead(ctx, "\n")
   203  		return err
   204  	})
   205  
   206  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   207  		return client.Activity.MarkThreadRead(ctx, "1")
   208  	})
   209  }
   210  
   211  func TestActivityService_MarkThreadDone(t *testing.T) {
   212  	t.Parallel()
   213  	client, mux, _ := setup(t)
   214  
   215  	mux.HandleFunc("/notifications/threads/1", func(w http.ResponseWriter, r *http.Request) {
   216  		testMethod(t, r, "DELETE")
   217  		w.WriteHeader(http.StatusResetContent)
   218  	})
   219  
   220  	ctx := context.Background()
   221  	_, err := client.Activity.MarkThreadDone(ctx, 1)
   222  	if err != nil {
   223  		t.Errorf("Activity.MarkThreadDone returned error: %v", err)
   224  	}
   225  
   226  	const methodName = "MarkThreadDone"
   227  	testBadOptions(t, methodName, func() (err error) {
   228  		_, err = client.Activity.MarkThreadDone(ctx, 0)
   229  		return err
   230  	})
   231  
   232  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   233  		return client.Activity.MarkThreadDone(ctx, 1)
   234  	})
   235  }
   236  
   237  func TestActivityService_GetThreadSubscription(t *testing.T) {
   238  	t.Parallel()
   239  	client, mux, _ := setup(t)
   240  
   241  	mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) {
   242  		testMethod(t, r, "GET")
   243  		fmt.Fprint(w, `{"subscribed":true}`)
   244  	})
   245  
   246  	ctx := context.Background()
   247  	sub, _, err := client.Activity.GetThreadSubscription(ctx, "1")
   248  	if err != nil {
   249  		t.Errorf("Activity.GetThreadSubscription returned error: %v", err)
   250  	}
   251  
   252  	want := &Subscription{Subscribed: Bool(true)}
   253  	if !cmp.Equal(sub, want) {
   254  		t.Errorf("Activity.GetThreadSubscription returned %+v, want %+v", sub, want)
   255  	}
   256  
   257  	const methodName = "GetThreadSubscription"
   258  	testBadOptions(t, methodName, func() (err error) {
   259  		_, _, err = client.Activity.GetThreadSubscription(ctx, "\n")
   260  		return err
   261  	})
   262  
   263  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   264  		got, resp, err := client.Activity.GetThreadSubscription(ctx, "1")
   265  		if got != nil {
   266  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   267  		}
   268  		return resp, err
   269  	})
   270  }
   271  
   272  func TestActivityService_SetThreadSubscription(t *testing.T) {
   273  	t.Parallel()
   274  	client, mux, _ := setup(t)
   275  
   276  	input := &Subscription{Subscribed: Bool(true)}
   277  
   278  	mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) {
   279  		v := new(Subscription)
   280  		assertNilError(t, json.NewDecoder(r.Body).Decode(v))
   281  
   282  		testMethod(t, r, "PUT")
   283  		if !cmp.Equal(v, input) {
   284  			t.Errorf("Request body = %+v, want %+v", v, input)
   285  		}
   286  
   287  		fmt.Fprint(w, `{"ignored":true}`)
   288  	})
   289  
   290  	ctx := context.Background()
   291  	sub, _, err := client.Activity.SetThreadSubscription(ctx, "1", input)
   292  	if err != nil {
   293  		t.Errorf("Activity.SetThreadSubscription returned error: %v", err)
   294  	}
   295  
   296  	want := &Subscription{Ignored: Bool(true)}
   297  	if !cmp.Equal(sub, want) {
   298  		t.Errorf("Activity.SetThreadSubscription returned %+v, want %+v", sub, want)
   299  	}
   300  
   301  	const methodName = "SetThreadSubscription"
   302  	testBadOptions(t, methodName, func() (err error) {
   303  		_, _, err = client.Activity.SetThreadSubscription(ctx, "\n", input)
   304  		return err
   305  	})
   306  
   307  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   308  		got, resp, err := client.Activity.SetThreadSubscription(ctx, "1", input)
   309  		if got != nil {
   310  			t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
   311  		}
   312  		return resp, err
   313  	})
   314  }
   315  
   316  func TestActivityService_DeleteThreadSubscription(t *testing.T) {
   317  	t.Parallel()
   318  	client, mux, _ := setup(t)
   319  
   320  	mux.HandleFunc("/notifications/threads/1/subscription", func(w http.ResponseWriter, r *http.Request) {
   321  		testMethod(t, r, "DELETE")
   322  		w.WriteHeader(http.StatusNoContent)
   323  	})
   324  
   325  	ctx := context.Background()
   326  	_, err := client.Activity.DeleteThreadSubscription(ctx, "1")
   327  	if err != nil {
   328  		t.Errorf("Activity.DeleteThreadSubscription returned error: %v", err)
   329  	}
   330  
   331  	const methodName = "DeleteThreadSubscription"
   332  	testBadOptions(t, methodName, func() (err error) {
   333  		_, err = client.Activity.DeleteThreadSubscription(ctx, "\n")
   334  		return err
   335  	})
   336  
   337  	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
   338  		return client.Activity.DeleteThreadSubscription(ctx, "1")
   339  	})
   340  }
   341  
   342  func TestNotification_Marshal(t *testing.T) {
   343  	t.Parallel()
   344  	testJSONMarshal(t, &Notification{}, "{}")
   345  
   346  	u := &Notification{
   347  		ID: String("id"),
   348  		Repository: &Repository{
   349  			ID:   Int64(1),
   350  			URL:  String("u"),
   351  			Name: String("n"),
   352  		},
   353  		Subject: &NotificationSubject{
   354  			Title:            String("t"),
   355  			URL:              String("u"),
   356  			LatestCommentURL: String("l"),
   357  			Type:             String("t"),
   358  		},
   359  		Reason:     String("r"),
   360  		Unread:     Bool(true),
   361  		UpdatedAt:  &Timestamp{referenceTime},
   362  		LastReadAt: &Timestamp{referenceTime},
   363  		URL:        String("u"),
   364  	}
   365  
   366  	want := `{
   367  		"id": "id",
   368  		"repository": {
   369  			"id": 1,
   370  			"url": "u",
   371  			"name": "n"
   372  		},
   373  		"subject": {
   374  			"title": "t",
   375  			"url": "u",
   376  			"latest_comment_url": "l",
   377  			"type": "t"
   378  		},
   379  		"reason": "r",
   380  		"unread": true,
   381  		"updated_at": ` + referenceTimeStr + `,
   382  		"last_read_at": ` + referenceTimeStr + `,
   383  		"url": "u"
   384  	}`
   385  
   386  	testJSONMarshal(t, u, want)
   387  }
   388  
   389  func TestNotificationSubject_Marshal(t *testing.T) {
   390  	t.Parallel()
   391  	testJSONMarshal(t, &NotificationSubject{}, "{}")
   392  
   393  	u := &NotificationSubject{
   394  		Title:            String("t"),
   395  		URL:              String("u"),
   396  		LatestCommentURL: String("l"),
   397  		Type:             String("t"),
   398  	}
   399  
   400  	want := `{
   401  		"title": "t",
   402  		"url": "u",
   403  		"latest_comment_url": "l",
   404  		"type": "t"
   405  	}`
   406  
   407  	testJSONMarshal(t, u, want)
   408  }
   409  
   410  func TestMarkReadOptions_Marshal(t *testing.T) {
   411  	t.Parallel()
   412  	testJSONMarshal(t, &markReadOptions{}, "{}")
   413  
   414  	u := &markReadOptions{
   415  		LastReadAt: Timestamp{referenceTime},
   416  	}
   417  
   418  	want := `{
   419  		"last_read_at": ` + referenceTimeStr + `
   420  	}`
   421  
   422  	testJSONMarshal(t, u, want)
   423  }