|
| 1 | +package work_packages_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "net/url" |
| 8 | + "testing" |
| 9 | + |
| 10 | + openerrors "github.com/opf/openproject-cli/components/errors" |
| 11 | + "github.com/opf/openproject-cli/components/printer" |
| 12 | + "github.com/opf/openproject-cli/components/requests" |
| 13 | + "github.com/opf/openproject-cli/components/resources/work_packages" |
| 14 | +) |
| 15 | + |
| 16 | +func TestCreateRejectsInvalidTypeBeforePosting(t *testing.T) { |
| 17 | + postCount := 0 |
| 18 | + server := httptest.NewServer(http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) { |
| 19 | + response.Header().Set("Content-Type", "application/json") |
| 20 | + switch { |
| 21 | + case request.Method == http.MethodGet && request.URL.Path == "/api/v3/projects/1": |
| 22 | + _, _ = response.Write([]byte(`{"_links":{"types":{"href":"/api/v3/projects/1/types"}}}`)) |
| 23 | + case request.Method == http.MethodGet && request.URL.Path == "/api/v3/projects/1/types": |
| 24 | + _, _ = response.Write([]byte(`{"_embedded":{"elements":[]}}`)) |
| 25 | + case request.Method == http.MethodPost: |
| 26 | + postCount++ |
| 27 | + _, _ = response.Write([]byte(`{"id":42}`)) |
| 28 | + default: |
| 29 | + http.Error(response, "unexpected request", http.StatusNotFound) |
| 30 | + } |
| 31 | + })) |
| 32 | + t.Cleanup(server.Close) |
| 33 | + |
| 34 | + host, err := url.Parse(server.URL) |
| 35 | + if err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + requests.Init(host, "", false) |
| 39 | + printer.Init(&printer.TestingPrinter{}) |
| 40 | + |
| 41 | + _, err = work_packages.Create("1", map[work_packages.CreateOption]string{ |
| 42 | + work_packages.CreateSubject: "Subject", |
| 43 | + work_packages.CreateType: "Missing", |
| 44 | + }) |
| 45 | + if !errors.Is(err, openerrors.ErrHandled) { |
| 46 | + t.Fatalf("Create error = %v, want ErrHandled", err) |
| 47 | + } |
| 48 | + if postCount != 0 { |
| 49 | + t.Errorf("POST count = %d, want 0", postCount) |
| 50 | + } |
| 51 | +} |
0 commit comments