diff --git a/src/api/acts.ts b/src/api/acts.ts index 476e29a..2a8139b 100644 --- a/src/api/acts.ts +++ b/src/api/acts.ts @@ -1,3 +1,4 @@ export * from "./acts/login.js"; export * from "./acts/client.js" -export * from "./acts/admin.js" \ No newline at end of file +export * from "./acts/admin.js" +export * from "./acts/rooms.js" \ No newline at end of file diff --git a/src/api/acts/client.ts b/src/api/acts/client.ts index ca9aabf..476ce1d 100644 --- a/src/api/acts/client.ts +++ b/src/api/acts/client.ts @@ -99,34 +99,38 @@ export const createRoom = { server: "string", // Unused/Ignored at the Moment title: "string-255", description: "string-255", - isPublic: "number", + public: "number", icon: "string-255" }, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { if (!await canCreateRoom(client.accID)) return void aws("error", "limit"); - let req = await insert( - rooms.name, - rooms.owner, - rooms.public, - rooms.title, - rooms.description, - rooms.icon - ).add( - data.room, - client.accID, - data.isPublic, - data.title, - data.description, - data.icon - ).query(db); - - if (req.affectedRows > 0) { - await insert(roomMembers.roomID, roomMembers.name, roomMembers.server, roomMembers.admin) - .add(req.insertId, client.name, "local", true) - .query(db); - aws("ok", ""); - } else { + try { + let req = await insert( + rooms.name, + rooms.owner, + rooms.public, + rooms.title, + rooms.description, + rooms.icon + ).add( + data.room, + client.accID, + data.public, + data.title, + data.description, + data.icon + ).query(db); + if (req.affectedRows > 0) { + await insert(roomMembers.roomID, roomMembers.name, roomMembers.server, roomMembers.admin) + .add(req.insertId, client.name, "local", true) + .query(db); + aws("ok", ""); + } else { + aws("error", "existence"); + } + } catch (error) { + client.suspect(); aws("error", "existence"); } } @@ -158,7 +162,6 @@ export const listPublicRooms = { right: PERMISSIONS.CAN_USE_API, data: {}, func: async (client: Client, data: any, aws: (code: string, data: any) => void) => { - let req = await select([ rooms.name, rooms.public, diff --git a/src/api/acts/rooms.ts b/src/api/acts/rooms.ts index 4d78b31..dcc37e6 100644 --- a/src/api/acts/rooms.ts +++ b/src/api/acts/rooms.ts @@ -1,4 +1,4 @@ -import { alias, and, eq, exists, innerJoinOn, insert, le, minus, naturalJoin, not, or, remove, select, update } from "dblang"; +import { alias, and, eq, exists, innerJoinOn, innerJoinUsing, insert, le, minus, naturalJoin, not, or, remove, select, update } from "dblang"; import { ROOM_RIGHTS } from "../../server/permissions.js"; import { accounts, db, roomMembers, roomOTAs, rooms } from "../../sys/db.js"; import { uts } from "../../sys/tools.js"; @@ -22,7 +22,7 @@ export const listLocalRooms: Act = { rooms.title, rooms.description, rooms.icon - ], naturalJoin(rooms, roomMembers)) + ], innerJoinUsing(rooms, roomMembers, rooms.roomID, roomMembers.roomID)) .where(and( eq(roomMembers.name, client.name), eq(roomMembers.server, client.state == STATE.client ? "local" : client.server.tag) diff --git a/test.juml b/test.juml index 52feed8..0a64008 100644 --- a/test.juml +++ b/test.juml @@ -22,6 +22,6 @@ database=outbag [Settings] maxUsers=2 -defaultMaxRooms=3 +defaultMaxRooms=2 defaultMaxRoomSize=10 defaultMaxUsersPerRoom=2 diff --git a/tests/post.js b/tests/post.js index c8e9700..6875d75 100644 --- a/tests/post.js +++ b/tests/post.js @@ -31,7 +31,10 @@ function shallowEqual(object1, object2) { return false; } for (let key of keys1) { - if (object1[key] != null && object1[key] != object2[key]) { + if (typeof object1[key] == "object" && object1[key] != null) { + if(typeof object2[key] != "object") return false; + if(!shallowEqual(object1[key], object2[key])) return false; + } else if (object1[key] != null && object1[key] != object2[key]) { return false; } } diff --git a/tests/tests/post.js b/tests/tests/post.js index f3fc8df..6faf8b5 100644 --- a/tests/tests/post.js +++ b/tests/tests/post.js @@ -6,6 +6,10 @@ let name2 = "testUser2"; let name3 = "testUser3"; let accountKey = "123456789"; +let room1 = "r1"; +let room2 = "r2"; +let room3 = "r3"; + let { privateKey, publicKey } = await generateSigningKey(); const list = [ @@ -96,7 +100,7 @@ const list = [ rights: PERMISSIONS.ALL, name: name1, viewable: true, - maxRooms: 3, + maxRooms: 2, maxRoomSize: 10, maxUsersPerRoom: 2, }); @@ -106,7 +110,7 @@ const list = [ rights: PERMISSIONS.DEFAULT, name: name2, viewable: true, - maxRooms: 3, + maxRooms: 2, maxRoomSize: 10, maxUsersPerRoom: 2, }); @@ -134,6 +138,104 @@ const list = [ server: "localhost:7224", accountKey: accountKey + "lol" }, "error", "auth"); + }], ["room Owner", async (req) => { + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "createRoom", { + room: room1, + server: "localhost:7224", + title: "Test Room 1", + description: "some desc", + public: 0, + icon: "shopping" + }, "ok", ""); + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "createRoom", { + room: room1, + server: "localhost:7224", + title: "Test Room 1", + description: "some desc", + public: 0, + icon: "shopping" + }, "error", "existence"); + + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "createRoom", { + room: room2, + server: "localhost:7224", + title: "Test Room 2", + description: "some desc 2", + public: 1, + icon: "" + }, "ok", ""); + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "createRoom", { + room: room3, + server: "localhost:7224", + title: "Test Room 2", + description: "some desc 2", + public: 1, + icon: "" + }, "error", "limit"); + + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "listLocalRooms", {}, "ok", [ + { + name: room1, + owner: name1, + rights: 0b11111, + title: "Test Room 1", + description: "some desc", + public: 0, + icon: "shopping" + }, { + name: room2, + owner: name1, + rights: 0b11111, + title: "Test Room 2", + description: "some desc 2", + public: 1, + icon: "" + } + ]); + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "deleteRoom", { + room: room2 + "lol", + server: "localhost:7224" + }, "error", "existence"); + + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "deleteRoom", { + room: room2, + server: "localhost:7224" + }, "ok", ""); + + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "deleteRoom", { + room: room2, + server: "localhost:7224" + }, "error", "existence"); + + await req({ + "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` + }, "listLocalRooms", {}, "ok", [ + { + name: room1, + owner: name1, + rights: 0b11111, + title: "Test Room 1", + description: "some desc", + public: 0, + icon: "shopping" + } + ]); }] ]; diff --git a/tests/tests/ws.js b/tests/tests/ws.js index f93419b..78a8f5e 100644 --- a/tests/tests/ws.js +++ b/tests/tests/ws.js @@ -6,6 +6,10 @@ let name2 = "testUser2"; let name3 = "testUser3"; let accountKey = "123456789"; +let room1 = "r1"; +let room2 = "r2"; +let room3 = "r3"; + let { privateKey, publicKey } = await generateSigningKey(); const list = [ @@ -86,7 +90,7 @@ const list = [ rights: PERMISSIONS.ALL, name: name1, viewable: true, - maxRooms: 3, + maxRooms: 2, maxRoomSize: 10, maxUsersPerRoom: 2, }); @@ -100,7 +104,7 @@ const list = [ rights: PERMISSIONS.DEFAULT, name: name2, viewable: true, - maxRooms: 3, + maxRooms: 2, maxRoomSize: 10, maxUsersPerRoom: 2, }); @@ -132,6 +136,91 @@ const list = [ server: "localhost:7224", accountKey: accountKey + "lol" }, "error", "auth"); + }], ["room Owner", async (handler, req, newHandler) => { + await req(handler, "signin", { + name: name1, + server: "localhost:7224", + accountKey + }, "ok", ""); + await req(handler, "createRoom", { + room: room1, + server: "localhost:7224", + title: "Test Room 1", + description: "some desc", + public: 0, + icon: "shopping" + }, "ok", ""); + await req(handler, "createRoom", { + room: room1, + server: "localhost:7224", + title: "Test Room 1", + description: "some desc", + public: 0, + icon: "shopping" + }, "error", "existence"); + + await req(handler, "createRoom", { + room: room2, + server: "localhost:7224", + title: "Test Room 2", + description: "some desc 2", + public: 1, + icon: "" + }, "ok", ""); + + await req(handler, "createRoom", { + room: room3, + server: "localhost:7224", + title: "Test Room 2", + description: "some desc 2", + public: 1, + icon: "" + }, "error", "limit"); + + await req(handler, "listLocalRooms", {}, "ok", [ + { + name: room1, + owner: name1, + rights: 0b11111, + title: "Test Room 1", + description: "some desc", + public: 0, + icon: "shopping" + }, { + name: room2, + owner: name1, + rights: 0b11111, + title: "Test Room 2", + description: "some desc 2", + public: 1, + icon: "" + } + ]); + await req(handler, "deleteRoom", { + room: room2 + "lol", + server: "localhost:7224" + }, "error", "existence"); + await req(handler, "deleteRoom", { + room: room2, + server: "localhost:7224" + }, "ok", ""); + await req(handler, "deleteRoom", { + room: room2, + server: "localhost:7224" + }, "error", "existence"); + await req(handler, "listLocalRooms", {}, "ok", [ + { + name: room1, + owner: name1, + rights: 0b11111, + title: "Test Room 1", + description: "some desc", + public: 0, + icon: "shopping" + } + ]); + + }] ]; diff --git a/tests/ws.js b/tests/ws.js index da62c28..bdaa263 100644 --- a/tests/ws.js +++ b/tests/ws.js @@ -90,7 +90,10 @@ function shallowEqual(object1, object2) { return false; } for (let key of keys1) { - if (object1[key] != null && object1[key] != object2[key]) { + if (typeof object1[key] == "object" && object1[key] != null) { + if(typeof object2[key] != "object") return false; + if(!shallowEqual(object1[key], object2[key])) return false; + } else if (object1[key] != null && object1[key] != object2[key]) { return false; } } @@ -104,7 +107,7 @@ async function wsTester(url) { console.log("Testing Act:", act, "with Data:", data); let resp = await conn.req(act, data); if (resp.state != expState) { - console.error(`Expected state: '${expState}', but got: '${resp.state}'`); + console.error(`Expected state: '${expState}', but got: '${resp.state}' (Data: ${typeof resp.data == "object" ? JSON.stringify(resp.data) : resp.data})`); kill(); process.exit(1); }