I'm currently working on a rest api connecting to MongoDB
Here is my AuthController.js file as a user creates a profile the CardID is generated in the POST request. My question is how do I get the CardID that's been placed into the Database.
exports.register = [
// Validate Fields
body("firstName").isLength({ min: 1 }).trim().withMessage("First name must be specified.")
.isAlphanumeric().withMessage("First name has non-alphanumeric characters."),
body("lastName").isLength({ min: 1 }).trim().withMessage("Last name must be specified.")
.isAlphanumeric().withMessage("Last name has non-alphanumeric characters."),
body("email").isLength({ min: 1 }).trim().withMessage("Email must be specified.")
.isEmail().withMessage("Email must be a valid email address.")
.custom((value) => {
return UserModel.findOne({email : value}).then((user) => {
if (user) {
return Promise.reject("E-mail already in use");
}
});
}),
body("phoneNumber").isLength({ min: 1 }).trim().withMessage("Phone number must be specificed.")
.isMobilePhone('en-GB').withMessage("Phone Number must be a valid UK number."),
body("pin").isLength({ min: 4, max: 4 }).trim().withMessage("Pin must be 4 characters.")
.isInt().withMessage("Pin must be numeric only."),
// Check fields.
check("firstName").escape(),
check("lastName").escape(),
check("email").escape(),
check("phoneNumber").escape(),
check("pin").escape(),
(req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
// Display sanitized values/errors messages.
return apiResponse.validationErrorWithData(res, "Validation Error.", errors.array());
} else {
try {
bcrypt.hash(req.body.pin, 10, function(err, hash) {
const user = new UserModel({
UEID: "ueid-" + uuidv4(),
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
phoneNumber: req.body.phoneNumber,
pin: hash,
cardID: utility.randomString(16, '#aA')
})
const newUser = user.save(function (err) {
if (err) return apiResponse.ErrorResponse(res, err);
return apiResponse.successResponseWithData(res,"Registration Success.", newUser);
});
})
} catch (err) {
//throw error in json response with status 500.
return apiResponse.ErrorResponse(res, err);
}
}}
]
Here is my test file as you can see I can't get to the CardID through the testData
describe("Auth", () => {
// Before each test we empty the database
before((done) => {
UserModel.deleteMany({}, (err) => { enter code here
done();
});
});
// Prepare data for testing
const testData = {
"firstName": "test",
"lastName": "testing",
"email": "test@testing.com",
"phoneNumber": "07899848930",
"pin": "1234",
"email": "test@testing.com"
};
/*
* Test the /POST route
*/
describe("/POST Tapcard", () => {
it("It should ask for registration if cardID is not available", (done) => {
chai.request(server)
.post("/api/auth/tapcard/")
.send({ "cardID": "dk3l4lkgmv3nt3f4" })
.end((err, res) => {
res.should.have(401);
res.body.should.have.property("message").eql("Please register this card.");
done();
});
});
it("It should login", (done) => {
chai.request(server)
.post("api/auth/tapcard/")
.send({ "cardID" : 'GNmSZ7Zv7BYf4UuA' })
.end((err, res) => {
res.should.have(200);
res.body.should.have.property("message").eql("Please enter pin.");
done();
})
})
})
/*
* Test the /POST route
*/
describe("/POST Login", () => {
it("It should send validation error for Login", (done) => {
chai.request(server)
.post("/api/auth/tapcard/login")
.send({"cardID": testData.cardID})
.end((err, res) => {
res.should.have.status(400);
done();
});
});
});
Aucun commentaire:
Enregistrer un commentaire