github.com/jcarley/cli@v0.0.0-20180201210820-966d90434c30/commands/certs/update_test.go (about)

     1  package certs
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/daticahealth/cli/commands/services"
     9  	"github.com/daticahealth/cli/commands/ssl"
    10  	"github.com/daticahealth/cli/test"
    11  )
    12  
    13  var certUpdateTests = []struct {
    14  	name        string
    15  	pubKeyPath  string
    16  	privKeyPath string
    17  	downStream  string
    18  	selfSigned  bool
    19  	resolve     bool
    20  	expectErr   bool
    21  }{
    22  	{certName, pubKeyPath, privKeyPath, test.DownStream, true, false, false},
    23  	{certName, invalidPath, privKeyPath, test.DownStream, true, false, true}, // invalid cert path
    24  	{certName, pubKeyPath, invalidPath, test.DownStream, true, false, true},  // invalid key path
    25  	{certName, pubKeyPath, privKeyPath, test.DownStream, false, false, true}, // cert not signed by CA
    26  	{certName, pubKeyPath, privKeyPath, test.DownStream, true, true, false},
    27  	{"bad-cert-name", pubKeyPath, privKeyPath, test.DownStream, true, false, true},
    28  }
    29  
    30  func TestCertsUpdate(t *testing.T) {
    31  	mux, server, baseURL := test.Setup()
    32  	defer test.Teardown(server)
    33  	settings := test.GetSettings(baseURL.String())
    34  	mux.HandleFunc("/environments/"+test.EnvID+"/services/"+test.SvcID+"/certs/"+certName,
    35  		func(w http.ResponseWriter, r *http.Request) {
    36  			test.AssertEquals(t, r.Method, "PUT")
    37  			fmt.Fprint(w, fmt.Sprintf(`{"name":"%s"}`, certName))
    38  		},
    39  	)
    40  	mux.HandleFunc("/environments/"+test.EnvID+"/services",
    41  		func(w http.ResponseWriter, r *http.Request) {
    42  			test.AssertEquals(t, r.Method, "GET")
    43  			fmt.Fprint(w, fmt.Sprintf(`[{"id":"%s","label":"%s"}]`, test.SvcID, test.DownStream))
    44  		},
    45  	)
    46  
    47  	for _, data := range certUpdateTests {
    48  		t.Logf("Data: %+v", data)
    49  
    50  		// test
    51  		err := CmdUpdate(data.name, data.pubKeyPath, data.privKeyPath, data.downStream, data.selfSigned, data.resolve, New(settings), services.New(settings), ssl.New(settings))
    52  
    53  		// assert
    54  		if err != nil != data.expectErr {
    55  			t.Errorf("Unexpected error: %s", err)
    56  			continue
    57  		}
    58  	}
    59  }