github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/github/resource_github_organization_webhook_test.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/google/go-github/github"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccGithubOrganizationWebhook_basic(t *testing.T) {
    17  	var hook github.Hook
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckGithubOrganizationWebhookDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccGithubOrganizationWebhookConfig,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckGithubOrganizationWebhookExists("github_organization_webhook.foo", &hook),
    28  					testAccCheckGithubOrganizationWebhookAttributes(&hook, &testAccGithubOrganizationWebhookExpectedAttributes{
    29  						Name:   "web",
    30  						Events: []string{"pull_request"},
    31  						Configuration: map[string]interface{}{
    32  							"url":          "https://google.de/webhook",
    33  							"content_type": "json",
    34  							"insecure_ssl": "1",
    35  						},
    36  						Active: true,
    37  					}),
    38  				),
    39  			},
    40  			{
    41  				Config: testAccGithubOrganizationWebhookUpdateConfig,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckGithubOrganizationWebhookExists("github_organization_webhook.foo", &hook),
    44  					testAccCheckGithubOrganizationWebhookAttributes(&hook, &testAccGithubOrganizationWebhookExpectedAttributes{
    45  						Name:   "web",
    46  						Events: []string{"issues"},
    47  						Configuration: map[string]interface{}{
    48  							"url":          "https://google.de/webhooks",
    49  							"content_type": "form",
    50  							"insecure_ssl": "0",
    51  						},
    52  						Active: false,
    53  					}),
    54  				),
    55  			},
    56  		},
    57  	})
    58  }
    59  
    60  func testAccCheckGithubOrganizationWebhookExists(n string, hook *github.Hook) resource.TestCheckFunc {
    61  	return func(s *terraform.State) error {
    62  		rs, ok := s.RootModule().Resources[n]
    63  		if !ok {
    64  			return fmt.Errorf("Not Found: %s", n)
    65  		}
    66  
    67  		hookID, _ := strconv.Atoi(rs.Primary.ID)
    68  		if hookID == 0 {
    69  			return fmt.Errorf("No repository name is set")
    70  		}
    71  
    72  		org := testAccProvider.Meta().(*Organization)
    73  		conn := org.client
    74  		getHook, _, err := conn.Organizations.GetHook(context.TODO(), org.name, hookID)
    75  		if err != nil {
    76  			return err
    77  		}
    78  		*hook = *getHook
    79  		return nil
    80  	}
    81  }
    82  
    83  type testAccGithubOrganizationWebhookExpectedAttributes struct {
    84  	Name          string
    85  	Events        []string
    86  	Configuration map[string]interface{}
    87  	Active        bool
    88  }
    89  
    90  func testAccCheckGithubOrganizationWebhookAttributes(hook *github.Hook, want *testAccGithubOrganizationWebhookExpectedAttributes) resource.TestCheckFunc {
    91  	return func(s *terraform.State) error {
    92  
    93  		if *hook.Name != want.Name {
    94  			return fmt.Errorf("got hook %q; want %q", *hook.Name, want.Name)
    95  		}
    96  		if *hook.Active != want.Active {
    97  			return fmt.Errorf("got hook %t; want %t", *hook.Active, want.Active)
    98  		}
    99  		if !strings.HasPrefix(*hook.URL, "https://") {
   100  			return fmt.Errorf("got http URL %q; want to start with 'https://'", *hook.URL)
   101  		}
   102  		if !reflect.DeepEqual(hook.Events, want.Events) {
   103  			return fmt.Errorf("got hook events %q; want %q", hook.Events, want.Events)
   104  		}
   105  		if !reflect.DeepEqual(hook.Config, want.Configuration) {
   106  			return fmt.Errorf("got hook configuration %q; want %q", hook.Config, want.Configuration)
   107  		}
   108  
   109  		return nil
   110  	}
   111  }
   112  
   113  func testAccCheckGithubOrganizationWebhookDestroy(s *terraform.State) error {
   114  	conn := testAccProvider.Meta().(*Organization).client
   115  	orgName := testAccProvider.Meta().(*Organization).name
   116  
   117  	for _, rs := range s.RootModule().Resources {
   118  		if rs.Type != "github_organization_webhook" {
   119  			continue
   120  		}
   121  
   122  		id, err := strconv.Atoi(rs.Primary.ID)
   123  		if err != nil {
   124  			return err
   125  		}
   126  
   127  		gotHook, resp, err := conn.Organizations.GetHook(context.TODO(), orgName, id)
   128  		if err == nil {
   129  			if gotHook != nil && *gotHook.ID == id {
   130  				return fmt.Errorf("Webhook still exists")
   131  			}
   132  		}
   133  		if resp.StatusCode != 404 {
   134  			return err
   135  		}
   136  		return nil
   137  	}
   138  	return nil
   139  }
   140  
   141  const testAccGithubOrganizationWebhookConfig = `
   142  resource "github_organization_webhook" "foo" {
   143    name = "web"
   144    configuration {
   145      url = "https://google.de/webhook"
   146      content_type = "json"
   147      insecure_ssl = true
   148    }
   149  
   150    events = ["pull_request"]
   151  }
   152  `
   153  
   154  const testAccGithubOrganizationWebhookUpdateConfig = `
   155  resource "github_organization_webhook" "foo" {
   156    name = "web"
   157    configuration {
   158      url = "https://google.de/webhooks"
   159      content_type = "form"
   160      insecure_ssl = false
   161    }
   162    active = false
   163  
   164    events = ["issues"]
   165  }
   166  `