Skip to content

Commit

Permalink
feat(pg-v5): credentials adding capabilities for numbered essential p…
Browse files Browse the repository at this point in the history
…lans (#2513)

* adding essential num plan tests

* wrong dir

* edit module

* extra line

* combining messaging

* adding cred check

* testing url and rotate

* editing based on credentials being available for num essentials too

* remove line

* error message changes

* fix expectation

---------

Co-authored-by: Justin Downing <jdowning@heroku.com>
  • Loading branch information
brittanyrjones and jdowning committed Oct 24, 2023
1 parent 2a71b1e commit 51dc3ac
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/pg-v5/commands/credentials/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function run(context, heroku) {
const {app, args, flags} = context

let db = await fetcher.addon(app, args.database)
if (util.essentialPlan(db)) throw new Error('You can’t perform this operation on Essential-tier databases.')
if (util.essentialPlan(db)) throw new Error("You can't create a custom credential on Essential-tier databases.")

let data = {
name: flags.name,
Expand Down
2 changes: 1 addition & 1 deletion packages/pg-v5/commands/credentials/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function run(context, heroku) {

let db = await fetcher.addon(app, args.database)
if (util.essentialPlan(db)) {
throw new Error('Essential-tier databases support only one default credential.')
throw new Error("You can't destroy the default credential on Essential-tier databases.")
}

let attachments = await heroku.get(`/addons/${db.name}/addon-attachments`)
Expand Down
4 changes: 2 additions & 2 deletions packages/pg-v5/commands/credentials/rotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ async function run(context, heroku) {
throw new Error('cannot pass both --all and --name')
}

if (util.essentialPlan(db) && cred !== 'default') {
throw new Error('Essential-tier databases support only one default credential.')
if (util.essentialNumPlan(db) || (util.legacyEssentialPlan(db) && cred !== 'default')) {
throw new Error("You can't rotate credentials on Essential-tier databases.")
}

if (all && flags.force) {
Expand Down
4 changes: 2 additions & 2 deletions packages/pg-v5/commands/credentials/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ async function run(context, heroku) {

let db = await fetcher.addon(app, args.database)
let cred = flags.name || 'default'
if (util.essentialPlan(db) && cred !== 'default') {
throw new Error('Essential-tier databases support only one default credential.')
if (util.essentialNumPlan(db) || (util.legacyEssentialPlan(db) && cred !== 'default')) {
throw new Error("You can't view credentials on Essential-tier databases.")
}

let credInfo = await heroku.get(`/postgres/v0/databases/${db.name}/credentials/${encodeURIComponent(cred)}`,
Expand Down
25 changes: 23 additions & 2 deletions packages/pg-v5/test/unit/commands/credentials/create.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,31 @@ Please define the new grants for the credential within Postgres: heroku pg:psql
.then(() => expect(cli.stderr).to.equal('Creating credential credname... done\n'))
})

it('throws an error when the db is numbered essential plan', () => {
const essentialAddon = {
name: 'postgres-1',
plan: {name: 'heroku-postgresql:essential-0'},
}

const fetcher = () => {
return {
database: () => db,
addon: () => essentialAddon,
}
}

const cmd = proxyquire('../../../../commands/credentials/create', {
'../../lib/fetcher': fetcher,
})

const err = "You can't create a custom credential on Essential-tier databases."
return expect(cmd.run({app: 'myapp', args: {}, flags: {name: 'jeff'}})).to.be.rejectedWith(Error, err)
})

it('throws an error when the db is essential plan', () => {
const hobbyAddon = {
name: 'postgres-1',
plan: {name: 'heroku-postgresql:hobby-dev'},
plan: {name: 'heroku-postgresql:mini'},
}

const fetcher = () => {
Expand All @@ -72,7 +93,7 @@ Please define the new grants for the credential within Postgres: heroku pg:psql
'../../lib/fetcher': fetcher,
})

const err = 'You can’t perform this operation on Essential-tier databases.'
const err = "You can't create a custom credential on Essential-tier databases."
return expect(cmd.run({app: 'myapp', args: {}, flags: {name: 'jeff'}})).to.be.rejectedWith(Error, err)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,28 @@ Database objects owned by credname will be assigned to the default credential.
'../../lib/fetcher': fetcher,
})

const err = 'Essential-tier databases support only one default credential.'
const err = "You can't destroy the default credential on Essential-tier databases."
return expect(cmd.run({app: 'myapp', args: {}, flags: {name: 'jeff'}})).to.be.rejectedWith(Error, err)
})

it('throws an error when the db is numbered essential plan', () => {
const essentialAddon = {
name: 'postgres-1',
plan: {name: 'heroku-postgresql:essential-0'},
}

const fetcher = () => {
return {
database: () => db,
addon: () => essentialAddon,
}
}

const cmd = proxyquire('../../../../commands/credentials/destroy', {
'../../lib/fetcher': fetcher,
})

const err = "You can't destroy the default credential on Essential-tier databases."
return expect(cmd.run({app: 'myapp', args: {}, flags: {name: 'jeff'}})).to.be.rejectedWith(Error, err)
})

Expand Down
27 changes: 24 additions & 3 deletions packages/pg-v5/test/unit/commands/credentials/rotate.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ describe('pg:credentials:rotate', () => {
return expect(cmd.run({app: 'myapp', args: {}, flags: {all: true, name: 'my_role', confirm: 'myapp'}})).to.be.rejectedWith(Error, err)
})

it('throws an error when the db is starter plan but the name is specified', () => {
it('throws an error when the db is essential plan but the name is specified', () => {
const hobbyAddon = {
name: 'postgres-1',
plan: {name: 'heroku-postgresql:hobby-dev'},
plan: {name: 'heroku-postgresql:mini'},
}

const fetcher = () => {
Expand All @@ -122,7 +122,28 @@ describe('pg:credentials:rotate', () => {
'../../lib/fetcher': fetcher,
})

const err = 'Essential-tier databases support only one default credential.'
const err = "You can't rotate credentials on Essential-tier databases."
return expect(cmd.run({app: 'myapp', args: {}, flags: {name: 'jeff'}})).to.be.rejectedWith(Error, err)
})

it('throws an error when the db is numbered essential plan', () => {
const essentialAddon = {
name: 'postgres-1',
plan: {name: 'heroku-postgresql:essential-0'},
}

const fetcher = () => {
return {
database: () => db,
addon: () => essentialAddon,
}
}

const cmd = proxyquire('../../../../commands/credentials/rotate', {
'../../lib/fetcher': fetcher,
})

const err = "You can't rotate credentials on Essential-tier databases."
return expect(cmd.run({app: 'myapp', args: {}, flags: {name: 'jeff'}})).to.be.rejectedWith(Error, err)
})

Expand Down
23 changes: 22 additions & 1 deletion packages/pg-v5/test/unit/commands/credentials/url.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,28 @@ Connection URL:
'../../lib/fetcher': fetcher,
})

const err = 'Essential-tier databases support only one default credential.'
const err = "You can't view credentials on Essential-tier databases."
return expect(cmd.run({app: 'myapp', args: {}, flags: {name: 'jeff'}})).to.be.rejectedWith(Error, err)
})

it('throws an error when the db is numbered essential plan', () => {
const essentialAddon = {
name: 'postgres-1',
plan: {name: 'heroku-postgresql:essential-0'},
}

const fetcher = () => {
return {
database: () => db,
addon: () => essentialAddon,
}
}

const cmd = proxyquire('../../../../commands/credentials/url', {
'../../lib/fetcher': fetcher,
})

const err = "You can't view credentials on Essential-tier databases."
return expect(cmd.run({app: 'myapp', args: {}, flags: {name: 'jeff'}})).to.be.rejectedWith(Error, err)
})

Expand Down

0 comments on commit 51dc3ac

Please sign in to comment.