@@ -31,4 +31,146 @@ describe('signedInAuthObject', () => {
3131 const token = await authObject . getToken ( ) ;
3232 expect ( token ) . toBe ( 'token' ) ;
3333 } ) ;
34+
35+ describe ( 'JWT v1' , ( ) => {
36+ it ( 'has() for orgs' , ( ) => {
37+ const mockAuthenticateContext = { sessionToken : 'authContextToken' } as AuthenticateContext ;
38+
39+ const partialJwtPayload = {
40+ ___raw : 'raw' ,
41+ act : { sub : 'actor' } ,
42+ sid : 'sessionId' ,
43+ org_id : 'orgId' ,
44+ org_role : 'org:admin' ,
45+ org_slug : 'orgSlug' ,
46+ org_permissions : [ 'org:f1:read' , 'org:f2:manage' ] ,
47+ sub : 'userId' ,
48+ } as Partial < JwtPayload > ;
49+
50+ const authObject = signedInAuthObject ( mockAuthenticateContext , 'token' , partialJwtPayload as JwtPayload ) ;
51+
52+ expect ( authObject . has ( { role : 'org:admin' } ) ) . toBe ( true ) ;
53+ expect ( authObject . has ( { permission : 'org:f1:read' } ) ) . toBe ( true ) ;
54+ expect ( authObject . has ( { permission : 'org:f1' } ) ) . toBe ( false ) ;
55+ expect ( authObject . has ( { permission : 'org:f2:manage' } ) ) . toBe ( true ) ;
56+ expect ( authObject . has ( { permission : 'org:f2' } ) ) . toBe ( false ) ;
57+
58+ expect ( authObject . has ( { feature : 'org:reservations' } ) ) . toBe ( false ) ;
59+ expect ( authObject . has ( { feature : 'org:impersonation' } ) ) . toBe ( false ) ;
60+ } ) ;
61+ } ) ;
62+
63+ describe ( 'JWT v2' , ( ) => {
64+ it ( 'has() for orgs' , ( ) => {
65+ const mockAuthenticateContext = { sessionToken : 'authContextToken' } as AuthenticateContext ;
66+
67+ const partialJwtPayload = {
68+ v : 2 ,
69+ ___raw : 'raw' ,
70+ act : { sub : 'actor' } ,
71+ sid : 'sessionId' ,
72+ fea : 'o:reservations,o:impersonation' ,
73+ o : {
74+ id : 'orgId' ,
75+ rol : 'admin' ,
76+ slg : 'orgSlug' ,
77+ per : 'read,manage' ,
78+ fpm : '3' ,
79+ } ,
80+
81+ sub : 'userId' ,
82+ } as Partial < JwtPayload > ;
83+
84+ const authObject = signedInAuthObject ( mockAuthenticateContext , 'token' , partialJwtPayload as JwtPayload ) ;
85+
86+ expect ( authObject . has ( { role : 'org:admin' } ) ) . toBe ( true ) ;
87+ expect ( authObject . has ( { permission : 'org:reservations:read' } ) ) . toBe ( true ) ;
88+ expect ( authObject . has ( { permission : 'org:reservations' } ) ) . toBe ( false ) ;
89+ expect ( authObject . has ( { permission : 'org:reservations:manage' } ) ) . toBe ( true ) ;
90+ expect ( authObject . has ( { permission : 'org:reservations' } ) ) . toBe ( false ) ;
91+ expect ( authObject . has ( { permission : 'org:impersonation:read' } ) ) . toBe ( false ) ;
92+ expect ( authObject . has ( { permission : 'org:impersonation:manage' } ) ) . toBe ( false ) ;
93+
94+ expect ( authObject . has ( { feature : 'org:reservations' } ) ) . toBe ( true ) ;
95+ expect ( authObject . has ( { feature : 'org:impersonation' } ) ) . toBe ( true ) ;
96+ } ) ;
97+
98+ it ( 'has() for billing with scopes' , ( ) => {
99+ const mockAuthenticateContext = { sessionToken : 'authContextToken' } as AuthenticateContext ;
100+
101+ const partialJwtPayload = {
102+ v : 2 ,
103+ ___raw : 'raw' ,
104+ act : { sub : 'actor' } ,
105+ sid : 'sessionId' ,
106+ fea : 'o:reservations,u:dashboard,uo:support-chat,o:impersonation' ,
107+ o : {
108+ id : 'orgId' ,
109+ rol : 'member' ,
110+ slg : 'orgSlug' ,
111+ per : 'read,manage' ,
112+ fpm : '2,3' ,
113+ } ,
114+ pla : 'u:pro,o:business' ,
115+ sub : 'userId' ,
116+ } as Partial < JwtPayload > ;
117+
118+ const authObject = signedInAuthObject ( mockAuthenticateContext , 'token' , partialJwtPayload as JwtPayload ) ;
119+
120+ expect ( authObject . has ( { permission : 'org:reservations:read' } ) ) . toBe ( true ) ;
121+ expect ( authObject . has ( { permission : 'org:reservations:manage' } ) ) . toBe ( false ) ;
122+
123+ expect ( authObject . has ( { permission : 'org:support-chat:read' } ) ) . toBe ( true ) ;
124+ expect ( authObject . has ( { permission : 'org:support-chat:manage' } ) ) . toBe ( true ) ;
125+
126+ expect ( authObject . has ( { permission : 'u:dashboard:manage' } ) ) . toBe ( false ) ;
127+ expect ( authObject . has ( { permission : 'u:dashboard:read' } ) ) . toBe ( false ) ;
128+
129+ expect ( authObject . has ( { feature : 'org:reservations' } ) ) . toBe ( true ) ;
130+ expect ( authObject . has ( { feature : 'user:reservations' } ) ) . toBe ( false ) ;
131+ expect ( authObject . has ( { feature : 'org:impersonation' } ) ) . toBe ( true ) ;
132+ expect ( authObject . has ( { feature : 'user:impersonation' } ) ) . toBe ( false ) ;
133+ expect ( authObject . has ( { feature : 'org:dashboard' } ) ) . toBe ( false ) ;
134+ expect ( authObject . has ( { feature : 'user:dashboard' } ) ) . toBe ( true ) ;
135+ expect ( authObject . has ( { feature : 'org:support-chat' } ) ) . toBe ( true ) ;
136+ expect ( authObject . has ( { feature : 'user:support-chat' } ) ) . toBe ( true ) ;
137+
138+ expect ( authObject . has ( { plan : 'org:business' } ) ) . toBe ( true ) ;
139+ expect ( authObject . has ( { plan : 'user:business' } ) ) . toBe ( false ) ;
140+
141+ expect ( authObject . has ( { plan : 'org:pro' } ) ) . toBe ( false ) ;
142+ expect ( authObject . has ( { plan : 'user:pro' } ) ) . toBe ( true ) ;
143+ } ) ;
144+
145+ it ( 'has() for billing without scopes' , ( ) => {
146+ const mockAuthenticateContext = { sessionToken : 'authContextToken' } as AuthenticateContext ;
147+
148+ const partialJwtPayload = {
149+ v : 2 ,
150+ ___raw : 'raw' ,
151+ act : { sub : 'actor' } ,
152+ sid : 'sessionId' ,
153+ fea : 'o:reservations,u:dashboard,uo:support-chat,o:impersonation' ,
154+ o : {
155+ id : 'orgId' ,
156+ rol : 'member' ,
157+ slg : 'orgSlug' ,
158+ per : 'read,manage' ,
159+ fpm : '2,3' ,
160+ } ,
161+ pla : 'u:pro,o:business' ,
162+ sub : 'userId' ,
163+ } as Partial < JwtPayload > ;
164+
165+ const authObject = signedInAuthObject ( mockAuthenticateContext , 'token' , partialJwtPayload as JwtPayload ) ;
166+
167+ expect ( authObject . has ( { feature : 'reservations' } ) ) . toBe ( true ) ; // because the org has it.
168+ expect ( authObject . has ( { feature : 'dashboard' } ) ) . toBe ( true ) ; // because the user has it.
169+ expect ( authObject . has ( { feature : 'pro' } ) ) . toBe ( false ) ; // `pro` is a plan
170+ expect ( authObject . has ( { feature : 'impersonation' } ) ) . toBe ( true ) ; // because the org has it.
171+
172+ expect ( authObject . has ( { plan : 'pro' } ) ) . toBe ( true ) ; // because the user has it.
173+ expect ( authObject . has ( { plan : 'business' } ) ) . toBe ( true ) ; // because the org has it.
174+ } ) ;
175+ } ) ;
34176} ) ;
0 commit comments