Skip to content

Commit

Permalink
API tests for user preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
brontolosone committed Sep 13, 2024
1 parent ed520ba commit 32670e6
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions test/integration/api/user-preferences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const { testService } = require('../setup');

describe('api: user-preferences', () => {

it('should not allow storing preferences for nonexistent projects', testService(async (service) => {
const asAlice = await service.login('alice');

await asAlice.put('/v1/user-preferences/project/123/someProperty')
.send({ propertyValue: 'someValue' })
.expect(400); // project 123 doesn't exist
}));

it('PUT/DELETE preference storage operations', testService(async (service) => {
const asAlice = await service.login('alice');

await asAlice.put('/v1/user-preferences/site/someSimpleSitePref')
.send({ propertyValue: true })
.expect(201);

await asAlice.put('/v1/user-preferences/site/someComplexSitePref')
.send({ propertyValue: { 1: 2, 3: 4 } })
.expect(201);

// overwrite the previous
await asAlice.put('/v1/user-preferences/site/someComplexSitePref')
.send({ propertyValue: [1, 2, 3] })
.expect(201);

await asAlice.put('/v1/user-preferences/site/toBeDeletedPref')
.send({ propertyValue: 'troep' })
.expect(201);

await asAlice.put('/v1/user-preferences/project/1/someSimpleProjectPref')
.send({ propertyValue: false })
.expect(201);

await asAlice.put('/v1/user-preferences/project/1/someComplexProjectPref')
.send({ propertyValue: [1, 2, 'many'] })
.expect(201);

// make a new project and read its ID
let newProjectID = null;
await asAlice.post('/v1/projects')
.send({ name: 'preferences test project' })
.expect(200)
.then(({ body }) => {
newProjectID = body.id;
});

await asAlice.put(`/v1/user-preferences/project/${newProjectID}/prefForSomeOtherProject`)
.send({ propertyValue: 9000 })
.expect(201);

await asAlice.put(`/v1/user-preferences/project/${newProjectID}/toBeDeletedPref`)
.send({ propertyValue: 'troep' })
.expect(201);

// insert a preference for Bob, which we don't want to find in the
// preferences for Alice that we will subsequently fetch.
await service.login('bob').then((asBob) =>
asBob.put('/v1/user-preferences/site/koosje-likes-milk')
.send({ propertyValue: true })
.expect(201)
);

const newProjectProps = {};
newProjectProps[newProjectID] = {
prefForSomeOtherProject: 9000,
toBeDeletedPref: 'troep',
};

await asAlice.get('/v1/user-preferences')
.expect(200)
.then(({ body }) => {
body.should.eql({
site: {
someSimpleSitePref: true,
someComplexSitePref: [1, 2, 3],
toBeDeletedPref: 'troep',
},
projects: Object.assign(
{
1: {
someSimpleProjectPref: false,
someComplexProjectPref: [1, 2, 'many'],
},
},
newProjectProps,
),
});
});

await asAlice.delete('/v1/user-preferences/site/toBeDeletedPref')
.send({ propertyValue: true })
.expect(204);

await asAlice.delete(`/v1/user-preferences/project/${newProjectID}/toBeDeletedPref`)
.send({ propertyValue: true })
.expect(204);

await asAlice.delete(`/v1/user-preferences/project/${newProjectID}/toBeDeletedPref`)
.send({ propertyValue: true })
.expect(404); // as we've just deleted it

delete newProjectProps[newProjectID].toBeDeletedPref;

await asAlice.get('/v1/user-preferences')
.expect(200)
.then(({ body }) => {
body.should.eql({
site: {
someSimpleSitePref: true,
someComplexSitePref: [1, 2, 3],
},
projects: Object.assign(
{
1: {
someSimpleProjectPref: false,
someComplexProjectPref: [1, 2, 'many'],
},
},
newProjectProps
),
});
});
}));
});

0 comments on commit 32670e6

Please sign in to comment.