|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stackitcloud/stackit-cli/internal/pkg/types" |
| 8 | + |
| 9 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 10 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 11 | + "github.com/stackitcloud/stackit-cli/internal/pkg/testutils" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 13 | + |
| 14 | + "github.com/google/go-cmp/cmp" |
| 15 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 16 | + "github.com/google/uuid" |
| 17 | + "github.com/stackitcloud/stackit-sdk-go/services/logs" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + testRegion = "eu01" |
| 22 | + |
| 23 | + testDisplayName = "display-name" |
| 24 | + testDescription = "description" |
| 25 | +) |
| 26 | + |
| 27 | +type testCtxKey struct{} |
| 28 | + |
| 29 | +var ( |
| 30 | + testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo") |
| 31 | + testClient = &logs.APIClient{} |
| 32 | + testProjectId = uuid.NewString() |
| 33 | + testInstanceId = uuid.NewString() |
| 34 | +) |
| 35 | + |
| 36 | +func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string { |
| 37 | + flagValues := map[string]string{ |
| 38 | + globalflags.ProjectIdFlag: testProjectId, |
| 39 | + globalflags.RegionFlag: testRegion, |
| 40 | + |
| 41 | + instanceIdFlag: testInstanceId, |
| 42 | + displayNameFlag: testDisplayName, |
| 43 | + descriptionFlag: testDescription, |
| 44 | + permissionsFlag: "read,write", |
| 45 | + lifetimeFlag: "0", |
| 46 | + } |
| 47 | + for _, mod := range mods { |
| 48 | + mod(flagValues) |
| 49 | + } |
| 50 | + return flagValues |
| 51 | +} |
| 52 | + |
| 53 | +func fixtureInputModel(mods ...func(model *inputModel)) *inputModel { |
| 54 | + model := &inputModel{ |
| 55 | + GlobalFlagModel: &globalflags.GlobalFlagModel{ |
| 56 | + Verbosity: globalflags.VerbosityDefault, |
| 57 | + ProjectId: testProjectId, |
| 58 | + Region: testRegion, |
| 59 | + }, |
| 60 | + |
| 61 | + InstanceId: testInstanceId, |
| 62 | + Description: utils.Ptr(testDescription), |
| 63 | + DisplayName: testDisplayName, |
| 64 | + Lifetime: utils.Ptr(int64(0)), |
| 65 | + Permissions: []string{ |
| 66 | + "read", |
| 67 | + "write", |
| 68 | + }, |
| 69 | + } |
| 70 | + for _, mod := range mods { |
| 71 | + mod(model) |
| 72 | + } |
| 73 | + return model |
| 74 | +} |
| 75 | + |
| 76 | +func fixtureRequest(mods ...func(request *logs.ApiCreateAccessTokenRequest)) logs.ApiCreateAccessTokenRequest { |
| 77 | + request := testClient.CreateAccessToken(testCtx, testProjectId, testRegion, testInstanceId) |
| 78 | + request = request.CreateAccessTokenPayload(fixturePayload()) |
| 79 | + for _, mod := range mods { |
| 80 | + mod(&request) |
| 81 | + } |
| 82 | + return request |
| 83 | +} |
| 84 | + |
| 85 | +func fixturePayload(mods ...func(payload *logs.CreateAccessTokenPayload)) logs.CreateAccessTokenPayload { |
| 86 | + payload := logs.CreateAccessTokenPayload{ |
| 87 | + DisplayName: utils.Ptr(testDisplayName), |
| 88 | + Description: utils.Ptr(testDescription), |
| 89 | + Lifetime: utils.Ptr(int64(0)), |
| 90 | + Permissions: utils.Ptr([]string{ |
| 91 | + "read", |
| 92 | + "write", |
| 93 | + }), |
| 94 | + } |
| 95 | + for _, mod := range mods { |
| 96 | + mod(&payload) |
| 97 | + } |
| 98 | + return payload |
| 99 | +} |
| 100 | + |
| 101 | +func TestParseInput(t *testing.T) { |
| 102 | + tests := []struct { |
| 103 | + description string |
| 104 | + argValues []string |
| 105 | + flagValues map[string]string |
| 106 | + isValid bool |
| 107 | + expectedModel *inputModel |
| 108 | + }{ |
| 109 | + { |
| 110 | + description: "base", |
| 111 | + flagValues: fixtureFlagValues(), |
| 112 | + isValid: true, |
| 113 | + expectedModel: fixtureInputModel(), |
| 114 | + }, |
| 115 | + { |
| 116 | + description: "only required flags", |
| 117 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 118 | + delete(flagValues, lifetimeFlag) |
| 119 | + delete(flagValues, descriptionFlag) |
| 120 | + }), |
| 121 | + isValid: true, |
| 122 | + expectedModel: fixtureInputModel(func(model *inputModel) { |
| 123 | + model.Lifetime = nil |
| 124 | + model.Description = nil |
| 125 | + }), |
| 126 | + }, |
| 127 | + { |
| 128 | + description: "one permission", |
| 129 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 130 | + flagValues[permissionsFlag] = "read" |
| 131 | + }), |
| 132 | + isValid: true, |
| 133 | + expectedModel: fixtureInputModel(func(model *inputModel) { |
| 134 | + model.Permissions = []string{ |
| 135 | + "read", |
| 136 | + } |
| 137 | + }), |
| 138 | + }, |
| 139 | + { |
| 140 | + description: "no values", |
| 141 | + flagValues: map[string]string{}, |
| 142 | + isValid: false, |
| 143 | + }, |
| 144 | + { |
| 145 | + description: "project id missing", |
| 146 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 147 | + delete(flagValues, globalflags.ProjectIdFlag) |
| 148 | + }), |
| 149 | + isValid: false, |
| 150 | + }, |
| 151 | + { |
| 152 | + description: "project id invalid 1", |
| 153 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 154 | + flagValues[globalflags.ProjectIdFlag] = "" |
| 155 | + }), |
| 156 | + isValid: false, |
| 157 | + }, |
| 158 | + { |
| 159 | + description: "project id invalid 2", |
| 160 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 161 | + flagValues[globalflags.ProjectIdFlag] = "invalid-uuid" |
| 162 | + }), |
| 163 | + isValid: false, |
| 164 | + }, |
| 165 | + { |
| 166 | + description: "instance id missing", |
| 167 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 168 | + delete(flagValues, instanceIdFlag) |
| 169 | + }), |
| 170 | + isValid: false, |
| 171 | + }, |
| 172 | + { |
| 173 | + description: "instance id invalid 1", |
| 174 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 175 | + flagValues[instanceIdFlag] = "" |
| 176 | + }), |
| 177 | + isValid: false, |
| 178 | + }, |
| 179 | + { |
| 180 | + description: "instance id invalid 2", |
| 181 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 182 | + flagValues[instanceIdFlag] = "invalid-uuid" |
| 183 | + }), |
| 184 | + isValid: false, |
| 185 | + }, |
| 186 | + { |
| 187 | + description: "lifetime invalid", |
| 188 | + flagValues: fixtureFlagValues(func(flagValues map[string]string) { |
| 189 | + flagValues[lifetimeFlag] = "invalid-integer" |
| 190 | + }), |
| 191 | + isValid: false, |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + for _, tt := range tests { |
| 196 | + t.Run(tt.description, func(t *testing.T) { |
| 197 | + testutils.TestParseInput(t, NewCmd, parseInput, tt.expectedModel, tt.argValues, tt.flagValues, tt.isValid) |
| 198 | + }) |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +func TestBuildRequest(t *testing.T) { |
| 203 | + var tests = []struct { |
| 204 | + description string |
| 205 | + model *inputModel |
| 206 | + expectedRequest logs.ApiCreateAccessTokenRequest |
| 207 | + }{ |
| 208 | + { |
| 209 | + description: "base", |
| 210 | + model: fixtureInputModel(), |
| 211 | + expectedRequest: fixtureRequest(), |
| 212 | + }, |
| 213 | + } |
| 214 | + for _, tt := range tests { |
| 215 | + t.Run(tt.description, func(t *testing.T) { |
| 216 | + request := buildRequest(testCtx, tt.model, testClient) |
| 217 | + |
| 218 | + diff := cmp.Diff(tt.expectedRequest, request, |
| 219 | + cmp.AllowUnexported(tt.expectedRequest), |
| 220 | + cmpopts.EquateComparable(testCtx), |
| 221 | + ) |
| 222 | + if diff != "" { |
| 223 | + t.Fatalf("Data does not match: %s", diff) |
| 224 | + } |
| 225 | + }) |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +func TestOutputResult(t *testing.T) { |
| 230 | + type args struct { |
| 231 | + outputFormat string |
| 232 | + instanceLabel string |
| 233 | + accessToken *logs.AccessToken |
| 234 | + } |
| 235 | + tests := []struct { |
| 236 | + name string |
| 237 | + args args |
| 238 | + wantErr bool |
| 239 | + }{ |
| 240 | + { |
| 241 | + name: "base", |
| 242 | + args: args{ |
| 243 | + instanceLabel: "", |
| 244 | + accessToken: utils.Ptr(logs.AccessToken{ |
| 245 | + Id: utils.Ptr(uuid.NewString()), |
| 246 | + Permissions: utils.Ptr([]string{ |
| 247 | + "read", |
| 248 | + "write", |
| 249 | + }), |
| 250 | + DisplayName: utils.Ptr("Token"), |
| 251 | + AccessToken: utils.Ptr("Secret access token"), |
| 252 | + Creator: utils.Ptr(uuid.NewString()), |
| 253 | + Expires: utils.Ptr(false), |
| 254 | + Status: utils.Ptr(logs.ACCESSTOKENSTATUS_ACTIVE), |
| 255 | + }), |
| 256 | + }, |
| 257 | + wantErr: false, |
| 258 | + }, |
| 259 | + { |
| 260 | + name: "empty access token", |
| 261 | + args: args{ |
| 262 | + instanceLabel: "", |
| 263 | + accessToken: utils.Ptr(logs.AccessToken{}), |
| 264 | + }, |
| 265 | + wantErr: false, |
| 266 | + }, |
| 267 | + { |
| 268 | + name: "empty", |
| 269 | + args: args{}, |
| 270 | + wantErr: true, |
| 271 | + }, |
| 272 | + } |
| 273 | + p := print.NewPrinter() |
| 274 | + p.Cmd = NewCmd(&types.CmdParams{Printer: p}) |
| 275 | + for _, tt := range tests { |
| 276 | + t.Run(tt.name, func(t *testing.T) { |
| 277 | + if err := outputResult(p, tt.args.outputFormat, tt.args.instanceLabel, tt.args.accessToken); (err != nil) != tt.wantErr { |
| 278 | + t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr) |
| 279 | + } |
| 280 | + }) |
| 281 | + } |
| 282 | +} |
0 commit comments