server OTA tests + fixes

This commit is contained in:
jusax23 2023-03-30 17:30:43 +02:00
parent ee6b9f0050
commit e4054c201c
Signed by: jusax23
GPG key ID: 499E2AA870C1CD41
4 changed files with 66 additions and 13 deletions

View file

@ -1,4 +1,4 @@
import { eq, insert, le, or, remove, select, update } from "dblang"; import { and, eq, geq, insert, le, leq, or, remove, select, update } from "dblang";
import { PERMISSIONS } from "../../server/permissions.js"; import { PERMISSIONS } from "../../server/permissions.js";
import { sha256 } from "../../sys/crypto.js"; import { sha256 } from "../../sys/crypto.js";
import { accounts, db, signupOTA } from "../../sys/db.js"; import { accounts, db, signupOTA } from "../../sys/db.js";
@ -116,7 +116,10 @@ export const getOTAs: Act = {
await remove(signupOTA) await remove(signupOTA)
.where(or( .where(or(
eq(signupOTA.usesLeft, 0), eq(signupOTA.usesLeft, 0),
le(signupOTA.expires, uts()) and(
le(signupOTA.expires, uts()),
geq(signupOTA.expires, 0)
)
)) ))
.query(db); .query(db);
let req = await select([ let req = await select([
@ -127,7 +130,7 @@ export const getOTAs: Act = {
.query(db); .query(db);
aws("ok", req.map(d => ({ aws("ok", req.map(d => ({
token: d[signupOTA.token], token: d[signupOTA.token],
expires: d[signupOTA.expires], expires: Number(d[signupOTA.expires]),
usesLeft: d[signupOTA.usesLeft] usesLeft: d[signupOTA.usesLeft]
}))); })));
} }

View file

@ -400,7 +400,7 @@ export const getItems: Act = {
.where(eq(listItems.roomID, roomID)) .where(eq(listItems.roomID, roomID))
.query(db); .query(db);
let out = req.map(d => { let out = req.map(d => {
let listItemID = d[listItems.listItemID]; let listItemID = Number(d[listItems.listItemID]);
let state = d[listItems.state]; let state = d[listItems.state];
let title = d[listItems.title]; let title = d[listItems.title];
let description = d[listItems.description]; let description = d[listItems.description];

View file

@ -1,4 +1,4 @@
import { alias, and, eq, exists, geq, innerJoinOn, innerJoinUsing, insert, le, minus, naturalJoin, not, or, remove, select, update } from "dblang"; import { alias, and, eq, exists, ge, geq, innerJoinOn, innerJoinUsing, insert, le, minus, naturalJoin, not, or, remove, select, update } from "dblang";
import { checkSelfTag, outbagURLfromTag } from "../../server/outbagURL.js"; import { checkSelfTag, outbagURLfromTag } from "../../server/outbagURL.js";
import { ROOM_RIGHTS } from "../../server/permissions.js"; import { ROOM_RIGHTS } from "../../server/permissions.js";
import { accounts, db, remoteRooms, roomMembers, roomOTAs, rooms } from "../../sys/db.js"; import { accounts, db, remoteRooms, roomMembers, roomOTAs, rooms } from "../../sys/db.js";
@ -243,9 +243,12 @@ export const joinRoom: Act = {
if (await isRoomFull(roomID)) return void aws("error", "limit"); if (await isRoomFull(roomID)) return void aws("error", "limit");
// TODO: Make Transaktion when possible // TODO: Make Transaktion when possible
await remove(roomOTAs) await remove(roomOTAs)
.where(and( .where(or(
eq(roomOTAs.usesLeft, 0),
and(
le(roomOTAs.expires, uts()), le(roomOTAs.expires, uts()),
geq(roomOTAs.expires, 0) geq(roomOTAs.expires, 0)
)
)) ))
.query(db); .query(db);
let req = await update(roomOTAs) let req = await update(roomOTAs)
@ -355,13 +358,22 @@ export const getRoomOTAs: Act = {
} }
let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA); let roomID = await client.isRoomAdmin(data.room, ROOM_RIGHTS.OTA);
if (roomID == -1) return void aws("error", "roomAdmin"); if (roomID == -1) return void aws("error", "roomAdmin");
await remove(roomOTAs)
.where(or(
eq(roomOTAs.usesLeft, 0),
and(
le(roomOTAs.expires, uts()),
geq(roomOTAs.expires, 0)
)
))
.query(db);
let req = await select([roomOTAs.token, roomOTAs.name, roomOTAs.expires, roomOTAs.usesLeft], roomOTAs) let req = await select([roomOTAs.token, roomOTAs.name, roomOTAs.expires, roomOTAs.usesLeft], roomOTAs)
.where(eq(roomOTAs.roomID, roomID)) .where(eq(roomOTAs.roomID, roomID))
.query(db); .query(db);
aws("ok", req.map(d => ({ aws("ok", req.map(d => ({
token: d[roomOTAs.token], token: d[roomOTAs.token],
name: d[roomOTAs.name], name: d[roomOTAs.name],
expires: d[roomOTAs.expires], expires: Number(d[roomOTAs.expires]),
usesLeft: d[roomOTAs.usesLeft], usesLeft: d[roomOTAs.usesLeft],
}))); })));
} }

View file

@ -1,5 +1,6 @@
import { generateSigningKey, sign } from '../../dist/sys/crypto.js'; import { generateSigningKey, sign } from '../../dist/sys/crypto.js';
import { PERMISSIONS } from '../../dist/server/permissions.js'; import { PERMISSIONS } from '../../dist/server/permissions.js';
import { uts, wait } from '../../dist/sys/tools.js';
let name1 = "testUser1"; let name1 = "testUser1";
let name2 = "testUser2"; let name2 = "testUser2";
@ -191,6 +192,43 @@ const list = [
maxUsersPerRoom: 2 maxUsersPerRoom: 2
} }
]); ]);
await req({
"authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}`
}, "addOTA", {
token: "12345678",
expires: uts() + 1,
usesLeft: -1
}, "ok", "");
await req({
"authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}`
}, "getOTAs", {}, "ok", [
{
token: "12345678",
expires: null,
usesLeft: -1
}
]);
await req({
"authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}`
}, "addOTA", {
token: "abc",
expires: -1,
usesLeft: -1
}, "ok", "");
await wait(2000);
await req({
"authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}`
}, "getOTAs", {}, "ok", [{
token: "abc",
expires: -1,
usesLeft: -1
}]);
await req({
"authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}`
}, "deleteOTA", { token: "abc" }, "ok", "");
await req({
"authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}`
}, "getOTAs", {}, "ok", []);
}], ["room Owner", async (req) => { }], ["room Owner", async (req) => {
await req({ await req({
"authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}` "authorization": `Digest name=${name1} server=localhost:7224 accountKey=${accountKey}`