mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
clean up,
tests, fixes
This commit is contained in:
@@ -13,13 +13,15 @@ const TodoSchema = Schema({
|
|||||||
});
|
});
|
||||||
|
|
||||||
TodoSchema.pre('save', async function () {
|
TodoSchema.pre('save', async function () {
|
||||||
const user = await this.model('User').findById(this.user);
|
if (this.isNew) {
|
||||||
user.todos.push(this._id);
|
const user = await this.model('User').findById(this.user);
|
||||||
await user.save();
|
user.todos.push(this._id);
|
||||||
|
await user.save();
|
||||||
|
|
||||||
const list = await this.model('TodoList').findById(this.list);
|
const list = await this.model('TodoList').findById(this.list);
|
||||||
list.todos.push(this._id);
|
list.todos.push(this._id);
|
||||||
await list.save();
|
await list.save();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
TodoSchema.pre('remove', async function () {
|
TodoSchema.pre('remove', async function () {
|
||||||
|
|||||||
@@ -12,16 +12,22 @@ const TodoListSchema = Schema({
|
|||||||
});
|
});
|
||||||
|
|
||||||
TodoListSchema.pre('save', async function () {
|
TodoListSchema.pre('save', async function () {
|
||||||
const user = await this.model('User').findById(this.user);
|
if (this.isNew) {
|
||||||
user.lists.push(this._id);
|
const user = await this.model('User').findById(this.user);
|
||||||
await user.save();
|
user.lists.push(this._id);
|
||||||
|
await user.save();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
TodoListSchema.pre('remove', async function () {
|
TodoListSchema.pre('remove', async function () {
|
||||||
const user = await this.model('User').findById(this.user);
|
const user = await this.model('User').findById(this.user);
|
||||||
user.lists.splice(user.todos.indexOf(this._id), 1);
|
user.lists.splice(user.lists.indexOf(this._id), 1);
|
||||||
await user.save();
|
await user.save();
|
||||||
await this.model('Todo').remove({ list: this._id });
|
|
||||||
|
const todos = await this.model('Todo')
|
||||||
|
.find({ list: this._id })
|
||||||
|
.exec();
|
||||||
|
await Promise.all(todos.map(todo => todo.remove()));
|
||||||
});
|
});
|
||||||
|
|
||||||
TodoListSchema.methods.toJson = function () {
|
TodoListSchema.methods.toJson = function () {
|
||||||
|
|||||||
@@ -22,8 +22,15 @@ UserSchema.plugin(passportLocalMongoose);
|
|||||||
UserSchema.plugin(uniqueValidator);
|
UserSchema.plugin(uniqueValidator);
|
||||||
|
|
||||||
UserSchema.pre('remove', async function () {
|
UserSchema.pre('remove', async function () {
|
||||||
await this.model('TodoList').remove({ user: this._id });
|
await this.model('TodoList')
|
||||||
await this.model('Todo').remove({ user: this._id });
|
.find({ user: this._id })
|
||||||
|
.remove()
|
||||||
|
.exec();
|
||||||
|
|
||||||
|
await this.model('Todo')
|
||||||
|
.find({ user: this._id })
|
||||||
|
.remove()
|
||||||
|
.exec();
|
||||||
});
|
});
|
||||||
|
|
||||||
UserSchema.methods.generateJwt = function () {
|
UserSchema.methods.generateJwt = function () {
|
||||||
|
|||||||
28
package-lock.json
generated
28
package-lock.json
generated
@@ -2975,12 +2975,14 @@
|
|||||||
"balanced-match": {
|
"balanced-match": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"brace-expansion": {
|
"brace-expansion": {
|
||||||
"version": "1.1.11",
|
"version": "1.1.11",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"balanced-match": "^1.0.0",
|
"balanced-match": "^1.0.0",
|
||||||
"concat-map": "0.0.1"
|
"concat-map": "0.0.1"
|
||||||
@@ -2995,17 +2997,20 @@
|
|||||||
"code-point-at": {
|
"code-point-at": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"concat-map": {
|
"concat-map": {
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"console-control-strings": {
|
"console-control-strings": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"core-util-is": {
|
"core-util-is": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
@@ -3122,7 +3127,8 @@
|
|||||||
"inherits": {
|
"inherits": {
|
||||||
"version": "2.0.3",
|
"version": "2.0.3",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"ini": {
|
"ini": {
|
||||||
"version": "1.3.5",
|
"version": "1.3.5",
|
||||||
@@ -3134,6 +3140,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"number-is-nan": "^1.0.0"
|
"number-is-nan": "^1.0.0"
|
||||||
}
|
}
|
||||||
@@ -3148,6 +3155,7 @@
|
|||||||
"version": "3.0.4",
|
"version": "3.0.4",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"brace-expansion": "^1.1.7"
|
"brace-expansion": "^1.1.7"
|
||||||
}
|
}
|
||||||
@@ -3155,12 +3163,14 @@
|
|||||||
"minimist": {
|
"minimist": {
|
||||||
"version": "0.0.8",
|
"version": "0.0.8",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"minipass": {
|
"minipass": {
|
||||||
"version": "2.2.4",
|
"version": "2.2.4",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"safe-buffer": "^5.1.1",
|
"safe-buffer": "^5.1.1",
|
||||||
"yallist": "^3.0.0"
|
"yallist": "^3.0.0"
|
||||||
@@ -3179,6 +3189,7 @@
|
|||||||
"version": "0.5.1",
|
"version": "0.5.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"minimist": "0.0.8"
|
"minimist": "0.0.8"
|
||||||
}
|
}
|
||||||
@@ -3259,7 +3270,8 @@
|
|||||||
"number-is-nan": {
|
"number-is-nan": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true
|
"dev": true,
|
||||||
|
"optional": true
|
||||||
},
|
},
|
||||||
"object-assign": {
|
"object-assign": {
|
||||||
"version": "4.1.1",
|
"version": "4.1.1",
|
||||||
@@ -3271,6 +3283,7 @@
|
|||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"wrappy": "1"
|
"wrappy": "1"
|
||||||
}
|
}
|
||||||
@@ -3392,6 +3405,7 @@
|
|||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"bundled": true,
|
"bundled": true,
|
||||||
"dev": true,
|
"dev": true,
|
||||||
|
"optional": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"code-point-at": "^1.0.0",
|
"code-point-at": "^1.0.0",
|
||||||
"is-fullwidth-code-point": "^1.0.0",
|
"is-fullwidth-code-point": "^1.0.0",
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ const router = express.Router();
|
|||||||
const TodoList = mongoose.model('TodoList');
|
const TodoList = mongoose.model('TodoList');
|
||||||
|
|
||||||
const asyncHelper = require('../asyncHelper');
|
const asyncHelper = require('../asyncHelper');
|
||||||
const auth = require('./auth');
|
|
||||||
const { NotFoundError } = require('../errors');
|
const { NotFoundError } = require('../errors');
|
||||||
|
|
||||||
// index
|
// index
|
||||||
@@ -36,9 +35,7 @@ router.delete(
|
|||||||
'/:listId',
|
'/:listId',
|
||||||
asyncHelper(async (req, res) => {
|
asyncHelper(async (req, res) => {
|
||||||
const { listId } = req.params;
|
const { listId } = req.params;
|
||||||
const list = await TodoList.find({ _id: listId, user: req.user.id })
|
const list = await TodoList.findOne({ _id: listId, user: req.user.id }).exec();
|
||||||
.populate('todos')
|
|
||||||
.exec();
|
|
||||||
await list.remove();
|
await list.remove();
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
}),
|
}),
|
||||||
@@ -50,7 +47,7 @@ router.patch(
|
|||||||
asyncHelper(async (req, res) => {
|
asyncHelper(async (req, res) => {
|
||||||
const { listId } = req.params;
|
const { listId } = req.params;
|
||||||
const { name } = req.body;
|
const { name } = req.body;
|
||||||
const list = await TodoList.find({ _id: listId, user: req.user.id });
|
const list = await TodoList.findOne({ _id: listId, user: req.user.id });
|
||||||
if (!list) {
|
if (!list) {
|
||||||
throw new NotFoundError("can't find list");
|
throw new NotFoundError("can't find list");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ router.patch(
|
|||||||
asyncHelper(async (req, res) => {
|
asyncHelper(async (req, res) => {
|
||||||
const { todoId } = req.params;
|
const { todoId } = req.params;
|
||||||
const { text, completed } = req.body;
|
const { text, completed } = req.body;
|
||||||
const todo = await Todo.find({ _id: todoId, user: req.user.id });
|
const todo = await Todo.findOne({ _id: todoId, user: req.user.id });
|
||||||
if (!todo) {
|
if (!todo) {
|
||||||
throw new NotFoundError("can't find todo");
|
throw new NotFoundError("can't find todo");
|
||||||
}
|
}
|
||||||
@@ -46,6 +46,7 @@ router.patch(
|
|||||||
if (completed !== undefined) {
|
if (completed !== undefined) {
|
||||||
todo.completed = completed;
|
todo.completed = completed;
|
||||||
}
|
}
|
||||||
|
await todo.save();
|
||||||
res.json({ success: true, data: todo.toJson() });
|
res.json({ success: true, data: todo.toJson() });
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@@ -55,7 +56,7 @@ router.delete(
|
|||||||
'/:todoId',
|
'/:todoId',
|
||||||
asyncHelper(async (req, res) => {
|
asyncHelper(async (req, res) => {
|
||||||
const { todoId } = req.params;
|
const { todoId } = req.params;
|
||||||
const todo = await Todo.find({ _id: todoId, user: req.user.id }).exec();
|
const todo = await Todo.findOne({ _id: todoId, user: req.user.id }).exec();
|
||||||
if (!todo) {
|
if (!todo) {
|
||||||
throw new NotFoundError(`can't find todo with id ${todoId}`);
|
throw new NotFoundError(`can't find todo with id ${todoId}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,8 @@ router.delete(
|
|||||||
'/user',
|
'/user',
|
||||||
auth.required,
|
auth.required,
|
||||||
asyncHelper(async (req, res) => {
|
asyncHelper(async (req, res) => {
|
||||||
await User.findByIdAndRemove(req.user.id).exec();
|
const user = await User.findById(req.user.id).exec();
|
||||||
|
await user.remove();
|
||||||
res.json({ success: true });
|
res.json({ success: true });
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -40,12 +40,39 @@ afterAll(async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('test lists', () => {
|
describe('test lists', () => {
|
||||||
|
test('should index lists', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.get('/lists')
|
||||||
|
.set('Authorization', `Bearer ${token}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body.success).toBeTruthy();
|
||||||
|
expect(response.body.data[0].name).toEqual('List1');
|
||||||
|
});
|
||||||
test('should not index lists without authentication', async () => {
|
test('should not index lists without authentication', async () => {
|
||||||
await request(server)
|
await request(server)
|
||||||
.get('/lists')
|
.get('/lists')
|
||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
.expect(401);
|
.expect(401);
|
||||||
});
|
});
|
||||||
|
test('should create list', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.post('/lists')
|
||||||
|
.send({
|
||||||
|
name: 'List2',
|
||||||
|
})
|
||||||
|
.set('Authorization', `Bearer ${token}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body.success).toBeTruthy();
|
||||||
|
expect(await TodoList.findOne({ name: 'List2' })).toBeTruthy();
|
||||||
|
const freshUser = await User.findById(user.id).exec();
|
||||||
|
expect(freshUser.lists).toContain(response.body.data.id);
|
||||||
|
});
|
||||||
test('should not create list without authentication', async () => {
|
test('should not create list without authentication', async () => {
|
||||||
await request(server)
|
await request(server)
|
||||||
.post('/lists')
|
.post('/lists')
|
||||||
@@ -56,4 +83,56 @@ describe('test lists', () => {
|
|||||||
.set('Accept', 'application/json')
|
.set('Accept', 'application/json')
|
||||||
.expect(401);
|
.expect(401);
|
||||||
});
|
});
|
||||||
|
test('should update list', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.patch(`/lists/${list._id}`)
|
||||||
|
.send({
|
||||||
|
name: 'List2',
|
||||||
|
})
|
||||||
|
.set('Authorization', `Bearer ${token}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body.success).toBeTruthy();
|
||||||
|
expect(await TodoList.findOne({ name: 'List2' })).toBeTruthy();
|
||||||
|
});
|
||||||
|
test('should not update list without authentication', async () => {
|
||||||
|
await request(server)
|
||||||
|
.patch(`/lists/${list._id}`)
|
||||||
|
.send({
|
||||||
|
name: 'List2',
|
||||||
|
})
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(401);
|
||||||
|
expect(await TodoList.findOne({ name: 'List2' })).toBeFalsy();
|
||||||
|
});
|
||||||
|
test('should remove list', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.delete(`/lists/${list._id}`)
|
||||||
|
.set('Authorization', `Bearer ${token}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body.success).toBeTruthy();
|
||||||
|
expect(await TodoList.findOne({ name: 'List1' }).exec()).toBeFalsy();
|
||||||
|
expect(await Todo.findOne({ text: 'Todo1' }).exec()).toBeFalsy();
|
||||||
|
const freshUser = await User.findById(user.id).exec();
|
||||||
|
expect(freshUser.lists).not.toContain(list._id);
|
||||||
|
expect(freshUser.todos).not.toContain(todo._id);
|
||||||
|
});
|
||||||
|
test('should not remove list without authentication', async () => {
|
||||||
|
await request(server)
|
||||||
|
.delete(`/lists/${list._id}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(401);
|
||||||
|
expect(await TodoList.findOne({ name: 'List1' }).exec()).toBeTruthy();
|
||||||
|
expect(await Todo.findOne({ text: 'Todo1' }).exec()).toBeTruthy();
|
||||||
|
const freshUser = await User.findById(user.id).exec();
|
||||||
|
expect(freshUser.lists).toContain(list._id);
|
||||||
|
expect(freshUser.todos).toContain(todo._id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,43 +1,6 @@
|
|||||||
const server = require('../../app.js');
|
const server = require('../../app.js');
|
||||||
|
|
||||||
const request = require('supertest');
|
const request = require('supertest');
|
||||||
const mongoose = require('mongoose');
|
|
||||||
|
|
||||||
const Todo = mongoose.model('Todo');
|
|
||||||
const TodoList = mongoose.model('TodoList');
|
|
||||||
const User = mongoose.model('User');
|
|
||||||
|
|
||||||
jest.setTimeout(60000);
|
|
||||||
const MongoDBMemoryServer = require('mongodb-memory-server').default;
|
|
||||||
const { seed, clean } = require('./utils');
|
|
||||||
|
|
||||||
let user;
|
|
||||||
let token;
|
|
||||||
let list;
|
|
||||||
let todo;
|
|
||||||
let mongoServer;
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
mongoServer = new MongoDBMemoryServer();
|
|
||||||
const mongoUri = await mongoServer.getConnectionString();
|
|
||||||
await mongoose.connect(mongoUri);
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
({
|
|
||||||
user, token, list, todo,
|
|
||||||
} = await seed());
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(async () => {
|
|
||||||
await clean();
|
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(async () => {
|
|
||||||
await mongoose.disconnect();
|
|
||||||
await mongoServer.stop();
|
|
||||||
await server.close();
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('test not found', () => {
|
describe('test not found', () => {
|
||||||
test('respond not found with json', async () => {
|
test('respond not found with json', async () => {
|
||||||
|
|||||||
137
tests/integration/todos.test.js
Normal file
137
tests/integration/todos.test.js
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
const server = require('../../app.js');
|
||||||
|
|
||||||
|
const request = require('supertest');
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
const Todo = mongoose.model('Todo');
|
||||||
|
const User = mongoose.model('User');
|
||||||
|
|
||||||
|
jest.setTimeout(60000);
|
||||||
|
const MongoDBMemoryServer = require('mongodb-memory-server').default;
|
||||||
|
const { seed, clean } = require('./utils');
|
||||||
|
|
||||||
|
let user;
|
||||||
|
let token;
|
||||||
|
let list;
|
||||||
|
let todo;
|
||||||
|
let mongoServer;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
mongoServer = new MongoDBMemoryServer();
|
||||||
|
const mongoUri = await mongoServer.getConnectionString();
|
||||||
|
await mongoose.connect(mongoUri);
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
({
|
||||||
|
user, token, list, todo,
|
||||||
|
} = await seed());
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await clean();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await mongoose.disconnect();
|
||||||
|
await mongoServer.stop();
|
||||||
|
await server.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('test todos', () => {
|
||||||
|
test('should index todos', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.get(`/lists/${list._id}/todos`)
|
||||||
|
.set('Authorization', `Bearer ${token}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body.success).toBeTruthy();
|
||||||
|
expect(response.body.data[0].text).toEqual('Todo1');
|
||||||
|
});
|
||||||
|
test('should not index todos without authentication', async () => {
|
||||||
|
await request(server)
|
||||||
|
.get(`/lists/${list._id}/todos`)
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(401);
|
||||||
|
});
|
||||||
|
test('should create todo', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.post(`/lists/${list._id}/todos`)
|
||||||
|
.send({
|
||||||
|
text: 'Todo2',
|
||||||
|
})
|
||||||
|
.set('Authorization', `Bearer ${token}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body.success).toBeTruthy();
|
||||||
|
expect(await Todo.findOne({ text: 'Todo2', list: list._id })).toBeTruthy();
|
||||||
|
const freshUser = await User.findById(user.id).exec();
|
||||||
|
expect(freshUser.todos).toContain(response.body.data.id);
|
||||||
|
const freshList = await User.findById(user.id).exec();
|
||||||
|
expect(freshList.todos).toContain(response.body.data.id);
|
||||||
|
});
|
||||||
|
test('should not create todo without authentication', async () => {
|
||||||
|
await request(server)
|
||||||
|
.post(`/lists/${list._id}/todos`)
|
||||||
|
.send({
|
||||||
|
text: 'Todo1',
|
||||||
|
})
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(401);
|
||||||
|
});
|
||||||
|
test('should update todo', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.patch(`/lists/${list._id}/todos/${todo._id}`)
|
||||||
|
.send({
|
||||||
|
text: 'Todo2',
|
||||||
|
})
|
||||||
|
.set('Authorization', `Bearer ${token}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body.success).toBeTruthy();
|
||||||
|
expect(await Todo.findOne({ text: 'Todo2' })).toBeTruthy();
|
||||||
|
expect(await Todo.findOne({ text: 'Todo1' })).toBeFalsy();
|
||||||
|
});
|
||||||
|
test('should not update todo without authentication', async () => {
|
||||||
|
await request(server)
|
||||||
|
.patch(`/lists/${list._id}/todos/${todo._id}`)
|
||||||
|
.send({
|
||||||
|
text: 'Todo2',
|
||||||
|
})
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(401);
|
||||||
|
expect(await Todo.findOne({ text: 'Todo1' })).toBeTruthy();
|
||||||
|
expect(await Todo.findOne({ text: 'Todo2' })).toBeFalsy();
|
||||||
|
});
|
||||||
|
test('should remove todo', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.delete(`/lists/${list._id}/todos/${todo._id}`)
|
||||||
|
.set('Authorization', `Bearer ${token}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body.success).toBeTruthy();
|
||||||
|
expect(await Todo.findOne({ text: 'Todo1' }).exec()).toBeFalsy();
|
||||||
|
const freshUser = await User.findById(user.id).exec();
|
||||||
|
expect(freshUser.todos).not.toContain(todo.id);
|
||||||
|
const freshList = await User.findById(user.id).exec();
|
||||||
|
expect(freshList.todos).not.toContain(todo.id);
|
||||||
|
});
|
||||||
|
test('should not remove todo without authentication', async () => {
|
||||||
|
await request(server)
|
||||||
|
.delete(`/lists/${list._id}/todos/${todo._id}`)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(401);
|
||||||
|
expect(await Todo.findOne({ text: 'Todo1' }).exec()).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -13,10 +13,7 @@ const MongoDBMemoryServer = require('mongodb-memory-server').default;
|
|||||||
const { seed, clean } = require('./utils');
|
const { seed, clean } = require('./utils');
|
||||||
const { secret } = require('../../config');
|
const { secret } = require('../../config');
|
||||||
|
|
||||||
let user;
|
|
||||||
let token;
|
let token;
|
||||||
let list;
|
|
||||||
let todo;
|
|
||||||
let mongoServer;
|
let mongoServer;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
@@ -26,9 +23,7 @@ beforeAll(async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
({
|
({ token } = await seed());
|
||||||
user, token, list, todo,
|
|
||||||
} = await seed());
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
@@ -160,6 +155,8 @@ describe('test users', () => {
|
|||||||
.expect(401);
|
.expect(401);
|
||||||
expect(response.body.success).toBeFalsy();
|
expect(response.body.success).toBeFalsy();
|
||||||
expect(await User.findOne({ username: 'User1' }).exec()).toBeTruthy();
|
expect(await User.findOne({ username: 'User1' }).exec()).toBeTruthy();
|
||||||
|
expect(await TodoList.findOne({ name: 'List1' }).exec()).toBeTruthy();
|
||||||
|
expect(await Todo.findOne({ text: 'Todo1' })).toBeTruthy();
|
||||||
});
|
});
|
||||||
test('should delete user', async () => {
|
test('should delete user', async () => {
|
||||||
const response = await request(server)
|
const response = await request(server)
|
||||||
@@ -171,5 +168,7 @@ describe('test users', () => {
|
|||||||
.expect('Content-Type', 'application/json; charset=utf-8');
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
expect(response.body.success).toBeTruthy();
|
expect(response.body.success).toBeTruthy();
|
||||||
expect(await User.findOne({ username: 'User1' }).exec()).toBeFalsy();
|
expect(await User.findOne({ username: 'User1' }).exec()).toBeFalsy();
|
||||||
|
expect(await TodoList.findOne({ name: 'List1' }).exec()).toBeFalsy();
|
||||||
|
expect(await Todo.findOne({ text: 'Todo1' }).exec()).toBeFalsy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user