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