-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor authorization #3529
Refactor authorization #3529
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import { | |
Authorizer, | ||
} from '../..'; | ||
import {AuthorizationTags} from '../../keys'; | ||
import {AuthenticationBindings, UserProfile} from '@loopback/authentication'; | ||
|
||
describe('Authorization', () => { | ||
let app: Application; | ||
|
@@ -26,7 +27,7 @@ describe('Authorization', () => { | |
let events: string[]; | ||
|
||
before(givenApplicationAndAuthorizer); | ||
beforeEach(givenRequestContext); | ||
beforeEach(() => givenRequestContext()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this change needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raymondfeng I am seeing this weird error... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I think that's because |
||
|
||
it('allows placeOrder for everyone', async () => { | ||
const orderId = await invokeMethod(controller, 'placeOrder', reqCtx, [ | ||
|
@@ -42,6 +43,7 @@ describe('Authorization', () => { | |
}); | ||
|
||
it('allows cancelOrder for customer_service', async () => { | ||
givenRequestContext({id: 'customer_service', name: 'customer_service'}); | ||
const orderId = await controller.placeOrder({ | ||
customerId: 'bob', | ||
productId: 'product-a', | ||
|
@@ -56,7 +58,7 @@ describe('Authorization', () => { | |
}); | ||
|
||
it('denies cancelOrder for bob', async () => { | ||
givenRequestContext({name: 'bob'}); | ||
givenRequestContext({id: 'bob', name: 'bob'}); | ||
const orderId = await controller.placeOrder({ | ||
customerId: 'bob', | ||
productId: 'product-a', | ||
|
@@ -111,10 +113,12 @@ describe('Authorization', () => { | |
.tag(AuthorizationTags.AUTHORIZER); | ||
} | ||
|
||
function givenRequestContext(user = {name: 'alice'}) { | ||
function givenRequestContext( | ||
user: UserProfile = {id: 'alice', name: 'alice'}, | ||
) { | ||
events = []; | ||
reqCtx = new Context(app); | ||
reqCtx.bind('current.user').to(user); | ||
reqCtx.bind(AuthenticationBindings.CURRENT_USER).to(user); | ||
controller = new OrderController(); | ||
} | ||
|
||
|
@@ -161,6 +165,6 @@ describe('Authorization', () => { | |
__dirname, | ||
'../../../fixtures/casbin/rbac_policy.csv', | ||
); | ||
return await casbin.newEnforcer(conf, policy); | ||
return casbin.newEnforcer(conf, policy); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some comments about this temporary workaround.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@raymondfeng I added an entry in the README.md file to explain the user injection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, let's change the initial version to be
0.0.1
so that the first release will be0.1.0
.