github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/github/resource_github_repository_collaborator_test.go (about)

     1  package github
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/google/go-github/github"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  const expectedPermission string = "admin"
    13  
    14  func TestAccGithubRepositoryCollaborator_basic(t *testing.T) {
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckGithubRepositoryCollaboratorDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccGithubRepositoryCollaboratorConfig,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckGithubRepositoryCollaboratorExists("github_repository_collaborator.test_repo_collaborator"),
    24  					testAccCheckGithubRepositoryCollaboratorPermission("github_repository_collaborator.test_repo_collaborator"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccGithubRepositoryCollaborator_importBasic(t *testing.T) {
    32  	resource.Test(t, resource.TestCase{
    33  		PreCheck:     func() { testAccPreCheck(t) },
    34  		Providers:    testAccProviders,
    35  		CheckDestroy: testAccCheckGithubRepositoryCollaboratorDestroy,
    36  		Steps: []resource.TestStep{
    37  			resource.TestStep{
    38  				Config: testAccGithubRepositoryCollaboratorConfig,
    39  			},
    40  			resource.TestStep{
    41  				ResourceName:      "github_repository_collaborator.test_repo_collaborator",
    42  				ImportState:       true,
    43  				ImportStateVerify: true,
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func testAccCheckGithubRepositoryCollaboratorDestroy(s *terraform.State) error {
    50  	conn := testAccProvider.Meta().(*Organization).client
    51  
    52  	for _, rs := range s.RootModule().Resources {
    53  		if rs.Type != "github_repository_collaborator" {
    54  			continue
    55  		}
    56  
    57  		o := testAccProvider.Meta().(*Organization).name
    58  		r, u := parseTwoPartID(rs.Primary.ID)
    59  		isCollaborator, _, err := conn.Repositories.IsCollaborator(o, r, u)
    60  
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		if isCollaborator {
    66  			return fmt.Errorf("Repository collaborator still exists")
    67  		}
    68  
    69  		return nil
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func testAccCheckGithubRepositoryCollaboratorExists(n string) resource.TestCheckFunc {
    76  	return func(s *terraform.State) error {
    77  		rs, ok := s.RootModule().Resources[n]
    78  		if !ok {
    79  			return fmt.Errorf("Not Found: %s", n)
    80  		}
    81  
    82  		if rs.Primary.ID == "" {
    83  			return fmt.Errorf("No membership ID is set")
    84  		}
    85  
    86  		conn := testAccProvider.Meta().(*Organization).client
    87  		o := testAccProvider.Meta().(*Organization).name
    88  		r, u := parseTwoPartID(rs.Primary.ID)
    89  
    90  		isCollaborator, _, err := conn.Repositories.IsCollaborator(o, r, u)
    91  
    92  		if err != nil {
    93  			return err
    94  		}
    95  
    96  		if !isCollaborator {
    97  			return fmt.Errorf("Repository collaborator does not exist")
    98  		}
    99  
   100  		return nil
   101  	}
   102  }
   103  
   104  func testAccCheckGithubRepositoryCollaboratorPermission(n string) resource.TestCheckFunc {
   105  	return func(s *terraform.State) error {
   106  		rs, ok := s.RootModule().Resources[n]
   107  		if !ok {
   108  			return fmt.Errorf("Not Found: %s", n)
   109  		}
   110  
   111  		if rs.Primary.ID == "" {
   112  			return fmt.Errorf("No membership ID is set")
   113  		}
   114  
   115  		conn := testAccProvider.Meta().(*Organization).client
   116  		o := testAccProvider.Meta().(*Organization).name
   117  		r, u := parseTwoPartID(rs.Primary.ID)
   118  
   119  		collaborators, _, err := conn.Repositories.ListCollaborators(o, r, &github.ListOptions{})
   120  
   121  		if err != nil {
   122  			return err
   123  		}
   124  
   125  		for _, c := range collaborators {
   126  			if *c.Login == u {
   127  				permName, err := getRepoPermission(c.Permissions)
   128  
   129  				if err != nil {
   130  					return err
   131  				}
   132  
   133  				if permName != expectedPermission {
   134  					return fmt.Errorf("Expected permission %s on repository collaborator, actual permission %s", expectedPermission, permName)
   135  				}
   136  
   137  				return nil
   138  			}
   139  		}
   140  
   141  		return fmt.Errorf("Repository collaborator did not appear in list of collaborators on repository")
   142  	}
   143  }
   144  
   145  var testAccGithubRepositoryCollaboratorConfig string = fmt.Sprintf(`
   146    resource "github_repository_collaborator" "test_repo_collaborator" {
   147      repository = "%s"
   148      username = "%s"
   149      permission = "%s"
   150    }
   151  `, testRepo, testCollaborator, expectedPermission)