Skip to content

Commit

Permalink
Upgrade mocha and tests (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
halkeye committed Apr 29, 2020
1 parent 8bdf448 commit 5fbaab8
Show file tree
Hide file tree
Showing 13 changed files with 1,958 additions and 1,734 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ nbproject
dump.rdb

config/datastores.js
test/output/*
.env
.nyc_output/*
coverage/*
assets/images/sdtdIcons
coverage
assets/images/sdtdIcons
db-data/
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ before_install:
install:
- npm install
script:
- npm test
- npm run coverage
- npm run cover
after_script:
- cat coverage/lcov.info | node_modules/.bin/coveralls
- cat coverage/lcov.info | node_modules/.bin/codecov
after_failure:
- wget https://raw.githubusercontent.com/DiscordHooks/travis-ci-discord-webhook/master/send.sh
- chmod +x send.sh
Expand Down
8 changes: 8 additions & 0 deletions api/controllers/CustomCommand/create-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module.exports = {

exits: {
success: {},
brokenDonator: {
responseType: 'badRequest'
},
badCommand: {
responseType: 'badRequest'
},
Expand Down Expand Up @@ -53,6 +56,11 @@ module.exports = {
let amountOfExistingCommands = await CustomCommand.count({
server: server.id
});
if (!sails.config.custom.donorConfig[donatorRole]) {
const err = new Error(`Donator status of ${donatorRole} is unmanaged, contact an admin`);
sails.log.error(err);
return exists.brokenDonator(err.message);
}
let maxCustomCommands = sails.config.custom.donorConfig[donatorRole].maxCustomCommands;

if (amountOfExistingCommands >= maxCustomCommands) {
Expand Down
9 changes: 4 additions & 5 deletions api/helpers/meta/check-donator-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {

fn: async function (inputs, exits) {

let donorStatus = "free";
let donorStatus;

if (_.isUndefined(inputs.serverId) && _.isUndefined(inputs.userId)) {
return exits.error(`Must provide serverId OR userId parameter.`);
Expand All @@ -42,23 +42,22 @@ module.exports = {

if (_.isNull(currentStatus) || inputs.reload) {
currentStatus = await sails.helpers.meta.getDonatorStatus(inputs.serverId);
await sails.helpers.redis.set(`server:${inputs.serverId}:donorStatus`, currentStatus);
}
await sails.helpers.redis.set(`server:${inputs.serverId}:donorStatus`, currentStatus);
donorStatus = currentStatus;

}

if (!_.isUndefined(inputs.userId)) {
let currentStatus = await sails.helpers.redis.get(`user:${inputs.userId}:donorStatus`);

if (_.isNull(currentStatus) || inputs.reload) {
currentStatus = await sails.helpers.meta.getDonatorStatus(undefined, inputs.userId);
await sails.helpers.redis.set(`server:${inputs.userId}:donorStatus`, currentStatus);
}
await sails.helpers.redis.set(`server:${inputs.userId}:donorStatus`, currentStatus);
donorStatus = currentStatus;
}

return exits.success(donorStatus);
return exits.success(donorStatus || "free");
}


Expand Down
27 changes: 13 additions & 14 deletions api/hooks/sdtdLogs/logProcessor.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
const Redis = require("redis");
const dotenv = require("dotenv");
const promisify = require("util").promisify;
const {promisify} = require("util");
const SdtdApi = require("7daystodie-api-wrapper");
const _ = require("lodash");

const handleLogLine = require("./handleLogLine");

/**
* Set up Redis functionality
*/
dotenv.config();
const redisClient = Redis.createClient({
url: process.env.REDISSTRING
});
redisClient.get = promisify(redisClient.get);
redisClient.set = promisify(redisClient.set);
Redis.RedisClient.prototype.getAsync = promisify(Redis.RedisClient.prototype.get);
Redis.RedisClient.prototype.setAsync = promisify(Redis.RedisClient.prototype.set);

module.exports = async function(job) {
const redisClient = Redis.createClient({
url: process.env.REDISSTRING
});
const resultLogs = [];
// Get latest log line from Redis
let lastLogLine = parseInt(
await redisClient.get(
await redisClient.getAsync(
`sdtdserver:${job.data.server.id}:sdtdLogs:lastLogLine`
)
);
Expand All @@ -42,31 +41,31 @@ module.exports = async function(job) {
lastLogLine = lastLogLine + newLogs.entries.length;

// Set latest log line in Redis
await redisClient.set(
await redisClient.setAsync(
`sdtdserver:${job.data.server.id}:sdtdLogs:lastLogLine`,
lastLogLine
);

// Parse these logs into usable data
let index = -1;
_.each(newLogs.entries, async line => {
for (const line of newLogs.entries) {
index++;
if (newLogs.entries[index + 1]) {
if (newLogs.entries[index + 1].msg.includes("handled by mod")) {
//Message is being handled by a mod, skip to the next line with possibly mod-controlled data
return;
continue;
}
}

let parsedLogLine = handleLogLine(line);
if (!_.isUndefined(parsedLogLine)) {
if (!parsedLogLine) {
parsedLogLine.server = job.data.server;
resultLogs.push(parsedLogLine);
}
});
}

// Set last success date in Redis
await redisClient.set(
await redisClient.setAsync(
`sdtdserver:${job.data.server.id}:sdtdLogs:lastSuccess`,
Date.now()
);
Expand Down
6 changes: 5 additions & 1 deletion config/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
*/

module.exports.bootstrap = async function (done) {
if (process.env.IS_TEST) {
sails.cache = new Object();
return done();
}

await sails.helpers.meta.startUsageStatsGathering();
sails.log.info(`Started the system stats gathering interval`);
if (process.env.REDISSTRING === "" || process.env.REDISSTRING === undefined) {
if (!process.env.REDISSTRING) {
sails.log.warn(`Not using redis as cache. Defaulting to in-memory caching. Be aware that this is not ideal for production environments!`);
sails.cache = new Object();
}
Expand Down
17 changes: 6 additions & 11 deletions config/datastores.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,13 @@ let datastores = {
* (See https://sailsjs.com/config/datastores for help.) *
* *
***************************************************************************/
adapter: require('sails-mysql'),
url: process.env.DBSTRING

adapter: 'sails-disk',
inMemoryOnly: true
},
cache: {
adapter: 'sails-disk',
inMemoryOnly: true
},

};

if (process.env.REDISSTRING) {
datastores.cache = {
adapter: 'sails-redis',
url: process.env.REDISSTRING
}
}

module.exports.datastores = datastores;
1 change: 1 addition & 0 deletions config/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ morgan.token('userId', function (req, res) {
const morganLogger = morgan(':remote-addr - :userId - [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"', {
"stream": customLogger.stream,
skip: (req, res) => {
if (process.env.IS_TEST) { return true; }
return !req.originalUrl.includes('api')
}
});
Expand Down
Loading

0 comments on commit 5fbaab8

Please sign in to comment.