Files
Enterprise-Knowledge-Base/cli/cmd/auth/login_test.go
T
nullkey 1b20b06f5e feat(cli): --json field-select, --jq, auth token, doc --from-url
Output ergonomics:
- `--json` accepts a comma-separated field list (gh-parity); selects
  named keys from the per-command payload. Bare `--json` keeps the
  full shape.
- `--jq <expr>` evaluates a gojq expression over the JSON; pairs with
  `--json field-list` so projection runs before jq.
- `--version` is a global cobra flag in addition to the `version`
  subcommand; both render the same line.
- Per-command `--help` now renders the available JSON field list under
  "JSON fields available via `--json id,name,...`" (field-discovery
  parity with gh / kubectl `-o jsonpath`).

New commands:
- `auth token` — print the active context's credential to stdout for
  shell command substitution (`WEKNORA_TOKEN=$(weknora auth token)`).
  Default: raw secret, no trailing newline. `--json` emits
  `{token, mode, context}`.
- `doc upload --from-url <URL>` — ingest a remote URL via the SDK
  `CreateKnowledgeFromURL`. `--name` forwarded as `FileName` so the
  server's known-extension heuristic upgrades crawl-mode to
  file-download-mode where appropriate.

Includes the simplify post-review polish pass (field-filter unit
tests, --json/--jq compose check, agent_help copy fixes).
2026-05-15 12:03:56 +08:00

125 lines
4.1 KiB
Go

package auth
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
"github.com/Tencent/WeKnora/cli/internal/config"
"github.com/Tencent/WeKnora/cli/internal/iostreams"
"github.com/Tencent/WeKnora/cli/internal/prompt"
"github.com/Tencent/WeKnora/cli/internal/secrets"
"github.com/Tencent/WeKnora/cli/internal/testutil"
sdk "github.com/Tencent/WeKnora/client"
)
// fakeLoginService captures the email/password it received.
type fakeLoginService struct {
resp *sdk.LoginResponse
err error
got struct{ email, password string }
}
func (f *fakeLoginService) Login(_ context.Context, req sdk.LoginRequest) (*sdk.LoginResponse, error) {
f.got.email = req.Email
f.got.password = req.Password
return f.resp, f.err
}
// scriptedPrompter satisfies prompt.Prompter with predetermined values.
type scriptedPrompter struct{ email, password string }
func (s scriptedPrompter) Input(string, string) (string, error) { return s.email, nil }
func (s scriptedPrompter) Password(string) (string, error) { return s.password, nil }
func (s scriptedPrompter) Confirm(string, bool) (bool, error) { return true, nil }
func newTestFactoryWithConfig(t *testing.T, p prompt.Prompter) (*cmdutil.Factory, *secrets.MemStore) {
t.Helper()
testutil.XDGTempDir(t)
store := secrets.NewMemStore()
return &cmdutil.Factory{
Config: func() (*config.Config, error) { return config.Load() },
Client: func() (*sdk.Client, error) { panic("client") },
Prompter: func() prompt.Prompter { return p },
Secrets: func() (secrets.Store, error) { return store, nil },
}, store
}
func TestRunLogin_PasswordMode(t *testing.T) {
iostreams.SetForTest(t)
f, store := newTestFactoryWithConfig(t, scriptedPrompter{email: "a@b.c", password: "secret"})
svc := &fakeLoginService{resp: &sdk.LoginResponse{
Success: true,
Token: "jwt-access",
User: &sdk.AuthUser{ID: "u1", Email: "a@b.c", TenantID: 7},
}}
opts := &LoginOptions{
Host: "https://kb.example.com",
Context: "prod",
}
require.NoError(t, runLogin(context.Background(), opts, nil, f, svc))
assert.Equal(t, "a@b.c", svc.got.email)
assert.Equal(t, "secret", svc.got.password)
got, _ := store.Get("prod", "access")
assert.Equal(t, "jwt-access", got)
}
func TestRunLogin_WithToken(t *testing.T) {
iostreams.SetForTest(t)
f, store := newTestFactoryWithConfig(t, prompt.AgentPrompter{})
opts := &LoginOptions{
Host: "https://kb.example.com",
Context: "ci",
WithToken: true,
StdinReader: strings.NewReader(" sk-1234 \n"),
}
require.NoError(t, runLogin(context.Background(), opts, nil, f, nil))
got, _ := store.Get("ci", "api_key")
assert.Equal(t, "sk-1234", got)
}
func TestRunLogin_WithToken_Empty(t *testing.T) {
iostreams.SetForTest(t)
f, _ := newTestFactoryWithConfig(t, prompt.AgentPrompter{})
opts := &LoginOptions{
Host: "https://kb.example.com",
Context: "ci",
WithToken: true,
StdinReader: strings.NewReader(""),
}
err := runLogin(context.Background(), opts, nil, f, nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "input.missing_flag")
}
func TestRunLogin_BadHost(t *testing.T) {
iostreams.SetForTest(t)
f, _ := newTestFactoryWithConfig(t, prompt.AgentPrompter{})
err := runLogin(context.Background(), &LoginOptions{Host: "ftp://nope"}, nil, f, nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "input.invalid_argument")
}
func TestRunLogin_LoginRefused(t *testing.T) {
iostreams.SetForTest(t)
f, _ := newTestFactoryWithConfig(t, scriptedPrompter{email: "a@b.c", password: "x"})
svc := &fakeLoginService{resp: &sdk.LoginResponse{Success: false, Message: "bad password"}}
err := runLogin(context.Background(), &LoginOptions{Host: "https://x", Context: "p"}, nil, f, svc)
require.Error(t, err)
assert.Contains(t, err.Error(), "auth.bad_credential")
}
func TestValidateHost(t *testing.T) {
require.NoError(t, validateHost("https://kb.example.com"))
require.NoError(t, validateHost("http://localhost:8080"))
require.Error(t, validateHost(""))
require.Error(t, validateHost("ftp://x"))
require.Error(t, validateHost("not a url"))
}