|
| 1 | +const { Op } = require('sequelize') |
| 2 | +const should = require('should') // eslint-disable-line |
| 3 | + |
| 4 | +const { KEY_STACK_UPGRADE_HOUR } = require('../../../../../../forge/db/models/ProjectSettings') |
| 5 | +const enforceTeamRulesTask = require('../../../../../../forge/ee/lib/autoUpdateStacks/tasks/enforce-team-rules') |
| 6 | +const setup = require('../../setup') |
| 7 | + |
| 8 | +describe('Automatic Stack Upgrade', function () { |
| 9 | + let app |
| 10 | + |
| 11 | + before(async function () { |
| 12 | + setup.setupStripe() |
| 13 | + app = await setup() |
| 14 | + |
| 15 | + const defaultTeamType = await app.db.models.TeamType.findOne({ where: { id: 1 } }) |
| 16 | + |
| 17 | + const autoTeamTypeProperties = { |
| 18 | + name: 'AutoUpdate', |
| 19 | + order: 2, |
| 20 | + description: 'TeamType that forces Stack Updates', |
| 21 | + properties: { |
| 22 | + users: { |
| 23 | + limit: 10 |
| 24 | + }, |
| 25 | + runtimes: { |
| 26 | + limit: 20 |
| 27 | + }, |
| 28 | + devices: { |
| 29 | + productId: 'prod_device', |
| 30 | + priceId: 'price_device', |
| 31 | + description: '$5/month', |
| 32 | + limit: 10 |
| 33 | + }, |
| 34 | + billing: { |
| 35 | + productId: 'prod_team', |
| 36 | + priceId: 'price_team', |
| 37 | + description: '$10/month', |
| 38 | + proration: 'always_invoice' |
| 39 | + }, |
| 40 | + trial: { |
| 41 | + active: false |
| 42 | + }, |
| 43 | + enableAllFeatures: true, |
| 44 | + features: { |
| 45 | + fileStorageLimit: null, |
| 46 | + contextLimit: null |
| 47 | + }, |
| 48 | + teamBroker: { |
| 49 | + clients: { |
| 50 | + limit: 10 |
| 51 | + } |
| 52 | + }, |
| 53 | + autoStackUpdate: { |
| 54 | + enabled: true, |
| 55 | + days: [0, 6], |
| 56 | + hours: [0, 1, 2, 3, 4], |
| 57 | + allowDisable: false |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + autoTeamTypeProperties.instances = defaultTeamType.properties.instances |
| 63 | + |
| 64 | + const autoTeamType = await app.db.models.TeamType.create(autoTeamTypeProperties) |
| 65 | + app.autoTeamType = autoTeamType |
| 66 | + }) |
| 67 | + |
| 68 | + after(async function () { |
| 69 | + if (app) { |
| 70 | + await app.close() |
| 71 | + app = null |
| 72 | + } |
| 73 | + setup.resetStripe() |
| 74 | + }) |
| 75 | + |
| 76 | + describe('Update after TeamType change', async function () { |
| 77 | + it('Instance properties updated', async function () { |
| 78 | + const instanceStartSettings = await app.db.models.ProjectSettings.findAll({ |
| 79 | + where: { |
| 80 | + ProjectId: app.instance.id, |
| 81 | + key: { |
| 82 | + [Op.like]: `${KEY_STACK_UPGRADE_HOUR}_%` |
| 83 | + } |
| 84 | + } |
| 85 | + }) |
| 86 | + instanceStartSettings.should.have.length(0) |
| 87 | + await app.team.updateTeamType(app.autoTeamType, { interval: 'month' }) |
| 88 | + await enforceTeamRulesTask.run(app) |
| 89 | + const instanceAfterSettings = await app.db.models.ProjectSettings.findAll({ |
| 90 | + where: { |
| 91 | + ProjectId: app.instance.id, |
| 92 | + key: { |
| 93 | + [Op.like]: `${KEY_STACK_UPGRADE_HOUR}_%` |
| 94 | + } |
| 95 | + } |
| 96 | + }) |
| 97 | + instanceAfterSettings.should.have.length(1) |
| 98 | + instanceAfterSettings[0].value.hour.should.be.oneOf([0, 1, 2, 3, 4]) |
| 99 | + const day = parseInt(instanceAfterSettings[0].key.split('_')[1]) |
| 100 | + day.should.be.oneOf([0, 6]) |
| 101 | + }) |
| 102 | + it('Existing Instance properties not updated', async function () { |
| 103 | + // depends on previous test |
| 104 | + const instanceStartSettings = await app.db.models.ProjectSettings.findAll({ |
| 105 | + where: { |
| 106 | + ProjectId: app.instance.id, |
| 107 | + key: { |
| 108 | + [Op.like]: `${KEY_STACK_UPGRADE_HOUR}_%` |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + instanceStartSettings.should.have.length(1) |
| 113 | + const day = parseInt(instanceStartSettings[0].key.split('_')[1]) |
| 114 | + const hour = instanceStartSettings[0].value.hour |
| 115 | + await enforceTeamRulesTask.run(app) |
| 116 | + const instanceAfterSettings = await app.db.models.ProjectSettings.findAll({ |
| 117 | + where: { |
| 118 | + ProjectId: app.instance.id, |
| 119 | + key: { |
| 120 | + [Op.like]: `${KEY_STACK_UPGRADE_HOUR}_%` |
| 121 | + } |
| 122 | + } |
| 123 | + }) |
| 124 | + instanceAfterSettings.should.have.length(1) |
| 125 | + day.should.equal(parseInt(instanceAfterSettings[0].key.split('_')[1])) |
| 126 | + hour.should.equal(instanceAfterSettings[0].value.hour) |
| 127 | + }) |
| 128 | + }) |
| 129 | +}) |
0 commit comments