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