github.com/section/sectionctl@v1.12.3/commands/apps_test.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "net/url" 9 "testing" 10 11 "github.com/section/sectionctl/api" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestCommandsAppsCreateAttemptsToValidateStackOnError(t *testing.T) { 16 assert := assert.New(t) 17 18 // Setup 19 var stackCalled bool 20 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 21 t.Logf("test request: %s\n", r.URL.Path) 22 switch r.URL.Path { 23 case "/api/v1/account/0/application/create": 24 w.WriteHeader(http.StatusForbidden) 25 fmt.Fprint(w, "{}") 26 case "/api/v1/stack": 27 stackCalled = true 28 fmt.Fprint(w, string(helperLoadBytes(t, "apps/create.response.with_success.json"))) 29 default: 30 assert.FailNowf("unhandled URL", "URL: %s", r.URL.Path) 31 } 32 })) 33 ur, err := url.Parse(ts.URL) 34 assert.NoError(err) 35 api.PrefixURI = ur 36 37 cmd := AppsCreateCmd{ 38 StackName: "helloworld-1.0.0", 39 } 40 41 42 // Invoke 43 logWriters := LogWriters{ConsoleWriter: io.Discard,FileWriter: io.Discard,ConsoleOnly: io.Discard,CarriageReturnWriter: io.Discard} 44 err = cmd.Run(&logWriters) 45 46 // Test 47 assert.True(stackCalled) 48 assert.Error(err) 49 assert.Regexp("bad request: unable to find stack", err) 50 }