我已将用户角色添加到Meteor应用程序,需要测试某些服务器方法的权限。编写这些方法,例如:
Meteor.methods({
'inventory.update.qty'(id, qty) {
// SECURITY
if (!Roles.userIsInRole(this.userId, ['manage-stock'], 'inventory-items')) {
throw new Meteor.Error('inv.crud', 'Access denied');
}
const modifier = {
$inc: { qty }
};
InventoryItems.update(id, modifier);
},
});
但是如何在运行测试时设置此this.userId
?
答案 0 :(得分:1)
您应该能够使用this.setUserId()
(这是一个绑定到方法调用对象的函数...... this
对象)来执行此操作。
根据您运行测试的方式,您可以定义一个“logMeInAs”流星方法,您可以在运行测试之前将客户端称为特定用户。
Meteor.methods({
signMeIn:function(userId) {
this.setUserId(userId);
}
});
然后在客户端上运行测试之前调用它。
Meteor.call(<id of user you are testing with>);