|
| 1 | +// Copyright (c) Codesphere Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package cmd_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + |
| 9 | + "github.com/codesphere-cloud/cs-go/cli/cmd" |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("AddTeamMember", func() { |
| 15 | + var ( |
| 16 | + mockEnv *cmd.MockEnv |
| 17 | + mockClient *cmd.MockClient |
| 18 | + c *cmd.AddTeamMemberCmd |
| 19 | + teamId int |
| 20 | + dcId int |
| 21 | + email string |
| 22 | + ) |
| 23 | + |
| 24 | + BeforeEach(func() { |
| 25 | + mockClient = cmd.NewMockClient(GinkgoT()) |
| 26 | + mockEnv = cmd.NewMockEnv(GinkgoT()) |
| 27 | + teamId = 42 |
| 28 | + email = "test@test.com" |
| 29 | + dcId = 1 // Default data center ID for testing |
| 30 | + c = &cmd.AddTeamMemberCmd{ |
| 31 | + Opts: cmd.AddTeamMemberOpts{ |
| 32 | + GlobalOptions: &cmd.GlobalOptions{ |
| 33 | + Env: mockEnv, |
| 34 | + TeamId: teamId, |
| 35 | + // OrgId is intentionally left empty here, will be set in BeforeEach for specific contexts |
| 36 | + }, |
| 37 | + Email: email, |
| 38 | + TeamId: teamId, |
| 39 | + }, |
| 40 | + ClientFactory: func(opts cmd.GlobalOptions) (cmd.Client, error) { |
| 41 | + return mockClient, nil |
| 42 | + }, |
| 43 | + } |
| 44 | + // Mock common environment calls needed for client creation |
| 45 | + }) |
| 46 | + |
| 47 | + AfterEach(func() { |
| 48 | + mockEnv.AssertExpectations(GinkgoT()) |
| 49 | + mockClient.AssertExpectations(GinkgoT()) |
| 50 | + }) |
| 51 | + |
| 52 | + Context("Validation", func() { |
| 53 | + It("should fail if the mail is empty", func() { |
| 54 | + |
| 55 | + err := c.AddTeamMember(mockClient, teamId, "", dcId) |
| 56 | + |
| 57 | + Expect(err).To(HaveOccurred()) |
| 58 | + Expect(err.Error()).To(Equal("email cannot be empty")) |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + Context("RunE execution flow", func() { |
| 63 | + It("should successfully add a member to a team", func() { |
| 64 | + mockClient.EXPECT().AddTeamMember(teamId, email, 0).Return(nil).Once() |
| 65 | + |
| 66 | + err := c.RunE(nil, []string{}) |
| 67 | + Expect(err).ToNot(HaveOccurred()) |
| 68 | + }) |
| 69 | + |
| 70 | + It("should fail when the token is not allowed to add a member", func() { |
| 71 | + mockClient.EXPECT().AddTeamMember(teamId, email, 0).Return(errors.New("failed")).Once() |
| 72 | + |
| 73 | + err := c.RunE(nil, []string{}) |
| 74 | + Expect(err).To(HaveOccurred()) |
| 75 | + Expect(err.Error()).To(ContainSubstring("failed to add member to team: ")) |
| 76 | + }) |
| 77 | + |
| 78 | + It("should fail when client creation fails", func() { |
| 79 | + c.ClientFactory = func(opts cmd.GlobalOptions) (cmd.Client, error) { |
| 80 | + return nil, errors.New("client init failed") |
| 81 | + } |
| 82 | + |
| 83 | + err := c.RunE(nil, []string{}) |
| 84 | + Expect(err).To(HaveOccurred()) |
| 85 | + Expect(err.Error()).To(ContainSubstring("failed to create Codespehre client: client init failed")) |
| 86 | + }) |
| 87 | + |
| 88 | + It("should fail when team ID is unavailable", func() { |
| 89 | + c.Opts.GlobalOptions.TeamId = -1 |
| 90 | + mockEnv.EXPECT().GetTeamId().Return(-1, errors.New("CS_TEAM_ID env var required, but not set")).Once() |
| 91 | + |
| 92 | + err := c.RunE(nil, []string{}) |
| 93 | + Expect(err).To(HaveOccurred()) |
| 94 | + Expect(err.Error()).To(Equal("CS_TEAM_ID env var required, but not set")) |
| 95 | + }) |
| 96 | + |
| 97 | + It("should fail when email is empty", func() { |
| 98 | + c.Opts.Email = "" |
| 99 | + err := c.RunE(nil, []string{}) |
| 100 | + Expect(err).To(HaveOccurred()) |
| 101 | + Expect(err.Error()).To(Equal("email cannot be empty")) |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | +}) |
0 commit comments