Expose API approval prompts to app state and UI

This commit is contained in:
Joe Julian
2026-03-29 23:14:39 -07:00
parent 722d2eefa0
commit a269217c60
4 changed files with 298 additions and 0 deletions
+48
View File
@@ -5,6 +5,8 @@ import (
"slices"
"testing"
"git.julianfamily.org/keepassgo/apiapproval"
"git.julianfamily.org/keepassgo/apitokens"
"git.julianfamily.org/keepassgo/session"
"git.julianfamily.org/keepassgo/vault"
"git.julianfamily.org/keepassgo/webdav"
@@ -41,6 +43,36 @@ func TestVisibleEntriesFollowsCurrentPathWithoutSearch(t *testing.T) {
}
}
func TestPendingApprovalsReturnsManagerRequests(t *testing.T) {
t.Parallel()
state := State{
Approvals: &stubApprovalManager{
pending: []apiapproval.Request{
{ID: "approval-1", TokenName: "CLI", Operation: apitokens.OperationListEntries},
},
},
}
got := state.PendingApprovals()
if len(got) != 1 || got[0].ID != "approval-1" {
t.Fatalf("PendingApprovals() = %#v, want approval-1", got)
}
}
func TestResolveApprovalDelegatesToManager(t *testing.T) {
t.Parallel()
manager := &stubApprovalManager{}
state := State{Approvals: manager}
if err := state.ResolveApproval("approval-1", apiapproval.OutcomeAllowPermanent); err != nil {
t.Fatalf("ResolveApproval() error = %v", err)
}
if manager.lastID != "approval-1" || manager.lastOutcome != apiapproval.OutcomeAllowPermanent {
t.Fatalf("ResolveApproval() delegated (%q, %q), want (approval-1, allow-permanent)", manager.lastID, manager.lastOutcome)
}
}
func TestVisibleEntriesUsesGlobalSearchWhenQueryPresent(t *testing.T) {
t.Parallel()
@@ -1378,3 +1410,19 @@ func (s *lifecycleStubSession) ChangeMasterKey(key vault.MasterKey) error {
s.changedKey = key
return nil
}
type stubApprovalManager struct {
pending []apiapproval.Request
lastID string
lastOutcome apiapproval.Outcome
}
func (s stubApprovalManager) Pending() []apiapproval.Request {
return append([]apiapproval.Request(nil), s.pending...)
}
func (s *stubApprovalManager) Resolve(id string, outcome apiapproval.Outcome) (apiapproval.Request, *apitokens.PolicyRule, error) {
s.lastID = id
s.lastOutcome = outcome
return apiapproval.Request{ID: id}, nil, nil
}