Repository navigation
fix(apps): keep openapi-key one-time notice inside JSON envelope #2898
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
raistlin042
wants to merge
4
commits into
main
Choose a base branch
from
fix/openapi-key-json-notice
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 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1e5dab7
test(apps): pin one-time key notice placement for json vs human formats
raistlin042 c95d2f2
fix(apps): keep openapi-key one-time notice inside JSON envelope
raistlin042 97afe8d
fix(apps): align one-time notice routing with emitter format resolution
raistlin042 5a3a3ad
docs(apps): clarify one-time key placement for json vs ndjson
raistlin042 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
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,155 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package apps | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/larksuite/cli/internal/httpmock" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| // shownOnceNotice is the one-time-secret guidance shared by create/reset. | ||
| const shownOnceNotice = "this api_key is shown only once" | ||
|
|
||
| func issuedKeyErrBuffer(t *testing.T, rctx *common.RuntimeContext) *bytes.Buffer { | ||
| t.Helper() | ||
| buf, ok := rctx.IO().ErrOut.(*bytes.Buffer) | ||
| if !ok { | ||
| t.Fatalf("ErrOut is not *bytes.Buffer: %T", rctx.IO().ErrOut) | ||
| } | ||
| return buf | ||
| } | ||
|
|
||
| func registerCreateIssuedKey(reg *httpmock.Registry) { | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/open-apis/spark/v1/apps/app_x/oapi_apikeys", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, "msg": "", | ||
| "data": map[string]interface{}{ | ||
| "api_key_id": "k1", | ||
| "info": map[string]interface{}{ | ||
| "api_key_id": "k1", "name": "partner-test", | ||
| "api_key": "xxxxxxxxxxxx", "status": float64(1), | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func registerResetIssuedKey(reg *httpmock.Registry) { | ||
| reg.Register(&httpmock.Stub{ | ||
| Method: "POST", | ||
| URL: "/open-apis/spark/v1/apps/app_x/oapi_apikeys/1/refresh", | ||
| Body: map[string]interface{}{ | ||
| "code": 0, "msg": "", | ||
| "data": map[string]interface{}{ | ||
| "api_key": "xxxxxxxxxxxx", | ||
| "info": map[string]interface{}{"api_key_id": "k1", "name": "k", "api_key": "xxxxxxxxxxxx", "status": float64(1)}, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| // In JSON mode the one-time notice rides inside the success envelope and | ||
| // nothing is written to stderr, so stdout stays one parseable JSON document | ||
| // even when a caller merges the streams with 2>&1. | ||
| func TestOpenAPIKeyCreate_JSON_NoticeInEnvelopeNotStderr(t *testing.T) { | ||
| rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, | ||
| createFlagDefs(), | ||
| map[string]string{"app-id": "app_x", "name": "partner-test"}) | ||
| registerCreateIssuedKey(reg) | ||
| stderrBuf := issuedKeyErrBuffer(t, rctx) | ||
|
|
||
| if err := AppsOpenAPIKeyCreate.Execute(context.Background(), rctx); err != nil { | ||
| t.Fatalf("Execute() = %v", err) | ||
| } | ||
|
|
||
| var env struct { | ||
| OK bool `json:"ok"` | ||
| Data map[string]interface{} `json:"data"` | ||
| } | ||
| if err := json.Unmarshal(stdoutBuf.Bytes(), &env); err != nil { | ||
| t.Fatalf("stdout must be a single pure JSON document: %v\n%s", err, stdoutBuf.String()) | ||
| } | ||
| if !env.OK { | ||
| t.Errorf("envelope ok = false, want true") | ||
| } | ||
| notice, _ := env.Data["notice"].(string) | ||
| if !strings.Contains(notice, shownOnceNotice) { | ||
| t.Errorf("data.notice must carry the one-time guidance, got %q", notice) | ||
| } | ||
| if env.Data["api_key"] != "xxxxxxxxxxxx" { | ||
| t.Errorf("data.api_key must carry the raw secret once, got %v", env.Data["api_key"]) | ||
| } | ||
| if strings.Contains(stderrBuf.String(), shownOnceNotice) || strings.Contains(stderrBuf.String(), "warning:") { | ||
| t.Errorf("json mode must not print the notice to stderr:\n%s", stderrBuf.String()) | ||
| } | ||
| // Even after `2>&1` the merged stream must still parse as JSON — the | ||
| // original bug had the warning prepended and broken decoding. | ||
| merged := append(append([]byte{}, stdoutBuf.Bytes()...), stderrBuf.Bytes()...) | ||
| var mergedEnv map[string]interface{} | ||
| if err := json.Unmarshal(merged, &mergedEnv); err != nil { | ||
| t.Errorf("merged stdout+stderr (2>&1) must still parse as JSON: %v\n%s", err, string(merged)) | ||
| } | ||
| } | ||
|
|
||
| // Reset shares the same output path and must follow the same JSON contract. | ||
| func TestOpenAPIKeyReset_JSON_NoticeInEnvelopeNotStderr(t *testing.T) { | ||
| rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, | ||
| map[string]string{"app-id": "string", "key-id": "string", "yes": "bool"}, | ||
| map[string]string{"app-id": "app_x", "key-id": "1", "yes": "true"}) | ||
| registerResetIssuedKey(reg) | ||
| stderrBuf := issuedKeyErrBuffer(t, rctx) | ||
|
|
||
| if err := AppsOpenAPIKeyReset.Execute(context.Background(), rctx); err != nil { | ||
| t.Fatalf("Execute() = %v", err) | ||
| } | ||
|
|
||
| var env struct { | ||
| OK bool `json:"ok"` | ||
| Data map[string]interface{} `json:"data"` | ||
| } | ||
| if err := json.Unmarshal(stdoutBuf.Bytes(), &env); err != nil { | ||
| t.Fatalf("stdout must be a single pure JSON document: %v\n%s", err, stdoutBuf.String()) | ||
| } | ||
| if notice, _ := env.Data["notice"].(string); !strings.Contains(notice, shownOnceNotice) { | ||
| t.Errorf("data.notice must carry the one-time guidance, got %q", notice) | ||
| } | ||
| if strings.Contains(stderrBuf.String(), shownOnceNotice) { | ||
| t.Errorf("json mode must not print the notice to stderr:\n%s", stderrBuf.String()) | ||
| } | ||
| } | ||
|
|
||
| // In human (pretty) mode the warning keeps going to stderr and stdout carries | ||
| // the readable key block — behavior is unchanged for interactive use. | ||
| func TestOpenAPIKeyCreate_Pretty_WarningOnStderr(t *testing.T) { | ||
| rctx, stdoutBuf, reg := newOpenAPIKeyRCtx(t, | ||
| createFlagDefs(), | ||
| map[string]string{"app-id": "app_x", "name": "partner-test"}) | ||
| rctx.Format = "pretty" | ||
| registerCreateIssuedKey(reg) | ||
| stderrBuf := issuedKeyErrBuffer(t, rctx) | ||
|
|
||
| if err := AppsOpenAPIKeyCreate.Execute(context.Background(), rctx); err != nil { | ||
| t.Fatalf("Execute() = %v", err) | ||
| } | ||
|
|
||
| stderr := stderrBuf.String() | ||
| if !strings.Contains(stderr, "warning:") || !strings.Contains(stderr, shownOnceNotice) { | ||
| t.Errorf("pretty mode must keep the one-time warning on stderr:\n%s", stderr) | ||
| } | ||
| stdout := stdoutBuf.String() | ||
| if !strings.Contains(stdout, "API key:") || !strings.Contains(stdout, "xxxxxxxxxxxx") { | ||
| t.Errorf("pretty mode must print the readable key block to stdout:\n%s", stdout) | ||
| } | ||
| if strings.Contains(stdout, "\"ok\"") { | ||
| t.Errorf("pretty mode must not emit the JSON envelope:\n%s", stdout) | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.