Skip to content

Commit

Permalink
fix: adding tests and use null for non existant records
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Harley committed Oct 26, 2017
1 parent 21eb89f commit e6b530e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/permission/rule-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export default class RuleApplication {
}
const recordData = this.recordsData[this.params.name]
if (typeof recordData !== UNDEFINED) {
return recordData ? recordData._d : {}
return recordData ? recordData._d : null
}
this.loadRecord(this.params.name)
}
Expand Down
46 changes: 46 additions & 0 deletions test/permission/config-permission-handler-basicSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,52 @@ describe('permission handler applies basic permissions referencing their own dat
})).toBe(false)
})

it('checks against existing data for non-existant record reads', (next) => {
const permissions = getBasePermissions()

permissions.record['nonExistingRecord'] = {
read: 'oldData.xyz === "hello"'
}

const message = {
topic: C.TOPIC.RECORD,
action: C.RECORD_ACTIONS.READ,
name: 'nonExistingRecord',
}

const callback = function (error, result) {
expect(lastError()).toContain('Cannot read property \'xyz\' of null')
expect(result).toBe(false)
next()
}

testPermission(permissions, message, 'user', null, callback)
})

it('checks against existing data for non-existant list reads', (next) => {
const permissions = getBasePermissions()

permissions.record['nonExistingRecord'] = {
read: 'oldData.indexOf("hello") !== -1'
}

const message = {
topic: C.TOPIC.RECORD,
action: C.RECORD_ACTIONS.READ,
name: 'nonExistingRecord',
}

const callback = function (error, result) {
expect(lastError()).toContain('Cannot read property \'indexOf\' of null')
expect(error).toBe(C.AUTH_ACTIONS.MESSAGE_PERMISSION_ERROR)
expect(result).toBe(false)
next()
}

testPermission(permissions, message, 'user', null, callback)

})

it('deals with broken messages', next => {
const permissions = getBasePermissions()

Expand Down
2 changes: 1 addition & 1 deletion test/record/record-handlerSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ describe('record handler handles messages', () => {
recordHandler.handle(client.socketWrapper, M.recordDelete)

services.cache.get(M.recordDelete.name, (error, record) => {
expect(record).toEqual(undefined)
expect(record).toEqual(null)
})
})

Expand Down
4 changes: 2 additions & 2 deletions test/test-mocks/storage-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ export default class StorageMock {
const value = this.values[key]

if (this.nextGetWillBeSynchronous === true) {
callback(this.nextOperationWillBeSuccessful ? null : 'storageError', value ? Object.assign({}, value) : undefined)
callback(this.nextOperationWillBeSuccessful ? null : 'storageError', value ? Object.assign({}, value) : null)
} else {
this.getTimeout = setTimeout(() => {
callback(this.nextOperationWillBeSuccessful ? null : 'storageError', value ? Object.assign({}, value) : undefined)
callback(this.nextOperationWillBeSuccessful ? null : 'storageError', value ? Object.assign({}, value) : null)
}, 25)
}
}
Expand Down

0 comments on commit e6b530e

Please sign in to comment.