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: supposrt facebook authentication strategy (adrien2p#19)
- Loading branch information
Showing
17 changed files
with
761 additions
and
164 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 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
...s/medusa-plugin-auth/src/auth-strategies/facebook/__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('Facebook admin strategy verify callback', function () { | ||
const existsEmail = 'exists@test.fr'; | ||
|
||
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
...s/medusa-plugin-auth/src/auth-strategies/facebook/__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('Facebook store strategy verify callback', function () { | ||
const existsEmail = 'exists@test.fr'; | ||
const existsEmailWithMeta = 'exist2s@test.fr'; | ||
|
||
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.