Skip to content

Commit

Permalink
Merge pull request #1786 from flowforge/backport-1784
Browse files Browse the repository at this point in the history
Ensure a trial project can be restarted (backport #1784)
  • Loading branch information
knolleary committed Mar 9, 2023
2 parents a1b9010 + ceef95d commit ea18a99
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
12 changes: 10 additions & 2 deletions forge/containers/wrapper.js
Expand Up @@ -13,6 +13,13 @@ class SubscriptionHandler {
return subscription
}

async requireTrialProjectOrActiveSubscription (project) {
const billingState = await this._app.billing.getProjectBillingState(project)
if (billingState !== this.BILLING_STATES.TRIAL) {
await this.requireActiveSubscription(project.Team)
}
}

async requireActiveSubscription (team) {
const subscription = await this.requireSubscription(team)

Expand Down Expand Up @@ -218,20 +225,21 @@ module.exports = {
},
startFlows: async (project, options) => {
if (this._isBillingEnabled()) {
await this._subscriptionHandler.requireActiveSubscription(project.Team)
await this._subscriptionHandler.requireTrialProjectOrActiveSubscription(project)
}
if (this._driver.startFlows) {
await this._driver.startFlows(project, options)
}
},
stopFlows: async (project) => {
// Always allows flows to be stopped regardless of billing state
if (this._driver.stopFlows) {
await this._driver.stopFlows(project)
}
},
restartFlows: async (project, options) => {
if (this._isBillingEnabled()) {
await this._subscriptionHandler.requireActiveSubscription(project.Team)
await this._subscriptionHandler.requireTrialProjectOrActiveSubscription(project)
}
if (this._driver.restartFlows) {
this._driver.restartFlows(project, options)
Expand Down
58 changes: 58 additions & 0 deletions test/unit/forge/containers/index_spec.js
Expand Up @@ -155,6 +155,8 @@ describe('Container Wrapper', function () {
app.billing.removeProject.callsFake(async (team, project) => {
billingProjects[team.id][project.id] = null
})
app.billing.getProjectBillingState.restore()
app.billing.setProjectBillingState.restore()
})

afterEach(function () {
Expand Down Expand Up @@ -216,6 +218,17 @@ describe('Container Wrapper', function () {
billingProjects.should.have.property(1)
billingProjects[1].should.have.property(project.id, null)
})

it('starts project if in trial mode without adding to billing', async function () {
const project = await setupProject()
const team = await app.db.models.Team.byName('ATeam')
await app.db.controllers.Subscription.createTrialSubscription(team, Date.now() + (5 * 86400000))
await app.billing.setProjectBillingState(project, app.db.models.ProjectSettings.BILLING_STATES.TRIAL)
const result = await app.containers.start(project)
await result.started
billingProjects.should.not.have.property(team.id)
app.billing.addProject.callCount.should.equal(0)
})
})

describe('stop', function () {
Expand Down Expand Up @@ -279,6 +292,20 @@ describe('Container Wrapper', function () {
await app.containers.stop(project)
project.state.should.equal('suspended')
})
it('stops project if in trial mode without touching billing', async function () {
const project = await setupProject()
const team = await app.db.models.Team.byName('ATeam')
await app.db.controllers.Subscription.createTrialSubscription(team, Date.now() + (5 * 86400000))
await app.billing.setProjectBillingState(project, app.db.models.ProjectSettings.BILLING_STATES.TRIAL)
const result = await app.containers.start(project)
await result.started

await app.containers.stop(project)
project.state.should.equal('suspended')

app.billing.addProject.callCount.should.equal(0)
app.billing.removeProject.callCount.should.equal(0)
})
})

describe('remove', function () {
Expand Down Expand Up @@ -382,6 +409,22 @@ describe('Container Wrapper', function () {

mockDriver.startFlows.callCount.should.equal(1)
})
it('does not start the flows if the team has no subscription', async function () {
const project = await setupProject()
const promise = app.containers.startFlows(project, {})
await promise.should.be.rejectedWith(/No Subscription for this team/)
mockDriver.startFlows.callCount.should.equal(0)
})
it('starts if the project is in trial mode', async function () {
const project = await setupProject()
const team = await app.db.models.Team.byName('ATeam')
await app.db.controllers.Subscription.createTrialSubscription(team, Date.now() + (5 * 86400000))
await app.billing.setProjectBillingState(project, app.db.models.ProjectSettings.BILLING_STATES.TRIAL)

await app.containers.startFlows(project, {})

mockDriver.startFlows.callCount.should.equal(1)
})

it('does not start the flows if the teams subscription is cancelled', async function () {
const project = await setupProject()
Expand Down Expand Up @@ -425,7 +468,22 @@ describe('Container Wrapper', function () {

mockDriver.restartFlows.callCount.should.equal(1)
})
it('does not restart the flows if the team has no subscription', async function () {
const project = await setupProject()
const promise = app.containers.restartFlows(project, {})
await promise.should.be.rejectedWith(/No Subscription for this team/)
mockDriver.startFlows.callCount.should.equal(0)
})
it('restarts if the project is in trial mode', async function () {
const project = await setupProject()
const team = await app.db.models.Team.byName('ATeam')
await app.db.controllers.Subscription.createTrialSubscription(team, Date.now() + (5 * 86400000))
await app.billing.setProjectBillingState(project, app.db.models.ProjectSettings.BILLING_STATES.TRIAL)

await app.containers.restartFlows(project, {})

mockDriver.restartFlows.callCount.should.equal(1)
})
it('does not restart the flows if the teams subscription is cancelled', async function () {
const project = await setupProject()
const team = await app.db.models.Team.byName('ATeam')
Expand Down

0 comments on commit ea18a99

Please sign in to comment.