@@ -173,6 +173,58 @@ describe('requireQuota middleware:', () => {
173173 expect ( next ) . toHaveBeenCalled ( ) ;
174174 } ) ;
175175
176+ test ( 'should treat canceled subscription as free plan' , async ( ) => {
177+ mockSubscriptionRepository . findByOrganization . mockResolvedValue ( { plan : 'starter' , status : 'canceled' } ) ;
178+ mockBillingUsageService . get . mockResolvedValue ( { counters : { 'scraps.create' : 3 } } ) ;
179+
180+ await requireQuota ( 'scraps' , 'create' ) ( req , res , next ) ;
181+
182+ expect ( next ) . not . toHaveBeenCalled ( ) ;
183+ expect ( res . status ) . toHaveBeenCalledWith ( 429 ) ;
184+ expect ( res . json ) . toHaveBeenCalledWith ( expect . objectContaining ( {
185+ type : 'error' ,
186+ code : 429 ,
187+ } ) ) ;
188+ } ) ;
189+
190+ test ( 'should treat unpaid subscription as free plan' , async ( ) => {
191+ mockSubscriptionRepository . findByOrganization . mockResolvedValue ( { plan : 'starter' , status : 'unpaid' } ) ;
192+ mockBillingUsageService . get . mockResolvedValue ( { counters : { 'scraps.create' : 3 } } ) ;
193+
194+ await requireQuota ( 'scraps' , 'create' ) ( req , res , next ) ;
195+
196+ expect ( next ) . not . toHaveBeenCalled ( ) ;
197+ expect ( res . status ) . toHaveBeenCalledWith ( 429 ) ;
198+ expect ( res . json ) . toHaveBeenCalledWith ( expect . objectContaining ( {
199+ type : 'error' ,
200+ code : 429 ,
201+ } ) ) ;
202+ } ) ;
203+
204+ test ( 'should treat incomplete subscription as free plan' , async ( ) => {
205+ mockSubscriptionRepository . findByOrganization . mockResolvedValue ( { plan : 'starter' , status : 'incomplete' } ) ;
206+ mockBillingUsageService . get . mockResolvedValue ( { counters : { 'scraps.create' : 3 } } ) ;
207+
208+ await requireQuota ( 'scraps' , 'create' ) ( req , res , next ) ;
209+
210+ expect ( next ) . not . toHaveBeenCalled ( ) ;
211+ expect ( res . status ) . toHaveBeenCalledWith ( 429 ) ;
212+ expect ( res . json ) . toHaveBeenCalledWith ( expect . objectContaining ( {
213+ type : 'error' ,
214+ code : 429 ,
215+ } ) ) ;
216+ } ) ;
217+
218+ test ( 'should use subscription plan when status is trialing' , async ( ) => {
219+ mockSubscriptionRepository . findByOrganization . mockResolvedValue ( { plan : 'starter' , status : 'trialing' } ) ;
220+ mockBillingUsageService . get . mockResolvedValue ( { counters : { 'scraps.create' : 15 } } ) ;
221+
222+ await requireQuota ( 'scraps' , 'create' ) ( req , res , next ) ;
223+
224+ expect ( next ) . toHaveBeenCalled ( ) ;
225+ expect ( res . status ) . not . toHaveBeenCalled ( ) ;
226+ } ) ;
227+
176228 test ( 'should treat zero usage as under quota' , async ( ) => {
177229 mockSubscriptionRepository . findByOrganization . mockResolvedValue ( { plan : 'free' , status : 'active' } ) ;
178230 mockBillingUsageService . get . mockResolvedValue ( { counters : { } } ) ;
0 commit comments