fix(medusa): Use correct auth middleware in GET /store/auth (#2687)

* use correct authentication middleware

* remove guard from get-session since it's guarded by middleware doing the same check

* Add integration tests

* Create lazy-swans-agree.md

Co-authored-by: olivermrbl <oliver@mrbltech.com>
Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Philip Korsholm
2022-11-29 15:46:55 +01:00
committed by GitHub
co-authored by olivermrbl Oliver Windall Juhl
parent e18b59de66
commit 70a8d3450f
7 changed files with 148 additions and 58 deletions
@@ -4,6 +4,8 @@ const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const { Customer } = require("@medusajs/medusa")
jest.setTimeout(30000)
describe("/store/auth", () => {
@@ -57,4 +59,73 @@ describe("/store/auth", () => {
email: "test@testesen.dk",
})
})
describe("Store session management", () => {
beforeEach(async () => {
await dbConnection.manager.insert(Customer, {
id: "test_customer",
first_name: "oli",
last_name: "test",
email: "oli@test.dk",
password_hash:
"c2NyeXB0AAEAAAABAAAAAVMdaddoGjwU1TafDLLlBKnOTQga7P2dbrfgf3fB+rCD/cJOMuGzAvRdKutbYkVpuJWTU39P7OpuWNkUVoEETOVLMJafbI8qs8Qx/7jMQXkN", // password matching "test"
has_account: true,
})
await dbConnection.manager.insert(Customer, {
id: "test_customer_no_account",
first_name: "oli",
last_name: "test",
email: "oli+1@test.dk",
has_account: false,
})
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("successfully gets session", async () => {
const api = useApi()
const authResponse = await api.post("/store/auth", {
email: "oli@test.dk",
password: "test",
})
const [authCookie] = authResponse.headers["set-cookie"][0].split(";")
const me = await api.get("/store/auth", {
headers: {
Cookie: authCookie,
},
})
expect(me.status).toEqual(200)
})
it("throws 401 on customer without account", async () => {
expect.assertions(1)
const api = useApi()
try {
const authResponse = await api.post("/store/auth", {
email: "oli+1@test.dk",
password: "test",
})
const [authCookie] = authResponse.headers["set-cookie"][0].split(";")
await api.get("/store/auth", {
headers: {
Cookie: authCookie,
},
})
} catch (err) {
expect(err.response.status).toEqual(401)
}
})
})
})