vendredi 22 février 2019

Using Cypress fixtures and a third party library like diskdb

I am currently using diskdb (using local json files as a db) on this app and want to write a test that checks that the admin user can successfully update a user password.

I created an auth.json fixture file because I don't want to read and specially write to the real file and have my test modify a real user password.

So far I can read from the fixture file on login , then stub the post request body with the newly updated password but then how can I have cypress write the updated password to the fixture file knowing that my app's using a diskdb method that is configured when the server start and points to the original json file ? Once this is achieved, the test would continue with trying to login again with the updated password.

So far I only succeeded writing to the real file. I have seen cy.writeFile but as diskdb behaves like MongoDB (the body request password is massaged and encrypted before being saved) I don't see how I could be helpful.

Server.js

var db = require("diskdb");

// this connect the db to a local file named auth in the data folder
db.connect("data/", ["auth"]);

app.post("/update-password", (req, res) => {
  const { passwordOperator, passwordCustomer } = req.body;

  let salt = bcrypt.genSaltSync(saltRounds);

  if (passwordOperator) {
    // look for the operator object in the db
    const operator = db.auth.findOne({ isOperator: true });
    let operatorHash = bcrypt.hashSync(passwordOperator, salt);
    const newOperator = {
      username: "operator1",
      isOperator: true,
      password: operatorHash
    };
    db.auth.update(operator, newOperator);
  }

  if (passwordCustomer) {
    // look for the customer object in the db
    const customer = db.auth.findOne({ isOperator: false });
    let customerHash = bcrypt.hashSync(passwordCustomer, salt);
    const newCustomer = {
      username: "customer1",
      isOperator: false,
      password: customerHash
    };
    db.auth.update(customer, newCustomer);
  }
  res.send(db.auth.find());
});

How can I stub this process while still testing the encryption and data save works correctly ?

Aucun commentaire:

Enregistrer un commentaire