Skip to content

Commit

Permalink
Merge branch 'master' into roomType_bot_db_connection
Browse files Browse the repository at this point in the history
  • Loading branch information
iamigo committed Aug 21, 2017
2 parents 874c51c + e3c220a commit 9d6cd56
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 68 deletions.
2 changes: 1 addition & 1 deletion cache/models/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ module.exports = {

return db.Subject.findById(reqBody.subjectId)
.then((subjFromDb) => {
if (!subjFromDb) {
if (!subjFromDb || !subjFromDb.isPublished) {
throw new redisErrors.ResourceNotFoundError({
explanation: 'Subject not found.',
});
Expand Down
2 changes: 1 addition & 1 deletion cache/models/subject.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ module.exports = {
.then((subject) => {
if (!subject) {
throw new redisErrors.ResourceNotFoundError({
explanation: 'Sample not found.',
explanation: 'Subject not found.',
});
}

Expand Down
19 changes: 17 additions & 2 deletions db/model/sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ module.exports = function sample(seq, dataTypes) {
return new seq.Promise((resolve, reject) => {
u.getSubjectAndAspectBySampleName(seq, toUpsert.name)
.then((sa) => {
if (sa && sa.subject && !sa.subject.isPublished) {
const err = new dbErrors.ResourceNotFoundError();
err.resourceType = 'Subject';
err.resourceKey = sa.subject.id;
throw err;
}

subjasp = sa;
toUpsert.subjectId = sa.subject.id;
toUpsert.aspectId = sa.aspect.id;
Expand Down Expand Up @@ -326,9 +333,17 @@ module.exports = function sample(seq, dataTypes) {
return new seq.Promise((resolve, reject) =>
inst.getSubject()
.then((s) => {
inst.name = s.absolutePath + constants.sampleNameSeparator;
if (s && s.getDataValue('isPublished')) {
inst.name = s.absolutePath + constants.sampleNameSeparator;
} else {
const err = new dbErrors.ResourceNotFoundError();
err.resourceType = 'Subject';
err.resourceKey = s.id;
throw err;
}

return inst.getAspect();
})
.then(() => inst.getAspect())
.then((a) => {
if (a && a.getDataValue('isPublished')) {
inst.name += a.name;
Expand Down
103 changes: 65 additions & 38 deletions tests/cache/models/samples/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const u = require('./utils');
describe('tests/cache/models/samples/post.js >', () => {
describe(`api: redisStore: POST ${path} >`, () => {
let sampleToPost;
let subjectId;
let aspectId;
let token;
const sampleName = `${tu.namePrefix}TEST_SUBJECT` + '.' +
`${tu.namePrefix}CHILD_SUBJECT` + '|' + `${tu.namePrefix}TEST_ASPECT`;
Expand All @@ -46,6 +48,7 @@ describe('tests/cache/models/samples/post.js >', () => {
const samp = { value: '1' };
tu.db.Aspect.create(u.aspectToCreate)
.then((a) => {
aspectId = a.id;
samp.aspectId = a.id;
return tu.db.Subject.create(u.subjectToCreate);
})
Expand All @@ -55,6 +58,7 @@ describe('tests/cache/models/samples/post.js >', () => {
parentId: s.id,
}))
.then((s) => {
subjectId = s.id;
samp.subjectId = s.id;
resolve(samp);
})
Expand All @@ -73,6 +77,67 @@ describe('tests/cache/models/samples/post.js >', () => {
afterEach(rtu.flushRedis);
after(() => tu.toggleOverride('enableRedisSampleStore', false));

describe('unpublished subject/aspect fails >', () => {
it('unpublished aspect fails', (done) => {
tu.db.Aspect.create({
isPublished: false,
name: `${tu.namePrefix}UNPUBLISHED_ASPECT`,
timeout: '3d',
})
.then((a) => {
const sampleWithUnpublishedAspect = {
aspectId: a.id,
subjectId: subjectId,
value: '1',
};

api.post(path)
.set('Authorization', token)
.send(sampleWithUnpublishedAspect)
.expect(constants.httpStatus.NOT_FOUND)
.end((err, res) => {
if (err) {
return done(err);
}

const _err = res.body.errors[ZERO];
expect(_err.type).to.equal('ResourceNotFoundError');
expect(_err.description).to.equal('Aspect not found.');
done();
});
});
});

it('unpublished subject fails', (done) => {
tu.db.Subject.create({
isPublished: false,
name: `${tu.namePrefix}UNPUBLISHED_SUBJECT`,
})
.then((s) => {
const sampleWithUnpublishedSubject = {
aspectId: aspectId,
subjectId: s.id,
value: '1',
};

api.post(path)
.set('Authorization', token)
.send(sampleWithUnpublishedSubject)
.expect(constants.httpStatus.NOT_FOUND)
.end((err, res) => {
if (err) {
return done(err);
}

const _err = res.body.errors[ZERO];
expect(_err.type).to.equal('ResourceNotFoundError');
expect(_err.description).to.equal('Subject not found.');
done();
});
});
});
});

describe('post duplicate fails >', () => {
beforeEach((done) => {
tu.db.Sample.create(sampleToPost)
Expand Down Expand Up @@ -279,42 +344,4 @@ describe('tests/cache/models/samples/post.js >', () => {
.end(done);
});
});

describe(`api: redisStore: POST ${path} aspect isPublished false >`, () => {
let sampleToPost;
let token;

before((done) => {
tu.toggleOverride('enableRedisSampleStore', true);
tu.createToken()
.then((returnedToken) => {
token = returnedToken;
done();
})
.catch(done);
});

beforeEach((done) => {
u.doSetupAspectNotPublished()
.then((samp) => {
sampleToPost = samp;
return samstoinit.eradicate();
})
.then(() => samstoinit.init())
.then(() => done())
.catch(done);
});

afterEach(rtu.forceDelete);
afterEach(rtu.flushRedis);
after(() => tu.toggleOverride('enableRedisSampleStore', false));

it('cannot create sample if aspect not published', (done) => {
api.post(path)
.set('Authorization', token)
.send(sampleToPost)
.expect(constants.httpStatus.NOT_FOUND)
.end(done);
});
});
});
33 changes: 33 additions & 0 deletions tests/cache/models/samples/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,39 @@ describe(`tests/cache/models/samples/put.js, api: cache: PUT ${path}`, () => {
afterEach(rtu.flushRedis);
after(() => tu.toggleOverride('enableRedisSampleStore', false));

describe('unpublished subject/aspect fails >', () => {
it('on unpublish subject, sample is removed from cache', (done) => {
tu.db.Subject.findById(subjectId).then((subject) =>
subject.update({ isPublished: false }))
.then(() => redisOps.getHashPromise(redisOps.sampleType, sampleName))
.then((sample) => {
expect(sample).to.be.null;
done();
});
});

it('update to unpublished aspect fails', (done) => {
tu.db.Aspect.findById(aspectId).then((aspect) =>
aspect.update({ isPublished: false }))
.then(() => {
api.put(`${path}/${sampleName}`)
.set('Authorization', token)
.send({ aspectId, subjectId })
.expect(constants.httpStatus.NOT_FOUND)
.end((err, res) => {
if (err) {
return done(err);
}

const _err = res.body.errors[ZERO];
expect(_err.type).to.equal('ResourceNotFoundError');
expect(_err.description).to.equal('Aspect not found.');
done();
});
});
});
});

describe('Lists >', () => {
it('reject if name is in request body', (done) => {
api.put(`${path}/${sampleName}`)
Expand Down
17 changes: 12 additions & 5 deletions tests/cache/sampleStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('tests/cache/sampleStore.js >', () => {
let s1;
let s2;
let s3;
let s4;
let user1;
let user2;
let user3;
Expand Down Expand Up @@ -85,7 +86,7 @@ describe('tests/cache/sampleStore.js >', () => {
}))
.then((created) => (a3 = created))
.then(() => Aspect.create({
isPublished: false, // unpublished subject should still be found
isPublished: false, // unpublished aspect should still be found
name: `${tu.namePrefix}Aspect4`,
timeout: '10m',
valueType: 'BOOLEAN',
Expand Down Expand Up @@ -124,11 +125,17 @@ describe('tests/cache/sampleStore.js >', () => {
}))
.then((created) => (s2 = created))
.then(() => Subject.create({
isPublished: false,
isPublished: true,
name: `${tu.namePrefix}Subject3`,
parentId: s1.id,
}))
.then((created) => (s3 = created))
.then(() => Subject.create({
isPublished: false, // should still be found in cache
name: `${tu.namePrefix}Subject4`,
parentId: s1.id,
}))
.then((created) => (s4 = created))
.then(() => Sample.create({
subjectId: s2.id,
aspectId: a1.id,
Expand Down Expand Up @@ -166,8 +173,8 @@ describe('tests/cache/sampleStore.js >', () => {
.catch(done);
});

it('subject is populated', (done) => {
const absolutePath = '___subject1.___subject2';
it('unpublished subject is in cache', (done) => {
const absolutePath = '___subject1.___subject4';
samstoinit.eradicate()
.then(() => rcli.keysAsync(sampleStore.constants.prefix + '*'))
.then((res) => expect(res.length).to.eql(0))
Expand All @@ -176,7 +183,7 @@ describe('tests/cache/sampleStore.js >', () => {
.then((res) => {
expect(res.includes('samsto:subject:' + absolutePath))
.to.be.true;
expect(res.includes('samsto:subject:___subject1.___subject3'))
expect(res.includes('samsto:subject:___subject1.___subject4'))
.to.be.true;
})
.then(() => rcli.hgetallAsync('samsto:subject:' + absolutePath))
Expand Down
16 changes: 0 additions & 16 deletions tests/db/helpers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,22 +126,6 @@ describe('tests/db/helpers/common.js >', () => {
.catch(done);
});

it('sampleAspectAndSubjectArePublished : check for false', (done) => {
Subject.findById(sub.id)
.then((s) => s.update({ isPublished: false }))
.then(() => Sample.upsertByName({
name: `${tu.namePrefix}Subject|${tu.namePrefix}Aspect`,
value: '1',
}))
.then((samp) =>
common.sampleAspectAndSubjectArePublished(tu.db.sequelize, samp))
.then((pub) => {
expect(pub).to.equal(false);
})
.then(() => done())
.catch(done);
});

it('augmentSampleWithSubjectAspectInfo : returned sample should have' +
' subject and aspect information', (done) => {
Sample.upsertByName({
Expand Down

0 comments on commit 9d6cd56

Please sign in to comment.