|
4 | 4 | package task |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "errors" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/spf13/cobra" |
10 | 11 |
|
| 12 | + "github.com/larksuite/cli/errs" |
| 13 | + "github.com/larksuite/cli/internal/httpmock" |
| 14 | + "github.com/larksuite/cli/internal/output" |
11 | 15 | "github.com/larksuite/cli/shortcuts/common" |
12 | 16 | "github.com/smartystreets/goconvey/convey" |
13 | 17 | ) |
14 | 18 |
|
| 19 | +// TestAssignTask_RequiresAddOrRemove covers the Validate guard: neither --add |
| 20 | +// nor --remove yields a typed validation error (exit 2) before any API call. |
| 21 | +func TestAssignTask_RequiresAddOrRemove(t *testing.T) { |
| 22 | + f, stdout, _, _ := taskShortcutTestFactory(t) |
| 23 | + |
| 24 | + s := AssignTask |
| 25 | + args := []string{"+assign", "--task-id", "task-1", "--as", "bot", "--format", "json"} |
| 26 | + err := runMountedTaskShortcut(t, s, args, f, stdout) |
| 27 | + |
| 28 | + var ve *errs.ValidationError |
| 29 | + if !errors.As(err, &ve) { |
| 30 | + t.Fatalf("err = %T, want *errs.ValidationError; err = %v", err, err) |
| 31 | + } |
| 32 | + if ve.Subtype != errs.SubtypeInvalidArgument { |
| 33 | + t.Errorf("subtype = %q, want %q", ve.Subtype, errs.SubtypeInvalidArgument) |
| 34 | + } |
| 35 | + if got := output.ExitCodeOf(err); got != output.ExitValidation { |
| 36 | + t.Errorf("exit code = %d, want %d", got, output.ExitValidation) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// TestAssignTask_MalformedResponse covers the Execute parse-response arm: a |
| 41 | +// 200 with an unparseable body surfaces a typed internal invalid_response |
| 42 | +// error (exit 5). |
| 43 | +func TestAssignTask_MalformedResponse(t *testing.T) { |
| 44 | + f, stdout, _, reg := taskShortcutTestFactory(t) |
| 45 | + warmTenantToken(t, f, reg) |
| 46 | + |
| 47 | + reg.Register(&httpmock.Stub{ |
| 48 | + Method: "POST", |
| 49 | + URL: "/open-apis/task/v2/tasks/task-1/add_members", |
| 50 | + Status: 200, |
| 51 | + RawBody: []byte("{not-json"), |
| 52 | + }) |
| 53 | + |
| 54 | + s := AssignTask |
| 55 | + args := []string{"+assign", "--task-id", "task-1", "--add", "ou_user_1", "--as", "bot", "--format", "json"} |
| 56 | + err := runMountedTaskShortcut(t, s, args, f, stdout) |
| 57 | + |
| 58 | + var ie *errs.InternalError |
| 59 | + if !errors.As(err, &ie) { |
| 60 | + t.Fatalf("err = %T, want *errs.InternalError; err = %v", err, err) |
| 61 | + } |
| 62 | + if ie.Subtype != errs.SubtypeInvalidResponse { |
| 63 | + t.Errorf("subtype = %q, want %q", ie.Subtype, errs.SubtypeInvalidResponse) |
| 64 | + } |
| 65 | + if got := output.ExitCodeOf(err); got != output.ExitInternal { |
| 66 | + t.Errorf("exit code = %d, want %d", got, output.ExitInternal) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// TestAssignTask_MalformedResponse_RemoveArm covers the Execute remove-members |
| 71 | +// parse arm: with only --remove set, the add arm is skipped and the |
| 72 | +// remove_members POST returns a 200 with an unparseable body, which must |
| 73 | +// surface a typed internal invalid_response error (exit 5). |
| 74 | +func TestAssignTask_MalformedResponse_RemoveArm(t *testing.T) { |
| 75 | + f, stdout, _, reg := taskShortcutTestFactory(t) |
| 76 | + warmTenantToken(t, f, reg) |
| 77 | + |
| 78 | + reg.Register(&httpmock.Stub{ |
| 79 | + Method: "POST", |
| 80 | + URL: "/open-apis/task/v2/tasks/task-1/remove_members", |
| 81 | + Status: 200, |
| 82 | + RawBody: []byte("{not-json"), |
| 83 | + }) |
| 84 | + |
| 85 | + s := AssignTask |
| 86 | + args := []string{"+assign", "--task-id", "task-1", "--remove", "ou_user_1", "--as", "bot", "--format", "json"} |
| 87 | + err := runMountedTaskShortcut(t, s, args, f, stdout) |
| 88 | + |
| 89 | + var ie *errs.InternalError |
| 90 | + if !errors.As(err, &ie) { |
| 91 | + t.Fatalf("err = %T, want *errs.InternalError; err = %v", err, err) |
| 92 | + } |
| 93 | + if ie.Subtype != errs.SubtypeInvalidResponse { |
| 94 | + t.Errorf("subtype = %q, want %q", ie.Subtype, errs.SubtypeInvalidResponse) |
| 95 | + } |
| 96 | + if got := output.ExitCodeOf(err); got != output.ExitInternal { |
| 97 | + t.Errorf("exit code = %d, want %d", got, output.ExitInternal) |
| 98 | + } |
| 99 | +} |
| 100 | + |
15 | 101 | func TestBuildMembersBody(t *testing.T) { |
16 | 102 | convey.Convey("Build with ids and token", t, func() { |
17 | 103 | body := buildMembersBody("u1, u2 , ", "assignee", "token1") |
|
0 commit comments