From 07995e49c22dcb1e372c88ff12793b0194433e1c Mon Sep 17 00:00:00 2001 From: Glenn Lewis Date: Wed, 15 Jun 2016 10:40:32 -0700 Subject: [PATCH] make integration tests pass even if already watching this repo Note that `TestUsers_Keys` still fails for general users but will need to be addressed later. Fixes #366. Change-Id: I35d8a73c2daed08df7d60085b1585f48caee9a95 --- tests/integration/activity_test.go | 93 +++++++++++++++++------------- tests/integration/users_test.go | 1 + 2 files changed, 55 insertions(+), 39 deletions(-) diff --git a/tests/integration/activity_test.go b/tests/integration/activity_test.go index 1a1a711..a278584 100644 --- a/tests/integration/activity_test.go +++ b/tests/integration/activity_test.go @@ -13,14 +13,19 @@ import ( "github.com/google/go-github/github" ) +const ( + owner = "google" + repo = "go-github" +) + func TestActivity_Starring(t *testing.T) { - stargazers, _, err := client.Activity.ListStargazers("google", "go-github", nil) + stargazers, _, err := client.Activity.ListStargazers(owner, repo, nil) if err != nil { t.Fatalf("Activity.ListStargazers returned error: %v", err) } if len(stargazers) == 0 { - t.Errorf("Activity.ListStargazers('google', 'go-github') returned no stargazers") + t.Errorf("Activity.ListStargazers(%q, %q) returned no stargazers", owner, repo) } // the rest of the tests requires auth @@ -28,98 +33,108 @@ func TestActivity_Starring(t *testing.T) { return } - // first, check if already starred google/go-github - star, _, err := client.Activity.IsStarred("google", "go-github") + // first, check if already starred the target repository + star, _, err := client.Activity.IsStarred(owner, repo) if err != nil { t.Fatalf("Activity.IsStarred returned error: %v", err) } if star { - t.Fatalf("Already starring google/go-github. Please manually unstar it first.") + t.Fatalf("Already starring %v/%v. Please manually unstar it first.", owner, repo) } - // star google/go-github - _, err = client.Activity.Star("google", "go-github") + // star the target repository + _, err = client.Activity.Star(owner, repo) if err != nil { t.Fatalf("Activity.Star returned error: %v", err) } // check again and verify starred - star, _, err = client.Activity.IsStarred("google", "go-github") + star, _, err = client.Activity.IsStarred(owner, repo) if err != nil { t.Fatalf("Activity.IsStarred returned error: %v", err) } if !star { - t.Fatalf("Not starred google/go-github after starring it.") + t.Fatalf("Not starred %v/%v after starring it.", owner, repo) } // unstar - _, err = client.Activity.Unstar("google", "go-github") + _, err = client.Activity.Unstar(owner, repo) if err != nil { t.Fatalf("Activity.Unstar returned error: %v", err) } // check again and verify not watching - star, _, err = client.Activity.IsStarred("google", "go-github") + star, _, err = client.Activity.IsStarred(owner, repo) if err != nil { t.Fatalf("Activity.IsStarred returned error: %v", err) } if star { - t.Fatalf("Still starred google/go-github after unstarring it.") + t.Fatalf("Still starred %v/%v after unstarring it.", owner, repo) } } -func TestActivity_Watching(t *testing.T) { - watchers, _, err := client.Activity.ListWatchers("google", "go-github", nil) +func deleteSubscription(t *testing.T) { + // delete subscription + _, err := client.Activity.DeleteRepositorySubscription(owner, repo) if err != nil { - t.Fatalf("Activity.ListWatchers returned error: %v", err) - } - - if len(watchers) == 0 { - t.Errorf("Activity.ListWatchers('google', 'go-github') returned no watchers") - } - - // the rest of the tests requires auth - if !checkAuth("TestActivity_Watching") { - return + t.Fatalf("Activity.DeleteRepositorySubscription returned error: %v", err) } - // first, check if already watching google/go-github - sub, _, err := client.Activity.GetRepositorySubscription("google", "go-github") + // check again and verify not watching + sub, _, err := client.Activity.GetRepositorySubscription(owner, repo) if err != nil { t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err) } if sub != nil { - t.Fatalf("Already watching google/go-github. Please manually stop watching it first.") + t.Fatalf("Still watching %v/%v after deleting subscription.", owner, repo) } +} - // watch google/go-github - sub = &github.Subscription{Subscribed: github.Bool(true)} - _, _, err = client.Activity.SetRepositorySubscription("google", "go-github", sub) +func createSubscription(t *testing.T) { + // watch the target repository + sub := &github.Subscription{Subscribed: github.Bool(true)} + _, _, err := client.Activity.SetRepositorySubscription(owner, repo, sub) if err != nil { t.Fatalf("Activity.SetRepositorySubscription returned error: %v", err) } // check again and verify watching - sub, _, err = client.Activity.GetRepositorySubscription("google", "go-github") + sub, _, err = client.Activity.GetRepositorySubscription(owner, repo) if err != nil { t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err) } if sub == nil || !*sub.Subscribed { - t.Fatalf("Not watching google/go-github after setting subscription.") + t.Fatalf("Not watching %v/%v after setting subscription.", owner, repo) } +} - // delete subscription - _, err = client.Activity.DeleteRepositorySubscription("google", "go-github") +func TestActivity_Watching(t *testing.T) { + watchers, _, err := client.Activity.ListWatchers(owner, repo, nil) if err != nil { - t.Fatalf("Activity.DeleteRepositorySubscription returned error: %v", err) + t.Fatalf("Activity.ListWatchers returned error: %v", err) } - // check again and verify not watching - sub, _, err = client.Activity.GetRepositorySubscription("google", "go-github") + 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(owner, repo) if err != nil { t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err) } - if sub != nil { - t.Fatalf("Still watching google/go-github after deleting subscription.") + + 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) } } diff --git a/tests/integration/users_test.go b/tests/integration/users_test.go index 1ec1843..9d6764f 100644 --- a/tests/integration/users_test.go +++ b/tests/integration/users_test.go @@ -170,6 +170,7 @@ func TestUsers_Keys(t *testing.T) { return } + // TODO: make this integration test work for any authenticated user. keys, _, err = client.Users.ListKeys("", nil) if err != nil { t.Fatalf("Users.ListKeys('') returned error: %v", err)