Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Fixed accidental caching of classSearch data #5

Merged
merged 12 commits into from
Apr 7, 2020
1 change: 1 addition & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ jobs:
- run: mocha
env:
CI: true

7 changes: 6 additions & 1 deletion index/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ if (typeof(fs) === 'undefined'){
var readFile = promisify(fs.readFile);
}

if (typeof(excluded_methods) === 'undefined'){
var excluded_methods = process.env.EXCLUDED_METHODS.split(' ');
}

module.exports = {
Banner: Banner,
bannerObjs: {},
Expand All @@ -23,5 +27,6 @@ module.exports = {
},
dir: '/tmp',
useTmp: false,
counter: 1
counter: 1,
excluded_methods: excluded_methods
}
12 changes: 9 additions & 3 deletions index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,25 @@ async function requestData(school, term, method, params){
if(typeof(cache.bannerObjs[school] === 'undefined')){
cache.bannerObjs[school] = new cache.Banner(school);
}
cache.useTmp ? await createTmpDirEntries(school, term) : createCacheEntries(school, term);

//perform the request to Banner for the data
let request = cache.bannerObjs[school][method](params);

if (!cache.excluded_methods.includes(method)){
cache.useTmp ? await createTmpDirEntries(school, term) : createCacheEntries(school, term);
}

//put the data into the cache
let obj = {
//Timestamp is stored as SECONDS
timestamp: Date.now() / 1000,
data: await request
}
cache.useTmp ? await cache.fs.writeFile(`${cache.dir}/${school}/${term}/${method}.json`, JSON.stringify(obj), 'utf8')
: cache.bannerCache[school][term][method] = obj;
if (!cache.excluded_methods.includes(method)){
cache.useTmp ? await cache.fs.writeFile(`${cache.dir}/${school}/${term}/${method}.json`, JSON.stringify(obj), 'utf8')
: cache.bannerCache[school][term][method] = obj;
}

//return the data
return obj.data;
}
Expand Down
43 changes: 31 additions & 12 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
'use-strict'
process.env.RAM_THRESHOLD = 0.9;
process.env.CHECK_FREQUENCY = 10;
process.env.EXPIRE = 600;
process.env.EXCLUDED_METHODS = 'excludedMethod';

const assert = require('assert');
const rewire = require('rewire');
Expand All @@ -9,10 +13,6 @@ const mkdir = promisify(fs.mkdir);
const writeFile = promisify(fs.writeFile);
const rmdir = promisify(fs.rmdir);

process.env.RAM_THRESHOLD = 0.9;
process.env.CHECK_FREQUENCY = 10;
process.env.EXPIRE = 600;

function randInt(){
return Math.floor(Math.random() * Math.floor(999999));
}
Expand All @@ -31,6 +31,10 @@ const BannerMock = class {
badRequest(){
throw new Error('Request error');
}

excludedMethod(){
return 'data that should not be cached';
}
}

describe('Banner Proxy', function(){
Expand Down Expand Up @@ -231,6 +235,13 @@ describe('Banner Proxy', function(){
describe('#requestData', function(){
before(function(){
this.function = index.__get__('requestData');
this.term = 1;
this.goodSchool = 'foo';
this.badSchool = 'fakeSchool';
this.goodMethod = 'successfulRequest';
this.badMethod = 'badRequest';
this.fakeMethod = 'methodThatDoesntExist';
this.excludedMethod = 'excludedMethod';
this.cache = {
bannerCache: {},
bannerObjs: {},
Expand All @@ -239,14 +250,11 @@ describe('Banner Proxy', function(){
fs: {
mkdir: mkdir,
writeFile: writeFile
}
},
excluded_methods: [
this.excludedMethod
]
};
this.term = 1;
this.goodSchool = 'foo';
this.badSchool = 'fakeSchool';
this.goodMethod = 'successfulRequest';
this.badMethod = 'badRequest';
this.fakeMethod = 'methodThatDoesntExist';
index.__set__({cache: this.cache});
});

Expand All @@ -270,6 +278,13 @@ describe('Banner Proxy', function(){
assert.strict(fs.existsSync(`/tmp/${this.goodSchool}/${this.term}/${this.goodMethod}.json`));
});

it('Should NOT put data from excluded methods into the cache or tmp dir', async function(){
index.__set__({cache: this.cache});
await this.function(this.goodSchool, this.term, this.excludedMethod);
assert.strictEqual(index.__get__('cache').bannerCache[this.goodSchool][this.term][this.excludedMethod], undefined);
assert.strictEqual(fs.existsSync(`${this.cache.dir}/${this.goodSchool}/${this.term}/${this.excludedMethod}`), false);
});

after(async function(){
await rmdir(`/tmp/${this.school}`, {recursive: true});
});
Expand All @@ -290,6 +305,7 @@ describe('Banner Proxy', function(){
this.context = {
memoryLimitInMB: 0
};
this.excludedMethod = 'excludedMethod';
this.cache = {
counter: process.env.CHECK_FREQUENCY - 1,
useTmp: false,
Expand All @@ -300,7 +316,10 @@ describe('Banner Proxy', function(){
writeFile: writeFile
},
Banner: BannerMock,
dir: '/tmp'
dir: '/tmp',
excluded_methods: [
this.excludedMethod
]
}
index.__set__({cache: this.cache});
await index.handler(this.goodArgs, this.context);
Expand Down