|
| 1 | +package cmd_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + . "github.com/onsi/ginkgo/v2" |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + |
| 10 | + "github.com/codesphere-cloud/cs-go/api" |
| 11 | + "github.com/codesphere-cloud/cs-go/cli/cmd" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("CreateWorkspace", func() { |
| 15 | + var ( |
| 16 | + mockEnv *cmd.MockEnv |
| 17 | + mockClient *cmd.MockClient |
| 18 | + mockPrompt *cmd.MockPrompt |
| 19 | + c *cmd.DeleteWorkspaceCmd |
| 20 | + wsId int |
| 21 | + wsName string |
| 22 | + confirmed bool |
| 23 | + ws api.Workspace |
| 24 | + ) |
| 25 | + |
| 26 | + JustBeforeEach(func() { |
| 27 | + mockClient = cmd.NewMockClient(GinkgoT()) |
| 28 | + mockEnv = cmd.NewMockEnv(GinkgoT()) |
| 29 | + mockPrompt = cmd.NewMockPrompt(GinkgoT()) |
| 30 | + c = &cmd.DeleteWorkspaceCmd{ |
| 31 | + Opts: cmd.DeleteWorkspaceOpts{ |
| 32 | + GlobalOptions: cmd.GlobalOptions{ |
| 33 | + Env: mockEnv, |
| 34 | + WorkspaceId: &wsId, |
| 35 | + }, |
| 36 | + Confirmed: &confirmed, |
| 37 | + }, |
| 38 | + Prompt: mockPrompt, |
| 39 | + } |
| 40 | + }) |
| 41 | + BeforeEach(func() { |
| 42 | + ws = api.Workspace{ |
| 43 | + Id: wsId, |
| 44 | + Name: wsName, |
| 45 | + } |
| 46 | + }) |
| 47 | + Context("Unconfirmed", func() { |
| 48 | + BeforeEach(func() { |
| 49 | + confirmed = false |
| 50 | + wsName = "fake-ws" |
| 51 | + }) |
| 52 | + Context("Workspace exists", func() { |
| 53 | + Context("Workspace name entered in confirmation prompt", func() { |
| 54 | + It("deletes the workspace", func() { |
| 55 | + mockClient.EXPECT().GetWorkspace(wsId).Return(ws, nil) |
| 56 | + mockPrompt.EXPECT().InputPrompt("Confirmation delete").Return(ws.Name) |
| 57 | + mockClient.EXPECT().DeleteWorkspace(wsId).Return(nil) |
| 58 | + err := c.DeleteWorkspace(mockClient, wsId) |
| 59 | + Expect(err).ToNot(HaveOccurred()) |
| 60 | + }) |
| 61 | + }) |
| 62 | + Context("Wrong input entered in confirmation prompt", func() { |
| 63 | + It("Returns an error", func() { |
| 64 | + mockClient.EXPECT().GetWorkspace(wsId).Return(ws, nil) |
| 65 | + mockPrompt.EXPECT().InputPrompt("Confirmation delete").Return("other-workspace") |
| 66 | + err := c.DeleteWorkspace(mockClient, wsId) |
| 67 | + Expect(err).To(MatchError("confirmation failed")) |
| 68 | + }) |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + }) |
| 73 | + |
| 74 | + Context("Confirmed via CLI flag", func() { |
| 75 | + var ( |
| 76 | + getWsErr error |
| 77 | + ) |
| 78 | + BeforeEach(func() { |
| 79 | + wsId = 42 |
| 80 | + ws = api.Workspace{} |
| 81 | + confirmed = true |
| 82 | + getWsErr = nil |
| 83 | + }) |
| 84 | + JustBeforeEach(func() { |
| 85 | + fmt.Println(ws) |
| 86 | + mockClient.EXPECT().GetWorkspace(wsId).Return(ws, getWsErr) |
| 87 | + }) |
| 88 | + Context("Workspace exists", func() { |
| 89 | + BeforeEach(func() { |
| 90 | + ws = api.Workspace{ |
| 91 | + Id: wsId, |
| 92 | + } |
| 93 | + }) |
| 94 | + It("Deletes the workspace", func() { |
| 95 | + mockClient.EXPECT().DeleteWorkspace(wsId).Return(nil) |
| 96 | + err := c.DeleteWorkspace(mockClient, wsId) |
| 97 | + Expect(err).ToNot(HaveOccurred()) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + Context("Workspace doesn't exist", func() { |
| 102 | + BeforeEach(func() { |
| 103 | + ws = api.Workspace{} |
| 104 | + getWsErr = errors.New("404") |
| 105 | + }) |
| 106 | + |
| 107 | + It("Returns an error", func() { |
| 108 | + err := c.DeleteWorkspace(mockClient, wsId) |
| 109 | + Expect(err).To(MatchError("failed to get workspace 42: 404")) |
| 110 | + }) |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | +}) |
0 commit comments