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
6package github
7
8import (
9	"context"
10	"encoding/json"
11	"fmt"
12	"net/http"
13	"reflect"
14	"testing"
15)
16
17func TestActivityService_ListWatchers(t *testing.T) {
18	client, mux, _, teardown := setup()
19	defer teardown()
20
21	mux.HandleFunc("/repos/o/r/subscribers", func(w http.ResponseWriter, r *http.Request) {
22		testMethod(t, r, "GET")
23		testFormValues(t, r, values{
24			"page": "2",
25		})
26
27		fmt.Fprint(w, `[{"id":1}]`)
28	})
29
30	watchers, _, err := client.Activity.ListWatchers(context.Background(), "o", "r", &ListOptions{Page: 2})
31	if err != nil {
32		t.Errorf("Activity.ListWatchers returned error: %v", err)
33	}
34
35	want := []*User{{ID: Int64(1)}}
36	if !reflect.DeepEqual(watchers, want) {
37		t.Errorf("Activity.ListWatchers returned %+v, want %+v", watchers, want)
38	}
39}
40
41func TestActivityService_ListWatched_authenticatedUser(t *testing.T) {
42	client, mux, _, teardown := setup()
43	defer teardown()
44
45	mux.HandleFunc("/user/subscriptions", func(w http.ResponseWriter, r *http.Request) {
46		testMethod(t, r, "GET")
47		testFormValues(t, r, values{
48			"page": "2",
49		})
50		fmt.Fprint(w, `[{"id":1}]`)
51	})
52
53	watched, _, err := client.Activity.ListWatched(context.Background(), "", &ListOptions{Page: 2})
54	if err != nil {
55		t.Errorf("Activity.ListWatched returned error: %v", err)
56	}
57
58	want := []*Repository{{ID: Int64(1)}}
59	if !reflect.DeepEqual(watched, want) {
60		t.Errorf("Activity.ListWatched returned %+v, want %+v", watched, want)
61	}
62}
63
64func TestActivityService_ListWatched_specifiedUser(t *testing.T) {
65	client, mux, _, teardown := setup()
66	defer teardown()
67
68	mux.HandleFunc("/users/u/subscriptions", func(w http.ResponseWriter, r *http.Request) {
69		testMethod(t, r, "GET")
70		testFormValues(t, r, values{
71			"page": "2",
72		})
73		fmt.Fprint(w, `[{"id":1}]`)
74	})
75
76	watched, _, err := client.Activity.ListWatched(context.Background(), "u", &ListOptions{Page: 2})
77	if err != nil {
78		t.Errorf("Activity.ListWatched returned error: %v", err)
79	}
80
81	want := []*Repository{{ID: Int64(1)}}
82	if !reflect.DeepEqual(watched, want) {
83		t.Errorf("Activity.ListWatched returned %+v, want %+v", watched, want)
84	}
85}
86
87func TestActivityService_GetRepositorySubscription_true(t *testing.T) {
88	client, mux, _, teardown := setup()
89	defer teardown()
90
91	mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
92		testMethod(t, r, "GET")
93		fmt.Fprint(w, `{"subscribed":true}`)
94	})
95
96	sub, _, err := client.Activity.GetRepositorySubscription(context.Background(), "o", "r")
97	if err != nil {
98		t.Errorf("Activity.GetRepositorySubscription returned error: %v", err)
99	}
100
101	want := &Subscription{Subscribed: Bool(true)}
102	if !reflect.DeepEqual(sub, want) {
103		t.Errorf("Activity.GetRepositorySubscription returned %+v, want %+v", sub, want)
104	}
105}
106
107func TestActivityService_GetRepositorySubscription_false(t *testing.T) {
108	client, mux, _, teardown := setup()
109	defer teardown()
110
111	mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
112		testMethod(t, r, "GET")
113		w.WriteHeader(http.StatusNotFound)
114	})
115
116	sub, _, err := client.Activity.GetRepositorySubscription(context.Background(), "o", "r")
117	if err != nil {
118		t.Errorf("Activity.GetRepositorySubscription returned error: %v", err)
119	}
120
121	var want *Subscription
122	if !reflect.DeepEqual(sub, want) {
123		t.Errorf("Activity.GetRepositorySubscription returned %+v, want %+v", sub, want)
124	}
125}
126
127func TestActivityService_GetRepositorySubscription_error(t *testing.T) {
128	client, mux, _, teardown := setup()
129	defer teardown()
130
131	mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
132		testMethod(t, r, "GET")
133		w.WriteHeader(http.StatusBadRequest)
134	})
135
136	_, _, err := client.Activity.GetRepositorySubscription(context.Background(), "o", "r")
137	if err == nil {
138		t.Errorf("Expected HTTP 400 response")
139	}
140}
141
142func TestActivityService_SetRepositorySubscription(t *testing.T) {
143	client, mux, _, teardown := setup()
144	defer teardown()
145
146	input := &Subscription{Subscribed: Bool(true)}
147
148	mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
149		v := new(Subscription)
150		json.NewDecoder(r.Body).Decode(v)
151
152		testMethod(t, r, "PUT")
153		if !reflect.DeepEqual(v, input) {
154			t.Errorf("Request body = %+v, want %+v", v, input)
155		}
156
157		fmt.Fprint(w, `{"ignored":true}`)
158	})
159
160	sub, _, err := client.Activity.SetRepositorySubscription(context.Background(), "o", "r", input)
161	if err != nil {
162		t.Errorf("Activity.SetRepositorySubscription returned error: %v", err)
163	}
164
165	want := &Subscription{Ignored: Bool(true)}
166	if !reflect.DeepEqual(sub, want) {
167		t.Errorf("Activity.SetRepositorySubscription returned %+v, want %+v", sub, want)
168	}
169}
170
171func TestActivityService_DeleteRepositorySubscription(t *testing.T) {
172	client, mux, _, teardown := setup()
173	defer teardown()
174
175	mux.HandleFunc("/repos/o/r/subscription", func(w http.ResponseWriter, r *http.Request) {
176		testMethod(t, r, "DELETE")
177		w.WriteHeader(http.StatusNoContent)
178	})
179
180	_, err := client.Activity.DeleteRepositorySubscription(context.Background(), "o", "r")
181	if err != nil {
182		t.Errorf("Activity.DeleteRepositorySubscription returned error: %v", err)
183	}
184}
185