Skip to content

Commit

Permalink
Merge branch 'master' into authenticate-user-in-service
Browse files Browse the repository at this point in the history
  • Loading branch information
alxndrsn committed Oct 16, 2024
2 parents 89036a9 + 64ee88c commit 79257c0
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/formats/odata.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ const singleRowToOData = (fields, row, domain, originalUrl, query) => {
let { offset } = paging;

if (skipToken) {
offset = filtered.fiindIndex(s => skipToken.repeatId === s.__id);
offset = filtered.findIndex(s => skipToken.repeatId === s.__id) + 1;
}

const pared = filtered.slice(offset, offset + limit);
Expand Down
2 changes: 1 addition & 1 deletion lib/http/preprocessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const oidc = require('../util/oidc');
const Problem = require('../util/problem');
const { QueryOptions } = require('../util/db');
const { reject, getOrReject } = require('../util/promise');
const { SESSION_COOKIE } = require('../util/sessions');
const { SESSION_COOKIE } = require('./sessions');

// injects an empty/anonymous auth object into the request context.
const emptyAuthInjector = ({ Auth }, context) => context.with({ auth: Auth.by(null) });
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/resources/oidc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { parse, render } = require('mustache');

const { safeNextPathFrom } = require('../util/html');
const { redirect } = require('../util/http');
const { createUserSession } = require('../util/sessions');
const { createUserSession } = require('../http/sessions');
const { // eslint-disable-line object-curly-newline
CODE_CHALLENGE_METHOD,
SCOPES,
Expand Down
2 changes: 1 addition & 1 deletion lib/resources/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const Problem = require('../util/problem');
const { isBlank, noargs } = require('../util/util');
const { getOrReject, rejectIf } = require('../util/promise');
const { success } = require('../util/http');
const { SESSION_COOKIE, createUserSession } = require('../util/sessions');
const { SESSION_COOKIE, createUserSession } = require('../http/sessions');
const oidc = require('../util/oidc');

module.exports = (service, endpoint) => {
Expand Down
52 changes: 52 additions & 0 deletions test/integration/other/blobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,58 @@ describe('blob query module', () => {
.then(() => container.oneFirst(sql`select count(*) from blobs`))
.then((count) => count.should.equal(1)))));

it('should handle blob collisions with different filenames', testService((service, container) =>
// One one instance of the form, two files are uploaded
// On another instance of the form (different id), one file is uploaded
// and it creates another reference to one of the blobs with a different
// filename.
service.login('alice', (asAlice) =>
asAlice.post('/v1/projects/1/forms?publish=true')
.set('Content-Type', 'application/xml')
.send(testData.forms.binaryType)
.expect(200)
.then(() => container.oneFirst(sql`select count(*) from blobs`))
.then((count) => count.should.equal(0))
.then(() => asAlice.post('/v1/projects/1/submission')
.set('X-OpenRosa-Version', '1.0')
.attach('xml_submission_file', Buffer.from(testData.instances.binaryType.both), { filename: 'data.xml' })
.attach('here_is_file2.jpg', Buffer.from('this is test file two'), { filename: 'here_is_file2.jpg' })
.attach('my_file1.mp4', Buffer.from('this is test file one'), { filename: 'my_file1.mp4' })
.expect(201))
.then(() => container.oneFirst(sql`select count(*) from blobs`))
.then((count) => count.should.equal(2))
.then(() => asAlice.post('/v1/projects/1/forms?publish=true')
.set('Content-Type', 'application/xml')
.send(testData.forms.binaryType.replace('id="binaryType"', 'id="binaryType2"'))
.expect(200))
.then(() => asAlice.post('/v1/projects/1/submission')
.set('X-OpenRosa-Version', '1.0')
.attach(
'xml_submission_file',
Buffer.from(testData.instances.binaryType.one
.replace('id="binaryType"', 'id="binaryType2"')
.replace('<file1>my_file1.mp4</file1>', '<file1>my_file2.mp4</file1>')),
{ filename: 'data.xml' },
)
.attach('my_file2.mp4', Buffer.from('this is test file one'), { filename: 'my_file2.mp4' })
.expect(201))
.then(() => container.oneFirst(sql`select count(*) from blobs`))
.then((count) => count.should.equal(2))
.then(() => asAlice.get('/v1/projects/1/forms/binaryType/submissions/both/attachments/my_file1.mp4')
.expect(200)
.then(({ headers, body }) => {
headers['content-type'].should.equal('video/mp4');
headers['content-disposition'].should.equal('attachment; filename="my_file1.mp4"; filename*=UTF-8\'\'my_file1.mp4');
body.toString('utf8').should.equal('this is test file one');
}))
.then(() => asAlice.get('/v1/projects/1/forms/binaryType2/submissions/bone/attachments/my_file2.mp4')
.expect(200)
.then(({ headers, body }) => {
headers['content-type'].should.equal('video/mp4');
headers['content-disposition'].should.equal('attachment; filename="my_file2.mp4"; filename*=UTF-8\'\'my_file2.mp4');
body.toString('utf8').should.equal('this is test file one');
})))));

it('should handle blob collisions and not purge still attached blobs', testService((service, container) =>
// One one instance of the form, two files are uploaded
// On another instance of the form (different id), one file is uploaded
Expand Down
86 changes: 86 additions & 0 deletions test/unit/formats/odata.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,92 @@ describe('odata message composition', () => {
});
});

describe('with $skiptoken', () => {
const billy = { __id: 'cf9a1b5cc83c6d6270c1eb98860d294eac5d526d', age: 4, name: 'Billy' };
const blain = { __id: 'c76d0ccc6d5da236be7b93b985a80413d2e3e172', age: 6, name: 'Blaine' };

[
{
$top: 0,
skiptoken: { repeatId: billy.__id },
expectedNext: false,
expectedValue: [],
},
{
$top: 1,
skiptoken: { repeatId: billy.__id },
expectedNext: false,
expectedValue: [ blain ],
},
{
$top: 2,
skiptoken: { repeatId: billy.__id },
expectedNext: false,
expectedValue: [ blain ],
},
{
$top: undefined,
skiptoken: { repeatId: billy.__id },
expectedNext: false,
expectedValue: [ blain ],
},

{
$top: 0,
skiptoken: { repeatId: blain.__id },
expectedNext: false,
expectedValue: [],
},
{
$top: 1,
skiptoken: { repeatId: blain.__id },
expectedNext: false,
expectedValue: [],
},
{
$top: 2,
skiptoken: { repeatId: blain.__id },
expectedNext: false,
expectedValue: [],
},
{
$top: undefined,
skiptoken: { repeatId: blain.__id },
expectedNext: false,
expectedValue: [],
},
].forEach(({ $top, skiptoken, expectedNext, expectedValue }) =>
it(`should return expected result for ${[$top, JSON.stringify(skiptoken)]}`, () =>
fieldsFor(testData.forms.withrepeat).then((fields) => {
const submission = mockSubmission('two', testData.instances.withrepeat.two);
const $skiptoken = '01' + Buffer.from(JSON.stringify(skiptoken)).toString('base64');
const query = { $top, $skiptoken };
const originaUrl = "/withrepeat.svc/Submissions('two')/children/child"; // doesn't have to include query string
return singleRowToOData(fields, submission, 'http://localhost:8989', originaUrl, query)
.then(JSON.parse)
.then((res) => {
res['@odata.context'].should.eql('http://localhost:8989/withrepeat.svc/$metadata#Submissions.children.child');

const nextLink = res['@odata.nextLink'];
if (expectedNext === false) should(nextLink).be.undefined();
else {
should(nextLink).be.ok();
JSON.parse(
Buffer.from(
new URL(nextLink)
.searchParams
.get('$skiptoken')
.substr(2),
'base64',
).toString()
).should.deepEqual({ repeatId: expectedNext });
}

res.value.should.deepEqual(expectedValue.map(x => ({ ...x, '__Submissions-id': 'two' })));
});
})));
});

// eslint-disable-next-line arrow-body-style
it('should retain other parameters when giving the nextUrl', () => {
return fieldsFor(testData.forms.withrepeat).then((fields) => {
Expand Down

0 comments on commit 79257c0

Please sign in to comment.