refactor tests for sync and async allow/deny configurations

This commit is contained in:
Nacho Codoñer
2024-12-02 17:33:25 +01:00
parent 40c67dd80e
commit c7d88f8cb3

View File

@@ -1225,216 +1225,181 @@ Tinytest.addAsync(
}
);
function configAllAsyncAllowDeny(collection, configType = 'allow', enabled) {
collection[configType]({
async insertAsync(selector, doc) {
if (doc.force) return true;
await Meteor._sleepForMs(100);
return enabled;
},
async updateAsync() {
await Meteor._sleepForMs(100);
return enabled;
},
async removeAsync() {
await Meteor._sleepForMs(100);
return enabled;
},
});
function configAllAllowDeny(collection, configType = 'allow', enabled, isAsync = true) {
const handler = isAsync
? {
async insertAsync(selector, doc) {
if (doc.force) return configType === 'allow';
await Meteor._sleepForMs(100);
return enabled;
},
async updateAsync() {
await Meteor._sleepForMs(100);
return enabled;
},
async removeAsync() {
await Meteor._sleepForMs(100);
return enabled;
},
}
: {
insert(selector, doc) {
if (doc.force) return configType === 'allow';
return enabled;
},
update() {
return enabled;
},
remove() {
return enabled;
},
};
collection[configType](handler);
}
async function runAllAsyncExpect(test, collection, allow) {
async function runAllExpect(test, collection, allow, isAsync = true) {
let id;
/* async tests */
const resolveSyncCallback = (resolve, reject) =>
(error, result) => {
if (error) {
reject(error);
return;
}
resolve(result);
};
const methods = isAsync
? {
insert: async (doc) => await collection.insertAsync(doc),
update: async (id, modifier) => await collection.updateAsync(id, modifier),
remove: async (id) => await collection.removeAsync(id),
}
: {
insert: (doc) =>
new Promise((resolve, reject) =>
collection.insert(doc, resolveSyncCallback(resolve, reject))
),
update: (id, modifier) =>
new Promise((resolve, reject) =>
collection.update(id, modifier, resolveSyncCallback(resolve, reject))
),
remove: (id) =>
new Promise((resolve, reject) =>
collection.remove(id, resolveSyncCallback(resolve, reject))
),
};
try {
id = await collection.insertAsync({ num: 2 });
id = await methods.insert({ num: 2 });
test.isTrue(allow);
} catch (e) {
test.isTrue(!allow);
}
try {
id = await collection.insertAsync({ force: true });
await collection.updateAsync(id, { $set: { num: 22 } });
id = await methods.insert({ force: true });
await methods.update(id, { $set: { num: 22 } });
test.isTrue(allow);
} catch (e) {
test.isTrue(!allow);
}
try {
await collection.removeAsync(id);
id = await methods.insert({ force: true });
await methods.remove(id);
test.isTrue(allow);
} catch (e) {
test.isTrue(!allow);
}
}
var AllowDenyAsyncRulesCollections = {};
const AllowDenyRulesCollections = {};
testAsyncMulti("collection - async definitions on allow/deny rules", [
async function (test) {
AllowDenyAsyncRulesCollections.allowed =
AllowDenyAsyncRulesCollections.allowed ||
new Mongo.Collection(`allowdeny-async-rules-allowed`);
if (Meteor.isServer) {
await AllowDenyAsyncRulesCollections.allowed.removeAsync();
}
function createAllowDenyRulesTest(collections, isAsync = true) {
return [
async function (test) {
const collectionName = `allowdeny-${isAsync ? "async" : "sync"}-rules-noRules`;
collections.noRules =
collections.noRules || new Mongo.Collection(collectionName);
if (Meteor.isServer) {
await collections.noRules.removeAsync();
}
configAllAsyncAllowDeny(AllowDenyAsyncRulesCollections.allowed, 'allow', true);
if (Meteor.isClient) {
await runAllAsyncExpect(test, AllowDenyAsyncRulesCollections.allowed, true);
}
},
async function (test) {
AllowDenyAsyncRulesCollections.notAllowed =
AllowDenyAsyncRulesCollections.notAllowed ||
new Mongo.Collection(`allowdeny-async-rules-notAllowed`);
if (Meteor.isServer) {
await AllowDenyAsyncRulesCollections.notAllowed.removeAsync();
}
configAllAsyncAllowDeny(AllowDenyAsyncRulesCollections.notAllowed, 'allow', false);
if (Meteor.isClient) {
await runAllAsyncExpect(test, AllowDenyAsyncRulesCollections.notAllowed, false);
}
},
async function (test) {
AllowDenyAsyncRulesCollections.denied =
AllowDenyAsyncRulesCollections.denied ||
new Mongo.Collection(`allowdeny-async-rules-denied`);
if (Meteor.isServer) {
await AllowDenyAsyncRulesCollections.denied.removeAsync();
}
configAllAsyncAllowDeny(AllowDenyAsyncRulesCollections.denied, 'deny', true);
if (Meteor.isClient) {
await runAllAsyncExpect(test, AllowDenyAsyncRulesCollections.denied, false);
}
},
]);
function configAllSyncAllowDeny(collection, configType = 'allow', enabled) {
collection[configType]({
insert(selector, doc) {
if (doc.force) return configType === 'allow';
return enabled;
if (Meteor.isClient) {
await runAllExpect(test, collections.noRules, false, isAsync);
}
},
update() {
return enabled;
async function (test) {
const collectionName = `allowdeny-${isAsync ? "async" : "sync"}-rules-allowed`;
collections.allowed =
collections.allowed || new Mongo.Collection(collectionName);
if (Meteor.isServer) {
await collections.allowed.removeAsync();
}
configAllAllowDeny(collections.allowed, 'allow', true, isAsync);
if (Meteor.isClient) {
await runAllExpect(test, collections.allowed, true, isAsync);
}
},
remove() {
return enabled;
async function (test) {
const collectionName = `allowdeny-${isAsync ? "async" : "sync"}-rules-notAllowed`;
collections.notAllowed =
collections.notAllowed || new Mongo.Collection(collectionName);
if (Meteor.isServer) {
await collections.notAllowed.removeAsync();
}
configAllAllowDeny(collections.notAllowed, 'allow', false, isAsync);
if (Meteor.isClient) {
await runAllExpect(test, collections.notAllowed, false, isAsync);
}
},
});
async function (test) {
const collectionName = `allowdeny-${isAsync ? "async" : "sync"}-rules-denied`;
collections.denied =
collections.denied || new Mongo.Collection(collectionName);
if (Meteor.isServer) {
await collections.denied.removeAsync();
}
configAllAllowDeny(collections.denied, 'deny', true, isAsync);
if (Meteor.isClient) {
await runAllExpect(test, collections.denied, false, isAsync);
}
},
async function (test) {
const collectionName = `allowdeny-${isAsync ? "async" : "sync"}-rules-allowThenDeny`;
collections.allowThenDeny =
collections.allowThenDeny || new Mongo.Collection(collectionName);
if (Meteor.isServer) {
await collections.allowThenDeny.removeAsync();
}
configAllAllowDeny(collections.allowThenDeny, 'allow', true, isAsync);
configAllAllowDeny(collections.allowThenDeny, 'deny', true, isAsync);
if (Meteor.isClient) {
await runAllExpect(test, collections.allowThenDeny, false, isAsync);
}
},
async function (test) {
const collectionName = `allowdeny-${isAsync ? "async" : "sync"}-rules-allowThenNotDenied`;
collections.allowThenNotDenied =
collections.allowThenNotDenied || new Mongo.Collection(collectionName);
if (Meteor.isServer) {
await collections.allowThenNotDenied.removeAsync();
}
configAllAllowDeny(collections.allowThenNotDenied, 'allow', true, isAsync);
configAllAllowDeny(collections.allowThenNotDenied, 'deny', false, isAsync);
if (Meteor.isClient) {
await runAllExpect(test, collections.allowThenNotDenied, true, isAsync);
}
},
];
}
async function runAllSyncExpect(test, collection, expected) {
let id;
/* sync tests */
await new Promise((resolve) => {
id = collection.insert({ num: 2 }, (error, result) => {
if (error) {
test.isTrue(!expected);
resolve();
return;
}
test.isTrue(expected && result != null);
resolve();
});
});
testAsyncMulti("collection - async definitions on allow/deny rules", createAllowDenyRulesTest(AllowDenyRulesCollections, true));
await new Promise((resolve) => {
id = collection.insert({ force: true });
collection.update(id, { $set: { num: 22 } }, (error, result) => {
if (error) {
test.isTrue(!expected);
resolve();
return;
}
test.isTrue(expected && result != null);
resolve();
});
});
await new Promise((resolve) => {
id = collection.insert({ force: true });
collection.remove(id, (error, result) => {
if (error) {
test.isTrue(!expected);
resolve();
return;
}
test.isTrue(expected && result != null);
resolve();
});
});
}
var AllowDenySyncRulesCollections = {};
testAsyncMulti("collection - sync definitions on allow/deny rules", [
async function (test) {
AllowDenySyncRulesCollections.allowed =
AllowDenySyncRulesCollections.allowed ||
new Mongo.Collection(`allowdeny-sync-rules-allowed`);
if (Meteor.isServer) {
await AllowDenySyncRulesCollections.allowed.removeAsync();
}
configAllSyncAllowDeny(AllowDenySyncRulesCollections.allowed, 'allow', true);
if (Meteor.isClient) {
await runAllSyncExpect(test, AllowDenySyncRulesCollections.allowed, true);
}
},
async function (test) {
AllowDenySyncRulesCollections.notAllowed =
AllowDenySyncRulesCollections.notAllowed ||
new Mongo.Collection(`allowdeny-sync-rules-notAllowed`);
if (Meteor.isServer) {
await AllowDenySyncRulesCollections.notAllowed.removeAsync();
await AllowDenySyncRulesCollections.notAllowed.insertAsync({ num: 2 });
}
configAllSyncAllowDeny(AllowDenySyncRulesCollections.notAllowed, 'allow', false);
if (Meteor.isClient) {
await runAllSyncExpect(test, AllowDenySyncRulesCollections.notAllowed, false);
}
},
async function (test) {
AllowDenySyncRulesCollections.denied =
AllowDenySyncRulesCollections.denied ||
new Mongo.Collection(`allowdeny-sync-rules-denied`);
if (Meteor.isServer) {
await AllowDenySyncRulesCollections.denied.removeAsync();
}
configAllSyncAllowDeny(AllowDenySyncRulesCollections.denied, 'deny', true);
if (Meteor.isClient) {
await runAllSyncExpect(test, AllowDenySyncRulesCollections.denied, false);
}
},
async function (test) {
AllowDenySyncRulesCollections.noRules =
AllowDenySyncRulesCollections.noRules ||
new Mongo.Collection(`allowdeny-sync-rules-noRules`);
if (Meteor.isServer) {
await AllowDenySyncRulesCollections.noRules.removeAsync();
}
if (Meteor.isClient) {
await runAllSyncExpect(test, AllowDenySyncRulesCollections.noRules, false);
}
},
async function (test) {
AllowDenySyncRulesCollections.allowThenDeny =
AllowDenySyncRulesCollections.allowThenDeny ||
new Mongo.Collection(`allowdeny-sync-rules-allowThenDeny`);
if (Meteor.isServer) {
await AllowDenySyncRulesCollections.allowThenDeny.removeAsync();
}
configAllSyncAllowDeny(AllowDenySyncRulesCollections.allowThenDeny, 'allow', true);
configAllSyncAllowDeny(AllowDenySyncRulesCollections.allowThenDeny, 'deny', true);
if (Meteor.isClient) {
await runAllSyncExpect(test, AllowDenySyncRulesCollections.allowThenDeny, false);
}
},
]);
testAsyncMulti("collection - sync definitions on allow/deny rules", createAllowDenyRulesTest(AllowDenyRulesCollections, false));