diff --git a/.eslintrc.js b/.eslintrc.js index c1233b4..8b3018f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -6,13 +6,18 @@ module.exports = { jsx: true } }, + plugins: [ + "mocha" + ], extends: "eslint:recommended", env: { amd: true, node: true, - es6: true + es6: true, + mocha: true }, rules: { - "no-console": "off" + "no-console": "off", + "mocha/no-exclusive-tests": "error" } }; diff --git a/README.md b/README.md index 39c5510..a47354f 100644 --- a/README.md +++ b/README.md @@ -17,3 +17,32 @@ yarn yarn start ``` +# <<<<<<< HEAD + +Documentation: https://h4i-auth-infra-docs.now.sh/ + +API: https://github.com/hack4impact-uiuc/infra-authentication-api/ + +Client Example: https://github.com/hack4impact-uiuc/infra-authentication-client + +# To Run Locally + +``` +yarn +yarn start +``` + +> > > > > > > a26c9bd7d7a36a970f5a75191a0319e6b12b8598 + +Documentation: https://h4i-auth-infra-docs.now.sh/ + +API: https://github.com/hack4impact-uiuc/infra-authentication-api/ + +Client Example: https://github.com/hack4impact-uiuc/infra-authentication-client + +# To Run Locally + +``` +yarn +yarn start +``` diff --git a/config.yml b/config.yml index d256a08..ac0efe4 100644 --- a/config.yml +++ b/config.yml @@ -18,3 +18,5 @@ security_questions: useGoogleAuth: true security_question: true gmail: true +test_db: + "mongodb://product:infra28@ds111441.mlab.com:11441/auth-infra-server-test" diff --git a/package.json b/package.json index edd6a58..01462f6 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "infra-authentication-server", "scripts": { "start": "nodemon src/startServer.js", - "docs": "docz dev" + "docs": "docz dev", + "test": "mocha" }, "repository": { "type": "git", @@ -21,6 +22,7 @@ "eslint": "^5.15.1", "eslint-config-airbnb-base": "^13.1.0", "eslint-plugin-import": "^2.16.0", + "eslint-plugin-mocha": "^5.3.0", "express": "^4.16.4", "express-jwt": "^5.3.1", "express-validator": "^5.3.1", diff --git a/test/models/User.js b/test/models/User.js new file mode 100644 index 0000000..d7e03ce --- /dev/null +++ b/test/models/User.js @@ -0,0 +1,19 @@ +const mongoose = require("mongoose"); + +const schema = mongoose.Schema({ + username: "string", + password: "string", + email: "string", + question: "string", + answer: "string", + pin: "number", + verified: "boolean", + expiration: "date", + userLevel: "string", + googleAuth: "boolean", + role: "string" +}); + +const User = mongoose.model("TestUser", schema); + +module.exports = User; diff --git a/test/register-login-tests.js b/test/register-login-tests.js new file mode 100644 index 0000000..9e1d7b8 --- /dev/null +++ b/test/register-login-tests.js @@ -0,0 +1,142 @@ +const app = require("../src/App"); +const request = require("supertest"); +const User = require("../test/models/User.js"); +const mongoose = require("mongoose"); +const assert = require("assert"); +const { getTestURI } = require("../src/utils/getConfigFile"); +let server; + +before(async () => { + // Make a DB connection before starting the tests so the first test + // doesn't throw off timing if doing performance testing TTFB + User.startSession(); + var options = { + useNewUrlParser: true + }; + // connect test_db and clear it before starting + await mongoose.connect(await getTestURI(), options); + await mongoose.connection.db + .dropDatabase() + .catch(error => console.log("Trying to drop", error)); + server = app.listen(8000); +}); + +after(async () => { + // wait for both the server close and the mongoose connection to finish + await mongoose.connection.db + .dropDatabase() + .catch(() => console.log("Trying to drop")); + await server.close(); + await mongoose.connection.close(); +}); + +describe("connection test", function() { + it("connection established and test_db cleared", async () => { + assert(1 === 1); + }); +}); + +const valid_register_test = { + email: "lmao_biss69@gmail.com", + password: "Bi$$420", + role: "guest", + questionIdx: 0, + answer: "yes" +}; + +describe("POST /register", function() { + it("returns 400 for empty body", async () => { + const response = await request(app) + .post("/register") + .type("form") + .send(""); + assert.equal(400, response.body.status); + assert.equal("Invalid Request", response.body.message); + }); + + it("returns 400 for invalid email", async () => { + const response = await request(app) + .post("/register") + .type("form") + .send("email=093j"); + assert.equal(400, response.body.status); + assert.equal("Invalid Request", response.body.message); + }); + + it("returns 400 for no password", async () => { + const response = await request(app) + .post("/register") + .type("form") + .send("email=helga_test@infra.org"); + assert.equal(400, response.body.status); + assert.equal("Invalid Request", response.body.message); + }); + + it("returns 200 for valid user", async function() { + const response = await request(app) + .post("/register") + .type("form") + .send(valid_register_test); + assert.equal(200, response.body.status); + assert.equal("User added successfully!", response.body.message); + }).timeout(5000); // add a longer timeout since there's a lot that has to get done when adding a user +}); + +const valid_login_test = { + email: "lmao_biss69@gmail.com", + password: "Bi$$420" +}; + +const user_doesnt_exist = { + email: "lmao_biss69@infra.org", + password: "69biss_cant_stop_dis_hoe420" +}; + +const wrong_pass = { + email: "lmao_biss69@gmail.com", + password: "bissssss6969" +}; + +describe("POST /login", function() { + it("returns 400 for no input", async () => { + const response = await request(app) + .post("/login") + .type("form") + .send(""); + assert.equal(400, response.body.status); + assert.equal("Invalid Request", response.body.message); + }); + + it("returns 400 for no such user in database", async () => { + const response = await request(app) + .post("/login") + .type("form") + .send(user_doesnt_exist); + assert.equal(400, response.body.status); + assert.equal( + "The information you provided does not match our database. Please check your inputs again.", + response.body.message + ); + }); + + it("returns 400 for wrong password", async () => { + const response = await request(app) + .post("/login") + .type("form") + .send(wrong_pass); + assert.equal(400, response.body.status); + assert.equal( + "Password incorrect. Please try again.", + response.body.message + ); + }); + + it("returns 200 for successful login", async () => { + const response = await request(app) + .post("/login") + .type("form") + .send(valid_login_test); + assert.equal(200, response.body.status); + assert.equal("Successful login!", response.body.message); + }); +});