Reformat permissions-tests.js to a consistent 4-space indent

This commit is contained in:
Thomas Goyne 2018-03-05 09:46:24 -08:00
parent d1a4e899d8
commit 9da59d8f04
1 changed files with 157 additions and 148 deletions

View File

@ -16,14 +16,13 @@
// //
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
'use strict'; 'use strict';
var Realm = require('realm'); var Realm = require('realm');
var TestCase = require('./asserts'); var TestCase = require('./asserts');
function uuid() { function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16); return v.toString(16);
}); });
@ -31,7 +30,8 @@ function uuid() {
function createUsersWithTestRealms(count) { function createUsersWithTestRealms(count) {
const createUserWithTestRealm = () => { const createUserWithTestRealm = () => {
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password') return Realm.Sync.User
.register('http://localhost:9080', uuid(), 'password')
.then(user => { .then(user => {
new Realm({sync: {user, url: 'realm://localhost:9080/~/test'}}).close(); new Realm({sync: {user, url: 'realm://localhost:9080/~/test'}}).close();
return user; return user;
@ -58,7 +58,7 @@ function repeatUntil(fn, predicate) {
} }
function subscribe(results) { function subscribe(results) {
const subscription = results.subscribe() const subscription = results.subscribe();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
subscription.addListener((subscription, state) => { subscription.addListener((subscription, state) => {
if (state == Realm.Sync.SubscriptionState.Complete) { if (state == Realm.Sync.SubscriptionState.Complete) {
@ -80,7 +80,7 @@ function waitForUpload(realm) {
session.removeProgressNotification(callback); session.removeProgressNotification(callback);
resolve(realm); resolve(realm);
} }
} };
session.addProgressNotification('upload', 'forCurrentlyOutstandingWork', callback); session.addProgressNotification('upload', 'forCurrentlyOutstandingWork', callback);
}); });
} }
@ -95,10 +95,11 @@ function permissionForPath(permissions, path) {
module.exports = { module.exports = {
testApplyAndGetGrantedPermissions() { testApplyAndGetGrantedPermissions() {
return createUsersWithTestRealms(1) return createUsersWithTestRealms(1).then(([user]) => {
.then(([user]) => {
const path = `/${user.identity}/test`; const path = `/${user.identity}/test`;
return user.applyPermissions({userId: `${user.identity}`}, `/${user.identity}/test`, 'read') return user
.applyPermissions({userId: `${user.identity}`},
`/${user.identity}/test`, 'read')
.then(repeatUntil(() => user.getGrantedPermissions('any'), .then(repeatUntil(() => user.getGrantedPermissions('any'),
permissions => { permissions => {
let permission = permissionForPath(permissions, path); let permission = permissionForPath(permissions, path);
@ -115,8 +116,7 @@ module.exports = {
}, },
testOfferPermissions() { testOfferPermissions() {
return createUsersWithTestRealms(2) return createUsersWithTestRealms(2).then(([user1, user2]) => {
.then(([user1, user2]) => {
const path = `/${user1.identity}/test`; const path = `/${user1.identity}/test`;
return user1.offerPermissions(`/${user1.identity}/test`, 'read') return user1.offerPermissions(`/${user1.identity}/test`, 'read')
.then(token => user2.acceptPermissionOffer(token)) .then(token => user2.acceptPermissionOffer(token))
@ -125,7 +125,8 @@ module.exports = {
return realmUrl; return realmUrl;
}) })
.then(repeatUntil(() => user2.getGrantedPermissions('any'), .then(repeatUntil(() => user2.getGrantedPermissions('any'),
permissions => permissions.length > 2 && permissionForPath(permissions, path))) permissions => permissions.length > 2
&& permissionForPath(permissions, path)))
.then(permissions => { .then(permissions => {
let permission = permissionForPath(permissions, path) let permission = permissionForPath(permissions, path)
TestCase.assertDefined(permission); TestCase.assertDefined(permission);
@ -144,13 +145,20 @@ module.exports = {
user2 = users[1]; user2 = users[1];
return user1.offerPermissions(`/${user1.identity}/test`, 'read'); return user1.offerPermissions(`/${user1.identity}/test`, 'read');
}) })
.then(t => { token = t; return user1.invalidatePermissionOffer(token); }) .then(t => {
// Since we don't yet support notification when the invalidation has gone through, token = t;
// wait for a bit and hope the server is done processing. return user1.invalidatePermissionOffer(token);
})
// Since we don't yet support notification when the invalidation has
// gone through, wait for a bit and hope the server is done
// processing.
.then(() => wait(100)) .then(() => wait(100))
.then(() => user2.acceptPermissionOffer(token)) .then(() => user2.acceptPermissionOffer(token))
// We want the call to fail, i.e. the catch() below should be called. // We want the call to fail, i.e. the catch() below should be
.then(() => { throw new Error("User was able to accept an invalid permission offer token"); }) // called.
.then(() => {
throw new Error("User was able to accept an invalid permission offer token");
})
.catch(error => { .catch(error => {
try { try {
TestCase.assertEqual(error.message, 'The permission offer is expired.'); TestCase.assertEqual(error.message, 'The permission offer is expired.');
@ -181,7 +189,8 @@ module.exports = {
}; };
}; };
let owner, otherUser let owner, otherUser
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password') return Realm.Sync.User
.register('http://localhost:9080', uuid(), 'password')
.then(user => { .then(user => {
owner = user; owner = user;
new Realm({sync: {user, url: 'realm://localhost:9080/~/test'}}).close(); new Realm({sync: {user, url: 'realm://localhost:9080/~/test'}}).close();
@ -189,7 +198,8 @@ module.exports = {
}) })
.then(user => { .then(user => {
otherUser = user; otherUser = user;
return owner.applyPermissions({userId: otherUser.identity}, `/${owner.identity}/test`, 'read') return owner.applyPermissions({userId: otherUser.identity},
`/${owner.identity}/test`, 'read')
}) })
.then(() => { .then(() => {
let realm = new Realm(config(owner, 'realm://localhost:9080/~/test')); let realm = new Realm(config(owner, 'realm://localhost:9080/~/test'));
@ -198,8 +208,8 @@ module.exports = {
let role = realm.create(Realm.Permissions.Role, {name: 'reader'}) let role = realm.create(Realm.Permissions.Role, {name: 'reader'})
role.members.push(user) role.members.push(user)
let obj1 = realm.create('Object', {value: 1}) let obj1 = realm.create('Object', {value: 1});
let obj2 = realm.create('Object', {value: 2}) let obj2 = realm.create('Object', {value: 2});
obj2.permissions.push(realm.create(Realm.Permissions.Permission, obj2.permissions.push(realm.create(Realm.Permissions.Permission,
{role: role, canRead: true, canUpdate: false})) {role: role, canRead: true, canUpdate: false}))
}); });
@ -227,4 +237,3 @@ module.exports = {
}); });
} }
} }