Skip to content

Commit

Permalink
fix: guard bad optional property types
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Apr 16, 2020
1 parent 5050097 commit ad7a79b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
const contacts = require('bindings')('contacts.node')

const validProperties = [
'jobTitle',
'departmentName',
'organizationName',
'middleName',
'note',
'contactImage',
'contactThumbnailImage',
'instantMessageAddresses',
'socialProfiles',
]

function getAllContacts(extraProperties = []) {
if (!Array.isArray(extraProperties)) {
throw new TypeError('extraProperties must be an array')
}

if (!extraProperties.every((p) => validProperties.includes(p))) {
throw new TypeError(
`properties in extraProperties must be one of ${validProperties.join(', ')}`,
)
}

return contacts.getAllContacts.call(this, extraProperties)
}

Expand All @@ -17,6 +35,12 @@ function getContactsByName(name, extraProperties = []) {
throw new TypeError('extraProperties must be an array')
}

if (!extraProperties.every((p) => validProperties.includes(p))) {
throw new TypeError(
`properties in extraProperties must be one of ${validProperties.join(', ')}`,
)
}

return contacts.getContactsByName.call(this, name, extraProperties)
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"build": "node-gyp build",
"build:dev": "node-gyp build --debug",
"clean": "node-gyp clean",
"lint": "prettier --check index.js",
"format": "clang-format -i contacts.mm && prettier --write index.js",
"lint": "prettier --check '**/*.js'",
"format": "clang-format -i contacts.mm && prettier --write '**/*.js'",
"rebuild": "node-gyp rebuild",
"rebuild:dev": "node-gyp rebuild --debug",
"test": "./node_modules/.bin/mocha --reporter spec"
Expand Down
28 changes: 24 additions & 4 deletions test/module.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const { expect } = require('chai')
const {
const {
getAuthStatus,
getContactsByName,
getAllContacts,
addNewContact,
deleteContact,
updateContact
updateContact,
} = require('../index')

describe('node-mac-contacts', () => {
describe('getAuthStatus()', () => {
it('should not throw', () => {
expect(() => { getAuthStatus() }).to.not.throw()
expect(() => {
getAuthStatus()
}).to.not.throw()
})

it('should return a string', () => {
Expand All @@ -32,6 +34,15 @@ describe('node-mac-contacts', () => {
getContactsByName('jim-bob', 12345)
}).to.throw(/extraProperties must be an array/)
})

it('should throw if extraProperties contains invalid properties', () => {
const errorMessage =
'properties in extraProperties must be one of jobTitle, departmentName, organizationName, middleName, note, contactImage, contactThumbnailImage, instantMessageAddresses, socialProfiles'

expect(() => {
getContactsByName('jim-bob', ['bad-property'])
}).to.throw(errorMessage)
})
})

describe('getAllContacts([extraProperties])', () => {
Expand All @@ -45,6 +56,15 @@ describe('node-mac-contacts', () => {
getAllContacts('tsk-bad-array')
}).to.throw(/extraProperties must be an array/)
})

it('should throw if extraProperties contains invalid properties', () => {
const errorMessage =
'properties in extraProperties must be one of jobTitle, departmentName, organizationName, middleName, note, contactImage, contactThumbnailImage, instantMessageAddresses, socialProfiles'

expect(() => {
getAllContacts(['bad-property'])
}).to.throw(errorMessage)
})
})

describe('addNewContact(contact)', () => {
Expand Down Expand Up @@ -150,4 +170,4 @@ describe('node-mac-contacts', () => {
}).to.throw(/emailAddresses must be an array/)
})
})
})
})

0 comments on commit ad7a79b

Please sign in to comment.