Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documented #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typings/
# dotenv environment variables file
.env
.env.test
.env.development

# parcel-bundler cache (https://parceljs.org/)
.cache
Expand All @@ -78,3 +79,5 @@ typings/

# DynamoDB Local files
.dynamodb/

package-lock.json
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
# back-end
# Authentication API
## Register JSON { "username": "Required", "password": "Required" }
### https://wunderlist3.herokuapp.com/api/auth/register

## Login JSON { "username": "Required", "password": "Required" }
### https://wunderlist3.herokuapp.com/api/auth/login
### Successful login returns JSON { "token": "jsonwebtoken" }

## Get users list
### https://wunderlist3.herokuapp.com/api/auth

# Category API (requires jsonwebtoken)
<p><code>headers:{ authorization: `Bearer ${token}` }</code></p>

## Get current user's categories
### https://wunderlist3.herokuapp.com/api/category

## Create a new category JSON { "category": "Required" }
### https://wunderlist3.herokuapp.com/api/category

## Update a category JSON { "category": "Required", "change": "Required" }
## "change": "is the category name to update to"
### https://wunderlist3.herokuapp.com/api/category

## Delete a category JSON { "category": "Required" }
### https://wunderlist3.herokuapp.com/api/category

# Task API (requires jsonwebtoken)
## Get my tasks
### https://wunderlist3.herokuapp.com/api/task

## Create task JSON { "category": "Required", "task": "Required", "description": "Optional", "scheduled": "Optional" }
### https://wunderlist3.herokuapp.com/api/task

## Update task JSON { "category": "Required", "task": "Required", "description": "Optional", "scheduled": "Optional", "changeTask": "Optional" }
## "changeTask": "is the task name to update to"
### https://wunderlist3.herokuapp.com/api/task

## Delete a task JSON { "category": "Required", "task": "Required" }
### https://wunderlist3.herokuapp.com/api/task
11 changes: 11 additions & 0 deletions api/server.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const request = require("supertest")
const db = require("../database/dbConfig.js")

const server = require("./server.js");

describe("server.js", () => {
test(" api: up" , async () => {
const res = await request(server).get("/")
expect(res.body).toEqual({ api: "up" })
})
})
35 changes: 35 additions & 0 deletions auth/auth-router.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const request = require("supertest")
const server = require("../api/server")
const db = require("../database/dbConfig.js")

describe("auth-router", () => {
afterEach(async () => {
await db("users")
})

describe("valid-register", () => {
it("creates user", async () => {
const randomUser = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
const req = await request(server)
.post("/api/auth/register")
.send({
username: `${randomUser}`,
password: "password"
})
.set("Accept", "application/json")
.expect(201)
})
})

describe("invalid-register", () => {
it("requires username/password", async () => {
const req = await request(server)
.post("/api/auth/register")
.send({
username: "test-user"
})
.set("Accept", "application/json")
.expect(406)
})
})
})
22 changes: 0 additions & 22 deletions auth/authRouter.spec.js

This file was deleted.

7 changes: 2 additions & 5 deletions auth/authenticate-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ const { jwtSecret } = require("../config/secrets.js")
module.exports = (req, res, next) => {
try {
const token = req.headers.authorization.split(" ")[1]

if (token) {
jwt.verify( token, jwtSecret, (err, decodedToken) => {
if (err) {
res.status(401).json({ message: "Unauthorized!" })
res.status(401).json(err)
} else {
req.decodedJwt = decodedToken
next()
}
})
} else {
throw new Error("Invalid token.")
}
} catch (err) {
res.status(401).json({ error: "You don't have a bearer token!" })
res.status(401).json(err)
}
}
2 changes: 1 addition & 1 deletion auth/generateToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function generateToken(user) {
username: user.username
}
const secret = jwtSecret
const options = { expiresIn: "1h" }
const options = { expiresIn: "2h" }

return jwt.sign(payload, secret, options)
}
Expand Down
45 changes: 45 additions & 0 deletions categories/categories-router.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const request = require("supertest")
const server = require("../api/server")

describe("category-router", () => {

let token
beforeAll((done) => {
request(server)
.post('/api/auth/login')
.send({
username: 'user1',
password: 'password',
})
.end((err, res) => {
token = res.body.token
done()
})
})

describe('POST /', () => {

it('return 401 unauthorized', async () => {
const randomCat = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
const res = await request(server)
.post('/api/category')
.set('Accept', 'application/json')
.send({
category: `${randomCat}`
})
expect(res.status).toBe(401)
})

it('returns 200 OK', async () => {
const randomCat = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
const res = await request(server)
.post('/api/category')
.set('Accept', 'application/json')
.set('authorization', `Bearer ${token}` )
.send({
category: `${randomCat}`
})
expect(res.status).toBe(200)
})
})
})
Binary file added database/backup-wunderlist.db3
Binary file not shown.
Binary file added database/bk2wunderlist.db3
Binary file not shown.
4 changes: 3 additions & 1 deletion database/dbConfig.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const knex = require("knex")
const knexConfig = require("../knexfile.js")

module.exports = knex(knexConfig.development)
const dbEnv = process.env.DB_ENV || 'development'

module.exports = knex(knexConfig[dbEnv])
Binary file added database/testing-wunderlist.db3
Binary file not shown.
Binary file modified database/wunderlist.db3
Binary file not shown.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require("dotenv").config()

const server = require("./api/server.js")

const PORT = process.env.PORT || 3300
server.listen(PORT, () => {
console.log(`\n=== Server listening on port ${PORT} ===\n`)
Expand Down
13 changes: 13 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ module.exports = {
connection: {
filename: "./database/wunderlist.db3"
},
seeds: {
directory: "./database"
},
useNullAsDefault: true
},
testing: {
client: "sqlite3",
connection: {
filename: "./database/testing-wunderlist.db3"
},
seeds: {
directory: "./database"
},
useNullAsDefault: true
}
}
49 changes: 15 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 4 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
{
"name": "back-end",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "cross-env DB_ENV=testing jest --watch --verbose",
"server": "nodemon index.js",
"start": "node index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Build-Week-Wunderlist3/back-end.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Build-Week-Wunderlist3/back-end/issues"
},
"homepage": "https://github.com/Build-Week-Wunderlist3/back-end#readme",
"dependencies": {
"axios": "^0.19.2",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"gitignore": "^0.6.0",
"helmet": "^3.23.1",
"jsonwebtoken": "^8.5.1",
"knex": "^0.21.1",
"knex-cleaner": "^1.3.0",
"sqlite3": "^4.2.0"
},
"devDependencies": {
"cross-env": "^7.0.2",
"jest": "^26.0.1",
"nodemon": "^2.0.4",
"supertest": "^4.0.2"
},
"jest": {
"testEnvironment": "node"
}
}
Loading