Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions __test__/dathost.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Unit tests for DatHost service (isDathostConfigured, releaseManagedServer with null).
* Does not require app or database.
*/
import { isDathostConfigured, releaseManagedServer } from "../src/services/dathost.js";

describe("DatHost service", () => {
it("isDathostConfigured returns false when user dathost is not configured", async () => {
await expect(isDathostConfigured(1)).resolves.toBe(false);
});

it("releaseManagedServer(null) resolves without throwing", async () => {
await expect(releaseManagedServer(null)).resolves.toBeUndefined();
});

it("releaseManagedServer(undefined) resolves without throwing", async () => {
await expect(releaseManagedServer(undefined)).resolves.toBeUndefined();
});
});
39 changes: 39 additions & 0 deletions __test__/matches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,45 @@ describe("Test the matches routes", () => {
return request.get('/matches/')
.expect(404);
});
it('Should reject use_dathost when server_id is also provided (400).', () => {
return request
.post("/matches/")
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.send([{
use_dathost: true,
server_id: 3,
team1_id: 4,
team2_id: 3,
max_maps: 1,
title: "Map {MAPNUMBER} of {MAXMAPS}",
veto_mappool: "de_dust2 de_mirage",
skip_veto: 0
}])
.expect(400)
.expect((result) => {
expect(result.body.message).toMatch(/use_dathost.*server_id/);
});
});
it('Should reject use_dathost when DatHost is not configured (503).', () => {
return request
.post("/matches/")
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.send([{
use_dathost: true,
team1_id: 4,
team2_id: 3,
max_maps: 1,
title: "Map {MAPNUMBER} of {MAXMAPS}",
veto_mappool: "de_dust2 de_mirage",
skip_veto: 0
}])
.expect(503)
.expect((result) => {
expect(result.body.message).toMatch(/DatHost|not configured/);
});
});
it('Should create a single match that is ready to be played with teams.', () => {
// Min required data.
let newMatchData = [
Expand Down
41 changes: 40 additions & 1 deletion __test__/queue.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { agent } from 'supertest';
import config from 'config';
import { jest } from '@jest/globals';
import app from '../app.js';
import { db } from '../src/services/db.js';
import { QueueService } from '../src/services/queue.js';
import {
QueueService,
QueueOwnerDatHostConfigMissingError
} from '../src/services/queue.js';
const request = agent(app);

describe('Queue routes', () => {
Expand Down Expand Up @@ -143,4 +148,38 @@ describe('Queue routes', () => {
Math.random = realRandom;
});
});

it('should throw a clear error when queue owner lacks DatHost config', async () => {
const originalHas = config.has.bind(config);
const originalGet = config.get.bind(config);
let descriptor;
try {
jest.spyOn(config, 'has').mockImplementation((key) => {
if (key === 'server.serverProvider') return true;
return originalHas(key);
});
jest.spyOn(config, 'get').mockImplementation((key) => {
if (key === 'server.serverProvider') return 'dathost';
return originalGet(key);
});

descriptor = await QueueService.createQueue(
'76561198025644195',
'OwnerNoDatHostConfig',
10,
false,
'cs2',
120
);

await expect(
QueueService.createMatchFromQueue(descriptor.name, [1, 2])
).rejects.toThrow(QueueOwnerDatHostConfigMissingError);
} finally {
if (descriptor?.name) {
await QueueService.deleteQueue(descriptor.name, '76561198025644195');
}
jest.restoreAllMocks();
}
});
});
2 changes: 2 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import usersRouter from "./src/routes/users.js";
import vetoesRouter from "./src/routes/vetoes.js";
import vetosidesRouter from "./src/routes/vetosides.js";
import passport from "./src/utility/auth.js";
import dathostConfigRouter from "./src/routes/dathost-config.js";
import {router as v2Router} from "./src/routes/v2/api.js";
import {router as v2DemoRouter} from "./src/routes/v2/demoapi.js";
import { router as v2BackupRouter } from "./src/routes/v2/backupapi.js";
Expand Down Expand Up @@ -149,6 +150,7 @@ app.use("/seasons", seasonsRouter);
app.use("/match", legacyAPICalls);
app.use("/leaderboard", leaderboardRouter);
app.use("/maps", mapListRouter);
app.use("/dathost-config", dathostConfigRouter);
app.use("/v2", v2Router);
app.use("/v2/demo", v2DemoRouter);
app.use("/v2/backup", v2BackupRouter);
Expand Down
3 changes: 2 additions & 1 deletion config/development.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"redisUrl": "redis://:super_secure@localhost:6379",
"redisTTL": 86400,
"queueTTL": 3600,
"serverPingTimeoutMs": 5000
"serverPingTimeoutMs": 5000,
"serverProvider": "local"
},
"development": {
"driver": "mysql",
Expand Down
3 changes: 2 additions & 1 deletion config/production.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"redisUrl": "$REDISURL",
"redisTTL": $REDISTTL,
"queueTTL": $QUEUETTL,
"serverPingTimeoutMs": $SERVERPINGTO
"serverPingTimeoutMs": $SERVERPINGTO,
"serverProvider": "$SERVERPROVIDER"
},
"production": {
"driver": "mysql",
Expand Down
3 changes: 2 additions & 1 deletion config/test.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"redisUrl": "redis://:super_secure@localhost:6379",
"redisTTL": 86400,
"queueTTL": 3600,
"serverPingTimeoutMs": 5000
"serverPingTimeoutMs": 5000,
"serverProvider": "local"
},
"test": {
"driver": "mysql",
Expand Down
13 changes: 13 additions & 0 deletions jest_config/jest.dathost.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
process.env.NODE_ENV = "test";
module.exports = {
preset: "ts-jest/presets/js-with-ts-esm",
resolver: "jest-ts-webcompat-resolver",
clearMocks: true,
roots: ["../__test__"],
testEnvironment: "node",
testMatch: [
"**/dathost.test.js",
"**/@(dathost.)+(spec|test).[tj]s?(x)"
],
verbose: false
};
53 changes: 53 additions & 0 deletions migrations/development/20250311120000-get5db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

var dbm;
var type;
var seed;

/**
* Add dathost_server_id and is_managed to game_server;
* add dathost_allowed to user for DatHost on-the-fly provisioning.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function (db) {
return db
.addColumn("game_server", "dathost_server_id", {
type: "string",
length: 64,
notNull: false
})
.then(function () {
return db.addColumn("game_server", "is_managed", {
type: "boolean",
notNull: true,
defaultValue: false
});
})
.then(function () {
return db.addColumn("user", "dathost_allowed", {
type: "boolean",
notNull: true,
defaultValue: false
});
});
};

exports.down = function (db) {
return db
.removeColumn("user", "dathost_allowed")
.then(function () {
return db.removeColumn("game_server", "is_managed");
})
.then(function () {
return db.removeColumn("game_server", "dathost_server_id");
});
};

exports._meta = {
version: 28
};
45 changes: 45 additions & 0 deletions migrations/development/20260317000000-get5db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";

var dbm;
var type;
var seed;

exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function (db) {
return db
.runSql(
"CREATE TABLE IF NOT EXISTS dathost_config (" +
"id INT NOT NULL AUTO_INCREMENT, " +
"email VARCHAR(512) NOT NULL, " +
"password VARCHAR(512) NOT NULL, " +
"steam_game_server_login_token VARCHAR(512) NOT NULL DEFAULT '', " +
"shutdown_delay_seconds INT NOT NULL DEFAULT 0, " +
"preferred_location VARCHAR(64) NOT NULL DEFAULT '', " +
"PRIMARY KEY (id)" +
")"
)
.then(function () {
return db.runSql(
"ALTER TABLE dathost_config " +
"ADD COLUMN user_id INT NULL, " +
"ADD UNIQUE INDEX uq_dathost_config_user_id (user_id)"
);
});
};

exports.down = function (db) {
return db.runSql(
"ALTER TABLE dathost_config " +
"DROP INDEX uq_dathost_config_user_id, " +
"DROP COLUMN user_id"
);
};

exports._meta = {
version: 29
};
53 changes: 53 additions & 0 deletions migrations/production/20250311120000-get5db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

var dbm;
var type;
var seed;

/**
* Add dathost_server_id and is_managed to game_server;
* add dathost_allowed to user for DatHost on-the-fly provisioning.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function (db) {
return db
.addColumn("game_server", "dathost_server_id", {
type: "string",
length: 64,
notNull: false
})
.then(function () {
return db.addColumn("game_server", "is_managed", {
type: "boolean",
notNull: true,
defaultValue: false
});
})
.then(function () {
return db.addColumn("user", "dathost_allowed", {
type: "boolean",
notNull: true,
defaultValue: false
});
});
};

exports.down = function (db) {
return db
.removeColumn("user", "dathost_allowed")
.then(function () {
return db.removeColumn("game_server", "is_managed");
})
.then(function () {
return db.removeColumn("game_server", "dathost_server_id");
});
};

exports._meta = {
version: 28
};
45 changes: 45 additions & 0 deletions migrations/production/20260317000000-get5db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"use strict";

var dbm;
var type;
var seed;

exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};

exports.up = function (db) {
return db
.runSql(
"CREATE TABLE IF NOT EXISTS dathost_config (" +
"id INT NOT NULL AUTO_INCREMENT, " +
"email VARCHAR(512) NOT NULL, " +
"password VARCHAR(512) NOT NULL, " +
"steam_game_server_login_token VARCHAR(512) NOT NULL DEFAULT '', " +
"shutdown_delay_seconds INT NOT NULL DEFAULT 0, " +
"preferred_location VARCHAR(64) NOT NULL DEFAULT '', " +
"PRIMARY KEY (id)" +
")"
)
.then(function () {
return db.runSql(
"ALTER TABLE dathost_config " +
"ADD COLUMN user_id INT NULL, " +
"ADD UNIQUE INDEX uq_dathost_config_user_id (user_id)"
);
});
};

exports.down = function (db) {
return db.runSql(
"ALTER TABLE dathost_config " +
"DROP INDEX uq_dathost_config_user_id, " +
"DROP COLUMN user_id"
);
};

exports._meta = {
version: 29
};
Loading