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

後端API測試 (Boxun/Jenny) #888

Open
wants to merge 162 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
162 commits
Select commit Hold shift + click to select a range
4cda3c1
feat: project init
boxunw Aug 22, 2023
0de9981
fix: modify lockfileVersion to 1
boxunw Aug 22, 2023
7f2448e
fix: modify package.lock.json for version consistence
boxunw Aug 22, 2023
aadac6d
feat: initialize project & add models
yanyanyaa Aug 24, 2023
6b799b9
feat: add Eslint package
boxunw Aug 24, 2023
0315728
merge feature/userlogin
boxunw Aug 24, 2023
9eb2e50
feat: add signup & login function
boxunw Aug 25, 2023
b7c5f62
merge orign master
boxunw Aug 25, 2023
a725a6c
Merge pull request #1 from boxunw/feature/userlogin
yanyanyaa Aug 25, 2023
4b40826
fix: modify login and admin login
boxunw Aug 25, 2023
9dbfef8
Merge pull request #2 from boxunw/feature/userlogin
yanyanyaa Aug 25, 2023
4cc64cc
feat: add getTweets
yanyanyaa Aug 25, 2023
c62c184
Merge remote-tracking branch 'origin/master' into feature/getTweets
yanyanyaa Aug 25, 2023
a538788
merge origin master
yanyanyaa Aug 25, 2023
0e7e314
Merge pull request #3 from boxunw/feature/getTweets
boxunw Aug 26, 2023
1185cf3
fix: modify cross-env installation
boxunw Aug 26, 2023
eb7e83b
feat: modify config for heroku
boxunw Aug 26, 2023
a6ed041
feat: add getTweets success status
boxunw Aug 26, 2023
c4cc17f
feat: add CORS
boxunw Aug 26, 2023
174ba07
fix: modify cors origin
boxunw Aug 26, 2023
2ac88b8
fix: modify cors allow origin
boxunw Aug 26, 2023
ac4ed56
fix: modify cors to all origin
boxunw Aug 26, 2023
95ef62a
fix: modify CORS comment
boxunw Aug 26, 2023
997c517
feat: add ensureAuthenticated to _helper
boxunw Aug 27, 2023
7541e27
feat: add admin getTweets
yanyanyaa Aug 27, 2023
bd7d99e
Merge branch 'master' into feature/getTweets
boxunw Aug 27, 2023
cb82745
Merge pull request #4 from boxunw/feature/getTweets
boxunw Aug 27, 2023
23c2756
feature: add catch err
yanyanyaa Aug 27, 2023
f8653d4
feat: add getTweet
yanyanyaa Aug 27, 2023
c994354
feat: add getTweetReplies
yanyanyaa Aug 27, 2023
1a88ca7
Merge pull request #5 from boxunw/feature/getTweet
boxunw Aug 27, 2023
a2f9429
fix: modify passwordCheck to checkPassword
boxunw Aug 27, 2023
59e6f47
Merge branch 'master' into feature/getUser
boxunw Aug 27, 2023
e9b0a0d
Merge pull request #6 from boxunw/feature/getUser
yanyanyaa Aug 27, 2023
20dfe6c
feat: add getUser
boxunw Aug 27, 2023
d7b441e
merge
yanyanyaa Aug 27, 2023
a76e62b
merge
yanyanyaa Aug 27, 2023
9586961
merge
yanyanyaa Aug 27, 2023
aeb4445
fix: modify login & signup & getuser res body format
boxunw Aug 27, 2023
5216015
feat: add getUser api
boxunw Aug 27, 2023
8b0e485
Merge branch 'master' into feature/getUser
boxunw Aug 27, 2023
35c9e08
Merge pull request #7 from boxunw/feature/getUser
yanyanyaa Aug 27, 2023
7558ec6
fix: modify getTweets and getTweet response data format
yanyanyaa Aug 27, 2023
ffab374
feat: add getCurrentUser
boxunw Aug 27, 2023
fd878a4
Merge pull request #8 from boxunw/feature/getCurrentUser
yanyanyaa Aug 27, 2023
321c08f
feat: add postTweet
yanyanyaa Aug 27, 2023
c1a5faa
feat: add postTweetReply
yanyanyaa Aug 28, 2023
bc98e08
Merge remote-tracking branch 'origin' into feature/postTweet
yanyanyaa Aug 28, 2023
aa00ee5
feat: add addFolllowing
yanyanyaa Aug 28, 2023
9735428
Merge pull request #9 from boxunw/feature/postTweet
boxunw Aug 28, 2023
4f8fa48
feat: add follow/unfollow
yanyanyaa Aug 28, 2023
0514588
feat: modidy data format
yanyanyaa Aug 28, 2023
d5f32e3
Merge pull request #10 from boxunw/feature/postTweet
boxunw Aug 28, 2023
320da68
merge master
yanyanyaa Aug 28, 2023
a2871f4
feat: add putUserAccount
boxunw Aug 28, 2023
983a254
feat: add addFollowing and removeFollow
yanyanyaa Aug 28, 2023
71ec414
Merge pull request #11 from boxunw/feature/follow
boxunw Aug 28, 2023
f1fc135
faet: finish setUserAccount
boxunw Aug 28, 2023
b5061c6
Merge branch 'master' into feature/setUserAccount
boxunw Aug 28, 2023
2c16633
Merge pull request #12 from boxunw/feature/setUserAccount
yanyanyaa Aug 28, 2023
fca5fbe
merge master
yanyanyaa Aug 28, 2023
6936d3a
Merge pull request #13 from boxunw/feature/follow
boxunw Aug 28, 2023
5808300
feat: add postTweetLike
yanyanyaa Aug 28, 2023
77343c3
feat: add postTweetUnlike
yanyanyaa Aug 28, 2023
f77b8ba
feat: add putUserProfile
boxunw Aug 28, 2023
828c22f
Merge branch 'master' into feature/putUserProfile
boxunw Aug 28, 2023
9f89256
Merge pull request #14 from boxunw/feature/putUserProfile
yanyanyaa Aug 28, 2023
1c5c7d1
feat: add isLiked on getTweets and getTweet
yanyanyaa Aug 28, 2023
2c3e90a
Merge remote-tracking branch 'origin' into feature/like
yanyanyaa Aug 28, 2023
2e04f48
Merge pull request #15 from boxunw/feature/like
boxunw Aug 29, 2023
81fe264
fix: modify all helper.getuser in user controller
boxunw Aug 29, 2023
1913d60
Merge branch 'master' into feature/putUserProfile
boxunw Aug 29, 2023
967e4dc
Merge pull request #16 from boxunw/feature/putUserProfile
yanyanyaa Aug 29, 2023
2b789f4
feat: add getUserTweets
yanyanyaa Aug 29, 2023
2bdb00c
feat: add getUserReplies
yanyanyaa Aug 29, 2023
8bde7ca
Merge 'origin'
yanyanyaa Aug 29, 2023
874c37c
feat: modify postTweetReply
yanyanyaa Aug 29, 2023
34bb0d5
Merge pull request #17 from boxunw/getUserTweets
boxunw Aug 29, 2023
539c88f
feat: add getUserLikes
boxunw Aug 29, 2023
7affc9f
fix: modify sequelize require
boxunw Aug 29, 2023
0a69c54
fix: modify sequelize require
boxunw Aug 29, 2023
64f51f1
feat: finish getUserLikes
boxunw Aug 29, 2023
8771028
feat: finish getUserLikes
boxunw Aug 29, 2023
9b7af0b
merge master
boxunw Aug 29, 2023
5975ec3
Merge pull request #18 from boxunw/feature/getUserLikes
yanyanyaa Aug 29, 2023
9d078e8
fix: modify getUser & getCurrentUser
boxunw Aug 29, 2023
9c3b150
Merge pull request #19 from boxunw/fix/getuser
yanyanyaa Aug 29, 2023
05f35b2
fix: modify Tweet model
yanyanyaa Aug 29, 2023
0cb68d0
fix: modify Reply medel
yanyanyaa Aug 29, 2023
e2c7b12
feat: tidy up
yanyanyaa Aug 29, 2023
1ca6e47
merge origin master
yanyanyaa Aug 29, 2023
44803c6
Merge pull request #20 from boxunw/feature/getFollowers
boxunw Aug 29, 2023
331794a
feat: add getFollowers
yanyanyaa Aug 30, 2023
12ac263
feat: modify getFollowings and getFollowers
yanyanyaa Aug 30, 2023
f62b1e6
feat: modify getTweetReplies
yanyanyaa Aug 30, 2023
824eaef
feat: add get top 10 user
boxunw Aug 30, 2023
ba7d88e
Merge pull request #21 from boxunw/feature/gettop
yanyanyaa Aug 30, 2023
a1958bc
feat: modify getFollowings and getFollowers
yanyanyaa Aug 30, 2023
fbed670
merge master
yanyanyaa Aug 30, 2023
8e0d11b
Merge pull request #22 from boxunw/feature/getFollowers
boxunw Aug 30, 2023
d07907e
feat: add admin get users
boxunw Aug 30, 2023
ff4b2f1
Merge branch 'master' into feature/admin_users
boxunw Aug 30, 2023
e6a85d8
fix: remove unnecessary declarations
boxunw Aug 30, 2023
b855f6d
Merge pull request #23 from boxunw/feature/admin_users
yanyanyaa Aug 30, 2023
56924bf
feat: modify postTweetReplies
yanyanyaa Aug 30, 2023
b03db17
Merge origin master
yanyanyaa Aug 30, 2023
458a019
Merge pull request #24 from boxunw/feature/getFollowers
boxunw Aug 30, 2023
3a0df80
feat: add delete tweet
boxunw Aug 30, 2023
39f0a80
Merge branch 'master' into feature/deletetweet
boxunw Aug 30, 2023
d269fa4
Merge pull request #25 from boxunw/feature/deletetweet
yanyanyaa Aug 30, 2023
699650b
fix: modify postTweet and potTweetReplies
yanyanyaa Aug 30, 2023
50be301
merge origin master
yanyanyaa Aug 30, 2023
f62e420
Merge pull request #26 from boxunw/bugFix
boxunw Aug 30, 2023
e8c5eba
feat: modify postTweets and addFollowing
yanyanyaa Aug 31, 2023
1b4f207
Merge pull request #27 from boxunw/bugFix
boxunw Aug 31, 2023
8d390a4
merge origin master
yanyanyaa Aug 31, 2023
80b03bc
feat: mofify getUserTweets
yanyanyaa Aug 31, 2023
f0ad565
Merge pull request #29 from boxunw/bugFix
boxunw Aug 31, 2023
9c87ec7
fix: modify admin getusers
boxunw Aug 31, 2023
18b3f67
Merge branch 'master' into fix/admin_users
boxunw Aug 31, 2023
35b88af
Merge pull request #30 from boxunw/fix/admin_users
yanyanyaa Aug 31, 2023
1d4d362
fix: add travis env
boxunw Aug 31, 2023
af5d26e
fix: modify travis node version
boxunw Aug 31, 2023
348f8c3
fix: modify travis
boxunw Aug 31, 2023
906088e
feat: add user seed data
yanyanyaa Aug 31, 2023
8fc8461
Merge remote-tracking branch 'origin' into seeder/user
yanyanyaa Aug 31, 2023
287931a
feat: add tweets seed data
yanyanyaa Aug 31, 2023
39bf595
Merge pull request #31 from boxunw/seeder/user
boxunw Aug 31, 2023
ae43b9a
feat: add replies seed
boxunw Aug 31, 2023
3710994
modify CORS accepted origins
boxunw Aug 31, 2023
da3759d
fix: modify reply seed
boxunw Aug 31, 2023
5faa9ca
Merge branch 'master' into feature/replyseed
boxunw Aug 31, 2023
f56490f
Merge pull request #32 from boxunw/feature/replyseed
yanyanyaa Aug 31, 2023
1b2b695
fix: modify getfollowers
boxunw Aug 31, 2023
fa7794c
fix: addFollowing bug fix
yanyanyaa Aug 31, 2023
150429f
fix: getFollowing and getFollower bug fix
yanyanyaa Aug 31, 2023
4574dfa
Merge branch 'master' into bugFix
boxunw Aug 31, 2023
85e2cc8
Merge pull request #33 from boxunw/bugFix
boxunw Aug 31, 2023
83c00c1
fix: modify getUserTweets isliked
boxunw Sep 1, 2023
e5dead0
Create README.md
yanyanyaa Sep 2, 2023
c68538e
Merge pull request #34 from boxunw/READ.md
boxunw Sep 2, 2023
17d3e51
Update README.md
yanyanyaa Sep 2, 2023
b8eff83
Merge pull request #35 from boxunw/README.md
boxunw Sep 2, 2023
85c20ea
Update README.md
yanyanyaa Sep 2, 2023
552b3be
Merge pull request #36 from boxunw/README.md-1
boxunw Sep 2, 2023
a0192c5
Update README.md
boxunw Sep 2, 2023
d7e6050
fix: modify post tweet like response
boxunw Sep 2, 2023
d0fae29
Update README.md
boxunw Sep 3, 2023
1236f12
feat: prepare AWS deployment
boxunw Sep 26, 2023
413f069
chore: revise node.js version in package.json
boxunw Sep 26, 2023
6fc378e
chore: delete aws eb folder
boxunw Sep 26, 2023
a412feb
chore: revise aws deployment files
boxunw Sep 26, 2023
2dc3381
chore: revise aws deployment setting
boxunw Sep 26, 2023
34358fe
chore: revise aws deployment setting
boxunw Sep 26, 2023
a2ea860
chore: revise aws deployment setting
boxunw Sep 26, 2023
921b02d
chore: revise aws deployment setting
boxunw Sep 26, 2023
25f37a8
chore: delete aws deployment setting
boxunw Sep 26, 2023
02dfcba
feat: aws deployment
boxunw Oct 1, 2023
0b8296d
Merge pull request #37 from boxunw/aws_deployment3
boxunw Oct 3, 2023
f125589
fix: delete seeder migrate in migration.config
boxunw Oct 21, 2023
d7d09e1
fix: revise admin login action
boxunw Oct 24, 2023
ff58f30
Update README.md
boxunw Nov 23, 2023
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
5 changes: 5 additions & 0 deletions .ebextensions/migration.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
option_settings:
container_commands:
01_schema_migrate:
command: ./node_modules/.bin/sequelize db:migrate
leader_only: true
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
JWT_SECRET=
IMGUR_CLIENT_ID=
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/*
/tests/*
12 changes: 12 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
env:
browser: true
commonjs: true
es2021: true
extends:
- standard
parserOptions:
ecmaVersion: 12
rules:
arrow-parens:
- warn
- as-needed
53 changes: 53 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
* text=auto

# Source code
*.css text diff=css
*.html text diff=html
*.js text
*.json text

# Documentation
*.markdown text diff=markdown
*.md text diff=markdown
*.txt text

# Templates
*.handlebars text
*.hbs text

# Configs
*.cnf text
*.conf text
*.config text
.editorconfig text
.env text
.env.* text
.gitattributes text
.gitconfig text
*.lock text -diff
package.json text eol=lf
package-lock.json text -diff
yarn.lock text -diff
*.yaml text
*.yml text
browserslist text

# Heroku
Procfile text

# Graphics
*.gif binary
*.ico binary
*.jpg binary
*.jpeg binary
*.pdf binary
*.png binary
# SVG treated as an asset (binary) by default.
*.svg text
# If you want to treat it as binary,
# use the following line instead.
# *.svg binary
*.webp binary

# Ignore files (like .npmignore or .gitignore)
*.*ignore text
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,13 @@ typings/
.fusebox/

# DynamoDB Local files
.dynamodb/
.dynamodb/

# image upload directories
temp/
upload/

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "14"
- "16"

services:
- mysql
Expand All @@ -13,6 +13,7 @@ env:
before_install:
- mysql -e 'CREATE DATABASE IF NOT EXISTS ac_twitter_workspace_test default character set utf8mb4 collate utf8mb4_unicode_ci;'
- npm install sequelize-cli -g
- export JWT_SECRET=alphacamp

# 在執行測試之前的指令
before_script:
Expand Down
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: NODE_ENV=production node app.js
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Simple Twitter API
- 這是一個類 Twtter 的社群網站,透過推文、回覆、喜歡、追蹤等功能與其他人進行互動
- 本專案採用前後分離的開發模式,並透過 RESTful 風格進行設計
- 後端成員:[Boxun](https://github.com/boxunw), [Jenny](https://github.com/yanyanyaa)
- 前端成員:[James](https://github.com/James-Lee-01), [雅云](https://github.com/Ya-Yun-Zheng)
- [前端 repo](https://github.com/James-Lee-01/simple-twitter)
- [網站入口](https://simple-twitter-eight.vercel.app)

# 功能
- 使用者可以註冊/登入帳號
- 使用者可以瀏覽所有推文
- 使用者可以回覆所有推文
- 使用者可以喜歡/收回喜歡任何推文
- 使用者可以追蹤/取消追蹤其他使用者
- 使用者可以瀏覽其他使用者的個人頁面
- 使用者可以編輯個人資料與帳戶設定
- 後台管理者可以瀏覽所有推文清單,並且快覽推文的前 50 字
- 後台管理者可以瀏覽所有使用者清單,並附有使用者社群活躍數據
- 後台管理者可以刪除任何一篇推文
# API 文件
[API List](https://documenter.getpostman.com/view/29236995/2s9Y5cu1Rv#f83c365c-642e-4f90-b6c1-185551187722)
# 安裝
1. 請先確認有安裝 node.js 與 npm
2. 打開終端機,clone 此專案
```
git clone https://github.com/boxunw/simple-twitter-api.git
```
3. 打開專案資料夾
```
cd simple-twitter-api
```
4. 安裝 npm 套件
```
npm install
```
5. 參考 .env.example 檔案,建立 .env 檔案並設定環境變數
6. 打開 /config/config.json 檔案,修改為資料庫使用的帳號密碼
```
"development": {
"username": "<your_mysql_workbench_name>", // 修改此處
"password": "<your_mysql_workbench_password>", // 修改此處
"database": "ac_twitter_workspace",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "<your_mysql_workbench_name>", // 修改此處
"password": "<your_mysql_workbench_password>", // 修改此處
"database": "ac_twitter_workspace_test",
"host": "127.0.0.1",
"dialect": "mysql",
"logging": false
},
```
7. 建立資料庫,可在 MySQL Workbench 輸入以下指令
```
create database ac_twitter_workspace;
create database ac_twitter_workspace_test;
```
8. 建立資料表
```
npx sequelize db:migrate
```
9. 建立種子資料
```
npx sequelize db:seed:all
```
10. 啟動伺服器
```
npm run dev
```
11. 當終端機出現以下字樣,表示伺服器已成功啟動
```
Example app listening on port 3000!
```
# 測試帳號
```
管理者
Account: root
Password: 12345678

一般使用者
Account: user1
Password: 12345678
```
# 自動化測試
1. 切換至測試環境
```
export NODE_ENV=test
```
2. 執行自動化測試(全部)
```
npm run test
```
3. 執行自動化測試(單一檔案)
```
npx mocha test/requests/user.spec.js --exit // 範例
```


11 changes: 8 additions & 3 deletions _helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@

function getUser(req) {
return req.user;
function getUser (req) {
return req.user || null
}

function ensureAuthenticated (req) {
return req.isAuthenticated()
}

module.exports = {
getUser,
};
ensureAuthenticated
}
35 changes: 28 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const path = require('path')
const express = require('express')
const helpers = require('./_helpers');
const cors = require('cors')
const methodOverride = require('method-override')
const { apis } = require('./routes')
const passport = require('./config/passport')

const app = express()
const port = 3000
const port = process.env.PORT || 3000

// use helpers.getUser(req) to replace req.user
function authenticated(req, res, next){
// passport.authenticate('jwt', { ses...
};
// set CORS
const corsOptions = {
origin: [
'https://james-lee-01.github.io',
'http://localhost:3000'
],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
allowedHeaders: ['Content-Type', 'Authorization']
}

app.get('/', (req, res) => res.send('Hello World!'))
app.use(cors(corsOptions))

app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride('_method'))
app.use('/upload', express.static(path.join(__dirname, 'upload')))
app.use('/api', apis)
app.get('/', (req, res) => res.json({ status: 'success' }))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))

module.exports = app
9 changes: 3 additions & 6 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
"logging": false
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
"use_env_variable": "MYSQL_DATABASE_URL",
"operatorsAliases": false
},
"travis": {
"username": "travis",
Expand All @@ -34,4 +31,4 @@
"host": "127.0.0.1",
"dialect": "mysql"
}
}
}
44 changes: 43 additions & 1 deletion config/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,47 @@
const passport = require('passport')
const LocalStrategy = require('passport-local')
const passportJWT = require('passport-jwt')
const bcrypt = require('bcryptjs')
const { User } = require('../models')

const JWTStrategy = passportJWT.Strategy
const ExtractJWT = passportJWT.ExtractJwt

// set up Passport Local strategy
passport.use(new LocalStrategy(
// customize user field
{
usernameField: 'account',
passwordField: 'password'
},
// authenticate user
(account, password, cb) => {
User.findOne({ where: { account } })
.then(user => {
if (!user) return cb(Error('帳號不存在'), false)
bcrypt.compare(password, user.password).then(res => {
if (!res) return cb(Error('輸入密碼錯誤'), false)
return cb(null, user)
})
})
.catch(err => cb(err))
}
))

module.exports = passport
// set up Passport Jwt strategy
const jwtOptions = {
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET
}
passport.use(new JWTStrategy(jwtOptions, (jwtPayload, cb) => {
User.findByPk(jwtPayload.id, {
include: [
{ model: User, as: 'Followers' },
{ model: User, as: 'Followings' }
]
})
.then(user => cb(null, user))
.catch(err => cb(err))
}))

module.exports = passport
Loading