-
-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: adds test that ensures ForbiddenError default error message use…
…s subject type name
- Loading branch information
Showing
9 changed files
with
104 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { ForbiddenError, getDefaultErrorMessage, PureAbility, SubjectType } from '../src' | ||
|
||
describe('`ForbiddenError` class', () => { | ||
describe('`throwUnlessCan` method', () => { | ||
it('raises forbidden exception on disallowed action', () => { | ||
const { error } = setup() | ||
expect(() => error.throwUnlessCan('archive', 'Post')).toThrow(ForbiddenError as unknown as Error) | ||
}) | ||
|
||
it('does not raise forbidden exception on allowed action', () => { | ||
const { error } = setup() | ||
expect(() => error.throwUnlessCan('read', 'Post')).not.toThrow(ForbiddenError as unknown as Error) | ||
}) | ||
|
||
it('raises error with context information', () => { | ||
let thrownError: ForbiddenError<PureAbility> | undefined | ||
const { error } = setup() | ||
|
||
try { | ||
error.throwUnlessCan('archive', 'Post') | ||
} catch (abilityError) { | ||
thrownError = abilityError as ForbiddenError<PureAbility> | ||
} | ||
|
||
expect(thrownError!.action).toBe('archive') | ||
expect(thrownError!.subject).toBe('Post') | ||
expect(thrownError!.subjectType).toBe('Post') | ||
}) | ||
|
||
it('raises error with message provided in `reason` field of forbidden rule', () => { | ||
const NO_CARD_MESSAGE = 'No credit card provided' | ||
const { ability, error } = setup() | ||
|
||
ability.update([{ | ||
action: 'update', | ||
subject: 'Post', | ||
inverted: true, | ||
reason: NO_CARD_MESSAGE | ||
}]) | ||
|
||
expect(() => error.throwUnlessCan('update', 'Post')).toThrow(NO_CARD_MESSAGE) | ||
}) | ||
|
||
it('can raise error with custom message', () => { | ||
const message = 'My custom error message' | ||
const { error } = setup() | ||
|
||
expect(() => error.setMessage(message).throwUnlessCan('update', 'Post')).toThrow(message) | ||
}) | ||
|
||
it('correctly extracts subject type name from class subject types', () => { | ||
class Post {} | ||
|
||
const ability = new PureAbility([ | ||
{ action: 'read', subject: Post } | ||
], { | ||
detectSubjectType: o => o.constructor as SubjectType | ||
}) | ||
|
||
try { | ||
ForbiddenError.from(ability).throwUnlessCan('update', Post) | ||
expect('this code').toBe('never reached') | ||
} catch (error) { | ||
expect((error as ForbiddenError<PureAbility>).subjectType).toBe('Post') | ||
expect((error as ForbiddenError<PureAbility>).message).toBe('Cannot execute "update" on "Post"') | ||
} | ||
}) | ||
}) | ||
|
||
describe('`setDefaultMessage` method', () => { | ||
afterEach(() => { | ||
ForbiddenError.setDefaultMessage(getDefaultErrorMessage) | ||
}) | ||
|
||
it('sets default message from function', () => { | ||
ForbiddenError.setDefaultMessage(err => `errror -> ${err.action}-${err.subjectType}`) | ||
const { error } = setup() | ||
|
||
expect(() => error.throwUnlessCan('update', 'Post')).toThrow('errror -> update-Post') | ||
}) | ||
}) | ||
|
||
function setup() { | ||
const ability = new PureAbility([ | ||
{ action: 'read', subject: 'Post' } | ||
]) | ||
const error = ForbiddenError.from(ability) | ||
|
||
return { ability, error } | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import chai from 'chai' | ||
import spies from 'chai-spies' | ||
import '../../dx/lib/spec_helper'; | ||
|
||
chai.use(spies) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters