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

Add tests for existing code #11

Open
wants to merge 1 commit 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
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -39,6 +39,10 @@
"sequelize": "^5.6.1",
"validator": "^10.11.0"
},
"devDependencies": {
"mocha": "^8.0.0",
"chai": "^4.2.0"
},
"directories": {
"test": "test"
}
Expand Down
122 changes: 122 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});