diff --git a/github/resource_github_organization_webhook_test.go b/github/resource_github_organization_webhook_test.go index a8584a2943..08b09eb3d4 100644 --- a/github/resource_github_organization_webhook_test.go +++ b/github/resource_github_organization_webhook_test.go @@ -1,231 +1,143 @@ package github import ( - "context" "fmt" - "reflect" - "strconv" "strings" "testing" - "github.com/google/go-github/v32/github" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" ) -func TestAccGithubOrganizationWebhook_basic(t *testing.T) { - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } - - var hook github.Hook - randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) - rn := "github_organization_webhook.foo" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubOrganizationWebhookDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubOrganizationWebhookConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubOrganizationWebhookExists(rn, &hook), - testAccCheckGithubOrganizationWebhookAttributes(&hook, &testAccGithubOrganizationWebhookExpectedAttributes{ - Events: []string{"pull_request"}, - Configuration: map[string]interface{}{ - "url": "https://google.de/webhook", - "content_type": "json", - "insecure_ssl": "1", - }, - Active: true, - }), - ), - }, - { - Config: testAccGithubOrganizationWebhookUpdateConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubOrganizationWebhookExists(rn, &hook), - testAccCheckGithubOrganizationWebhookAttributes(&hook, &testAccGithubOrganizationWebhookExpectedAttributes{ - Events: []string{"issues"}, - Configuration: map[string]interface{}{ - "url": "https://google.de/webhooks", - "content_type": "form", - "insecure_ssl": "0", - }, - Active: false, - }), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("foo-%s/", randString), - }, - }, - }) -} +func TestAccGithubOrganizationWebhook(t *testing.T) { -func TestAccGithubOrganizationWebhook_secret(t *testing.T) { - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } - - rn := "github_organization_webhook.foo" - randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubOrganizationWebhookDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubOrganizationWebhookConfig_secret, - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubOrganizationWebhookSecret(rn, "VerySecret"), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("foo-%s/", randString), - }, - }, - }) -} + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) -func testAccCheckGithubOrganizationWebhookExists(n string, hook *github.Hook) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not Found: %s", n) - } + t.Run("creates and updates webhooks without error", func(t *testing.T) { - hookID, err := strconv.ParseInt(rs.Primary.ID, 10, 64) - if err != nil { - return unconvertibleIdErr(rs.Primary.ID, err) - } - if hookID == 0 { - return fmt.Errorf("No repository name is set") - } + config := fmt.Sprintf(` - org := testAccProvider.Meta().(*Owner) - conn := org.v3client - getHook, _, err := conn.Organizations.GetHook(context.TODO(), org.name, hookID) - if err != nil { - return err - } - *hook = *getHook - return nil - } -} + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } -type testAccGithubOrganizationWebhookExpectedAttributes struct { - Events []string - Configuration map[string]interface{} - Active bool -} + resource "github_organization_webhook" "test" { + configuration { + url = "https://google.de/webhook" + content_type = "json" + insecure_ssl = true + } -func testAccCheckGithubOrganizationWebhookAttributes(hook *github.Hook, want *testAccGithubOrganizationWebhookExpectedAttributes) resource.TestCheckFunc { - return func(s *terraform.State) error { + events = ["pull_request"] + } - if active := hook.GetActive(); active != want.Active { - return fmt.Errorf("got hook %t; want %t", active, want.Active) - } - if URL := hook.GetURL(); !strings.HasPrefix(URL, "https://") { - return fmt.Errorf("got http URL %q; want to start with 'https://'", URL) - } - if !reflect.DeepEqual(hook.Events, want.Events) { - return fmt.Errorf("got hook events %q; want %q", hook.Events, want.Events) + `, randomID) + + checks := map[string]resource.TestCheckFunc{ + "before": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_organization_webhook.test", "configuration.0.url", + "https://google.de/webhook", + ), + ), + "after": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_organization_webhook.test", "configuration.0.url", + "https://google.de/updated", + ), + ), } - if !reflect.DeepEqual(hook.Config, want.Configuration) { - return fmt.Errorf("got hook configuration %q; want %q", hook.Config, want.Configuration) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], + }, + { + Config: strings.Replace(config, + "https://google.de/webhook", + "https://google.de/updated", 1), + Check: checks["after"], + }, + }, + }) } - return nil - } -} + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) -func testAccCheckGithubOrganizationWebhookSecret(r, secret string) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[r] - if !ok { - return fmt.Errorf("Not Found: %s", r) - } + t.Run("with an individual account", func(t *testing.T) { + t.Skip("individual account not supported for this operation") + }) - if rs.Primary.Attributes["configuration.0.secret"] != secret { - return fmt.Errorf("Configured secret in %s does not match secret in state. (Expected: %s, Actual: %s)", r, secret, rs.Primary.Attributes["configuration.0.secret"]) - } + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + }) - return nil - } -} + t.Run("imports webhooks without error", func(t *testing.T) { -func testAccCheckGithubOrganizationWebhookDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*Owner).v3client - orgName := testAccProvider.Meta().(*Owner).name + config := fmt.Sprintf(` - for _, rs := range s.RootModule().Resources { - if rs.Type != "github_organization_webhook" { - continue - } + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } - id, err := strconv.ParseInt(rs.Primary.ID, 10, 64) - if err != nil { - return unconvertibleIdErr(rs.Primary.ID, err) - } + resource "github_organization_webhook" "test" { + configuration { + url = "https://google.de/import" + content_type = "json" + insecure_ssl = true + } - gotHook, resp, err := conn.Organizations.GetHook(context.TODO(), orgName, id) - if err == nil { - if gotHook != nil && gotHook.GetID() == id { - return fmt.Errorf("Webhook still exists") + events = ["issues"] } + + `, randomID) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_organization_webhook.test", "events.#", + "1", + ), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + { + ResourceName: "github_organization_webhook.test", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) } - if resp.StatusCode != 404 { - return err - } - return nil - } - return nil -} -const testAccGithubOrganizationWebhookConfig = ` -resource "github_organization_webhook" "foo" { - configuration { - url = "https://google.de/webhook" - content_type = "json" - insecure_ssl = true - } + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + t.Skip("individual account not supported for this operation") + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + }) - events = ["pull_request"] -} -` - -const testAccGithubOrganizationWebhookUpdateConfig = ` -resource "github_organization_webhook" "foo" { - configuration { - url = "https://google.de/webhooks" - content_type = "form" - insecure_ssl = false - } - active = false - - events = ["issues"] -} -` - -const testAccGithubOrganizationWebhookConfig_secret = ` -resource "github_organization_webhook" "foo" { - configuration { - url = "https://www.terraform.io/webhook" - content_type = "json" - secret = "VerySecret" - insecure_ssl = false - } - - events = ["pull_request"] } -`