|
| 1 | +package partneraccount |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/commands" |
| 7 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/config" |
| 8 | + smock "github.com/UpCloudLtd/upcloud-cli/v3/internal/mock" |
| 9 | + "github.com/UpCloudLtd/upcloud-cli/v3/internal/mockexecute" |
| 10 | + |
| 11 | + "github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCreateCommand(t *testing.T) { |
| 16 | + targetMethod := "CreatePartnerAccount" |
| 17 | + password := "superSecret123" |
| 18 | + |
| 19 | + generateArgs := func(namePhoneEmail bool, country string, state string, fullDetails bool) (args []string) { |
| 20 | + args = append(args, |
| 21 | + "--username", testPartnerAccount.Username, |
| 22 | + "--password", password, |
| 23 | + ) |
| 24 | + if namePhoneEmail { |
| 25 | + args = append(args, |
| 26 | + "--first-name", testPartnerAccount.FirstName, |
| 27 | + "--last-name", testPartnerAccount.LastName, |
| 28 | + "--phone", testPartnerAccount.Phone, |
| 29 | + "--email", testPartnerAccount.Email, |
| 30 | + ) |
| 31 | + } |
| 32 | + if country != "" { |
| 33 | + args = append(args, "--country", country) |
| 34 | + } |
| 35 | + if state != "" { |
| 36 | + args = append(args, "--state", state) |
| 37 | + } else if fullDetails { |
| 38 | + args = append(args, |
| 39 | + "--state", testPartnerAccount.State, |
| 40 | + "--company", testPartnerAccount.Company, |
| 41 | + "--address", testPartnerAccount.Address, |
| 42 | + "--postal-code", testPartnerAccount.PostalCode, |
| 43 | + "--city", testPartnerAccount.City, |
| 44 | + "--vat-number", testPartnerAccount.VATNumber, |
| 45 | + ) |
| 46 | + } |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + generateRequest := func(country string, state string, fullDetails bool) (req request.CreatePartnerAccountRequest) { |
| 51 | + req.Username = testPartnerAccount.Username |
| 52 | + req.Password = password |
| 53 | + if country != "" { |
| 54 | + cd := &request.CreatePartnerAccountContactDetails{} |
| 55 | + cd.FirstName = testPartnerAccount.FirstName |
| 56 | + cd.LastName = testPartnerAccount.LastName |
| 57 | + cd.Country = country |
| 58 | + cd.Phone = testPartnerAccount.Phone |
| 59 | + cd.Email = testPartnerAccount.Email |
| 60 | + if state != "" { |
| 61 | + cd.State = state |
| 62 | + } else if fullDetails { |
| 63 | + cd.State = testPartnerAccount.State |
| 64 | + cd.Company = testPartnerAccount.Company |
| 65 | + cd.Address = testPartnerAccount.Address |
| 66 | + cd.PostalCode = testPartnerAccount.PostalCode |
| 67 | + cd.City = testPartnerAccount.City |
| 68 | + cd.VATNumber = testPartnerAccount.VATNumber |
| 69 | + } |
| 70 | + req.ContactDetails = cd |
| 71 | + } |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + for _, test := range []struct { |
| 76 | + name string |
| 77 | + args []string |
| 78 | + request request.CreatePartnerAccountRequest |
| 79 | + error string |
| 80 | + }{ |
| 81 | + { |
| 82 | + name: "no args", |
| 83 | + args: []string{}, |
| 84 | + error: `required flag(s) "username", "password" not set`, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "no contact details", |
| 88 | + args: generateArgs(false, "", "", false), |
| 89 | + request: generateRequest("", "", false), |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "partial contact details", |
| 93 | + args: generateArgs(true, "", "", false), |
| 94 | + error: `when contact details are given, the following flags are required: "first-name", "last-name", "country", "phone", "email"`, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "minimal contact details", |
| 98 | + args: generateArgs(true, testPartnerAccount.Country, "", false), |
| 99 | + request: generateRequest(testPartnerAccount.Country, "", false), |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "USA without a state", |
| 103 | + args: generateArgs(true, "USA", "", false), |
| 104 | + error: `when contact country is "USA", flag "state" is also required`, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "USA with a state", |
| 108 | + args: generateArgs(true, "USA", "Florida", false), |
| 109 | + request: generateRequest("USA", "Florida", false), |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "full contact details", |
| 113 | + args: generateArgs(true, testPartnerAccount.Country, "", true), |
| 114 | + request: generateRequest(testPartnerAccount.Country, "", true), |
| 115 | + }, |
| 116 | + } { |
| 117 | + t.Run(test.name, func(t *testing.T) { |
| 118 | + mService := smock.Service{} |
| 119 | + req := test.request |
| 120 | + mService.On(targetMethod, &req).Return(&testPartnerAccount, nil) |
| 121 | + |
| 122 | + conf := config.New() |
| 123 | + command := commands.BuildCommand(CreateCommand(), nil, conf) |
| 124 | + command.Cobra().SetArgs(test.args) |
| 125 | + _, err := mockexecute.MockExecute(command, &mService, conf) |
| 126 | + |
| 127 | + if test.error != "" { |
| 128 | + assert.EqualError(t, err, test.error) |
| 129 | + } else { |
| 130 | + assert.NoError(t, err) |
| 131 | + mService.AssertNumberOfCalls(t, targetMethod, 1) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments