@@ -781,57 +781,70 @@ describe('linking a claimed license', () => {
781781 expect ( ( await readLicense ( license . licenseArn ) ) ?. organizationId ) . toBe ( otherOrganizationId )
782782 } )
783783
784- it ( 'refuses an organization paying Stripe for a self-serve plan , but not one on Enterprise ' , async ( ) => {
785- async function organizationOnPlan ( plan : string ) {
784+ it ( 'refuses an organization with a live Stripe self-serve subscription , but not Enterprise or an abandoned checkout ' , async ( ) => {
785+ async function organizationWith ( sub : { plan : string ; status : string ; bound : boolean } ) {
786786 const planOwnerId = await createUser ( )
787787 const id = await createOrganization ( [ { userId : planOwnerId , role : 'owner' } ] )
788788 await db . insert ( subscription ) . values ( {
789789 id : generateId ( ) ,
790- plan,
790+ plan : sub . plan ,
791791 referenceId : id ,
792- status : 'active' ,
793- stripeSubscriptionId : `sub_${ generateId ( ) } ` ,
794- metadata : { plan, referenceId : id , seats : 10 , monthlyPrice : 100 } ,
792+ status : sub . status ,
793+ stripeSubscriptionId : sub . bound ? `sub_${ generateId ( ) } ` : null ,
794+ metadata : { plan : sub . plan , referenceId : id , seats : 10 , monthlyPrice : 100 } ,
795795 } )
796796 return { id, planOwnerId }
797797 }
798- const onTeam = await organizationOnPlan ( 'team_6000' )
799- const onEnterprise = await organizationOnPlan ( 'enterprise' )
800- const refused = await createClaimedLicense ( )
801- const linked = await createClaimedLicense ( )
802- entitlementsByLicense . set ( refused . licenseArn , [ currentEntitlement ( ) ] )
803- entitlementsByLicense . set ( linked . licenseArn , [ currentEntitlement ( ) ] )
798+ async function linkTo ( organization : { id : string ; planOwnerId : string } ) {
799+ const license = await createClaimedLicense ( )
800+ entitlementsByLicense . set ( license . licenseArn , [ currentEntitlement ( ) ] )
801+ const refusal = await linkAwsMarketplaceLicense
802+ . execute ( {
803+ principal : createSessionPrincipal ( { userId : organization . planOwnerId } ) ,
804+ input : {
805+ ...CONTACT ,
806+ organizationId : organization . id ,
807+ licenseId : license . id ,
808+ claimToken : license . token ,
809+ } ,
810+ } )
811+ . then (
812+ ( ) => null ,
813+ ( error : unknown ) => error
814+ )
815+ return { license, refusal }
816+ }
804817
805- const error = await rejection (
806- linkAwsMarketplaceLicense . execute ( {
807- principal : createSessionPrincipal ( { userId : onTeam . planOwnerId } ) ,
808- input : {
809- ...CONTACT ,
810- organizationId : onTeam . id ,
811- licenseId : refused . id ,
812- claimToken : refused . token ,
813- } ,
814- } )
818+ const trial = await linkTo (
819+ await organizationWith ( { plan : 'team_6000' , status : 'trialing' , bound : true } )
815820 )
816- await linkAwsMarketplaceLicense . execute ( {
817- principal : createSessionPrincipal ( { userId : onEnterprise . planOwnerId } ) ,
818- input : {
819- ...CONTACT ,
820- organizationId : onEnterprise . id ,
821- licenseId : linked . id ,
822- claimToken : linked . token ,
823- } ,
821+ const onEnterprise = await organizationWith ( {
822+ plan : 'enterprise' ,
823+ status : 'active' ,
824+ bound : true ,
824825 } )
826+ const enterprise = await linkTo ( onEnterprise )
827+ const abandoned = await organizationWith ( {
828+ plan : 'team_6000' ,
829+ status : 'incomplete' ,
830+ bound : false ,
831+ } )
832+ const afterAbandoned = await linkTo ( abandoned )
825833
826- expect ( error ) . toMatchObject ( {
834+ expect ( trial . refusal ) . toMatchObject ( {
827835 code : 'conflict' ,
828836 message : expect . stringMatching ( / a l r e a d y p a y s / ) ,
829837 } )
830- expect ( await readLicense ( refused . licenseArn ) ) . toMatchObject ( {
838+ expect ( await readLicense ( trial . license . licenseArn ) ) . toMatchObject ( {
831839 organizationId : null ,
832- claimTokenHash : hashClaimToken ( refused . token ) ,
840+ claimTokenHash : hashClaimToken ( trial . license . token ) ,
833841 } )
834- expect ( ( await readLicense ( linked . licenseArn ) ) ?. organizationId ) . toBe ( onEnterprise . id )
842+ expect ( enterprise . refusal ) . toBeNull ( )
843+ expect ( afterAbandoned . refusal ) . toBeNull ( )
844+ expect ( ( await readLicense ( enterprise . license . licenseArn ) ) ?. organizationId ) . toBe ( onEnterprise . id )
845+ expect ( ( await readLicense ( afterAbandoned . license . licenseArn ) ) ?. organizationId ) . toBe (
846+ abandoned . id
847+ )
835848 } )
836849
837850 it ( 'links under a newer active read without overwriting it with its own older one' , async ( ) => {
0 commit comments