|
| 1 | +// Copyright (c) 2026 Lark Technologies Pte. Ltd. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package contact |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "errors" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/larksuite/cli/errs" |
| 12 | + "github.com/larksuite/cli/internal/cmdutil" |
| 13 | + "github.com/larksuite/cli/internal/httpmock" |
| 14 | +) |
| 15 | + |
| 16 | +func TestGetUser_BotCurrentUserValidationTyped(t *testing.T) { |
| 17 | + f, stdout, _, _ := cmdutil.TestFactory(t, searchUserDefaultConfig()) |
| 18 | + |
| 19 | + err := mountAndRun(t, ContactGetUser, []string{"+get-user", "--as", "bot"}, f, stdout) |
| 20 | + if err == nil { |
| 21 | + t.Fatalf("expected validation error") |
| 22 | + } |
| 23 | + var validation *errs.ValidationError |
| 24 | + if !errors.As(err, &validation) { |
| 25 | + t.Fatalf("expected validation error, got %T: %v", err, err) |
| 26 | + } |
| 27 | + if validation.Param != "--user-id" { |
| 28 | + t.Fatalf("param: got %q, want --user-id", validation.Param) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestGetUser_DryRunShapes(t *testing.T) { |
| 33 | + cases := []struct { |
| 34 | + name string |
| 35 | + args []string |
| 36 | + want []string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "current user", |
| 40 | + args: []string{"+get-user", "--dry-run", "--as", "user"}, |
| 41 | + want: []string{"GET", "/authen/v1/user_info", "current_user"}, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "bot specific user", |
| 45 | + args: []string{"+get-user", "--user-id", "ou_a", "--dry-run", "--as", "bot"}, |
| 46 | + want: []string{"GET", "/contact/v3/users/ou_a", "ou_a", "open_id"}, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "user basic batch", |
| 50 | + args: []string{"+get-user", "--user-id", "ou_a", "--dry-run", "--as", "user"}, |
| 51 | + want: []string{"POST", "/contact/v3/users/basic_batch", "ou_a", "open_id"}, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for _, tc := range cases { |
| 56 | + t.Run(tc.name, func(t *testing.T) { |
| 57 | + f, stdout, _, _ := cmdutil.TestFactory(t, searchUserDefaultConfig()) |
| 58 | + if err := mountAndRun(t, ContactGetUser, tc.args, f, stdout); err != nil { |
| 59 | + t.Fatalf("dry-run: %v", err) |
| 60 | + } |
| 61 | + out := stdout.String() |
| 62 | + for _, want := range tc.want { |
| 63 | + if !bytes.Contains(stdout.Bytes(), []byte(want)) { |
| 64 | + t.Fatalf("dry-run output missing %q: %s", want, out) |
| 65 | + } |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestGetUser_CurrentUserAPIFailureTyped(t *testing.T) { |
| 72 | + f, stdout, _, reg := cmdutil.TestFactory(t, searchUserDefaultConfig()) |
| 73 | + reg.Register(&httpmock.Stub{ |
| 74 | + Method: "GET", |
| 75 | + URL: "/open-apis/authen/v1/user_info", |
| 76 | + Body: map[string]interface{}{"code": 123456, "msg": "upstream rejected contact request"}, |
| 77 | + }) |
| 78 | + |
| 79 | + err := mountAndRun(t, ContactGetUser, []string{"+get-user", "--as", "user"}, f, stdout) |
| 80 | + if err == nil { |
| 81 | + t.Fatalf("expected API error") |
| 82 | + } |
| 83 | + p, ok := errs.ProblemOf(err) |
| 84 | + if !ok { |
| 85 | + t.Fatalf("expected typed problem, got %T: %v", err, err) |
| 86 | + } |
| 87 | + if p.Code != 123456 { |
| 88 | + t.Fatalf("code: got %d, want 123456", p.Code) |
| 89 | + } |
| 90 | + if p.Category != errs.CategoryAPI { |
| 91 | + t.Fatalf("category: got %q, want %q", p.Category, errs.CategoryAPI) |
| 92 | + } |
| 93 | + if stdout.Len() != 0 { |
| 94 | + t.Fatalf("stdout should stay empty on API failure, got %q", stdout.String()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestGetUser_UserBasicBatchUsesTypedAPI(t *testing.T) { |
| 99 | + f, stdout, _, reg := cmdutil.TestFactory(t, searchUserDefaultConfig()) |
| 100 | + stub := &httpmock.Stub{ |
| 101 | + Method: "POST", |
| 102 | + URL: "/open-apis/contact/v3/users/basic_batch?user_id_type=open_id", |
| 103 | + Body: map[string]interface{}{ |
| 104 | + "code": 0, |
| 105 | + "msg": "ok", |
| 106 | + "data": map[string]interface{}{ |
| 107 | + "users": []interface{}{ |
| 108 | + map[string]interface{}{"user_id": "ou_a", "name": "Alice"}, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | + reg.Register(stub) |
| 114 | + |
| 115 | + err := mountAndRun(t, ContactGetUser, []string{"+get-user", "--user-id", "ou_a", "--as", "user", "--format", "json"}, f, stdout) |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("execute: %v", err) |
| 118 | + } |
| 119 | + if !bytes.Contains(stub.CapturedBody, []byte(`"ou_a"`)) { |
| 120 | + t.Fatalf("request body should include user id, got %s", string(stub.CapturedBody)) |
| 121 | + } |
| 122 | + if !bytes.Contains(stdout.Bytes(), []byte(`"user"`)) { |
| 123 | + t.Fatalf("stdout should include user object, got %s", stdout.String()) |
| 124 | + } |
| 125 | +} |
0 commit comments