项目作者: Pierre-D-G

项目描述 :
Backend API for Wesbos Learn Redux Course
高级语言: JavaScript
项目地址: git://github.com/Pierre-D-G/Reduxstagram-Backend-API.git
创建时间: 2017-07-24T15:41:31Z
项目社区:https://github.com/Pierre-D-G/Reduxstagram-Backend-API

开源协议:

下载


Reduxstagram API

Reduxstagram Backend API for Wesbro’ Learn Redux course

An API created for Wesbos’ [Learn Redux][https://learnredux.com/] to simulate a real backend api to retrieve data from instead of json files.

Developing

Built With

  • Node v8.2.1
  • Express v4.15.3
  • PostgreSQL v9.6
  • Sequelize v4.2.1 as ORM
  • A test driven development approach

Prerequisites

Node

PostgreSQL

Setting up Dev

  1. Download or clone the repository
  1. git clone https://github.com/Pierre-D-G/Reduxstagram-API.git
  1. cd to Reduxtagram API/ or navigate to the root of the repository

  2. Create a database in PostgreSQL with the name of your choosing

  3. Create a .env file in the root directory of the repository with the format:

  1. DB= <the name of the database you created>
  2. DB_HOST=localhost
  3. DB_USER= <Username of the owner of the database>
  4. DB_PASS= <Password of the owner of the database>
  5. SESSION_SECRET=reduxtagram
  1. Run npm install to install dependancies

  2. Run npm start to start the server

Tests

  • run npm run test-unit to run the integration tests which test database input/output

  • run npm run test-integration to run the units tests which test the API endpoints

Test Suite

  • Mocha v2.4.5 as test runner
  • Chai v^3.5.0 as assertion library
  • Chai-http v^2.0.1 for making Http requests to api
  • Supertest v^3.0.0 for making Http requests to routes which require authentication

Example Integration Test

  1. let newUser = {
  2. username: "Redux",
  3. password: '123456',
  4. confirmPassword: '123456',
  5. email: 'test@email.com',
  6. first_name: 'React',
  7. last_name: 'Reduxstagram',
  8. bio: "testing"
  9. }
  10. let dupeUsername = {
  11. username: "Redux",
  12. password: '123456',
  13. confirmPassword: '123456',
  14. email: 'test@dupeusername.com',
  15. first_name: 'React',
  16. last_name: 'Reduxstagram',
  17. bio: "testing"
  18. }
  19. describe('Not register a user if email or username is in use: ', () => {
  20. beforeEach((done) => {
  21. chai.request('http://localhost:3000')
  22. .post('/api/register')
  23. .send(newUser)
  24. .end((err) => {
  25. done();
  26. })
  27. });
  28. afterEach((done) => {
  29. sequelize.sync({ force: true }).then(() => {
  30. done();
  31. });
  32. });
  33. it('it should not register a user if the requested email address is already in use', (done) => {
  34. chai.request('http://localhost:3000')
  35. .post('/api/register')
  36. .send(newUser)
  37. .end((err, res) => {
  38. res.should.have.status(500);
  39. res.should.be.a('object');
  40. res.body.should.have.property('message');
  41. res.body.should.have.property('message').eql('This email address is already in use')
  42. done();
  43. })
  44. })
  45. it('it should not register a user if the requested username is already in use', (done) => {
  46. chai.request('http://localhost:3000')
  47. .post('/api/register')
  48. .send(dupeUsername)
  49. .end((err, res) => {
  50. res.should.have.status(500);
  51. res.should.be.a('object');
  52. res.body.should.have.property('message');
  53. res.body.should.have.property('message').eql('This username is already in use')
  54. done();
  55. })
  56. })
  57. });

Example Unit Test

  1. let newUser = {
  2. userId: '3c207bbb-1e87-4a3f-8cc0-f757e4d5f643',
  3. username: 'Testy',
  4. password: 'pwtest',
  5. email: 'test@gmail.com',
  6. first_name: 'Testing',
  7. last_name: 'Tester',
  8. bio: 'I am an insert test'
  9. },
  10. describe('Insert a user', () => {
  11. it('it should insert the details of a user into the database and return it', (done) => {
  12. const User = models.user;
  13. User.create(newUser, {
  14. returning: true,
  15. raw: true,
  16. plain: true
  17. }).then(user => {
  18. let createdUser = user.dataValues;
  19. expect(createdUser).to.be.a('object');
  20. expect(createdUser).to.have.property('userId').equal('3c207bbb-1e87-4a3f-8cc0-f757e4d5f643');
  21. expect(createdUser).to.have.property('sign_up');
  22. expect(createdUser).to.have.property('username').equal('Testy');
  23. expect(createdUser).to.have.property('password');
  24. expect(createdUser).to.have.property('email').equal('test@gmail.com');
  25. expect(createdUser).to.have.property('first_name').equal('Testing');
  26. expect(createdUser).to.have.property('last_name').equal('Tester');
  27. expect(createdUser).to.have.property('bio').equal('I am an insert test');
  28. done();
  29. })
  30. })
  31. });

Api Reference

Database

Database used - PostgreSQL v9.6

Database Design

Table: photos

photoId: Integer, Primary ID that auto increments

userId: UUID, ID of the user who owns this photo (Indexed field)

caption: String, Photo caption

image_path: String, Path to image

date_created: DateTime, When was this image uploaded?

Table: comments

comment: Text, a text field containing the comment

photoId: Integer, ID of the photo

userId: UUID, ID of user who commented

Table: Likes

userId: UUID, ID of user who liked (Indexed field)

photoId: Integer, ID of the photo being liked (Indexed field)

Table: Users

user_id: UUID, Primary ID

username: String, Username (Unique Index)

email: String, Email address (Unique Index)

salted_password: String, Salted password

bio: Text, user bio

first_name: String, First name of user

last_name: String, Last name of user

sign_up: DateTime, When did this user sign up?

Development Path

Database

  1. * Create database
  2. * Create Tables
  3. - Sequelize Models
  4. - ~~users~~
  5. - ~~photos~~
  6. - ~~comments~~
  7. - ~~likes~~
  8. * ~~Model Associations~~

Tests

  1. * To be created for each route handler
  2. * ~~To be created for database Input/Output~~

Route Handlers

  1. * ~~Authentication~~
  2. * ~~Login~~
  3. * ~~Logout~~
  4. * User
  5. * ~~Get a User's Own Data - for user profile~~
  6. * ~~Create a User - Sign Up~~
  7. * ~~Get User By Id~~
  8. * Photos
  9. * ~~Get a Photo by id~~
  10. * ~~Upload a Photo~~
  11. * ~~Update a photo~~
  12. * ~~Delete a photo~~
  13. * Comments
  14. * ~~Create a Comment~~
  15. * ~~Get a specific Comment~~
  16. * ~~Update a Comment~~
  17. * ~~Delete a Comment~~
  18. * Likes
  19. * Create a Like
  20. * ~~Get a Photo's Likes~~
  21. * delete a Like

Licensing

MIT