1
2
3
4
5
6
7
8
9 package integration
10
11 import (
12 "context"
13 "testing"
14
15 "github.com/google/go-github/v55/github"
16 )
17
18 const (
19 owner = "google"
20 repo = "go-github"
21 )
22
23 func TestActivity_Starring(t *testing.T) {
24 stargazers, _, err := client.Activity.ListStargazers(context.Background(), owner, repo, nil)
25 if err != nil {
26 t.Fatalf("Activity.ListStargazers returned error: %v", err)
27 }
28
29 if len(stargazers) == 0 {
30 t.Errorf("Activity.ListStargazers(%q, %q) returned no stargazers", owner, repo)
31 }
32
33
34 if !checkAuth("TestActivity_Starring") {
35 return
36 }
37
38
39 star, _, err := client.Activity.IsStarred(context.Background(), owner, repo)
40 if err != nil {
41 t.Fatalf("Activity.IsStarred returned error: %v", err)
42 }
43 if star {
44 t.Fatalf("Already starring %v/%v. Please manually unstar it first.", owner, repo)
45 }
46
47
48 _, err = client.Activity.Star(context.Background(), owner, repo)
49 if err != nil {
50 t.Fatalf("Activity.Star returned error: %v", err)
51 }
52
53
54 star, _, err = client.Activity.IsStarred(context.Background(), owner, repo)
55 if err != nil {
56 t.Fatalf("Activity.IsStarred returned error: %v", err)
57 }
58 if !star {
59 t.Fatalf("Not starred %v/%v after starring it.", owner, repo)
60 }
61
62
63 _, err = client.Activity.Unstar(context.Background(), owner, repo)
64 if err != nil {
65 t.Fatalf("Activity.Unstar returned error: %v", err)
66 }
67
68
69 star, _, err = client.Activity.IsStarred(context.Background(), owner, repo)
70 if err != nil {
71 t.Fatalf("Activity.IsStarred returned error: %v", err)
72 }
73 if star {
74 t.Fatalf("Still starred %v/%v after unstarring it.", owner, repo)
75 }
76 }
77
78 func deleteSubscription(t *testing.T) {
79
80 _, err := client.Activity.DeleteRepositorySubscription(context.Background(), owner, repo)
81 if err != nil {
82 t.Fatalf("Activity.DeleteRepositorySubscription returned error: %v", err)
83 }
84
85
86 sub, _, err := client.Activity.GetRepositorySubscription(context.Background(), owner, repo)
87 if err != nil {
88 t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err)
89 }
90 if sub != nil {
91 t.Fatalf("Still watching %v/%v after deleting subscription.", owner, repo)
92 }
93 }
94
95 func createSubscription(t *testing.T) {
96
97 sub := &github.Subscription{Subscribed: github.Bool(true)}
98 _, _, err := client.Activity.SetRepositorySubscription(context.Background(), owner, repo, sub)
99 if err != nil {
100 t.Fatalf("Activity.SetRepositorySubscription returned error: %v", err)
101 }
102
103
104 sub, _, err = client.Activity.GetRepositorySubscription(context.Background(), owner, repo)
105 if err != nil {
106 t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err)
107 }
108 if sub == nil || !*sub.Subscribed {
109 t.Fatalf("Not watching %v/%v after setting subscription.", owner, repo)
110 }
111 }
112
113 func TestActivity_Watching(t *testing.T) {
114 watchers, _, err := client.Activity.ListWatchers(context.Background(), owner, repo, nil)
115 if err != nil {
116 t.Fatalf("Activity.ListWatchers returned error: %v", err)
117 }
118
119 if len(watchers) == 0 {
120 t.Errorf("Activity.ListWatchers(%q, %q) returned no watchers", owner, repo)
121 }
122
123
124 if !checkAuth("TestActivity_Watching") {
125 return
126 }
127
128
129 sub, _, err := client.Activity.GetRepositorySubscription(context.Background(), owner, repo)
130 if err != nil {
131 t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err)
132 }
133
134 switch {
135 case sub != nil:
136 deleteSubscription(t)
137 createSubscription(t)
138 case sub == nil:
139 createSubscription(t)
140 deleteSubscription(t)
141 }
142 }
143
View as plain text