-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add mail sender allow/block list shortcuts #1599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
infeng
wants to merge
5
commits into
larksuite:main
Choose a base branch
from
infeng:feat/71d5802
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6cd86bb
feat: add mail sender list shortcuts
infeng 191fa94
fix: align mail sender shortcut API calls
infeng 018adbf
fix: mark mail sender delete as delete risk
infeng fb273ce
fix: use stable mail sender API helpers
infeng c716979
fix: keep sender shortcuts compatible with fork base
infeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,315 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package mail | ||
|
|
||
| import ( | ||
| "errors" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/larksuite/cli/internal/httpmock" | ||
| "github.com/larksuite/cli/internal/output" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| func assertValidationError(t *testing.T, err error, wantSubstr string) { | ||
| t.Helper() | ||
| if err == nil { | ||
| t.Fatal("expected validation error, got nil") | ||
| } | ||
| var exitErr *output.ExitError | ||
| if !errors.As(err, &exitErr) { | ||
| t.Fatalf("expected output.ExitError, got %T: %v", err, err) | ||
| } | ||
| if exitErr.Code != output.ExitValidation { | ||
| t.Fatalf("exit code = %d, want ExitValidation", exitErr.Code) | ||
| } | ||
| if wantSubstr != "" && !strings.Contains(exitErr.Error(), wantSubstr) { | ||
| t.Fatalf("error = %q, want substring %q", exitErr.Error(), wantSubstr) | ||
| } | ||
| } | ||
|
|
||
| func TestParseSenderAddressList(t *testing.T) { | ||
| got, err := parseSenderAddressList([]string{" Alice@Example.COM, bob@example.com ", "alice@example.com"}, true) | ||
| if err != nil { | ||
| t.Fatalf("parseSenderAddressList() error = %v", err) | ||
| } | ||
| if strings.Join(got, ",") != "alice@example.com,bob@example.com" { | ||
| t.Fatalf("normalized addresses = %v", got) | ||
| } | ||
|
|
||
| got, err = parseSenderAddressList([]string{" Alice@Example.COM "}, false) | ||
| if err != nil { | ||
| t.Fatalf("parseSenderAddressList(delete) error = %v", err) | ||
| } | ||
| if got[0] != "Alice@Example.COM" { | ||
| t.Fatalf("delete address should preserve case, got %q", got[0]) | ||
| } | ||
|
|
||
| _, err = parseSenderAddressList([]string{"not-an-address"}, true) | ||
| assertValidationError(t, err, "invalid email address") | ||
| } | ||
|
|
||
| func TestMailSenderValidateErrors(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| shortcut common.Shortcut | ||
| args []string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "set requires type", | ||
| shortcut: MailSenderSet, | ||
| args: []string{"+sender-set", "--address", "a@example.com"}, | ||
| want: "--type is required", | ||
| }, | ||
| { | ||
| name: "set rejects all", | ||
| shortcut: MailSenderSet, | ||
| args: []string{"+sender-set", "--type", "all", "--address", "a@example.com"}, | ||
| want: "allowed: allow, block", | ||
| }, | ||
| { | ||
| name: "set requires address", | ||
| shortcut: MailSenderSet, | ||
| args: []string{"+sender-set", "--type", "allow"}, | ||
| want: "--address is required", | ||
| }, | ||
| { | ||
| name: "query requires query", | ||
| shortcut: MailSenderQuery, | ||
| args: []string{"+sender-query", "--type", "allow"}, | ||
| want: "--query is required", | ||
| }, | ||
| { | ||
| name: "list page size", | ||
| shortcut: MailSenderList, | ||
| args: []string{"+sender-list", "--page-size", "101"}, | ||
| want: "--page-size must be between", | ||
| }, | ||
| { | ||
| name: "bot me", | ||
| shortcut: MailSenderList, | ||
| args: []string{"+sender-list", "--as", "bot", "--mailbox", "me"}, | ||
| want: "does not support --mailbox me", | ||
| }, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| f, stdout, _, _ := mailShortcutTestFactory(t) | ||
| err := runMountedMailShortcut(t, tc.shortcut, tc.args, f, stdout) | ||
| assertValidationError(t, err, tc.want) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestMailSenderDryRun(t *testing.T) { | ||
| f, stdout, _, _ := mailShortcutTestFactory(t) | ||
| err := runMountedMailShortcut(t, MailSenderQuery, []string{ | ||
| "+sender-query", | ||
| "--mailbox", "me", | ||
| "--type", "all", | ||
| "--query", "Example.COM", | ||
| "--page-size", "25", | ||
| "--page-token", "tok", | ||
| "--dry-run", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("dry-run error = %v", err) | ||
| } | ||
| out := stdout.String() | ||
| for _, want := range []string{ | ||
| `"method": "GET"`, | ||
| `"url": "/open-apis/mail/v1/user_mailboxes/me/allow_senders"`, | ||
| `"url": "/open-apis/mail/v1/user_mailboxes/me/blocked_senders"`, | ||
| `"keyword": "Example.COM"`, | ||
| `"page_size": 25`, | ||
| `"page_token": "tok"`, | ||
| } { | ||
| if !strings.Contains(out, want) { | ||
| t.Fatalf("dry-run output missing %q:\n%s", want, out) | ||
| } | ||
| } | ||
|
|
||
| stdout.Reset() | ||
| err = runMountedMailShortcut(t, MailSenderSet, []string{ | ||
| "+sender-set", | ||
| "--type", "allow", | ||
| "--address", "Alice@Example.COM,bob@example.com", | ||
| "--dry-run", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("set dry-run error = %v", err) | ||
| } | ||
| out = stdout.String() | ||
| for _, want := range []string{ | ||
| `"method": "POST"`, | ||
| `"url": "/open-apis/mail/v1/user_mailboxes/me/allow_senders/batch_create"`, | ||
| `"address": "alice@example.com"`, | ||
| `"address": "bob@example.com"`, | ||
| } { | ||
| if !strings.Contains(out, want) { | ||
| t.Fatalf("set dry-run output missing %q:\n%s", want, out) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestMailSenderListExecuteAll(t *testing.T) { | ||
| f, stdout, _, reg := mailShortcutTestFactory(t) | ||
| registerSenderListStub(reg, "allow_senders", map[string]interface{}{ | ||
| "items": []map[string]interface{}{{"address": "allow@example.com", "timestamp": float64(171)}}, | ||
| }) | ||
| registerSenderListStub(reg, "blocked_senders", map[string]interface{}{ | ||
| "items": []map[string]interface{}{{"address": "block@example.com", "timestamp": float64(172)}}, | ||
| "has_more": true, | ||
| "page_token": "next-block", | ||
| }) | ||
|
|
||
| err := runMountedMailShortcut(t, MailSenderList, []string{ | ||
| "+sender-list", | ||
| "--type", "all", | ||
| "--page-size", "50", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("execute list error = %v", err) | ||
| } | ||
| data := decodeShortcutEnvelopeData(t, stdout) | ||
| if data["total"].(float64) != 2 { | ||
| t.Fatalf("total = %v, want 2", data["total"]) | ||
| } | ||
| items := data["items"].([]interface{}) | ||
| gotTypes := []string{ | ||
| items[0].(map[string]interface{})["list_type"].(string), | ||
| items[1].(map[string]interface{})["list_type"].(string), | ||
| } | ||
| if strings.Join(gotTypes, ",") != "allow,block" { | ||
| t.Fatalf("list types = %v", gotTypes) | ||
| } | ||
| tokens := data["next_page_tokens"].(map[string]interface{}) | ||
| if tokens["block"] != "next-block" { | ||
| t.Fatalf("next_page_tokens = %v", tokens) | ||
| } | ||
| } | ||
|
|
||
| func TestMailSenderQueryExactExecute(t *testing.T) { | ||
| f, stdout, _, reg := mailShortcutTestFactory(t) | ||
| registerSenderListStub(reg, "allow_senders", map[string]interface{}{ | ||
| "items": []map[string]interface{}{ | ||
| {"address": "Alice@Example.com", "timestamp": float64(171)}, | ||
| {"address": "other@example.com", "timestamp": float64(172)}, | ||
| }, | ||
| }) | ||
|
|
||
| err := runMountedMailShortcut(t, MailSenderQuery, []string{ | ||
| "+sender-query", | ||
| "--type", "allow", | ||
| "--query", "alice@example.COM", | ||
| "--exact", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("execute query error = %v", err) | ||
| } | ||
| data := decodeShortcutEnvelopeData(t, stdout) | ||
| items := data["items"].([]interface{}) | ||
| if len(items) != 1 { | ||
| t.Fatalf("items len = %d, want 1; data=%v", len(items), data) | ||
| } | ||
| item := items[0].(map[string]interface{}) | ||
| if item["address"] != "Alice@Example.com" || item["list_type"] != "allow" { | ||
| t.Fatalf("item = %v", item) | ||
| } | ||
| } | ||
|
|
||
| func TestMailSenderSetAndDeleteExecute(t *testing.T) { | ||
| f, stdout, _, reg := mailShortcutTestFactory(t) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/user_mailboxes/me/blocked_senders/batch_create", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{ | ||
| "failed_items": []map[string]interface{}{{"address": "bad@example.com", "reason": "invalid"}}, | ||
| }, | ||
| }, | ||
| }) | ||
| err := runMountedMailShortcut(t, MailSenderSet, []string{ | ||
| "+sender-set", | ||
| "--type", "block", | ||
| "--address", "Bad@Example.COM", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("set error = %v", err) | ||
| } | ||
| data := decodeShortcutEnvelopeData(t, stdout) | ||
| failed := data["failed_items"].([]interface{}) | ||
| if len(failed) != 1 || failed[0].(map[string]interface{})["address"] != "bad@example.com" { | ||
| t.Fatalf("failed_items = %v", failed) | ||
| } | ||
|
|
||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/user_mailboxes/me/blocked_senders/batch_remove", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": map[string]interface{}{"deleted_count": 1}, | ||
| }, | ||
| }) | ||
| err = runMountedMailShortcut(t, MailSenderDelete, []string{ | ||
| "+sender-delete", | ||
| "--type", "block", | ||
| "--address", "Bad@Example.COM", | ||
| }, f, stdout) | ||
| if err != nil { | ||
| t.Fatalf("delete error = %v", err) | ||
| } | ||
| data = decodeShortcutEnvelopeData(t, stdout) | ||
| if data["deleted_count"].(float64) != 1 { | ||
| t.Fatalf("deleted_count = %v", data["deleted_count"]) | ||
| } | ||
| addresses := data["addresses"].([]interface{}) | ||
| if addresses[0] != "Bad@Example.COM" { | ||
| t.Fatalf("delete should preserve address case, got %v", addresses) | ||
| } | ||
| } | ||
|
|
||
| func TestMailSenderAPI456AddsRetryHint(t *testing.T) { | ||
| f, stdout, _, reg := mailShortcutTestFactory(t) | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "GET", | ||
| URL: "/user_mailboxes/me/allow_senders", | ||
| Body: map[string]interface{}{ | ||
| "code": 456, | ||
| "msg": "search cache warming", | ||
| }, | ||
| }) | ||
| err := runMountedMailShortcut(t, MailSenderQuery, []string{ | ||
| "+sender-query", | ||
| "--type", "allow", | ||
| "--query", "a", | ||
| }, f, stdout) | ||
| if err == nil { | ||
| t.Fatal("expected API error") | ||
| } | ||
| var exitErr *output.ExitError | ||
| if !errors.As(err, &exitErr) { | ||
| t.Fatalf("expected output.ExitError, got %T: %v", err, err) | ||
| } | ||
| if exitErr.Detail == nil || exitErr.Detail.Code != 456 || !strings.Contains(exitErr.Detail.Hint, "retry later") { | ||
| t.Fatalf("problem = %#v", exitErr.Detail) | ||
| } | ||
| if exitErr.Code != output.ExitAPI { | ||
| t.Fatalf("exit code = %d, want ExitAPI", exitErr.Code) | ||
| } | ||
| } | ||
|
|
||
| func registerSenderListStub(reg *httpmock.Registry, resource string, data map[string]interface{}) { | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "GET", | ||
| URL: "/user_mailboxes/me/" + resource, | ||
| Body: map[string]interface{}{ | ||
| "code": 0, | ||
| "data": data, | ||
| }, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # mail +sender-delete | ||
|
|
||
| 从当前用户邮箱的白名单或黑名单删除发件人地址。删除前必须向用户确认名单类型和地址数量。 | ||
|
|
||
| 本 skill 对应 shortcut:`lark-cli mail +sender-delete`。 | ||
|
|
||
| ## 命令 | ||
|
|
||
| ```bash | ||
| # 从白名单删除 | ||
| lark-cli mail +sender-delete --as user --type allow --address notice@example.com | ||
|
|
||
| # 从黑名单批量删除 | ||
| lark-cli mail +sender-delete --as user --type block \ | ||
| --address spam@example.com,ads@example.com | ||
|
|
||
| # 查看将要调用的接口和 body | ||
| lark-cli mail +sender-delete --as user --type block --address spam@example.com --dry-run | ||
| ``` | ||
|
|
||
| ## 参数 | ||
|
|
||
| | 参数 | 必填 | 说明 | | ||
| |------|------|------| | ||
| | `--mailbox <email>` | 否 | 邮箱地址,默认 `me`。`--as bot` 不能与 `--mailbox me` 一起使用。 | | ||
| | `--type allow\|block` | 是 | 从白名单或黑名单删除;不支持 `all`。 | | ||
| | `--address <email>` | 是 | 一个或多个邮箱地址,支持重复 flag 和逗号分隔,最多 100 个。删除时保留原始大小写,避免影响历史数据匹配。 | | ||
|
|
||
| ## 输出 | ||
|
|
||
| 输出包含 `list_type`、`addresses` 和 `deleted_count`。`deleted_count` 小于输入数量时,应提示用户可能已有部分地址不存在或服务端未删除。 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Make the new error-path tests assert typed metadata.
These assertions only check
output.ExitErrorand message text, so they will still pass ifcategory,subtype,param, or cause preservation regresses. Once the shortcut returns typederrs.*, assert problem fields viaerrs.ProblemOf(err), useerrors.As(...*errs.ValidationError)forParam, and verify the 456 path still unwraps the original cause.As per coding guidelines,
**/*_test.go: error-path tests must assert typed metadata and cause preservation. Based on learnings,errs.ProblemOfdoes not exposeParam, so that field needs a separateerrors.Asassertion on*errs.ValidationError.Also applies to: 294-303
🤖 Prompt for AI Agents
Sources: Coding guidelines, Learnings