|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/opf/openproject-cli/components/printer" |
| 11 | + "github.com/opf/openproject-cli/components/requests" |
| 12 | + "github.com/opf/openproject-cli/components/routes" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCreateWorkPackagePrintsDryRunJSONWithParent(t *testing.T) { |
| 16 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | + switch r.URL.Path { |
| 18 | + case "/api/v3/work_packages/74316": |
| 19 | + _, _ = io.WriteString(w, `{ |
| 20 | + "id": 74316, |
| 21 | + "subject": "Expand op CLI to support scripted work package workflows", |
| 22 | + "_embedded": { |
| 23 | + "project": { |
| 24 | + "id": 1482, |
| 25 | + "identifier": "cli", |
| 26 | + "name": "CLI" |
| 27 | + } |
| 28 | + }, |
| 29 | + "_links": { |
| 30 | + "self": {"href": "/api/v3/work_packages/74316"}, |
| 31 | + "project": {"href": "/api/v3/projects/1482", "title": "CLI"}, |
| 32 | + "status": {"href": "/api/v3/statuses/1", "title": "new"}, |
| 33 | + "type": {"href": "/api/v3/types/6", "title": "Feature"}, |
| 34 | + "assignee": {"href": null, "title": ""} |
| 35 | + } |
| 36 | + }`) |
| 37 | + default: |
| 38 | + t.Fatalf("unexpected path %s", r.URL.Path) |
| 39 | + } |
| 40 | + })) |
| 41 | + defer server.Close() |
| 42 | + |
| 43 | + host, err := url.Parse(server.URL) |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + |
| 48 | + requests.Init(host, "token", false) |
| 49 | + routes.Init(host) |
| 50 | + |
| 51 | + activePrinter := &printer.TestingPrinter{} |
| 52 | + printer.Init(activePrinter) |
| 53 | + |
| 54 | + projectId = 0 |
| 55 | + parentWorkPackageID = 74316 |
| 56 | + shouldOpenWorkPackageInBrowser = false |
| 57 | + printCreatedWorkPackageAsJSON = true |
| 58 | + dryRunCreateWorkPackage = true |
| 59 | + typeFlag = "" |
| 60 | + |
| 61 | + createWorkPackage(nil, []string{"Build reusable skill"}) |
| 62 | + |
| 63 | + expected := "{\"valid\":true,\"operation\":\"create\",\"project_id\":1482,\"parent_id\":74316,\"work_package\":{\"subject\":\"Build reusable skill\",\"type\":\"\"}}\n" |
| 64 | + if activePrinter.Result != expected { |
| 65 | + t.Fatalf("expected %s, got %s", expected, activePrinter.Result) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestCreateWorkPackagePrintsJSONErrorForFlagConflict(t *testing.T) { |
| 70 | + activePrinter := &printer.TestingPrinter{} |
| 71 | + printer.Init(activePrinter) |
| 72 | + |
| 73 | + projectId = 1482 |
| 74 | + parentWorkPackageID = 0 |
| 75 | + shouldOpenWorkPackageInBrowser = true |
| 76 | + printCreatedWorkPackageAsJSON = true |
| 77 | + dryRunCreateWorkPackage = false |
| 78 | + typeFlag = "" |
| 79 | + |
| 80 | + createWorkPackage(nil, []string{"Build reusable skill"}) |
| 81 | + |
| 82 | + expected := "{\"error\":{\"code\":\"conflicting_arguments\",\"message\":\"cannot use --open together with --json\"}}\n" |
| 83 | + if activePrinter.Result != expected { |
| 84 | + t.Fatalf("expected %s, got %s", expected, activePrinter.Result) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestValidateCreateWorkPackageFlagsRejectsDryRunWithoutJSON(t *testing.T) { |
| 89 | + projectId = 1482 |
| 90 | + parentWorkPackageID = 0 |
| 91 | + shouldOpenWorkPackageInBrowser = false |
| 92 | + printCreatedWorkPackageAsJSON = false |
| 93 | + dryRunCreateWorkPackage = true |
| 94 | + typeFlag = "" |
| 95 | + |
| 96 | + err := validateCreateWorkPackageFlags() |
| 97 | + if err == nil { |
| 98 | + t.Fatal("expected validation error") |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestCreateWorkPackagePrintsJSONWithoutChildrenQuery(t *testing.T) { |
| 103 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 104 | + switch r.URL.Path { |
| 105 | + case "/api/v3/projects/1482": |
| 106 | + _, _ = io.WriteString(w, `{ |
| 107 | + "id": 1482, |
| 108 | + "identifier": "cli", |
| 109 | + "name": "CLI", |
| 110 | + "_links": { |
| 111 | + "types": {"href": "/api/v3/projects/1482/types/available"} |
| 112 | + } |
| 113 | + }`) |
| 114 | + case "/api/v3/projects/1482/types/available": |
| 115 | + _, _ = io.WriteString(w, `{ |
| 116 | + "_embedded": { |
| 117 | + "elements": [ |
| 118 | + { |
| 119 | + "id": 7, |
| 120 | + "name": "Implementation", |
| 121 | + "_links": { |
| 122 | + "self": {"href": "/api/v3/types/7"} |
| 123 | + } |
| 124 | + } |
| 125 | + ] |
| 126 | + } |
| 127 | + }`) |
| 128 | + case "/api/v3/projects/1482/work_packages": |
| 129 | + _, _ = io.WriteString(w, `{ |
| 130 | + "id": 74415, |
| 131 | + "subject": "Build reusable skill", |
| 132 | + "_links": { |
| 133 | + "self": {"href": "/api/v3/work_packages/74415"}, |
| 134 | + "project": {"href": "/api/v3/projects/1482", "title": "CLI"}, |
| 135 | + "status": {"href": "/api/v3/statuses/1", "title": "new"}, |
| 136 | + "type": {"href": "/api/v3/types/7", "title": "Implementation"}, |
| 137 | + "assignee": {"href": null, "title": ""} |
| 138 | + }, |
| 139 | + "_embedded": { |
| 140 | + "project": { |
| 141 | + "id": 1482, |
| 142 | + "identifier": "cli", |
| 143 | + "name": "CLI" |
| 144 | + } |
| 145 | + } |
| 146 | + }`) |
| 147 | + case "/api/v3/work_packages/74415": |
| 148 | + _, _ = io.WriteString(w, `{ |
| 149 | + "id": 74415, |
| 150 | + "subject": "Build reusable skill", |
| 151 | + "_links": { |
| 152 | + "self": {"href": "/api/v3/work_packages/74415"}, |
| 153 | + "project": {"href": "/api/v3/projects/1482", "title": "CLI"}, |
| 154 | + "status": {"href": "/api/v3/statuses/1", "title": "new"}, |
| 155 | + "type": {"href": "/api/v3/types/7", "title": "Implementation"}, |
| 156 | + "assignee": {"href": null, "title": ""} |
| 157 | + }, |
| 158 | + "_embedded": { |
| 159 | + "project": { |
| 160 | + "id": 1482, |
| 161 | + "identifier": "cli", |
| 162 | + "name": "CLI" |
| 163 | + } |
| 164 | + } |
| 165 | + }`) |
| 166 | + case "/api/v3/work_packages": |
| 167 | + t.Fatalf("unexpected children query: %s", r.URL.Path) |
| 168 | + default: |
| 169 | + t.Fatalf("unexpected path %s", r.URL.Path) |
| 170 | + } |
| 171 | + })) |
| 172 | + defer server.Close() |
| 173 | + |
| 174 | + host, err := url.Parse(server.URL) |
| 175 | + if err != nil { |
| 176 | + t.Fatal(err) |
| 177 | + } |
| 178 | + |
| 179 | + requests.Init(host, "token", false) |
| 180 | + routes.Init(host) |
| 181 | + |
| 182 | + activePrinter := &printer.TestingPrinter{} |
| 183 | + printer.Init(activePrinter) |
| 184 | + |
| 185 | + projectId = 1482 |
| 186 | + parentWorkPackageID = 0 |
| 187 | + shouldOpenWorkPackageInBrowser = false |
| 188 | + printCreatedWorkPackageAsJSON = true |
| 189 | + dryRunCreateWorkPackage = false |
| 190 | + typeFlag = "Implementation" |
| 191 | + |
| 192 | + createWorkPackage(nil, []string{"Build reusable skill"}) |
| 193 | + |
| 194 | + expected := "{\"work_package\":{\"id\":74415,\"subject\":\"Build reusable skill\",\"type\":\"Implementation\",\"status\":\"new\",\"assignee\":\"\",\"description\":\"\",\"parent_id\":null,\"project\":{\"id\":1482,\"identifier\":\"cli\",\"name\":\"CLI\"},\"fields\":{},\"field_labels\":{}},\"children\":[]}\n" |
| 195 | + if activePrinter.Result != expected { |
| 196 | + t.Fatalf("expected %s, got %s", expected, activePrinter.Result) |
| 197 | + } |
| 198 | +} |
0 commit comments