Skip to content

Commit

Permalink
chore(certs-v5): add code coverage reports (#2300)
Browse files Browse the repository at this point in the history
* Remove unused code

* Remove chain command and test

* Update certs:info app flag and tests

* Remove unused code and incomplete command

* Comment out broken commands & update tests
  • Loading branch information
zwhitfield3 authored Apr 19, 2023
1 parent 2e4feb6 commit 5c7180b
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 102 deletions.
43 changes: 0 additions & 43 deletions packages/certs-v5/commands/certs/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,17 @@
let cli = require('heroku-cli-util')
let _ = require('lodash')
let inquirer = require('inquirer')

let error = require('../../lib/error.js')
let findMatch = require('../../lib/find_match.js')
let endpoints = require('../../lib/endpoints.js')
let displayWarnings = require('../../lib/display_warnings.js')
let certificateDetails = require('../../lib/certificate_details.js')
let isWildcard = require('../../lib/is_wildcard.js')
let isWildcardMatch = require('../../lib/is_wildcard_match.js')
let getCertAndKey = require('../../lib/get_cert_and_key.js')
let matchDomains = require('../../lib/match_domains.js')
let { waitForDomains } = require('../../lib/domains')

function Domains (domains) {
this.domains = domains

this.added = this.domains.filter((domain) => !domain._failed)
this.failed = this.domains.filter((domain) => domain._failed)

this.hasFailed = this.failed.length > 0
}

async function getMeta(context, heroku) {
return endpoints.meta(context.app, 'sni')
}

function hasMatch (certDomains, domain) {
return _.find(certDomains, (certDomain) => (certDomain === domain || isWildcardMatch(certDomain, domain)))
}

function getPromptChoices (context, certDomains, existingDomains, newDomains) {
let nonWildcardDomains = newDomains.filter((domain) => !isWildcard(domain))

if (nonWildcardDomains.length === 0) {
return Promise.resolve({ domains: [] })
}

return inquirer.prompt([{
type: 'checkbox',
name: 'domains',
message: 'Select domains you would like to add',
choices: nonWildcardDomains.map(function (domain) {
return { name: domain }
})
}])
}

async function getChoices(certDomains, newDomains, existingDomains, context) {
if (newDomains.length === 0) {
return []
} else {
return ((await getPromptChoices(context, certDomains, existingDomains, newDomains))).domains;
}
}

async function configureDomains(context, heroku, meta, cert) {
let certDomains = cert.ssl_cert.cert_domains
let apiDomains = await waitForDomains(context, heroku)
Expand Down
18 changes: 10 additions & 8 deletions packages/certs-v5/commands/certs/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

let cli = require('heroku-cli-util')

let error = require('../../lib/error.js')
let readFile = require('../../lib/read_file.js')
// let error = require('../../lib/error.js')
// let readFile = require('../../lib/read_file.js')

async function run(context) {
if (context.args.length === 0) {
error.exit(1, 'Usage: heroku certs:chain CRT [CRT ...]\nMust specify at least one certificate file.')
}
async function run (context) {
// TODO: Fix chain command

let res = await Promise.all(context.args.map(function (arg) { return readFile(arg) }))
// if (context.args.length === 0) {
// error.exit(1, 'Usage: heroku certs:chain CRT [CRT ...]\nMust specify at least one certificate file.')
// }

cli.console.writeLog(body)
// let res = await Promise.all(context.args.map(function (arg) { return readFile(arg) }))

// cli.console.writeLog(body)
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion packages/certs-v5/commands/certs/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function run(context, heroku) {
if (context.flags['show-domains']) {
let domains = await Promise.all(endpoint.domains.map(domain => {
return heroku.request({
path: `/apps/${context.flags.app}/domains/${domain}`
path: `/apps/${context.app}/domains/${domain}`
}).then(response => response.hostname)
}))
cert.domains = domains
Expand Down
16 changes: 9 additions & 7 deletions packages/certs-v5/commands/certs/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

let cli = require('heroku-cli-util')

let error = require('../../lib/error.js')
let readFile = require('../../lib/read_file.js')
// let error = require('../../lib/error.js')
// let readFile = require('../../lib/read_file.js')

async function run(context) {
if (context.args.length < 2) {
error.exit(1, 'Usage: heroku certs:key CRT KEY [KEY ...]\nMust specify one certificate file and at least one key file.')
}
// TODO: Fix key command

let res = await Promise.all(context.args.map(function (arg) { return readFile(arg) }))
// if (context.args.length < 2) {
// error.exit(1, 'Usage: heroku certs:key CRT KEY [KEY ...]\nMust specify one certificate file and at least one key file.')
// }

cli.console.writeLog(body.key)
// let res = await Promise.all(context.args.map(function (arg) { return readFile(arg) }))

// cli.console.writeLog(body.key)
}

module.exports = {
Expand Down
9 changes: 0 additions & 9 deletions packages/certs-v5/lib/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,7 @@ async function all(appName, heroku) {
return tagAndSort(appName, sniCerts)
}

async function hasSpace(app, heroku) {
return await heroku.request({
path: `/apps/${app}`
}).then(function (data) {
return !!data.space
});
}

module.exports = {
hasSpace,
meta,
all
}
35 changes: 18 additions & 17 deletions packages/certs-v5/test/commands/certs/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@
/* globals describe it beforeEach afterEach cli */

let expect = require('chai').expect
let nock = require('nock')
var fs = require('fs')
var sinon = require('sinon')
// let nock = require('nock')
// var fs = require('fs')
// var sinon = require('sinon')

let certs = require('../../../commands/certs/chain.js')
let assertExit = require('../../assert_exit.js')
let error = require('../../../lib/error.js')
const unwrap = require('../../unwrap')
// let assertExit = require('../../assert_exit.js')
// let error = require('../../../lib/error.js')
// const unwrap = require('../../unwrap')

// TODO: Update tests once fix has been implemented

describe('heroku certs:chain', function () {
beforeEach(function () {
cli.mockConsole()
error.exit.mock()
// error.exit.mock()

sinon.stub(fs, 'readFile')
fs.readFile.throws('unstubbed')
// sinon.stub(fs, 'readFile')
// fs.readFile.throws('unstubbed')

nock.cleanAll()
// nock.cleanAll()
})

afterEach(function () {
fs.readFile.restore()
})
// afterEach(function () {
// fs.readFile.restore()
// })

it('# validates that at least one argument is passed', function () {
return assertExit(1, certs.run({ app: 'example', args: [] })).then(function () {
expect(unwrap(cli.stderr)).to.equal(
'Usage: heroku certs:chain CRT [CRT ...] Must specify at least one certificate file.\n')
it('# checks command does nothing', function () {
return certs.run({ app: 'example', args: [] }).then(function () {
expect(cli.stdout).to.equal('')
expect(cli.stderr).to.equal('')
})
})
})
24 changes: 24 additions & 0 deletions packages/certs-v5/test/commands/certs/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ let expect = require('chai').expect

let sharedSni = require('./shared_sni.js')
let endpoint = require('../../stubs/sni-endpoints.js').endpoint
let endpointWithDomains = require('../../stubs/sni-endpoints.js').endpoint_with_domains
let endpointUntrusted = require('../../stubs/sni-endpoints.js').endpoint_untrusted
let endpointTrusted = require('../../stubs/sni-endpoints.js').endpoint_trusted
let certificateDetails = require('../../stubs/sni-endpoints.js').certificate_details
Expand Down Expand Up @@ -37,6 +38,29 @@ ${certificateDetails}
})
})

it('returns domains when show-domains flag is passed', function () {
let mockSni = nock('https://api.heroku.com')
.get('/apps/example/sni-endpoints')
.reply(200, [endpointWithDomains])

let mock = nock('https://api.heroku.com', {
reqheaders: { 'Accept': 'application/vnd.heroku+json; version=3' }
})
.get('/apps/example/sni-endpoints/tokyo-1050')
.reply(200, endpointWithDomains)

let mockDomains = nock('https://api.heroku.com')
.get('/apps/example/domains/example.heroku.com')
.reply(200, [endpointWithDomains])

return certs.run({ app: 'example', args: {}, flags: { 'show-domains': true } }).then(function () {
mockSni.done()
mock.done()
mockDomains.done()
expect(mockDomains.isDone()).to.equal(true)
})
})

it('shows certificate details when not trusted', function () {
let mockSni = nock('https://api.heroku.com')
.get('/apps/example/sni-endpoints')
Expand Down
35 changes: 18 additions & 17 deletions packages/certs-v5/test/commands/certs/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@
/* globals describe it beforeEach afterEach cli */

let expect = require('chai').expect
let nock = require('nock')
var fs = require('fs')
var sinon = require('sinon')
// let nock = require('nock')
// var fs = require('fs')
// var sinon = require('sinon')

let certs = require('../../../commands/certs/key.js')
let assertExit = require('../../assert_exit.js')
let error = require('../../../lib/error.js')
const unwrap = require('../../unwrap')
// let assertExit = require('../../assert_exit.js')
// let error = require('../../../lib/error.js')
// const unwrap = require('../../unwrap')

// TODO: Update tests once fix has been implemented

describe('heroku certs:key', function () {
beforeEach(function () {
cli.mockConsole()
error.exit.mock()
// error.exit.mock()

sinon.stub(fs, 'readFile')
fs.readFile.throws('unstubbed')
// sinon.stub(fs, 'readFile')
// fs.readFile.throws('unstubbed')

nock.cleanAll()
// nock.cleanAll()
})

afterEach(function () {
fs.readFile.restore()
})
// afterEach(function () {
// fs.readFile.restore()
// })

it('# validates that at least one argument is passed', function () {
return assertExit(1, certs.run({ app: 'example', args: ['foo'] })).then(function () {
expect(unwrap(cli.stderr)).to.equal(
'Usage: heroku certs:key CRT KEY [KEY ...] Must specify one certificate file and at least one key file.\n')
it('# checks command does nothing', function () {
return certs.run({ app: 'example', args: [] }).then(function () {
expect(cli.stdout).to.equal('')
expect(cli.stderr).to.equal('')
})
})
})
13 changes: 13 additions & 0 deletions packages/certs-v5/test/stubs/sni-endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ SSL certificate is self signed.`,
'subject': '/C=US/ST=California/L=San Francisco/O=Heroku by Salesforce/CN=secure.example.org'
}
},
endpoint_with_domains: { 'name': 'tokyo-1050',
'cname': 'tokyo-1050.herokussl.com',
'domains': ['example.heroku.com'],
'display_name': 'my-tokyo-1050',
'ssl_cert': {
'ca_signed?': false,
'cert_domains': [ 'example.org' ],
'starts_at': '2012-08-01T21:34:23Z',
'expires_at': '2013-08-01T21:34:23Z',
'issuer': '/C=US/ST=California/L=San Francisco/O=Heroku by Salesforce/CN=secure.example.org',
'subject': '/C=US/ST=California/L=San Francisco/O=Heroku by Salesforce/CN=secure.example.org'
}
},
endpoint_untrusted: { 'name': 'tokyo-1050',
'cname': 'tokyo-1050.herokussl.com',
'ssl_cert': {
Expand Down

0 comments on commit 5c7180b

Please sign in to comment.