|
| 1 | +package inspect |
| 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 TestInspectWorkPackagePrintsJSONWithChildren(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 | + "description": {"raw": "Body"}, |
| 23 | + "customField130": 3, |
| 24 | + "_embedded": { |
| 25 | + "project": { |
| 26 | + "id": 1482, |
| 27 | + "identifier": "cli", |
| 28 | + "name": "CLI" |
| 29 | + } |
| 30 | + }, |
| 31 | + "_links": { |
| 32 | + "self": {"href": "/api/v3/work_packages/74316"}, |
| 33 | + "project": {"href": "/api/v3/projects/1482", "title": "CLI"}, |
| 34 | + "schema": {"href": "/api/v3/work_packages/schemas/1482-6"}, |
| 35 | + "status": {"href": "/api/v3/statuses/1", "title": "new"}, |
| 36 | + "type": {"href": "/api/v3/types/6", "title": "Feature"}, |
| 37 | + "assignee": {"href": null, "title": ""} |
| 38 | + } |
| 39 | + }`) |
| 40 | + case "/api/v3/work_packages/schemas/1482-6": |
| 41 | + _, _ = io.WriteString(w, `{"customField130": {"name": "Votes", "type": "Integer", "writable": true}}`) |
| 42 | + case "/api/v3/work_packages": |
| 43 | + _, _ = io.WriteString(w, `{ |
| 44 | + "_embedded": { |
| 45 | + "elements": [ |
| 46 | + { |
| 47 | + "id": 74413, |
| 48 | + "subject": "Build a reusable SKILL.md based on OpenProject CLI", |
| 49 | + "_links": { |
| 50 | + "type": {"title": "Implementation"}, |
| 51 | + "status": {"title": "new"}, |
| 52 | + "project": {"href": "/api/v3/projects/1482", "title": "CLI"}, |
| 53 | + "parent": {"href": "/api/v3/work_packages/74316", "title": "Expand op CLI to support scripted work package workflows"} |
| 54 | + } |
| 55 | + } |
| 56 | + ] |
| 57 | + }, |
| 58 | + "_type": "Collection", |
| 59 | + "total": 1, |
| 60 | + "count": 1, |
| 61 | + "pageSize": -1, |
| 62 | + "offset": 1 |
| 63 | + }`) |
| 64 | + default: |
| 65 | + t.Fatalf("unexpected path %s", r.URL.Path) |
| 66 | + } |
| 67 | + })) |
| 68 | + defer server.Close() |
| 69 | + |
| 70 | + host, err := url.Parse(server.URL) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + requests.Init(host, "token", false) |
| 76 | + routes.Init(host) |
| 77 | + |
| 78 | + activePrinter := &printer.TestingPrinter{} |
| 79 | + printer.Init(activePrinter) |
| 80 | + |
| 81 | + shouldOpenWorkPackageInBrowser = false |
| 82 | + listAvailableTypes = false |
| 83 | + includeChildrenInJson = true |
| 84 | + printWorkPackageAsJSON = true |
| 85 | + |
| 86 | + inspectWorkPackage(nil, []string{"74316"}) |
| 87 | + |
| 88 | + expected := "{\"work_package\":{\"id\":74316,\"subject\":\"Expand op CLI to support scripted work package workflows\",\"type\":\"Feature\",\"status\":\"new\",\"assignee\":\"\",\"description\":\"Body\",\"parent_id\":null,\"project\":{\"id\":1482,\"identifier\":\"cli\",\"name\":\"CLI\"},\"fields\":{\"customField130\":3},\"field_labels\":{\"Votes\":\"customField130\"}},\"children\":[{\"id\":74413,\"subject\":\"Build a reusable SKILL.md based on OpenProject CLI\",\"type\":\"Implementation\",\"status\":\"new\",\"parent_id\":74316}]}\n" |
| 89 | + if activePrinter.Result != expected { |
| 90 | + t.Fatalf("expected %s, got %s", expected, activePrinter.Result) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestInspectWorkPackagePrintsJSONWithoutChildrenQuery(t *testing.T) { |
| 95 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 96 | + switch r.URL.Path { |
| 97 | + case "/api/v3/work_packages/74316": |
| 98 | + _, _ = io.WriteString(w, `{ |
| 99 | + "id": 74316, |
| 100 | + "subject": "Expand op CLI to support scripted work package workflows", |
| 101 | + "description": {"raw": "Body"}, |
| 102 | + "customField130": 3, |
| 103 | + "_embedded": { |
| 104 | + "project": { |
| 105 | + "id": 1482, |
| 106 | + "identifier": "cli", |
| 107 | + "name": "CLI" |
| 108 | + } |
| 109 | + }, |
| 110 | + "_links": { |
| 111 | + "self": {"href": "/api/v3/work_packages/74316"}, |
| 112 | + "project": {"href": "/api/v3/projects/1482", "title": "CLI"}, |
| 113 | + "schema": {"href": "/api/v3/work_packages/schemas/1482-6"}, |
| 114 | + "status": {"href": "/api/v3/statuses/1", "title": "new"}, |
| 115 | + "type": {"href": "/api/v3/types/6", "title": "Feature"}, |
| 116 | + "assignee": {"href": null, "title": ""} |
| 117 | + } |
| 118 | + }`) |
| 119 | + case "/api/v3/work_packages/schemas/1482-6": |
| 120 | + _, _ = io.WriteString(w, `{"customField130": {"name": "Votes", "type": "Integer", "writable": true}}`) |
| 121 | + case "/api/v3/work_packages": |
| 122 | + t.Fatalf("unexpected children query: %s", r.URL.Path) |
| 123 | + default: |
| 124 | + t.Fatalf("unexpected path %s", r.URL.Path) |
| 125 | + } |
| 126 | + })) |
| 127 | + defer server.Close() |
| 128 | + |
| 129 | + host, err := url.Parse(server.URL) |
| 130 | + if err != nil { |
| 131 | + t.Fatal(err) |
| 132 | + } |
| 133 | + |
| 134 | + requests.Init(host, "token", false) |
| 135 | + routes.Init(host) |
| 136 | + |
| 137 | + activePrinter := &printer.TestingPrinter{} |
| 138 | + printer.Init(activePrinter) |
| 139 | + |
| 140 | + shouldOpenWorkPackageInBrowser = false |
| 141 | + listAvailableTypes = false |
| 142 | + includeChildrenInJson = false |
| 143 | + printWorkPackageAsJSON = true |
| 144 | + |
| 145 | + inspectWorkPackage(nil, []string{"74316"}) |
| 146 | + |
| 147 | + expected := "{\"work_package\":{\"id\":74316,\"subject\":\"Expand op CLI to support scripted work package workflows\",\"type\":\"Feature\",\"status\":\"new\",\"assignee\":\"\",\"description\":\"Body\",\"parent_id\":null,\"project\":{\"id\":1482,\"identifier\":\"cli\",\"name\":\"CLI\"},\"fields\":{\"customField130\":3},\"field_labels\":{\"Votes\":\"customField130\"}},\"children\":[]}\n" |
| 148 | + if activePrinter.Result != expected { |
| 149 | + t.Fatalf("expected %s, got %s", expected, activePrinter.Result) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func TestValidateInspectWorkPackageFlagsRejectsOpenAndJSON(t *testing.T) { |
| 154 | + shouldOpenWorkPackageInBrowser = true |
| 155 | + listAvailableTypes = false |
| 156 | + includeChildrenInJson = false |
| 157 | + printWorkPackageAsJSON = true |
| 158 | + |
| 159 | + err := validateInspectWorkPackageFlags() |
| 160 | + if err == nil { |
| 161 | + t.Fatal("expected validation error") |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func TestValidateInspectWorkPackageFlagsRejectsTypesAndJSON(t *testing.T) { |
| 166 | + shouldOpenWorkPackageInBrowser = false |
| 167 | + listAvailableTypes = true |
| 168 | + includeChildrenInJson = false |
| 169 | + printWorkPackageAsJSON = true |
| 170 | + |
| 171 | + err := validateInspectWorkPackageFlags() |
| 172 | + if err == nil { |
| 173 | + t.Fatal("expected validation error") |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func TestInspectWorkPackagePrintsJSONErrorForFlagConflict(t *testing.T) { |
| 178 | + activePrinter := &printer.TestingPrinter{} |
| 179 | + printer.Init(activePrinter) |
| 180 | + |
| 181 | + shouldOpenWorkPackageInBrowser = true |
| 182 | + listAvailableTypes = false |
| 183 | + includeChildrenInJson = false |
| 184 | + printWorkPackageAsJSON = true |
| 185 | + |
| 186 | + inspectWorkPackage(nil, []string{"74316"}) |
| 187 | + |
| 188 | + expected := "{\"error\":{\"code\":\"conflicting_arguments\",\"message\":\"cannot use --open together with --json\"}}\n" |
| 189 | + if activePrinter.Result != expected { |
| 190 | + t.Fatalf("expected %s, got %s", expected, activePrinter.Result) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestInspectWorkPackagePrintsJSONErrorForTypesAndJSON(t *testing.T) { |
| 195 | + activePrinter := &printer.TestingPrinter{} |
| 196 | + printer.Init(activePrinter) |
| 197 | + |
| 198 | + shouldOpenWorkPackageInBrowser = false |
| 199 | + listAvailableTypes = true |
| 200 | + includeChildrenInJson = false |
| 201 | + printWorkPackageAsJSON = true |
| 202 | + |
| 203 | + inspectWorkPackage(nil, []string{"74316"}) |
| 204 | + |
| 205 | + expected := "{\"error\":{\"code\":\"conflicting_arguments\",\"message\":\"cannot use --types together with --json or --children\"}}\n" |
| 206 | + if activePrinter.Result != expected { |
| 207 | + t.Fatalf("expected %s, got %s", expected, activePrinter.Result) |
| 208 | + } |
| 209 | +} |
0 commit comments