mardi 31 mai 2016

How to fake IP address in Meteor.Method for testing with chai?

I have a method to store a vote for a specific dataset into the database. If the same dataset has already been added to the database, the IP address is stored in an array in the dataset. If the IP is already in the array the method should throw an error. If the length of the array is over a treshold, the dataset is stored into another collection.

Meteor.methods({
  'validate'(dataset){
     check(dataset, datasetSchema);
     if (!this.connection) {
       throw new Meteor.Error('This method should not be called from the server.');
     }
     if (Meteor.call('notInDatabase', dataset)) { //true if not in database
       dataset.votes = [this.connection.clientAddress];
       MyCollection.insert(dataset);
     } else {
       const datasetFromDB = MyCollection.findOne({hashId: dataset.hashId});
       if(_.contains(datasetFromDB.votes, this.connection.clientAddress)){
         throw new Meteor.Error('Already voted');
       }
       if(compareDatasets(dataset, datasetFromDB)){
         MyCollection.update({_id: datasetFromDB._id}, {$push: {votes: this.connection.clientAddress});
         if(dataset.votes.length + 1 >= treshold){
           //do something
         }
       }
    }         
  }
});

I want to test this method with meteor/practicalmeteor:chai. In order to do this, I have to 'fake' different IP addresses. Is there a way to do this with chai? The only solution I could come up with is to use Meteor.isTest in the method, but it doesn't feel right.

Aucun commentaire:

Enregistrer un commentaire