Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
};
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 2 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ security_questions:
useGoogleAuth: true
security_question: true
gmail: true
test_db:
"mongodb://product:[email protected]:11441/auth-infra-server-test"
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions test/models/User.js
Original file line number Diff line number Diff line change
@@ -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;
142 changes: 142 additions & 0 deletions test/register-login-tests.js
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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 protected]");
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: "[email protected]",
password: "Bi$$420"
};

const user_doesnt_exist = {
email: "[email protected]",
password: "69biss_cant_stop_dis_hoe420"
};

const wrong_pass = {
email: "[email protected]",
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);
});
});