@@ -0,0 +1,37 @@
|
||||
package dingtalkdiscovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCredentialStatusReadOnlyMetadata(t *testing.T) {
|
||||
key := "auth status --readonly --format json"
|
||||
runner := &fakeRunner{responses: map[string]string{key: `{"authenticated":true,"refresh_token_valid":true,"expires_at":"2026-10-26T20:21:10+08:00","refresh_expires_at":"2026-11-25T18:21:10+08:00","access_token":"not-a-real-secret"}`}}
|
||||
result, err := ReadCredentialStatus(context.Background(), runner)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !result.Authenticated || !result.RefreshTokenValid || len(runner.seen) != 1 || runner.seen[0] != key {
|
||||
t.Fatalf("unexpected status or command: %+v %+v", result, runner.seen)
|
||||
}
|
||||
raw, _ := json.Marshal(result)
|
||||
if strings.Contains(string(raw), "not-a-real-secret") || strings.Contains(string(raw), "access_token") {
|
||||
t.Fatal("credential leaked")
|
||||
}
|
||||
}
|
||||
func TestCredentialStatusRejectsUnknownOutput(t *testing.T) {
|
||||
_, err := ReadCredentialStatus(context.Background(), &fakeRunner{})
|
||||
if err == nil {
|
||||
t.Fatal("missing authentication field accepted")
|
||||
}
|
||||
}
|
||||
func TestCredentialStatusLoggedOut(t *testing.T) {
|
||||
runner := &fakeRunner{responses: map[string]string{"auth status --readonly --format json": `{"authenticated":false}`}}
|
||||
got, err := ReadCredentialStatus(context.Background(), runner)
|
||||
if err != nil || got.Authenticated {
|
||||
t.Fatalf("%+v %v", got, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user