Skip to content

Commit 2e6e750

Browse files
committed
public folder unit tests [WIP]
1 parent ec67e12 commit 2e6e750

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

app/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ The following variables enable and enforce the use of OIDC Bearer Authentication
8585
| `bucket` | `OBJECTSTORAGE_BUCKET` | | The object storage bucket name |
8686
| `endpoint` | `OBJECTSTORAGE_ENDPOINT` | | Object store URL. eg: `https://nrs.objectstore.gov.bc.ca` |
8787
| `key` | `OBJECTSTORAGE_KEY` | | The base path for storage location |
88+
| `public` | `OBJECTSTORAGE_PUBLIC | | Whether to make the storage location public |
8889
| `secretAccessKey` | `OBJECTSTORAGE_SECRETACCESSKEY` | | The Secret Access Key for your S3 compatible object storage account |
8990

9091
### Server Variables

app/config/custom-environment-variables.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"enabled": "OBJECTSTORAGE_ENABLED",
3131
"endpoint": "OBJECTSTORAGE_ENDPOINT",
3232
"key": "OBJECTSTORAGE_KEY",
33+
"public": "OBJECTSTORAGE_PUBLIC",
3334
"secretAccessKey": "OBJECTSTORAGE_SECRETACCESSKEY"
3435
},
3536
"server": {

app/src/middleware/authorization.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ const hasPermission = (permission) => {
198198
log.debug('Basic authTypes are always permitted', { function: 'hasPermission' });
199199
}
200200
// if reading a public object
201-
else if (req.params.objectId && await isObjectPublic(req.currentObject) && permission === Permissions.READ) {
201+
else if (req.params.objectId && await _isObjectPublic(req.currentObject) && permission === Permissions.READ) {
202202
log.debug('Read requests on public objects are always permitted', { function: 'hasPermission' });
203203
}
204204
// if reading a public bucket
205-
else if (req.params.bucketId && await isBucketPublic(req.params.bucketId) && permission === Permissions.READ) {
205+
else if (req.params.bucketId && await _isBucketPublic(req.params.bucketId) && permission === Permissions.READ) {
206206
log.debug('Read requests on public buckets are always permitted', { function: 'hasPermission' });
207207
}
208208
else if (!await _checkPermission(req, permission)) {
@@ -280,17 +280,17 @@ const checkElevatedUser = async (req, _res, next) => {
280280
* get public status from COMS database
281281
* checks current object and all parent folders
282282
*/
283-
const isObjectPublic = async (currentObject) => {
283+
const _isObjectPublic = async (currentObject) => {
284284
if (currentObject.public) return true;
285-
if (await isBucketPublic(currentObject.bucketId)) return true;
285+
if (await _isBucketPublic(currentObject.bucketId)) return true;
286286
return false;
287287
};
288288

289289
/**
290290
* get public status from COMS database
291291
* checks current folder and all parent folders
292292
*/
293-
const isBucketPublic = async (bucketId) => {
293+
const _isBucketPublic = async (bucketId) => {
294294
const bucket = await bucketService.read(bucketId);
295295
if (bucket.public) return true;
296296
const parentBuckets = await bucketService.searchParentBuckets(bucket);
@@ -305,7 +305,7 @@ module.exports = {
305305
checkS3BasicAccess,
306306
currentObject,
307307
hasPermission,
308-
isBucketPublic,
309-
isObjectPublic,
308+
_isObjectPublic,
309+
_isBucketPublic,
310310
restrictNonIdirUserSearch,
311311
};

app/tests/unit/components/utils.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ describe('getBucket', () => {
130130
.mockReturnValueOnce(cdata.public) // objectStorage.public
131131
.mockReturnValueOnce(cdata.secretAccessKey) // objectStorage.secretAccessKey
132132
.mockReturnValueOnce(cdata.region); // objectStorage.region
133+
config.has.mockReturnValueOnce(true); // config.has(objectStorage.region)
133134

134135
const result = await utils.getBucket();
135136

app/tests/unit/middleware/authorization.spec.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jest.mock('config');
1616
jest.mock('../../../src/components/utils');
1717

1818
const checkPermissionSpy = jest.spyOn(mw, '_checkPermission');
19+
const isObjectPublicSpy = jest.spyOn(mw, '_isObjectPublic');
20+
const isBucketPublicSpy = jest.spyOn(mw, '_isBucketPublic');
1921

2022
beforeEach(() => {
2123
jest.resetAllMocks();
@@ -409,7 +411,7 @@ describe('hasPermission', () => {
409411

410412
// TODO: public folder feature - update tests
411413
// (remove .skip() when done!)
412-
describe.skip('given currentObject with public false and currentUser', () => {
414+
describe('given currentObject with public false and currentUser', () => {
413415
beforeEach(() => {
414416
req.currentObject = {};
415417
req.currentUser = {};
@@ -451,3 +453,51 @@ describe('hasPermission', () => {
451453
});
452454
});
453455
});
456+
457+
describe('isObjectPublic', () => {
458+
459+
beforeAll(() => {
460+
isObjectPublicSpy.mockRestore();
461+
});
462+
463+
// Test cases:
464+
// currentObject.public = true, false
465+
// _isBucketPublic = true, false
466+
467+
it('should return true when object is public and bucket is public', async () => {
468+
// isBucketPublicSpy.mockResolvedValueOnce(true);
469+
const result = await mw._isObjectPublic({ public: true });
470+
471+
expect(result).toEqual(true);
472+
expect(isBucketPublicSpy).toHaveBeenCalledTimes(0);
473+
});
474+
475+
it('should return true when object is public and bucket is not public', async () => {
476+
// isBucketPublicSpy.mockResolvedValueOnce(false);
477+
const result = await mw._isObjectPublic({ public: true });
478+
479+
expect(result).toEqual(true);
480+
expect(isBucketPublicSpy).toHaveBeenCalledTimes(0);
481+
});
482+
483+
it('should return true when object is not public and bucket is public', async () => {
484+
isBucketPublicSpy.mockResolvedValueOnce(true);
485+
486+
const result = await mw._isObjectPublic({ public: false });
487+
488+
expect(result).toEqual(true);
489+
expect(isBucketPublicSpy).toHaveBeenCalledTimes(1);
490+
});
491+
492+
it('should return false when object is not public and bucket is not public', async () => {
493+
isBucketPublicSpy.mockResolvedValueOnce(false);
494+
495+
const result = await mw._isObjectPublic({ public: false });
496+
497+
expect(result).toEqual(false);
498+
expect(isBucketPublicSpy).toHaveBeenCalledTimes(1);
499+
});
500+
501+
502+
503+
});

0 commit comments

Comments
 (0)