fix(auth-emailpass): better handle identity with same email error (#13537)
* fix(auth-emailpass): better handle identity with same email error * add test * Create blue-laws-argue.md * check for empty object * trueeee * nit * flip condition
This commit is contained in:
+74
-1
@@ -1,6 +1,7 @@
|
||||
import { MedusaError } from "@medusajs/framework/utils"
|
||||
import Scrypt from "scrypt-kdf"
|
||||
import { EmailPassAuthService } from "../../src/services/emailpass"
|
||||
|
||||
jest.setTimeout(100000)
|
||||
|
||||
describe("Email password auth provider", () => {
|
||||
@@ -152,11 +153,83 @@ describe("Email password auth provider", () => {
|
||||
)
|
||||
})
|
||||
|
||||
it("throw if auth identity with email already exists", async () => {
|
||||
it("updates identity if it exists but doesnt have app_metadata", async () => {
|
||||
const authServiceSpies = {
|
||||
retrieve: jest.fn().mockImplementation(() => {
|
||||
return { success: true }
|
||||
}),
|
||||
update: jest.fn().mockImplementation(() => {
|
||||
return {
|
||||
provider_identities: [
|
||||
{
|
||||
entity_id: "test@admin.com",
|
||||
provider: "emailpass",
|
||||
provider_metadata: {
|
||||
password: "somehash",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
||||
const resp = await emailpassService.register(
|
||||
{ body: { email: "test@admin.com", password: "test" } },
|
||||
authServiceSpies
|
||||
)
|
||||
|
||||
expect(authServiceSpies.retrieve).toHaveBeenCalled()
|
||||
expect(authServiceSpies.update).toHaveBeenCalled()
|
||||
|
||||
expect(resp.authIdentity?.provider_identities?.[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
entity_id: "test@admin.com",
|
||||
provider_metadata: {},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("updates identity if it exists but app_metadata is empty", async () => {
|
||||
const authServiceSpies = {
|
||||
retrieve: jest.fn().mockImplementation(() => {
|
||||
return { success: true, app_metadata: {} }
|
||||
}),
|
||||
update: jest.fn().mockImplementation(() => {
|
||||
return {
|
||||
provider_identities: [
|
||||
{
|
||||
entity_id: "test@admin.com",
|
||||
provider: "emailpass",
|
||||
provider_metadata: {
|
||||
password: "somehash",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
||||
const resp = await emailpassService.register(
|
||||
{ body: { email: "test@admin.com", password: "test" } },
|
||||
authServiceSpies
|
||||
)
|
||||
|
||||
expect(authServiceSpies.retrieve).toHaveBeenCalled()
|
||||
expect(authServiceSpies.update).toHaveBeenCalled()
|
||||
|
||||
expect(resp.authIdentity?.provider_identities?.[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
entity_id: "test@admin.com",
|
||||
provider_metadata: {},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("throw if auth identity with email already exists and has app_metadata", async () => {
|
||||
const authServiceSpies = {
|
||||
retrieve: jest.fn().mockImplementation(() => {
|
||||
return { success: true, app_metadata: {"user_id": "some-id"} }
|
||||
}),
|
||||
create: jest.fn().mockImplementation(() => {
|
||||
return {
|
||||
provider_identities: [
|
||||
|
||||
Reference in New Issue
Block a user