test(api): share API v1 test helpers and check scopes in Go

Move the end-to-end call/setup/mint helpers to a suite-level test client
and the apiauth login/mustMint helpers to package level. Replace the
hardcoded vacuum x-scope enum with a Go test that every known scope is
a valid spec Scope; the gate already rejects unknown x-scope values.
This commit is contained in:
Deluan 2026-09-26 02:41:23 -04:00
commit 45d8a7724d
8 changed files with 172 additions and 178 deletions

View file

@ -32,3 +32,20 @@ func createUser(ctx context.Context, password string, admin bool) model.User {
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return *stored
}
// login runs the full client flow for a user whose password is "pw": grant, resolve, then mint.
func login(ctx context.Context, svc *Service, u model.User) (*Issued, *Principal, *AccessToken) {
issued, err := svc.Login(ctx, u.UserName, "pw", meta, nil)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
p, err := svc.ResolveGrant(ctx, issued.Secret, "")
ExpectWithOffset(1, err).ToNot(HaveOccurred())
tok, err := svc.Mint(ctx, p, nil)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return issued, p, tok
}
func mustMint(ctx context.Context, svc *Service, p *Principal) string {
tok, err := svc.Mint(ctx, p, nil)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return tok.Token
}

View file

@ -18,16 +18,6 @@ var _ = Describe("Service: sessions", func() {
var svc *Service
var now time.Time
login := func(u model.User) (*Issued, *Principal, *AccessToken) {
issued, err := svc.Login(ctx, u.UserName, "pw", meta, nil)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
p, err := svc.ResolveGrant(ctx, issued.Secret, "")
ExpectWithOffset(1, err).ToNot(HaveOccurred())
tok, err := svc.Mint(ctx, p, nil)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return issued, p, tok
}
BeforeEach(func() {
ctx = GinkgoT().Context()
DeferCleanup(configtest.SetupConfig())
@ -39,7 +29,7 @@ var _ = Describe("Service: sessions", func() {
Describe("Authenticate", func() {
It("returns the token's scopes and marks the grant used", func() {
u := createUser(ctx, "pw", false)
issued, _, tok := login(u)
issued, _, tok := login(ctx, svc, u)
now = now.Add(10 * time.Minute)
p, err := svc.Authenticate(ctx, tok.Token, "10.1.1.1")
Expect(err).ToNot(HaveOccurred())
@ -51,7 +41,7 @@ var _ = Describe("Service: sessions", func() {
It("reports an expired token as ErrTokenExpired", func() {
u := createUser(ctx, "pw", false)
_, _, tok := login(u)
_, _, tok := login(ctx, svc, u)
now = now.Add(TokenTTL + clockSkew + time.Second)
_, err := svc.Authenticate(ctx, tok.Token, "")
Expect(err).To(MatchError(ErrTokenExpired))
@ -59,7 +49,7 @@ var _ = Describe("Service: sessions", func() {
It("rejects a token at once on the node that revoked its grant", func() {
u := createUser(ctx, "pw", false)
_, p, tok := login(u)
_, p, tok := login(ctx, svc, u)
_, err := svc.Authenticate(ctx, tok.Token, "")
Expect(err).ToNot(HaveOccurred())
Expect(svc.Logout(ctx, p)).To(Succeed())
@ -69,7 +59,7 @@ var _ = Describe("Service: sessions", func() {
It("stops a token revoked on another node within the cache time", func() {
u := createUser(ctx, "pw", false)
issued, _, tok := login(u)
issued, _, tok := login(ctx, svc, u)
_, err := svc.Authenticate(ctx, tok.Token, "")
Expect(err).ToNot(HaveOccurred())
@ -83,7 +73,7 @@ var _ = Describe("Service: sessions", func() {
It("kills grants when the password changes anywhere else", func() {
u := createUser(ctx, "pw", false)
_, _, tok := login(u)
_, _, tok := login(ctx, svc, u)
u.NewPassword = "reset-by-admin"
Expect(realDS.User().Put(ctx, &u)).To(Succeed())
_, err := svc.Authenticate(ctx, tok.Token, "")
@ -92,7 +82,7 @@ var _ = Describe("Service: sessions", func() {
It("does not kill a grant kept by a password change made through another node", func() {
u := createUser(ctx, "pw", false)
_, p, tok := login(u)
_, p, tok := login(ctx, svc, u)
_, err := svc.Authenticate(ctx, tok.Token, "") // caches the old epoch
Expect(err).ToNot(HaveOccurred())
@ -109,7 +99,7 @@ var _ = Describe("Service: sessions", func() {
KnownScopes = []string{ScopeRead, ScopePassword, ScopeAdmin}
DeferCleanup(func() { KnownScopes = saved })
u := createUser(ctx, "pw", true)
_, p, tok := login(u)
_, p, tok := login(ctx, svc, u)
Expect(tok.Scopes).To(ContainElement(ScopeAdmin))
u.IsAdmin = false
@ -124,7 +114,7 @@ var _ = Describe("Service: sessions", func() {
It("rejects a live token after its user is deleted, and the grant row is gone", func() {
u := createUser(ctx, "pw", false)
issued, _, tok := login(u)
issued, _, tok := login(ctx, svc, u)
Expect(realDS.User().Delete(request.WithUser(ctx, model.User{IsAdmin: true}), u.ID)).To(Succeed())
_, err := realDS.Grant().Get(ctx, issued.Grant.ID)
Expect(err).To(MatchError(model.ErrNotFound))
@ -135,7 +125,7 @@ var _ = Describe("Service: sessions", func() {
It("grants no scopes to a signed token claiming all", func() {
u := createUser(ctx, "pw", true)
_, p, _ := login(u)
_, p, _ := login(ctx, svc, u)
sg, err := svc.signer()
Expect(err).ToNot(HaveOccurred())
tok, err := sg.sign(claims{UserID: u.ID, GrantID: p.GrantID, Scopes: []string{ScopeAll, "unknown"}, IssuedAt: now, ExpiresAt: now.Add(TokenTTL)})
@ -148,7 +138,7 @@ var _ = Describe("Service: sessions", func() {
It("rejects a token whose grant belongs to another user, even across an epoch change", func() {
alice := createUser(ctx, "pw", false)
bob := createUser(ctx, "pw", false)
bobGrant, _, _ := login(bob)
bobGrant, _, _ := login(ctx, svc, bob)
alice.NewPassword = "bumped"
Expect(realDS.User().Put(ctx, &alice)).To(Succeed())
@ -162,7 +152,7 @@ var _ = Describe("Service: sessions", func() {
It("does not delete a kept grant when the user was read before a password change", func() {
u := createUser(ctx, "pw", false)
_, p, _ := login(u)
_, p, _ := login(ctx, svc, u)
stale, err := realDS.User().Get(ctx, u.ID) // read before the change lands
Expect(err).ToNot(HaveOccurred())
Expect(svc.ChangePassword(request.WithUser(ctx, p.User), p, "pw", "pw2", false)).To(Succeed())
@ -202,8 +192,8 @@ var _ = Describe("Service: sessions", func() {
Describe("grant management", func() {
It("lists the user's grants and marks the current one", func() {
u := createUser(ctx, "pw", false)
first, _, _ := login(u)
_, p, _ := login(u)
first, _, _ := login(ctx, svc, u)
_, p, _ := login(ctx, svc, u)
grants, total, err := svc.ListGrants(ctx, p, 0, 10)
Expect(err).ToNot(HaveOccurred())
Expect(total).To(Equal(int64(2)))
@ -213,7 +203,7 @@ var _ = Describe("Service: sessions", func() {
It("lists only grants on the user's current epoch", func() {
u := createUser(ctx, "pw", false)
login(u)
login(ctx, svc, u)
u.NewPassword = "reset-by-admin" // old-UI reset leaves the old grant on the previous epoch
Expect(realDS.User().Put(ctx, &u)).To(Succeed())
issued, err := svc.Login(ctx, u.UserName, "reset-by-admin", meta, nil)
@ -230,7 +220,7 @@ var _ = Describe("Service: sessions", func() {
It("logs out successfully when the grant is already gone", func() {
u := createUser(ctx, "pw", false)
_, p, tok := login(u)
_, p, tok := login(ctx, svc, u)
_, err := svc.Authenticate(ctx, tok.Token, "")
Expect(err).ToNot(HaveOccurred())
Expect(realDS.Grant().Delete(ctx, p.GrantID)).To(Succeed()) // another node
@ -243,8 +233,8 @@ var _ = Describe("Service: sessions", func() {
It("refuses to revoke another user's grant", func() {
alice := createUser(ctx, "pw", false)
bob := createUser(ctx, "pw", false)
aliceGrant, _, _ := login(alice)
_, bobP, _ := login(bob)
aliceGrant, _, _ := login(ctx, svc, alice)
_, bobP, _ := login(ctx, svc, bob)
Expect(svc.RevokeGrant(ctx, bobP, aliceGrant.Grant.ID)).To(MatchError(model.ErrNotFound))
})
})
@ -252,8 +242,8 @@ var _ = Describe("Service: sessions", func() {
Describe("ChangePassword", func() {
It("revokes other grants by default and keeps the caller's", func() {
u := createUser(ctx, "pw", false)
_, _, otherTok := login(u)
_, p, myTok := login(u)
_, _, otherTok := login(ctx, svc, u)
_, p, myTok := login(ctx, svc, u)
Expect(svc.ChangePassword(request.WithUser(ctx, p.User), p, "pw", "pw2", true)).To(Succeed())
_, err := svc.Authenticate(ctx, myTok.Token, "")
@ -268,8 +258,8 @@ var _ = Describe("Service: sessions", func() {
It("keeps every grant when revokeOthers is false", func() {
u := createUser(ctx, "pw", false)
_, _, otherTok := login(u)
_, p, _ := login(u)
_, _, otherTok := login(ctx, svc, u)
_, p, _ := login(ctx, svc, u)
Expect(svc.ChangePassword(request.WithUser(ctx, p.User), p, "pw", "pw2", false)).To(Succeed())
now = now.Add(cacheTTL)
_, err := svc.Authenticate(ctx, otherTok.Token, "")
@ -278,7 +268,7 @@ var _ = Describe("Service: sessions", func() {
It("rejects a wrong current password without changing anything", func() {
u := createUser(ctx, "pw", false)
_, p, _ := login(u)
_, p, _ := login(ctx, svc, u)
err := svc.ChangePassword(request.WithUser(ctx, p.User), p, "wrong", "pw2", true)
Expect(err).To(MatchError(ErrCurrentPasswordMismatch))
_, err = svc.Login(ctx, u.UserName, "pw", meta, nil)
@ -288,14 +278,14 @@ var _ = Describe("Service: sessions", func() {
It("is forbidden for non-admins when user editing is off", func() {
conf.Server.EnableUserEditing = false
u := createUser(ctx, "pw", false)
_, p, _ := login(u)
_, p, _ := login(ctx, svc, u)
err := svc.ChangePassword(request.WithUser(ctx, p.User), p, "pw", "pw2", true)
Expect(err).To(MatchError(model.ErrNotAuthorized))
})
It("does not revive grants killed by an earlier reset when keeping grants", func() {
u := createUser(ctx, "pw", false)
killed, _, _ := login(u)
killed, _, _ := login(ctx, svc, u)
u.NewPassword = "reset-by-admin" // old-UI reset: the killed grant stays on the old epoch until presented
Expect(realDS.User().Put(ctx, &u)).To(Succeed())
@ -311,7 +301,7 @@ var _ = Describe("Service: sessions", func() {
It("rejects a caller whose grant was revoked before the change ran", func() {
u := createUser(ctx, "pw", false)
_, p, _ := login(u)
_, p, _ := login(ctx, svc, u)
Expect(realDS.Grant().Delete(ctx, p.GrantID)).To(Succeed())
err := svc.ChangePassword(request.WithUser(ctx, p.User), p, "pw", "pw2", true)
Expect(err).To(MatchError(model.ErrInvalidAuth))
@ -321,7 +311,7 @@ var _ = Describe("Service: sessions", func() {
It("rolls back the password and epoch when a grant update fails", func() {
u := createUser(ctx, "pw", false)
_, p, _ := login(u)
_, p, _ := login(ctx, svc, u)
failing := New(failingEpochDS{realDS})
failing.SetClock(func() time.Time { return now })
@ -332,7 +322,7 @@ var _ = Describe("Service: sessions", func() {
Expect(reloaded.TokenEpoch).To(Equal(u.TokenEpoch))
_, err = svc.Login(ctx, u.UserName, "pw", meta, nil)
Expect(err).ToNot(HaveOccurred())
_, err = svc.Authenticate(ctx, mustMint(svc, ctx, p), "")
_, err = svc.Authenticate(ctx, mustMint(ctx, svc, p), "")
Expect(err).ToNot(HaveOccurred())
})
})
@ -368,9 +358,3 @@ type failingGrants struct{ model.GrantRepository }
func (failingGrants) SetEpoch(context.Context, string, int, int, string) error {
return errors.New("boom")
}
func mustMint(svc *Service, ctx context.Context, p *Principal) string {
tok, err := svc.Mint(ctx, p, nil)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return tok.Token
}

View file

@ -111,15 +111,9 @@ var _ = Describe("Service: grants and tokens", func() {
Describe("ResolveGrant and Mint", func() {
It("mints a token with the grant's expanded scopes and a 1h lifetime", func() {
u := createUser(ctx, "pw", false)
issued, _ := svc.Login(ctx, u.UserName, "pw", meta, nil)
p, err := svc.ResolveGrant(ctx, issued.Secret, "10.0.0.9")
Expect(err).ToNot(HaveOccurred())
issued, p, tok := login(ctx, svc, u)
Expect(p.GrantID).To(Equal(issued.Grant.ID))
Expect(p.Scopes).To(Equal([]string{ScopePassword, ScopeRead}))
tok, err := svc.Mint(ctx, p, nil)
Expect(err).ToNot(HaveOccurred())
Expect(tok.ExpiresIn).To(Equal(time.Hour))
Expect(tok.Scopes).To(Equal([]string{ScopePassword, ScopeRead}))