|
| 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_CurrentUserAPIFailureTyped(t *testing.T) { |
| 33 | + f, stdout, _, reg := cmdutil.TestFactory(t, searchUserDefaultConfig()) |
| 34 | + reg.Register(&httpmock.Stub{ |
| 35 | + Method: "GET", |
| 36 | + URL: "/open-apis/authen/v1/user_info", |
| 37 | + Body: map[string]interface{}{"code": 123456, "msg": "upstream rejected contact request"}, |
| 38 | + }) |
| 39 | + |
| 40 | + err := mountAndRun(t, ContactGetUser, []string{"+get-user", "--as", "user"}, f, stdout) |
| 41 | + if err == nil { |
| 42 | + t.Fatalf("expected API error") |
| 43 | + } |
| 44 | + p, ok := errs.ProblemOf(err) |
| 45 | + if !ok { |
| 46 | + t.Fatalf("expected typed problem, got %T: %v", err, err) |
| 47 | + } |
| 48 | + if p.Code != 123456 { |
| 49 | + t.Fatalf("code: got %d, want 123456", p.Code) |
| 50 | + } |
| 51 | + if p.Category != errs.CategoryAPI { |
| 52 | + t.Fatalf("category: got %q, want %q", p.Category, errs.CategoryAPI) |
| 53 | + } |
| 54 | + if stdout.Len() != 0 { |
| 55 | + t.Fatalf("stdout should stay empty on API failure, got %q", stdout.String()) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestGetUser_UserBasicBatchUsesTypedAPI(t *testing.T) { |
| 60 | + f, stdout, _, reg := cmdutil.TestFactory(t, searchUserDefaultConfig()) |
| 61 | + stub := &httpmock.Stub{ |
| 62 | + Method: "POST", |
| 63 | + URL: "/open-apis/contact/v3/users/basic_batch?user_id_type=open_id", |
| 64 | + Body: map[string]interface{}{ |
| 65 | + "code": 0, |
| 66 | + "msg": "ok", |
| 67 | + "data": map[string]interface{}{ |
| 68 | + "users": []interface{}{ |
| 69 | + map[string]interface{}{"user_id": "ou_a", "name": "Alice"}, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + } |
| 74 | + reg.Register(stub) |
| 75 | + |
| 76 | + err := mountAndRun(t, ContactGetUser, []string{"+get-user", "--user-id", "ou_a", "--as", "user", "--format", "json"}, f, stdout) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("execute: %v", err) |
| 79 | + } |
| 80 | + if !bytes.Contains(stub.CapturedBody, []byte(`"ou_a"`)) { |
| 81 | + t.Fatalf("request body should include user id, got %s", string(stub.CapturedBody)) |
| 82 | + } |
| 83 | + if !bytes.Contains(stdout.Bytes(), []byte(`"user"`)) { |
| 84 | + t.Fatalf("stdout should include user object, got %s", stdout.String()) |
| 85 | + } |
| 86 | +} |
0 commit comments