forked from adrien2p/medusa-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow to pass a custom verify callback for google strategy (adr…
…ien2p#18) * feat: Allow to pass a custon verify callback for google strategy * chore: Rename pipeline name for the auth plugin * style: cleanup
- Loading branch information
Showing
8 changed files
with
458 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: medusa-plugin-auth | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
unit-test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-verion: [16.x] | ||
medusajs-version: [1.3.x, 1.4.x, 1.5.x] | ||
steps: | ||
- name: Cancel Previous Runs | ||
uses: styfle/[email protected] | ||
with: | ||
access_token: ${{ github.token }} | ||
|
||
- name: Checkout | ||
uses: actions/[email protected] | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Setup Node.js environment | ||
uses: actions/[email protected] | ||
with: | ||
node-version: ${{ matrix.node-verion }} | ||
|
||
- name: 'yarn install' | ||
working-directory: ./packages/medusa-plugin-auth | ||
run: yarn | ||
|
||
- name: 'run unit tests' | ||
working-directory: ./packages/medusa-plugin-auth | ||
run: yarn run test:ci | ||
env: | ||
MEDUSAJS_VERSION: ${{ matrix.medusajs-version }} |
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
70 changes: 70 additions & 0 deletions
70
...ges/medusa-plugin-auth/src/auth-strategies/google/__tests__/admin/verify-callback.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,70 @@ | ||
import { verifyAdminCallback } from '../../admin'; | ||
import { MedusaContainer } from '@medusajs/medusa/dist/types/global'; | ||
|
||
describe('Google admin strategy verify callback', function () { | ||
const existsEmail = '[email protected]'; | ||
|
||
let container: MedusaContainer; | ||
let req: Request; | ||
let accessToken: string; | ||
let refreshToken: string; | ||
let profile: { emails: { value: string }[]; name?: { givenName?: string; familyName?: string } }; | ||
|
||
beforeEach(() => { | ||
profile = { | ||
emails: [{ value: existsEmail }], | ||
}; | ||
|
||
container = { | ||
resolve: (name: string) => { | ||
const container_ = { | ||
userService: { | ||
retrieveByEmail: jest.fn().mockImplementation(async (email: string) => { | ||
if (email === existsEmail) { | ||
return { | ||
id: 'test', | ||
}; | ||
} | ||
|
||
return; | ||
}), | ||
}, | ||
}; | ||
|
||
return container_[name]; | ||
}, | ||
} as MedusaContainer; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should success', async () => { | ||
profile = { | ||
emails: [{ value: existsEmail }], | ||
}; | ||
|
||
const done = (err, data) => { | ||
expect(data).toEqual( | ||
expect.objectContaining({ | ||
id: 'test', | ||
}) | ||
); | ||
}; | ||
|
||
await verifyAdminCallback(container, req, accessToken, refreshToken, profile, done); | ||
}); | ||
|
||
it('should fail if the user does not exists', async () => { | ||
profile = { | ||
emails: [{ value: 'fake' }], | ||
}; | ||
|
||
const done = (err) => { | ||
expect(err).toEqual(new Error(`Unable to authenticate the user with the email fake`)); | ||
}; | ||
|
||
await verifyAdminCallback(container, req, accessToken, refreshToken, profile, done); | ||
}); | ||
}); |
112 changes: 112 additions & 0 deletions
112
...ges/medusa-plugin-auth/src/auth-strategies/google/__tests__/store/verify-callback.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,112 @@ | ||
import { verifyStoreCallback } from '../../store'; | ||
import { MedusaContainer } from '@medusajs/medusa/dist/types/global'; | ||
import { ENTITY_METADATA_KEY } from '../../index'; | ||
|
||
describe('Google store strategy verify callback', function () { | ||
const existsEmail = '[email protected]'; | ||
const existsEmailWithMeta = '[email protected]'; | ||
|
||
let container: MedusaContainer; | ||
let req: Request; | ||
let accessToken: string; | ||
let refreshToken: string; | ||
let profile: { emails: { value: string }[]; name?: { givenName?: string; familyName?: string } }; | ||
|
||
beforeEach(() => { | ||
profile = { | ||
emails: [{ value: existsEmail }], | ||
}; | ||
|
||
container = { | ||
resolve: <T>(name: string): T => { | ||
const container_ = { | ||
manager: { | ||
transaction: function (cb) { | ||
cb(); | ||
}, | ||
}, | ||
customerService: { | ||
withTransaction: function () { | ||
return this; | ||
}, | ||
create: jest.fn().mockImplementation(async () => { | ||
return { id: 'test' }; | ||
}), | ||
retrieveByEmail: jest.fn().mockImplementation(async (email: string) => { | ||
if (email === existsEmail) { | ||
return { | ||
id: 'test', | ||
}; | ||
} | ||
|
||
if (email === existsEmailWithMeta) { | ||
return { | ||
id: 'test2', | ||
metadata: { | ||
[ENTITY_METADATA_KEY]: true, | ||
}, | ||
}; | ||
} | ||
|
||
return; | ||
}), | ||
}, | ||
}; | ||
|
||
return container_[name]; | ||
}, | ||
} as MedusaContainer; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should succeed', async () => { | ||
profile = { | ||
emails: [{ value: existsEmailWithMeta }], | ||
}; | ||
|
||
const done = (err, data) => { | ||
expect(data).toEqual( | ||
expect.objectContaining({ | ||
id: 'test2', | ||
}) | ||
); | ||
}; | ||
|
||
await verifyStoreCallback(container, req, accessToken, refreshToken, profile, done); | ||
}); | ||
|
||
it('should fail when the customer exists without the metadata', async () => { | ||
profile = { | ||
emails: [{ value: existsEmail }], | ||
}; | ||
|
||
const done = (err) => { | ||
expect(err).toEqual(new Error(`Customer with email ${existsEmail} already exists`)); | ||
}; | ||
|
||
await verifyStoreCallback(container, req, accessToken, refreshToken, profile, done); | ||
}); | ||
|
||
it('should succeed and create a new customer if it has not been found', async () => { | ||
profile = { | ||
emails: [{ value: 'fake' }], | ||
name: { | ||
givenName: 'test', | ||
familyName: 'test', | ||
}, | ||
}; | ||
|
||
const done = (err, data) => { | ||
expect(data).toEqual( | ||
expect.objectContaining({ | ||
id: 'test', | ||
}) | ||
); | ||
}; | ||
|
||
await verifyStoreCallback(container, req, accessToken, refreshToken, profile, done); | ||
}); | ||
}); |
Oops, something went wrong.