mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-12 15:24:18 +00:00
Reformat permissions-tests.js to a consistent 4-space indent
This commit is contained in:
parent
d1a4e899d8
commit
9da59d8f04
@ -16,33 +16,33 @@
|
|||||||
//
|
//
|
||||||
////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
'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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function createUsersWithTestRealms(count) {
|
function createUsersWithTestRealms(count) {
|
||||||
const createUserWithTestRealm = () => {
|
const createUserWithTestRealm = () => {
|
||||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password')
|
return Realm.Sync.User
|
||||||
.then(user => {
|
.register('http://localhost:9080', uuid(), 'password')
|
||||||
new Realm({sync: {user, url: 'realm://localhost:9080/~/test'}}).close();
|
.then(user => {
|
||||||
return user;
|
new Realm({sync: {user, url: 'realm://localhost:9080/~/test'}}).close();
|
||||||
});
|
return user;
|
||||||
};
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return Promise.all(Array.from({length: count}, createUserWithTestRealm));
|
return Promise.all(Array.from({length: count}, createUserWithTestRealm));
|
||||||
}
|
}
|
||||||
|
|
||||||
function wait(t) {
|
function wait(t) {
|
||||||
return new Promise(resolve => setTimeout(resolve, t));
|
return new Promise(resolve => setTimeout(resolve, t));
|
||||||
}
|
}
|
||||||
|
|
||||||
function repeatUntil(fn, predicate) {
|
function repeatUntil(fn, predicate) {
|
||||||
@ -58,18 +58,18 @@ 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) {
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
else if (state == Realm.Sync.SubscriptionState.Error) {
|
else if (state == Realm.Sync.SubscriptionState.Error) {
|
||||||
reject();
|
reject();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
setTimeout(() => reject("listener never called"), 5000);
|
setTimeout(() => reject("listener never called"), 5000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function waitForUpload(realm) {
|
function waitForUpload(realm) {
|
||||||
@ -80,151 +80,160 @@ function waitForUpload(realm) {
|
|||||||
session.removeProgressNotification(callback);
|
session.removeProgressNotification(callback);
|
||||||
resolve(realm);
|
resolve(realm);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
session.addProgressNotification('upload', 'forCurrentlyOutstandingWork', callback);
|
session.addProgressNotification('upload', 'forCurrentlyOutstandingWork', callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function permissionForPath(permissions, path) {
|
function permissionForPath(permissions, path) {
|
||||||
for (const permission of permissions) {
|
for (const permission of permissions) {
|
||||||
if (permission.path == path) {
|
if (permission.path == path) {
|
||||||
return permission;
|
return permission;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
return user.applyPermissions({userId: `${user.identity}`}, `/${user.identity}/test`, 'read')
|
.applyPermissions({userId: `${user.identity}`},
|
||||||
.then(repeatUntil(() => user.getGrantedPermissions('any'),
|
`/${user.identity}/test`, 'read')
|
||||||
permissions => {
|
.then(repeatUntil(() => user.getGrantedPermissions('any'),
|
||||||
let permission = permissionForPath(permissions, path);
|
permissions => {
|
||||||
return permission && !permission.mayWrite;
|
let permission = permissionForPath(permissions, path);
|
||||||
}))
|
return permission && !permission.mayWrite;
|
||||||
.then(permissions => {
|
}))
|
||||||
let permission = permissionForPath(permissions, path);
|
.then(permissions => {
|
||||||
TestCase.assertDefined(permission);
|
let permission = permissionForPath(permissions, path);
|
||||||
TestCase.assertEqual(permission.mayRead, true);
|
TestCase.assertDefined(permission);
|
||||||
TestCase.assertEqual(permission.mayWrite, false);
|
TestCase.assertEqual(permission.mayRead, true);
|
||||||
TestCase.assertEqual(permission.mayManage, false);
|
TestCase.assertEqual(permission.mayWrite, false);
|
||||||
});
|
TestCase.assertEqual(permission.mayManage, false);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
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))
|
.then(realmUrl => {
|
||||||
.then(realmUrl => {
|
TestCase.assertEqual(realmUrl, path);
|
||||||
TestCase.assertEqual(realmUrl, path);
|
return realmUrl;
|
||||||
return realmUrl;
|
})
|
||||||
})
|
.then(repeatUntil(() => user2.getGrantedPermissions('any'),
|
||||||
.then(repeatUntil(() => user2.getGrantedPermissions('any'),
|
permissions => permissions.length > 2
|
||||||
permissions => permissions.length > 2 && permissionForPath(permissions, path)))
|
&& permissionForPath(permissions, path)))
|
||||||
.then(permissions => {
|
.then(permissions => {
|
||||||
let permission = permissionForPath(permissions, path)
|
let permission = permissionForPath(permissions, path)
|
||||||
TestCase.assertDefined(permission);
|
TestCase.assertDefined(permission);
|
||||||
TestCase.assertEqual(permission.mayRead, true);
|
TestCase.assertEqual(permission.mayRead, true);
|
||||||
TestCase.assertEqual(permission.mayWrite, false);
|
TestCase.assertEqual(permission.mayWrite, false);
|
||||||
TestCase.assertEqual(permission.mayManage, false);
|
TestCase.assertEqual(permission.mayManage, false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
testInvalidatePermissionOffer() {
|
testInvalidatePermissionOffer() {
|
||||||
let user1, user2, token;
|
let user1, user2, token;
|
||||||
return createUsersWithTestRealms(2)
|
return createUsersWithTestRealms(2)
|
||||||
.then(users => {
|
.then(users => {
|
||||||
user1 = users[0];
|
user1 = users[0];
|
||||||
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);
|
||||||
.then(() => wait(100))
|
})
|
||||||
.then(() => user2.acceptPermissionOffer(token))
|
// Since we don't yet support notification when the invalidation has
|
||||||
// We want the call to fail, i.e. the catch() below should be called.
|
// gone through, wait for a bit and hope the server is done
|
||||||
.then(() => { throw new Error("User was able to accept an invalid permission offer token"); })
|
// processing.
|
||||||
.catch(error => {
|
.then(() => wait(100))
|
||||||
try {
|
.then(() => user2.acceptPermissionOffer(token))
|
||||||
TestCase.assertEqual(error.message, 'The permission offer is expired.');
|
// We want the call to fail, i.e. the catch() below should be
|
||||||
TestCase.assertEqual(error.statusCode, 701);
|
// called.
|
||||||
}
|
.then(() => {
|
||||||
catch (e) {
|
throw new Error("User was able to accept an invalid permission offer token");
|
||||||
throw new Error(e);
|
})
|
||||||
}
|
.catch(error => {
|
||||||
});
|
try {
|
||||||
|
TestCase.assertEqual(error.message, 'The permission offer is expired.');
|
||||||
|
TestCase.assertEqual(error.statusCode, 701);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
testObjectPermissions() {
|
testObjectPermissions() {
|
||||||
let config = (user, url) => {
|
let config = (user, url) => {
|
||||||
return {
|
return {
|
||||||
schema: [
|
schema: [
|
||||||
Realm.Permissions.Permission,
|
Realm.Permissions.Permission,
|
||||||
Realm.Permissions.User,
|
Realm.Permissions.User,
|
||||||
Realm.Permissions.Role,
|
Realm.Permissions.Role,
|
||||||
{
|
{
|
||||||
name: 'Object',
|
name: 'Object',
|
||||||
properties: {
|
properties: {
|
||||||
value: 'int',
|
value: 'int',
|
||||||
permissions: '__Permission[]'
|
permissions: '__Permission[]'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
sync: {user, url, partial: true}
|
sync: {user, url, partial: true}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
let owner, otherUser
|
||||||
let owner, otherUser
|
return Realm.Sync.User
|
||||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password')
|
.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();
|
||||||
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password')
|
return Realm.Sync.User.register('http://localhost:9080', uuid(), 'password')
|
||||||
})
|
})
|
||||||
.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(() => {
|
})
|
||||||
let realm = new Realm(config(owner, 'realm://localhost:9080/~/test'));
|
.then(() => {
|
||||||
realm.write(() => {
|
let realm = new Realm(config(owner, 'realm://localhost:9080/~/test'));
|
||||||
let user = realm.create(Realm.Permissions.User, {id: otherUser.identity})
|
realm.write(() => {
|
||||||
let role = realm.create(Realm.Permissions.Role, {name: 'reader'})
|
let user = realm.create(Realm.Permissions.User, {id: otherUser.identity})
|
||||||
role.members.push(user)
|
let role = realm.create(Realm.Permissions.Role, {name: 'reader'})
|
||||||
|
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}))
|
||||||
});
|
});
|
||||||
return waitForUpload(realm).then(() => realm.close());
|
return waitForUpload(realm).then(() => realm.close());
|
||||||
})
|
})
|
||||||
.then(() => Realm.open(config(otherUser, `realm://localhost:9080/${owner.identity}/test`)))
|
.then(() => Realm.open(config(otherUser, `realm://localhost:9080/${owner.identity}/test`)))
|
||||||
.then((realm) => subscribe(realm.objects('Object')).then(() => realm))
|
.then((realm) => subscribe(realm.objects('Object')).then(() => realm))
|
||||||
.then((realm) => {
|
.then((realm) => {
|
||||||
// Should have full access to the Realm as a whole
|
// Should have full access to the Realm as a whole
|
||||||
TestCase.assertSimilar('object', realm.privileges(),
|
TestCase.assertSimilar('object', realm.privileges(),
|
||||||
{read: true, update: true, modifySchema: true, setPermissions: true});
|
{read: true, update: true, modifySchema: true, setPermissions: true});
|
||||||
TestCase.assertSimilar('object', realm.privileges('Object'),
|
TestCase.assertSimilar('object', realm.privileges('Object'),
|
||||||
{read: true, update: true, create: true, subscribe: true, setPermissions: true});
|
{read: true, update: true, create: true, subscribe: true, setPermissions: true});
|
||||||
// Verify that checking via constructor works too
|
// Verify that checking via constructor works too
|
||||||
TestCase.assertSimilar('object', realm.privileges(Realm.Permissions.User),
|
TestCase.assertSimilar('object', realm.privileges(Realm.Permissions.User),
|
||||||
{read: true, update: true, create: true, subscribe: true, setPermissions: true});
|
{read: true, update: true, create: true, subscribe: true, setPermissions: true});
|
||||||
|
|
||||||
// Should only be able to see the second object
|
// Should only be able to see the second object
|
||||||
let results = realm.objects('Object')
|
let results = realm.objects('Object')
|
||||||
TestCase.assertEqual(results.length, 1);
|
TestCase.assertEqual(results.length, 1);
|
||||||
TestCase.assertEqual(results[0].value, 2);
|
TestCase.assertEqual(results[0].value, 2);
|
||||||
TestCase.assertSimilar('object', realm.privileges(results[0]),
|
TestCase.assertSimilar('object', realm.privileges(results[0]),
|
||||||
{read: true, update: false, delete: false, setPermissions: false});
|
{read: true, update: false, delete: false, setPermissions: false});
|
||||||
realm.close();
|
realm.close();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user