github.com/go-chef/chef@v0.30.1/organization_test.go (about)

     1  package chef
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"net/http"
     9  	"os"
    10  	"reflect"
    11  	"testing"
    12  )
    13  
    14  var (
    15  	testOrganizationJSON = "test/organization.json"
    16  )
    17  
    18  func TestOrganizationFromJSONDecoder(t *testing.T) {
    19  	if file, err := os.Open(testOrganizationJSON); err != nil {
    20  		t.Error("Unexpected error '", err, "' during os.Open on", testOrganizationJSON)
    21  	} else {
    22  		dec := json.NewDecoder(file)
    23  		var g Organization
    24  		if err := dec.Decode(&g); err == io.EOF {
    25  			log.Fatal(g)
    26  		} else if err != nil {
    27  			log.Fatal(err)
    28  		}
    29  	}
    30  }
    31  
    32  func TestOrganizationslist(t *testing.T) {
    33  	setup()
    34  	defer teardown()
    35  
    36  	mux.HandleFunc("/organizations", func(w http.ResponseWriter, r *http.Request) {
    37  		switch {
    38  		case r.Method == "GET":
    39  			fmt.Fprintf(w, `{ "org_name1": "https://url/for/org_name1", "org_name2": "https://url/for/org_name2"}`)
    40  		case r.Method == "POST":
    41  			fmt.Fprintf(w, `{
    42  				 "name": "organization1"
    43  				"full_name": "This Organization"
    44  			 }`)
    45  		}
    46  	})
    47  
    48  	// Test list
    49  	organizations, err := client.Organizations.List()
    50  	if err != nil {
    51  		t.Errorf("Organizations.List returned error: %v", err)
    52  	}
    53  	listWant := map[string]string{"org_name1": "https://url/for/org_name1", "org_name2": "https://url/for/org_name2"}
    54  	if !reflect.DeepEqual(organizations, listWant) {
    55  		t.Errorf("Organizations.List returned %+v, want %+v", organizations, listWant)
    56  	}
    57  }
    58  
    59  func TestOrganizationsCreate(t *testing.T) {
    60  	setup()
    61  	defer teardown()
    62  
    63  	mux.HandleFunc("/organizations", func(w http.ResponseWriter, r *http.Request) {
    64  		switch {
    65  		case r.Method == "GET":
    66  			fmt.Fprintf(w, `{ "org_name1": "https://url/for/org_name1", "org_name2": "https://url/for/org_name2"}`)
    67  		case r.Method == "POST":
    68  			fmt.Fprintf(w, `{ "clientname": "organization3", "private_key": "mine", "uri": "https://url/for/organization3" }`)
    69  		}
    70  	})
    71  	var wantOrganization Organization
    72  	wantOrganization.Name = "organization3"
    73  	wantOrganization.FullName = "Chef Software"
    74  
    75  	res, err := client.Organizations.Create(wantOrganization)
    76  	if err != nil {
    77  		t.Errorf("Organizations.Create returned error: %s", err.Error())
    78  	}
    79  	createResult := OrganizationResult{ClientName: "organization3", PrivateKey: "mine", Uri: "https://url/for/organization3"}
    80  	if !reflect.DeepEqual(createResult, res) {
    81  		t.Errorf("Organizations.Post returned %+v, want %+v", res, createResult)
    82  	}
    83  }
    84  
    85  func TestOrganizationsUpdate(t *testing.T) {
    86  	setup()
    87  	defer teardown()
    88  
    89  	mux.HandleFunc("/organizations/organization3", func(w http.ResponseWriter, r *http.Request) {
    90  		switch {
    91  		case r.Method == "GET":
    92  			fmt.Fprintf(w, `{
    93                                  "name": "organization3",
    94                                  "full_name": "Chef Software",
    95                                  "guid": "f980d1asdfda0331235s00ff36862"
    96                          }`)
    97  		case r.Method == "PUT":
    98  			fmt.Fprintf(w, `{
    99                                  "name": "organization3",
   100                                  "full_name": "Updated Software",
   101                                  "guid": "f980d1asdfda0331235s00ff36862"
   102                          }`)
   103  		}
   104  	})
   105  	// test Update
   106  	organization, err := client.Organizations.Get("organization3")
   107  	organization.FullName = "Updated Software"
   108  	updateRes, err := client.Organizations.Update(organization)
   109  	if err != nil {
   110  		t.Errorf("Organizations.Update returned error: %v", err)
   111  	}
   112  	if !reflect.DeepEqual(updateRes, organization) {
   113  		t.Errorf("Organizations.Update returned %+v, want %+v", updateRes, organization)
   114  	}
   115  }
   116  
   117  func TestOrganizationsDelete(t *testing.T) {
   118  	setup()
   119  	defer teardown()
   120  
   121  	mux.HandleFunc("/organizations/organization3", func(w http.ResponseWriter, r *http.Request) {
   122  		switch {
   123  		// TODO: Add true test for PUT, updating an existing value.
   124  		case r.Method == "GET" || r.Method == "DELETE":
   125  			fmt.Fprintf(w, `{
   126                                  "name": "organization3",
   127                                  "full_name": "Chef Software",
   128                                  "guid": "f980d1asdfda0331235s00ff36862"
   129                          }`)
   130  		}
   131  	})
   132  
   133  	// test Delete
   134  	err := client.Organizations.Delete("organization3")
   135  	if err != nil {
   136  		t.Errorf("Organizations.Delete returned error: %v", err)
   137  	}
   138  }
   139  
   140  func TestOrganizationsGet(t *testing.T) {
   141  	// TODO: We should return HTTP response codes as defined by the Chef API so we can test for them.
   142  	setup()
   143  	defer teardown()
   144  
   145  	mux.HandleFunc("/organizations/organization3", func(w http.ResponseWriter, r *http.Request) {
   146  		switch {
   147  		// TODO: Add true test for PUT, updating an existing value.
   148  		case r.Method == "GET" || r.Method == "PUT" || r.Method == "DELETE":
   149  			fmt.Fprintf(w, `{
   150                  		"name": "organization3",
   151                  		"full_name": "Chef Software",
   152                  		"guid": "f980d1asdfda0331235s00ff36862"
   153              		}`)
   154  		}
   155  	})
   156  
   157  	// test Get
   158  	organization, err := client.Organizations.Get("organization3")
   159  	if err != nil {
   160  		t.Errorf("Organizations.Get returned error: %v", err)
   161  	}
   162  
   163  	var wantOrganization Organization
   164  	wantOrganization.Name = "organization3"
   165  	wantOrganization.FullName = "Chef Software"
   166  	wantOrganization.Guid = "f980d1asdfda0331235s00ff36862"
   167  	if !reflect.DeepEqual(organization, wantOrganization) {
   168  		t.Errorf("Organizations.Get returned %+v, want %+v", organization, wantOrganization)
   169  	}
   170  }