Feat/note on order (#399)
* added NoteService and related endpoints && tests * removed snapshots * corrected error in service * removed snapshot * added the ability to note down author using a string * updated model for note * refactored to access logged in user * added other user id option * removed snapshot * updated according to feedback * removed snapshots * reintroduced snapshots * updated to snake case * removed try catch from use-db
This commit is contained in:
committed by
GitHub
parent
a82332da3e
commit
897ccf475a
268
integration-tests/api/__tests__/admin/note.js
Normal file
268
integration-tests/api/__tests__/admin/note.js
Normal file
@@ -0,0 +1,268 @@
|
||||
const path = require("path")
|
||||
const { Note } = require("@medusajs/medusa")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
const adminSeeder = require("../../helpers/admin-seeder")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
const note = {
|
||||
id: "note1",
|
||||
value: "note text",
|
||||
resource_id: "resource1",
|
||||
resource_type: "type",
|
||||
author: { id: "admin_user" },
|
||||
}
|
||||
|
||||
describe("/admin/notes", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
describe("GET /admin/notes/:id", () => {
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await manager.insert(Note, note)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("properly retrieves note", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get("/admin/notes/note1", {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
|
||||
expect(response.data).toMatchObject({
|
||||
note: {
|
||||
id: "note1",
|
||||
resource_id: "resource1",
|
||||
resource_type: "type",
|
||||
value: "note text",
|
||||
author: { id: "admin_user" },
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/notes", () => {
|
||||
beforeEach(async () => {
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("creates a note", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.post(
|
||||
"/admin/notes",
|
||||
{
|
||||
resource_id: "resource-id",
|
||||
resource_type: "resource-type",
|
||||
value: "my note",
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.data).toMatchObject({
|
||||
note: {
|
||||
id: expect.stringMatching(/^note_*/),
|
||||
resource_id: "resource-id",
|
||||
resource_type: "resource-type",
|
||||
value: "my note",
|
||||
author_id: "admin_user",
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /admin/notes", () => {
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await manager.insert(Note, { ...note, id: "note1" })
|
||||
await manager.insert(Note, { ...note, id: "note2" })
|
||||
await manager.insert(Note, {
|
||||
...note,
|
||||
id: "note3",
|
||||
resource_id: "resource2",
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("lists notes only related to wanted resource", async () => {
|
||||
const api = useApi()
|
||||
const response = await api
|
||||
.get("/admin/notes?resource_id=resource1", {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.data.notes.length).toEqual(2)
|
||||
expect(response.data).toMatchObject({
|
||||
notes: [
|
||||
{
|
||||
id: "note1",
|
||||
resource_id: "resource1",
|
||||
resource_type: "type",
|
||||
value: "note text",
|
||||
author: { id: "admin_user" },
|
||||
},
|
||||
{
|
||||
id: "note2",
|
||||
resource_id: "resource1",
|
||||
resource_type: "type",
|
||||
value: "note text",
|
||||
author: { id: "admin_user" },
|
||||
},
|
||||
],
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/notes/:id", () => {
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await manager.insert(Note, note)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("updates the content of the note", async () => {
|
||||
const api = useApi()
|
||||
|
||||
await api
|
||||
.post(
|
||||
"/admin/notes/note1",
|
||||
{ value: "new text" },
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
const response = await api
|
||||
.get("/admin/notes/note1", {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.data.note.value).toEqual("new text")
|
||||
})
|
||||
})
|
||||
|
||||
describe("DELETE /admin/notes/:id", () => {
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager
|
||||
try {
|
||||
await adminSeeder(dbConnection)
|
||||
|
||||
await manager.insert(Note, note)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("deletes the wanted note", async () => {
|
||||
const api = useApi()
|
||||
|
||||
await api
|
||||
.delete("/admin/notes/note1", {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
let error
|
||||
await api
|
||||
.get("/admin/notes/note1", {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => (error = err))
|
||||
|
||||
expect(error.response.status).toEqual(404)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,11 +1,11 @@
|
||||
const Scrypt = require("scrypt-kdf");
|
||||
const { User } = require("@medusajs/medusa");
|
||||
const Scrypt = require("scrypt-kdf")
|
||||
const { User } = require("@medusajs/medusa")
|
||||
|
||||
module.exports = async (connection, data = {}) => {
|
||||
const manager = connection.manager;
|
||||
const manager = connection.manager
|
||||
|
||||
const buf = await Scrypt.kdf("secret_password", { logN: 1, r: 1, p: 1 });
|
||||
const password_hash = buf.toString("base64");
|
||||
const buf = await Scrypt.kdf("secret_password", { logN: 1, r: 1, p: 1 })
|
||||
const password_hash = buf.toString("base64")
|
||||
|
||||
await manager.insert(User, {
|
||||
id: "admin_user",
|
||||
@@ -13,5 +13,5 @@ module.exports = async (connection, data = {}) => {
|
||||
api_token: "test_token",
|
||||
password_hash,
|
||||
...data,
|
||||
});
|
||||
};
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user