-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathproject_test.go
More file actions
387 lines (315 loc) · 13.1 KB
/
project_test.go
File metadata and controls
387 lines (315 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//go:build integration
package integration
import (
"fmt"
asserts "github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"log"
"os"
"strings"
"testing"
"github.com/checkmarx/ast-cli/internal/commands/util/printer"
errorConstants "github.com/checkmarx/ast-cli/internal/constants/errors"
"github.com/checkmarx/ast-cli/internal/params"
"github.com/checkmarx/ast-cli/internal/wrappers"
"github.com/google/uuid"
"github.com/spf13/viper"
"gotest.tools/assert"
)
const SSHKeyFilePath = "ssh-key-file.txt"
// End-to-end test of project handling.
// - Create a project
// - Get and assert the project exists
// - Get all tags
// - Assert assigned tags exist
// - Assert project contains the tags
// - Assert project contains the groups
// - Delete the created project
// - Get and assert the project was deleted
func TestProjectsE2E(t *testing.T) {
projectID, _ := createProject(t, Tags, Groups)
response := listProjectByID(t, projectID)
assert.Equal(t, len(response), 1, "Total projects should be 1")
assert.Equal(t, response[0].ID, projectID, "Project ID should match the created project")
project := showProject(t, projectID)
assert.Equal(t, project.ID, projectID, "Project ID should match the created project")
assertTagsAndGroups(t, project, Groups)
deleteProject(t, projectID)
response = listProjectByID(t, projectID)
assert.Equal(t, len(response), 0, "Total projects should be 0 as the project was deleted")
}
func TestGetProjectByTagsFilter_whenProjectHasNoneTags_shouldReturnProjectWithNoTags(t *testing.T) {
projectID, _ := createProject(t, nil, nil)
defer deleteProject(t, projectID)
projects := listProjectByTagsAndLimit(t, "NONE", "NONE", "5")
fmt.Println("Projects length: ", len(projects))
for _, project := range projects {
assert.Equal(t, len(project.Tags), 0, "Project should have no tags")
}
}
// Assert project contains created tags and groups
func assertTagsAndGroups(t *testing.T, project wrappers.ProjectResponseModel, groups []string) {
allTags := getAllTags(t, "project")
for key := range Tags {
_, ok := allTags[key]
assert.Assert(t, ok, "Get all tags response should contain all created tags. Missing %s", key)
val, ok := project.Tags[key]
assert.Assert(t, ok, "Project should contain all created tags. Missing %s", key)
assert.Equal(t, val, Tags[key], "Tag value should be equal")
}
assert.Assert(t, len(project.Groups) >= len(groups), "The project must contain at least %d groups", len(groups))
}
// Create a project with empty project name should fail
func TestCreateEmptyProjectName(t *testing.T) {
err, _ := executeCommand(
t, "project", "create", flag(params.FormatFlag),
printer.FormatJSON, flag(params.ProjectName), "",
)
assertError(t, err, "Project name is required")
}
// Create the same project twice and assert that it fails
func TestCreateAlreadyExistingProject(t *testing.T) {
assertRequiredParameter(t, "Project name is required", "project", "create")
_, projectName := getRootProject(t)
err, _ := executeCommand(
t, "project", "create", flag(params.FormatFlag),
printer.FormatJSON, flag(params.ProjectName), projectName,
)
assertError(t, err, "Failed creating a project: CODE: 208, Failed to create a project, project name")
}
func TestProjectCreate_ApplicationDoesntExist_FailAndReturnErrorMessage(t *testing.T) {
err, _ := executeCommand(
t, "project", "create", flag(params.FormatFlag),
printer.FormatJSON, flag(params.ProjectName), projectNameRandom,
flag(params.ApplicationName), "application-that-doesnt-exist",
)
assertError(t, err, errorConstants.ApplicationDoesntExistOrNoPermission)
}
func TestProjectCreate_ApplicationExists_CreateProjectSuccessfully(t *testing.T) {
err, outBuffer := executeCommand(
t, "project", "create", flag(params.FormatFlag),
printer.FormatJSON, flag(params.ProjectName), projectNameRandom,
flag(params.ApplicationName), "my-application",
)
createdProject := wrappers.ProjectResponseModel{}
unmarshall(t, outBuffer, &createdProject, "Reading project create response JSON should pass")
defer deleteProject(t, createdProject.ID)
assert.NilError(t, err)
assert.Assert(t, createdProject.ID != "", "Project ID should not be empty")
assert.Assert(t, len(createdProject.ApplicationIds) == 1, "The project must be connected to the application")
}
func TestCreateWithInvalidGroup(t *testing.T) {
err, _ := executeCommand(
t, "project", "create", flag(params.FormatFlag),
printer.FormatJSON, flag(params.ProjectName), "project", flag(params.GroupList), "invalidGroup",
)
assertError(t, err, "Failed finding groups: [invalidGroup]")
}
// when both of these FF are on , validation based on permission of assign-groups-to-all is done by api call.
// if user is not having this permission and user is trying to assign other groups , error is thrown
func TestCreateProjectWhenUserdoes_not_have_groups_permission(t *testing.T) {
if !isFFEnabled(t, "ACCESS_MANAGEMENT_ENABLED") || !isFFEnabled(t, "GROUPS_VALIDATION_ENABLED") {
t.Skip("ACCESS_MANAGEMENT_ENABLED OR GROUPS_VALIDATION_ENABLED FFs are not enabled... Skipping test")
}
groups := []string{
"TT_Group1",
}
groupsStr := formatGroups(groups)
_, outBuffer := executeCommand(
t, "project", "create",
flag(params.FormatFlag),
printer.FormatJSON,
flag(params.ProjectName), projectNameRandom, flag(params.GroupList), groupsStr,
)
assert.Assert(t, outBuffer != nil, "Project creation output response should not be nil")
}
func TestCreateProjectWhenUserdoes_not_have_groups_permission_butonlyAM1_is_On(t *testing.T) {
if !isFFEnabled(t, "ACCESS_MANAGEMENT_ENABLED") || (isFFEnabled(t, "GROUPS_VALIDATION_ENABLED") && isFFEnabled(t, "ACCESS_MANAGEMENT_ENABLED")) {
t.Skip("ACCESS_MANAGEMENT_ENABLED FFs are not enabled... Skipping test")
}
groups := []string{
"it_test_group_1",
"it_test_group_2",
}
groupsStr := formatGroups(groups)
_, outBuffer := executeCommand(
t, "project", "create",
flag(params.FormatFlag),
printer.FormatJSON,
flag(params.ProjectName), projectNameRandom, flag(params.GroupList), groupsStr,
)
createdProject := wrappers.ProjectResponseModel{}
unmarshall(t, outBuffer, &createdProject, "Reading project create response JSON should pass")
fmt.Printf("New project created with id: %s \n", createdProject.ID)
assert.Assert(t, createdProject.ID != "", "Project ID should not be empty")
defer deleteProject(t, createdProject.ID)
}
func TestProjectCreate_WhenCreatingProjectWithExistingName_FailProjectCreation(t *testing.T) {
err, _ := executeCommand(
t, "project", "create",
flag(params.FormatFlag),
printer.FormatJSON,
flag(params.ProjectName), "project",
)
assertError(t, err, "Failed creating a project: CODE: 208, Failed to create a project, project name 'project' already exists\n")
}
// Test list project's branches
func TestProjectBranches(t *testing.T) {
assertRequiredParameter(
t,
"Failed getting branches for project: Please provide a project ID",
"project",
"branches",
)
projectID, _ := getRootProject(t)
buffer := executeCmdNilAssertion(t, "Branches should be listed", "project", "branches", "--project-id", projectID)
result, readingError := io.ReadAll(buffer)
assert.NilError(t, readingError, "Reading result should pass")
assert.Assert(t, strings.Contains(string(result), "[]"))
}
func createProject(t *testing.T, tags map[string]string, groups []string) (string, string) {
projectName := getProjectNameForTest() + "_for_project"
return createNewProject(t, tags, groups, projectName)
}
func createNewProject(t *testing.T, tags map[string]string, groups []string, projectName string) (string, string) {
tagsStr := formatTags(tags)
groupsStr := formatGroups(groups)
fmt.Printf("Creating project : %s \n", projectName)
outBuffer := executeCmdNilAssertion(
t, "Creating a project should pass",
"project", "create",
flag(params.FormatFlag), printer.FormatJSON,
flag(params.ProjectName), projectName,
flag(params.BranchFlag), "master",
flag(params.TagList), tagsStr,
flag(params.GroupList), groupsStr,
)
createdProject := wrappers.ProjectResponseModel{}
createdProjectJSON := unmarshall(t, outBuffer, &createdProject, "Reading project create response JSON should pass")
fmt.Println("Response after project is created : ", string(createdProjectJSON))
fmt.Printf("New project created with id: %s \n", createdProject.ID)
return createdProject.ID, projectName
}
func deleteProject(t *testing.T, projectID string) {
log.Println("Deleting the project with id ", projectID)
fmt.Println("Deleting the project with id ", projectID)
executeCmdNilAssertion(
t,
"Deleting a project should pass",
"project",
"delete",
flag(params.ProjectIDFlag),
projectID,
)
}
func deleteProjectByName(t *testing.T, projectName string) {
projectsWrapper := wrappers.NewHTTPProjectsWrapper(viper.GetString(params.ProjectsPathKey))
projectModel, _, err := projectsWrapper.GetByName(projectName)
if err == nil && projectModel != nil {
deleteProject(t, projectModel.ID)
}
}
func listProjectByID(t *testing.T, projectID string) []wrappers.ProjectResponseModel {
idFilter := fmt.Sprintf("ids=%s", projectID)
fmt.Println("Listing project for id ", projectID)
outputBuffer := executeCmdNilAssertion(
t,
"Getting the project should pass",
"project", "list",
flag(params.FormatFlag), printer.FormatJSON, flag(params.FilterFlag), idFilter,
)
var projects []wrappers.ProjectResponseModel
_ = unmarshall(t, outputBuffer, &projects, "Reading all projects response JSON should pass")
fmt.Println("Listing project for id projects length: ", len(projects))
return projects
}
func listProjectByTagsAndLimit(t *testing.T, tagsKeys string, tagsValues string, limit string) []wrappers.ProjectResponseModel {
tagsFilter := fmt.Sprintf("%s=%s,%s=%s", params.TagsKeyQueryParam, tagsKeys, params.TagsValueQueryParam, tagsValues)
limitFilter := fmt.Sprintf("%s=%s", params.LimitQueryParam, limit)
fmt.Println("Listing project for filters: ", tagsFilter, ",", limitFilter)
filters := tagsFilter + "," + limitFilter
outputBuffer := executeCmdNilAssertion(
t,
"Getting the project should pass",
"project", "list",
flag(params.FormatFlag), printer.FormatJSON, flag(params.FilterFlag), filters,
)
var projects []wrappers.ProjectResponseModel
_ = unmarshall(t, outputBuffer, &projects, "Reading all projects response JSON should pass")
fmt.Println("Listing project for tags projects length: ", len(projects))
return projects
}
func showProject(t *testing.T, projectID string) wrappers.ProjectResponseModel {
assertRequiredParameter(t, "Failed getting a project: Please provide a project ID", "project", "show")
outputBuffer := executeCmdNilAssertion(
t, "Getting the project should pass", "project", "show",
flag(params.FormatFlag), printer.FormatJSON,
flag(params.ProjectIDFlag), projectID,
)
var project wrappers.ProjectResponseModel
_ = unmarshall(t, outputBuffer, &project, "Reading project JSON should pass")
return project
}
func TestCreateProjectWithSSHKey(t *testing.T) {
projectName := fmt.Sprintf("ast-cli-tests_%s", uuid.New().String()) + "_for_project"
tagsStr := formatTags(Tags)
_ = viper.BindEnv("CX_SCAN_SSH_KEY")
sshKey := viper.GetString("CX_SCAN_SSH_KEY")
_ = ioutil.WriteFile(SSHKeyFilePath, []byte(sshKey), 0644)
defer func() { _ = os.Remove(SSHKeyFilePath) }()
cmd := createASTIntegrationTestCommand(t)
err := execute(
cmd,
"project", "create",
flag(params.FormatFlag), printer.FormatJSON,
flag(params.ProjectName), projectName,
flag(params.BranchFlag), "master",
flag(params.TagList), tagsStr,
flag(params.RepoURLFlag), SSHRepo,
flag(params.SSHKeyFlag), "",
)
assert.Assert(t, err != nil)
err = execute(
cmd,
"project", "create",
flag(params.FormatFlag), printer.FormatJSON,
flag(params.ProjectName), projectName,
flag(params.BranchFlag), "master",
flag(params.TagList), tagsStr,
flag(params.RepoURLFlag), "",
flag(params.SSHKeyFlag), SSHKeyFilePath,
)
assert.Assert(t, err != nil)
fmt.Printf("Creating project : %s \n", projectName)
outBuffer := executeCmdNilAssertion(
t, "Creating a project with ssh key should pass",
"project", "create",
flag(params.FormatFlag), printer.FormatJSON,
flag(params.ProjectName), projectName,
flag(params.BranchFlag), "master",
flag(params.TagList), tagsStr,
flag(params.RepoURLFlag), SSHRepo,
flag(params.SSHKeyFlag), SSHKeyFilePath,
)
createdProject := wrappers.ProjectResponseModel{}
createdProjectJSON := unmarshall(t, outBuffer, &createdProject, "Reading project create response JSON should pass")
fmt.Println("Response after project is created : ", string(createdProjectJSON))
fmt.Printf("New project created with id: %s \n", createdProject.ID)
deleteProject(t, createdProject.ID)
}
func TestProjectShow_MainBranch_Exist(t *testing.T) {
projectID, projectName := createProject(t, Tags, Groups)
defer deleteProject(t, projectID)
args := []string{
"scan", "create",
flag(params.ProjectName), projectName,
flag(params.SourcesFlag), "data/insecure.zip",
flag(params.BranchFlag), "dummy_branch",
flag(params.BranchPrimaryFlag),
}
err, _ := executeCommand(t, args...)
assert.NilError(t, err)
project := showProject(t, projectID)
asserts.Contains(t, project.MainBranch, "dummy_branch", "Project main branch should be 'dummy_branch'")
}