github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/get_health_check_command_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     7  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     8  	"github.com/liamawhite/cli-with-i18n/command/commandfakes"
     9  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    10  	. "github.com/liamawhite/cli-with-i18n/command/v2"
    11  	"github.com/liamawhite/cli-with-i18n/command/v2/v2fakes"
    12  	"github.com/liamawhite/cli-with-i18n/util/configv3"
    13  	"github.com/liamawhite/cli-with-i18n/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("get-health-check Command", func() {
    20  	var (
    21  		cmd             GetHealthCheckCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v2fakes.FakeGetHealthCheckActor
    26  		binaryName      string
    27  		executeErr      error
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    32  		fakeConfig = new(commandfakes.FakeConfig)
    33  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    34  		fakeActor = new(v2fakes.FakeGetHealthCheckActor)
    35  
    36  		cmd = GetHealthCheckCommand{
    37  			UI:          testUI,
    38  			Config:      fakeConfig,
    39  			SharedActor: fakeSharedActor,
    40  			Actor:       fakeActor,
    41  		}
    42  
    43  		binaryName = "faceman"
    44  		fakeConfig.BinaryNameReturns(binaryName)
    45  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    46  			Name: "some-org",
    47  		})
    48  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    49  			GUID: "some-space-guid",
    50  			Name: "some-space",
    51  		})
    52  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    53  	})
    54  
    55  	JustBeforeEach(func() {
    56  		executeErr = cmd.Execute(nil)
    57  	})
    58  
    59  	Context("when checking the target fails", func() {
    60  		BeforeEach(func() {
    61  			fakeSharedActor.CheckTargetReturns(
    62  				sharedaction.NotLoggedInError{BinaryName: binaryName})
    63  		})
    64  
    65  		It("returns a wrapped error", func() {
    66  			Expect(executeErr).To(MatchError(
    67  				translatableerror.NotLoggedInError{BinaryName: binaryName}))
    68  		})
    69  	})
    70  
    71  	Context("when getting the user returns an error", func() {
    72  		var expectedErr error
    73  
    74  		BeforeEach(func() {
    75  			expectedErr = errors.New("current user error")
    76  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    77  		})
    78  
    79  		It("returns a wrapped error", func() {
    80  			Expect(executeErr).To(MatchError(expectedErr))
    81  		})
    82  	})
    83  
    84  	Context("when getting the application returns an error", func() {
    85  		var expectedErr error
    86  
    87  		BeforeEach(func() {
    88  			cmd.RequiredArgs.AppName = "some-app"
    89  
    90  			expectedErr = errors.New("get health check error")
    91  			fakeActor.GetApplicationByNameAndSpaceReturns(
    92  				v2action.Application{}, v2action.Warnings{"warning-1"}, expectedErr)
    93  		})
    94  
    95  		It("displays warnings and returns the error", func() {
    96  			Expect(testUI.Err).To(Say("warning-1"))
    97  			Expect(executeErr).To(MatchError(expectedErr))
    98  		})
    99  	})
   100  
   101  	Context("when getting the application is successful", func() {
   102  		Context("when the health check type is not http", func() {
   103  			BeforeEach(func() {
   104  				cmd.RequiredArgs.AppName = "some-app"
   105  
   106  				fakeActor.GetApplicationByNameAndSpaceReturns(
   107  					v2action.Application{
   108  						HealthCheckType:         "some-health-check-type",
   109  						HealthCheckHTTPEndpoint: "/some-endpoint",
   110  					}, v2action.Warnings{"warning-1"}, nil)
   111  			})
   112  
   113  			It("show a blank endpoint and displays warnings", func() {
   114  				Expect(executeErr).ToNot(HaveOccurred())
   115  
   116  				Expect(testUI.Out).To(Say("Getting health check type for app some-app in org some-org / space some-space as some-user..."))
   117  				Expect(testUI.Out).To(Say("\n\n"))
   118  				Expect(testUI.Out).To(Say("health check type:          some-health-check-type"))
   119  				Expect(testUI.Out).To(Say("endpoint \\(for http type\\):   \n"))
   120  
   121  				Expect(testUI.Err).To(Say("warning-1"))
   122  
   123  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   124  				config, targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
   125  				Expect(config).To(Equal(fakeConfig))
   126  				Expect(targetedOrganizationRequired).To(Equal(true))
   127  				Expect(targetedSpaceRequired).To(Equal(true))
   128  
   129  				Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   130  
   131  				Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   132  				name, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   133  				Expect(name).To(Equal("some-app"))
   134  				Expect(spaceGUID).To(Equal("some-space-guid"))
   135  			})
   136  		})
   137  
   138  		Context("when the health check type is http", func() {
   139  			BeforeEach(func() {
   140  				cmd.RequiredArgs.AppName = "some-app"
   141  
   142  				fakeActor.GetApplicationByNameAndSpaceReturns(
   143  					v2action.Application{
   144  						HealthCheckType:         "http",
   145  						HealthCheckHTTPEndpoint: "/some-endpoint",
   146  					}, v2action.Warnings{"warning-1"}, nil)
   147  			})
   148  
   149  			It("shows the endpoint", func() {
   150  				Expect(executeErr).ToNot(HaveOccurred())
   151  
   152  				Expect(testUI.Out).To(Say("Getting health check type for app some-app in org some-org / space some-space as some-user..."))
   153  				Expect(testUI.Out).To(Say("\n\n"))
   154  				Expect(testUI.Out).To(Say("health check type:          http"))
   155  				Expect(testUI.Out).To(Say("endpoint \\(for http type\\):   /some-endpoint"))
   156  			})
   157  		})
   158  	})
   159  })