diff --git a/package.json b/package.json index d53b5716..92f3ee1f 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "dev": "nodemon app.js", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha" }, "repository": { "type": "git", @@ -39,6 +39,10 @@ "sequelize": "^5.6.1", "validator": "^10.11.0" }, + "devDependencies": { + "mocha": "^8.0.0", + "chai": "^4.2.0" + }, "directories": { "test": "test" } diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 00000000..4cbf8d15 --- /dev/null +++ b/tests/test.js @@ -0,0 +1,122 @@ +const chai = require('chai'); +const chaiHttp = require('chai-http'); +const app = require('../app'); + +chai.use(chaiHttp); +const expect = chai.expect; + +describe('Admin API', () => { + it('should register a new admin', (done) => { + chai.request(app) + .post('/v1/admin/register') + .send({ + email: 'test@example.com', + password1: 'password123', + password2: 'password123', + nickname: 'testuser' + }) + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('msg', '注册成功'); + done(); + }); + }); + + it('should login an admin', (done) => { + chai.request(app) + .post('/v1/admin/login') + .send({ + email: 'test@example.com', + password: 'password123' + }) + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('msg', '登录成功'); + expect(res.body).to.have.property('token'); + done(); + }); + }); +}); + +describe('Article API', () => { + it('should create a new article', (done) => { + chai.request(app) + .post('/v1/article') + .send({ + title: 'Test Article', + author: 'Test Author', + content: 'This is a test article.', + cover: 'http://example.com/cover.jpg', + category_id: 1 + }) + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('msg', '创建文章成功'); + done(); + }); + }); + + it('should get article list', (done) => { + chai.request(app) + .get('/v1/article') + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('data'); + done(); + }); + }); +}); + +describe('Category API', () => { + it('should create a new category', (done) => { + chai.request(app) + .post('/v1/category') + .send({ + name: 'Test Category', + key: 'test-category' + }) + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('msg', '创建分类成功'); + done(); + }); + }); + + it('should get category list', (done) => { + chai.request(app) + .get('/v1/category') + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('data'); + done(); + }); + }); +}); + +describe('Comments API', () => { + it('should create a new comment', (done) => { + chai.request(app) + .post('/v1/comments') + .send({ + nickname: 'Test User', + email: 'testuser@example.com', + content: 'This is a test comment.', + article_id: 1 + }) + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('msg', '创建评论成功'); + done(); + }); + }); + + it('should get comments list', (done) => { + chai.request(app) + .get('/v1/comments') + .end((err, res) => { + expect(res).to.have.status(200); + expect(res.body).to.have.property('data'); + done(); + }); + }); +});