Skip to content

Commit 2ad453a

Browse files
committed
Add karate test for the @subscription ACL variable end to end (#678)
1 parent cdced29 commit 2ad453a

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Feature: @subscription ACL variable
2+
3+
# conf-overrides.yml grants role 'user' access to /restheart-test/stripe-gated only when
4+
# equals[@subscription.plan, "gold"] — see the fileAclAuthorizer permissions list. This
5+
# proves the chain end to end: a subscription state written to the team document is read
6+
# by SubscriptionVarResolver and reaches the Undertow predicate engine.
7+
8+
Background:
9+
* url baseUrl
10+
* configure followRedirects = false
11+
12+
* def basic =
13+
"""
14+
function(creds) {
15+
var temp = creds.username + ':' + creds.password;
16+
var Base64 = Java.type('java.util.Base64');
17+
var encoded = Base64.getEncoder().encodeToString(temp.toString().getBytes());
18+
return 'Basic ' + encoded;
19+
}
20+
"""
21+
* def adminAuth = basic({ username: 'admin', password: 'secret' })
22+
23+
* def ownerSetup = karate.call('classpath:karate/accounts/helpers/setup-owner.feature')
24+
* def ownerJwt = ownerSetup.ownerJwt
25+
* def authHeader = 'Bearer ' + ownerJwt
26+
27+
Given path '/auth/teams'
28+
And header Authorization = authHeader
29+
When method GET
30+
Then status 200
31+
* def activeTeams = karate.filter(response, function(x){ return x.active == true })
32+
* def teamId = activeTeams[0].id
33+
34+
Scenario: a free-plan owner is denied a path gated on equals[@subscription.plan, "gold"]
35+
Given path '/restheart-test/stripe-gated/free-owner-doc'
36+
And header Authorization = authHeader
37+
And request {}
38+
When method PUT
39+
Then status 403
40+
41+
Scenario: a gold-plan owner is allowed through the same gate
42+
# Admin writes the subscription state directly — equivalent to what a webhook-driven
43+
# state update would leave behind.
44+
Given path '/restheart-test/teams/' + teamId
45+
And header Authorization = adminAuth
46+
And request { subscription: { plan: 'gold', status: 'active', seats: 1, cancel_at_period_end: false } }
47+
When method PATCH
48+
Then status 200
49+
50+
Given path '/restheart-test/stripe-gated/gold-owner-doc'
51+
And header Authorization = authHeader
52+
And request {}
53+
When method PUT
54+
* match [200, 201] contains responseStatus
55+
56+
# Cleanup.
57+
Given path '/restheart-test/stripe-gated/gold-owner-doc'
58+
And header Authorization = adminAuth
59+
When method DELETE
60+
Then status 204

core/src/test/resources/etc/conf-overrides.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,15 @@
323323
or path-prefix[path="/stripe/licenses"])
324324
priority: 100
325325

326+
# Exercises the @subscription ACL variable end to end (webhook/admin-driven MongoDB
327+
# write -> SubscriptionVarResolver -> predicate). Exercised by
328+
# karate/stripe/subscription-acl-variable.feature.
329+
- role: user
330+
predicate: >
331+
path-prefix[path="/restheart-test/stripe-gated"]
332+
and equals[@subscription.plan, "gold"]
333+
priority: 100
334+
326335
/mongoAclAuthorizer/acl-db: restheart-test
327336
/mongoAclAuthorizer/cache-enabled: false
328337

0 commit comments

Comments
 (0)