feat(medusa-react,medusa,utils): add users/me endpoint + add missing specs (#6441)
**what:** - adds /me endpoint - adds fixes to routes - adds specs for auth endpoint - updates dotenv package versions Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { IAuthModuleService, ICustomerModuleService } from "@medusajs/types"
|
||||
import path from "path"
|
||||
import Scrypt from "scrypt-kdf"
|
||||
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../../environment-helpers/use-api"
|
||||
import { getContainer } from "../../../../environment-helpers/use-container"
|
||||
import { initDb, useDb } from "../../../../environment-helpers/use-db"
|
||||
import adminSeeder from "../../../../helpers/admin-seeder"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
|
||||
describe("POST /auth/emailpass", () => {
|
||||
let dbConnection
|
||||
let appContainer
|
||||
let shutdownServer
|
||||
let customerModuleService: ICustomerModuleService
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
appContainer = getContainer()
|
||||
customerModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.CUSTOMER
|
||||
)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
const password = "supersecret"
|
||||
const email = "test@test.com"
|
||||
|
||||
it("should return a token on successful login", async () => {
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
const authService: IAuthModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
await authService.create({
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
scope: "admin",
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const response = await api
|
||||
.post(`/auth/admin/emailpass`, {
|
||||
email: email,
|
||||
password: password,
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data).toEqual(
|
||||
expect.objectContaining({
|
||||
token: expect.any(String),
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should throw an error upon incorrect password", async () => {
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
const authService: IAuthModuleService = appContainer.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
await authService.create({
|
||||
provider: "emailpass",
|
||||
entity_id: email,
|
||||
scope: "admin",
|
||||
provider_metadata: {
|
||||
password: passwordHash,
|
||||
},
|
||||
})
|
||||
|
||||
const api = useApi() as any
|
||||
const error = await api
|
||||
.post(`/auth/admin/emailpass`, {
|
||||
email: email,
|
||||
password: "incorrect-password",
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(401)
|
||||
expect(error.response.data).toEqual({
|
||||
type: "unauthorized",
|
||||
message: "Invalid email or password",
|
||||
})
|
||||
})
|
||||
|
||||
it.skip("should throw an error upon logging in with a non existing auth user", async () => {
|
||||
const passwordHash = (
|
||||
await Scrypt.kdf(password, { logN: 15, r: 8, p: 1 })
|
||||
).toString("base64")
|
||||
|
||||
const api = useApi() as any
|
||||
const error = await api
|
||||
.post(`/auth/admin/emailpass`, {
|
||||
email: "should-not-exist",
|
||||
password: "should-not-exist",
|
||||
})
|
||||
.catch((e) => e)
|
||||
|
||||
// TODO: This is creating a user with a scope of admin. The client consuming the auth service
|
||||
// should reject this if its not being created by an admin user
|
||||
expect(error.response.status).toEqual(401)
|
||||
expect(error.response.data).toEqual({
|
||||
type: "unauthorized",
|
||||
message: "Invalid email or password",
|
||||
})
|
||||
})
|
||||
})
|
||||
51
integration-tests/modules/__tests__/users/get-me.spec.ts
Normal file
51
integration-tests/modules/__tests__/users/get-me.spec.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { initDb, useDb } from "../../../environment-helpers/use-db"
|
||||
|
||||
import { AxiosInstance } from "axios"
|
||||
import path from "path"
|
||||
import { startBootstrapApp } from "../../../environment-helpers/bootstrap-app"
|
||||
import { useApi } from "../../../environment-helpers/use-api"
|
||||
import { createAdminUser } from "../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(50000)
|
||||
|
||||
const env = { MEDUSA_FF_MEDUSA_V2: true }
|
||||
const adminHeaders = {
|
||||
headers: { "x-medusa-access-token": "test_token" },
|
||||
}
|
||||
|
||||
describe("POST /admin/users/me", () => {
|
||||
let dbConnection
|
||||
let shutdownServer
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
dbConnection = await initDb({ cwd, env } as any)
|
||||
shutdownServer = await startBootstrapApp({ cwd, env })
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await createAdminUser(dbConnection, adminHeaders)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
await shutdownServer()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("gets the current user", async () => {
|
||||
const api = useApi()! as AxiosInstance
|
||||
|
||||
const response = await api.get(`/admin/users/me`, adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data).toEqual({
|
||||
user: expect.objectContaining({ id: "admin_user" }),
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -5,6 +5,7 @@ const DB_PASSWORD = process.env.DB_PASSWORD
|
||||
const DB_NAME = process.env.DB_TEMP_NAME
|
||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`
|
||||
process.env.POSTGRES_URL = DB_URL
|
||||
process.env.LOG_LEVEL = "error"
|
||||
|
||||
const enableMedusaV2 = process.env.MEDUSA_FF_MEDUSA_V2 == "true"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user