fix: Newly created private collections do not return correct policies (#1188)

closes #1185
This commit is contained in:
Tom Moor
2020-02-24 23:16:24 -08:00
committed by GitHub
parent 3e7acc377e
commit 0b86714984
2 changed files with 28 additions and 4 deletions

View File

@@ -22,15 +22,16 @@ const router = new Router();
router.post('collections.create', auth(), async ctx => {
const { name, color, description, type } = ctx.body;
const isPrivate = ctx.body.private;
ctx.assertPresent(name, 'name is required');
if (color)
if (color) {
ctx.assertHexColor(color, 'Invalid hex value (please use format #FFFFFF)');
}
const user = ctx.state.user;
authorize(user, 'create', Collection);
const collection = await Collection.create({
let collection = await Collection.create({
name,
description,
color,
@@ -49,6 +50,13 @@ router.post('collections.create', auth(), async ctx => {
ip: ctx.request.ip,
});
// we must reload the collection to get memberships for policy presenter
if (isPrivate) {
collection = await Collection.scope({
method: ['withMembership', user.id],
}).findByPk(collection.id);
}
ctx.body = {
data: presentCollection(collection),
policies: presentPolicies(user, [collection]),

View File

@@ -511,7 +511,7 @@ describe('#collections.create', async () => {
it('should create collection', async () => {
const { user } = await seed();
const res = await server.post('/api/collections.create', {
body: { token: user.getJwtToken(), name: 'Test', type: 'atlas' },
body: { token: user.getJwtToken(), name: 'Test' },
});
const body = await res.json();
@@ -519,6 +519,22 @@ describe('#collections.create', async () => {
expect(body.data.id).toBeTruthy();
expect(body.data.name).toBe('Test');
expect(body.policies.length).toBe(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[0].abilities.export).toBeTruthy();
});
it('should return correct policies with private collection', async () => {
const { user } = await seed();
const res = await server.post('/api/collections.create', {
body: { token: user.getJwtToken(), name: 'Test', private: true },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.private).toBeTruthy();
expect(body.policies.length).toBe(1);
expect(body.policies[0].abilities.read).toBeTruthy();
expect(body.policies[0].abilities.export).toBeTruthy();
});
});