-
Notifications
You must be signed in to change notification settings - Fork 27
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
Feature: Canner PAT authenticator #181
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4bd1b29
feat: Canner PAT authenticator
onlyjackfrost 824d133
chore: remove unused code, handle axios error, add test case to test …
onlyjackfrost 08aa72d
fix(feature/authenticator): fix incorrect query structure, remove unu…
onlyjackfrost efdacb0
feat(feature/authenticator): move canner pat authenticator from serve…
onlyjackfrost 94a636e
chore(feature/authenticator): add playground vulcan.yaml example
onlyjackfrost 3d3ab85
fix(feat/authenticator): remove test cases cause logic change
onlyjackfrost 646b3d8
chore(feature/authenticator): fix typo
onlyjackfrost File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
179 changes: 179 additions & 0 deletions
179
packages/integration-testing/src/examples/example5-get-user-profile-CannerPAT.spec.ts
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,179 @@ | ||
import { IBuildOptions, VulcanBuilder } from '@vulcan-sql/build'; | ||
import { | ||
ServeConfig, | ||
VulcanServer, | ||
CannerPATAuthenticator, | ||
} from '@vulcan-sql/serve'; | ||
import * as supertest from 'supertest'; | ||
import defaultConfig from './projectConfig'; | ||
import * as sinon from 'ts-sinon'; | ||
|
||
describe('Example3: get user profile by GET /auth/user-profile API with Authorization', () => { | ||
let server: VulcanServer; | ||
let projectConfig: ServeConfig & IBuildOptions; | ||
const mockUser = { | ||
username: 'apple Hey', | ||
firstName: 'Hey', | ||
lastName: 'apple', | ||
accountRole: 'admin', | ||
|
||
attributes: { | ||
attr1: 100 * 10000, | ||
attr2: 'Los Angeles', | ||
}, | ||
createdAt: '2023-03-27T12:48:15.882Z', | ||
email: 'Alvina_Farrell82@yahoo.com', | ||
groups: [{ id: 1, name: 'group1' }], | ||
}; | ||
const mockToken = `Canner-PAT myPATToken`; | ||
const mockCannerUserResponse = { | ||
status: 200, | ||
data: { | ||
data: { | ||
userMe: mockUser, | ||
}, | ||
}, | ||
}; | ||
const expectedUserProfile = { | ||
name: mockUser.username, | ||
attr: { | ||
firstName: 'Hey', | ||
lastName: 'apple', | ||
accountRole: 'admin', | ||
|
||
attributes: { | ||
attr1: 100 * 10000, | ||
attr2: 'Los Angeles', | ||
}, | ||
createdAt: '2023-03-27T12:48:15.882Z', | ||
email: 'Alvina_Farrell82@yahoo.com', | ||
groups: [{ id: 1, name: 'group1' }], | ||
}, | ||
}; | ||
// stub the private function to manipulate getting user info from remote server | ||
const stubFetchCannerUser = (user: any) => { | ||
const stub = sinon.default.stub( | ||
CannerPATAuthenticator.prototype, | ||
<any>'fetchCannerUser' | ||
); | ||
stub.resolves(user); | ||
return stub; | ||
}; | ||
beforeEach(async () => { | ||
projectConfig = { | ||
...defaultConfig, | ||
auth: { | ||
enabled: true, | ||
options: { | ||
'canner-pat': { | ||
host: 'mockhost', | ||
port: 3000, | ||
ssl: false, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}); | ||
|
||
afterEach(async () => { | ||
sinon.default.restore(); | ||
await server?.close(); | ||
}); | ||
|
||
it('Example 3-1: set Authorization in header with default options', async () => { | ||
stubFetchCannerUser(mockCannerUserResponse); | ||
const builder = new VulcanBuilder(projectConfig); | ||
await builder.build(); | ||
server = new VulcanServer(projectConfig); | ||
const httpServer = (await server.start())['http']; | ||
|
||
const agent = supertest(httpServer); | ||
const result = await agent | ||
.get('/auth/user-profile') | ||
.set('Authorization', mockToken); | ||
expect(result.body).toEqual(expectedUserProfile); | ||
}, 10000); | ||
|
||
it('Example 3-2: set Authorization in querying with default options', async () => { | ||
projectConfig['auth-source'] = { | ||
options: { | ||
key: 'x-auth', | ||
}, | ||
}; | ||
stubFetchCannerUser(mockCannerUserResponse); | ||
const builder = new VulcanBuilder(projectConfig); | ||
await builder.build(); | ||
server = new VulcanServer(projectConfig); | ||
const httpServer = (await server.start())['http']; | ||
|
||
const agent = supertest(httpServer); | ||
const auth = Buffer.from( | ||
JSON.stringify({ | ||
Authorization: `Canner-PAT ${Buffer.from(mockToken).toString( | ||
'base64' | ||
)}`, | ||
}) | ||
).toString('base64'); | ||
const result = await agent.get(`/auth/user-profile?x-auth=${auth}`); | ||
|
||
expect(result.body).toEqual(expectedUserProfile); | ||
}, 10000); | ||
|
||
it('Example 3-3: set Authorization in querying with specific auth "key" options', async () => { | ||
projectConfig['auth-source'] = { | ||
options: { | ||
key: 'x-auth', | ||
}, | ||
}; | ||
stubFetchCannerUser(mockCannerUserResponse); | ||
const builder = new VulcanBuilder(projectConfig); | ||
await builder.build(); | ||
server = new VulcanServer(projectConfig); | ||
const httpServer = (await server.start())['http']; | ||
|
||
const agent = supertest(httpServer); | ||
const auth = Buffer.from( | ||
JSON.stringify({ | ||
Authorization: `Canner-PAT ${Buffer.from(mockToken).toString( | ||
'base64' | ||
)}`, | ||
}) | ||
).toString('base64'); | ||
const result = await agent.get(`/auth/user-profile?x-auth=${auth}`); | ||
|
||
expect(result.body).toEqual(expectedUserProfile); | ||
}, 10000); | ||
|
||
it('Example 3-4: set Authorization in json payload specific auth "x-key" options', async () => { | ||
projectConfig['auth-source'] = { | ||
options: { | ||
key: 'x-auth', | ||
in: 'payload', | ||
}, | ||
}; | ||
stubFetchCannerUser(mockCannerUserResponse); | ||
const builder = new VulcanBuilder(projectConfig); | ||
await builder.build(); | ||
server = new VulcanServer(projectConfig); | ||
const httpServer = (await server.start())['http']; | ||
|
||
const auth = Buffer.from( | ||
JSON.stringify({ | ||
Authorization: `Canner-PAT ${Buffer.from(mockToken).toString( | ||
'base64' | ||
)}`, | ||
}) | ||
).toString('base64'); | ||
|
||
const agent = supertest(httpServer); | ||
|
||
const result = await agent | ||
.get('/auth/user-profile') | ||
.send({ | ||
['x-auth']: auth, | ||
}) | ||
.set('Accept', 'application/json'); | ||
|
||
expect(result.body).toEqual(expectedUserProfile); | ||
}, 10000); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Suggest adding the comment to describe what reason make here should use the prototype to stub the method because it's an unregular way to stub the method of a class.
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, here you could also have another way to prevent using the
prototype
to stub private method, the solution ( Depends on you, two way both work, just need to add the comment if you use the unregular solution):CannerPATAuthenticator
class to the foldercanner/
fetchCannerUser
private method to a function expression object, and put it in ahelpers.ts
file, move thehelpers.ts
under thecanner/
, it means thehelpers.ts
only used in the related file under thecanner/
.fetchCannerUser
function fromhelpers
and call it in theCannerPATAuthenticator
, then you could stub the function in the test cases, and prevent stubbing the private method.PS: if your private method could be a common method and make multiple places to use, then you could create a folder
utils/
under thelibs/
and make the private common method a function and put it in a file under theutils/
, and write the test case for the function under thetest
You could refer to the flattenElements.ts and https://github.com/Canner/vulcan-sql/blob/2b5e282aa047cfaff1c80471e2e4bca119bde769/packages/core/src/lib/utils/normalizedStringValue.ts under the
core
package, we also write the tests case for them under thetest/utils
:)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.
Thanks for the solution!
I'll add some comments for now, and I think we can encapsulate how to make requests to Canner and move it to
utils
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.
Do you forget to add the
// TODO: refactor to avoid stubbing private method
?