Skip to content

Commit

Permalink
fix(addons): merged heroku/heroku-cli-addons#93
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jun 19, 2018
1 parent 36f4aef commit 2fdefc5
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/addons-v5/commands/addons/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function * run (context, heroku) {
const _ = require('lodash')

let plans = yield heroku.get(`/addon-services/${context.args.service}/plans`)
plans = _.sortBy(plans, 'price.cents')
plans = _.sortBy(plans, ['price.contract', 'price.cents'])

if (context.flags.json) {
cli.styledJSON(plans)
Expand Down
6 changes: 5 additions & 1 deletion packages/addons-v5/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ module.exports = {

grandfatheredPrice: function (addon) {
const price = addon.plan.price
return Object.assign({}, price, {cents: addon.billed_price.cents})
return Object.assign({}, price, {
cents: addon.billed_price.cents,
contract: addon.billed_price.contract
})
},

formatPrice: function (price) {
const printf = require('printf')

if (!price) return
if (price.contract) return 'contract'
if (price.cents === 0) return 'free'

let fmt = price.cents % 100 === 0 ? '$%.0f/%s' : '$%.02f/%s'
Expand Down
22 changes: 22 additions & 0 deletions packages/addons-v5/test/commands/addons.--all.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,28 @@ acme-inc-dwh dwh-db heroku-postgresql:standard-2 $100/month created`)
})
})

context('with a contract add-on', function () {
beforeEach(function () {
let addon = fixtures.addons['dwh-db']
addon.billed_price = { cents: 0, contract: true }

nock('https://api.heroku.com', {reqheaders: {
'Accept-Expansion': 'addon_service,plan'
}})
.get('/addons')
.reply(200, [addon])
})

it('prints add-ons in a table with contract', function () {
return cmd.run({flags: {}}).then(function () {
util.expectOutput(cli.stdout,
`Owning App Add-on Plan Price State
──────────── ────── ──────────────────────────── ──────── ───────
acme-inc-dwh dwh-db heroku-postgresql:standard-2 contract created`)
})
})
})

it('prints message when there are no add-ons', function () {
nock('https://api.heroku.com').get('/addons').reply(200, [])
return cmd.run({flags: {}}).then(function () {
Expand Down
24 changes: 24 additions & 0 deletions packages/addons-v5/test/commands/addons.--app.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,30 @@ The table above shows add-ons and the attachments to the current app (acme-inc-d
})
})

context('with a contract add-on', function () {
beforeEach(function () {
let addon = fixtures.addons['dwh-db']
addon.billed_price = {cents: 0, contract: true}

mockAPI('acme-inc-dwh', [
addon
], [
fixtures.attachments['acme-inc-dwh::DATABASE']
])
})

it('prints add-ons in a table with contract', function () {
return run('acme-inc-dwh', function () {
util.expectOutput(cli.stdout,
`Add-on Plan Price State
────────────────────────── ────────── ──────── ───────
heroku-postgresql (dwh-db) standard-2 contract created
└─ as DATABASE
The table above shows add-ons and the attachments to the current app (acme-inc-dwh) or other apps.`)
})
})
})

it('prints add-on line for attachment when add-on info is missing from API (e.g. no permissions on billing app)', function () {
mockAPI('acme-inc-api', [ /* no add-on ! */], [fixtures.attachments['acme-inc-api::WWW_DB']])

Expand Down
41 changes: 41 additions & 0 deletions packages/addons-v5/test/commands/addons/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ let util = require('../../util')
let cli = require('heroku-cli-util')
let nock = require('nock')
let cmd = require('../../../commands/addons/info')
let cache = require('../../../lib/resolve').addon.cache

describe('addons:info', function () {
beforeEach(function () {
cli.mockConsole()
nock.cleanAll()
cache.clear()
})

context('with add-ons', function () {
Expand Down Expand Up @@ -79,6 +81,10 @@ State: created

context('with app but not an app add-on', function () {
beforeEach(function () {
nock('https://api.heroku.com', {reqheaders: {'Accept-Expansion': 'addon_service,plan'}})
.post('/actions/addons/resolve', {'app': 'example', 'addon': 'www-db'})
.reply(200, [fixtures.addons['www-db']])

nock('https://api.heroku.com', {reqheaders: {'Accept-Expansion': 'addon_service,plan'}})
.get('/apps/example/addons/www-db')
.reply(404)
Expand Down Expand Up @@ -141,6 +147,41 @@ Owning app: acme-inc-dwh
Plan: heroku-postgresql:standard-2
Price: $100/month
State: created
`)
})
})
})

context('with a contract add-on', function () {
beforeEach(function () {
let addon = fixtures.addons['dwh-db']
addon.billed_price = { cents: 0, contract: true }

nock('https://api.heroku.com', {reqheaders: {'Accept-Expansion': 'addon_service,plan'}})
.post('/actions/addons/resolve', {'app': null, 'addon': 'dwh-db'})
.reply(200, [addon])

nock('https://api.heroku.com', {reqheaders: {
'Accept-Expansion': 'addon_service,plan'
}})
.get(`/addons/${addon['id']}`)
.reply(200, addon)

nock('https://api.heroku.com')
.get(`/addons/${fixtures.addons['dwh-db']['id']}/addon-attachments`)
.reply(200, [fixtures.attachments['acme-inc-dwh::DATABASE']])
})

it('prints add-ons in a table with contract', function () {
return cmd.run({flags: {}, args: {addon: 'dwh-db'}}).then(function () {
util.expectOutput(cli.stdout,
`=== dwh-db
Attachments: acme-inc-dwh::DATABASE
Installed at: Invalid Date
Owning app: acme-inc-dwh
Plan: heroku-postgresql:standard-2
Price: contract
State: created
`)
})
})
Expand Down
12 changes: 8 additions & 4 deletions packages/addons-v5/test/commands/addons/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ describe('addons:plans', function () {
let api = nock('https://api.heroku.com:443')
.get('/addon-services/daservice/plans')
.reply(200, [
{name: 'first', human_name: 'First', price: {cents: 0, unit: 'month'}, default: true},
{name: 'second', human_name: 'Second', price: {cents: 1000, unit: 'month'}, default: false}
{name: 'first', human_name: 'First', price: {cents: 0, unit: 'month', contract: false}, default: true},
{name: 'second', human_name: 'Second', price: {cents: 2000, unit: 'month', contract: false}, default: false},
{name: 'third', human_name: 'Third', price: {cents: 10000, unit: 'month', contract: false}, default: false},
{name: 'fourth', human_name: 'Fourth', price: {cents: 0, unit: 'month', contract: true}, default: false}
])
return cmd.run({args: {service: 'daservice'}, flags: {}})
.then(() => expect(cli.stdout).to.equal(` slug name price
─────── ────── ────── ─────────
─────── ────── ────── ─────────
default first First free
second Second $10/month
second Second $20/month
third Third $100/month
fourth Fourth contract
`))
.then(() => api.done())
})
Expand Down

0 comments on commit 2fdefc5

Please sign in to comment.