Преглед на файлове

Fix bugs to make tests green for user page pagination

sbkwgh преди 8 години
родител
ревизия
e1bd40ff1d
променени са 3 файла, в които са добавени 13 реда и са изтрити 14 реда
  1. 7 5
      models/user.js
  2. 3 6
      routes/user.js
  3. 3 3
      test/user.js

+ 7 - 5
models/user.js

@@ -26,11 +26,13 @@ module.exports = (sequelize, DataTypes) => {
 				let models = sequelize.models
 				let options = models.Post.includeOptions()[0]
 
-				options.where = { id: { $gt: lastId } }
-				options.limit = limit
-				options.order = [['id', 'ASC']]
-
-				return [options]
+				return [{
+					model: models.Post,
+					include: options,
+					limit,
+					where: { id: { $gt: lastId } },
+					order: [['id', 'ASC']]
+				}]
 			}
 		}
 	})

+ 3 - 6
routes/user.js

@@ -129,10 +129,7 @@ router.get('/:username', async (req, res) => {
 			if(+req.query.lastId > 0) lastId = +req.query.lastId
 			if(+req.query.limit > 0) limit = +req.query.limit
 
-			queryObj.include = [{
-				model: Post,
-				include: Post.includeOptions()
-			}]
+			queryObj.include = User.includeOptions(lastId, limit)
 
 			let user = await User.findOne(queryObj)
 			if(!user) throw Errors.accountDoesNotExist
@@ -147,10 +144,10 @@ router.get('/:username', async (req, res) => {
 				resUser.meta.nextURL = null
 			} else {
 				resUser.meta.nextURL =
-					`/api/v1/user/${user.id}?posts?=true&limit=${limit}&lastId=${lastPost.id}`
+					`/api/v1/user/${user.username}?posts=true&limit=${limit}&lastId=${lastPost.id}`
 			}
 
-			res.json(user)
+			res.json(resUser)
 		} else {
 			let user = await User.findOne(queryObj)
 			if(!user) throw Errors.accountDoesNotExist

+ 3 - 3
test/user.js

@@ -336,9 +336,9 @@ describe('User', () => {
 			let agent = chai.request.agent(server)
 
 			await agent
-				.post('/api/v1/user/adminaccount/login')
+				.post('/api/v1/user')
 				.set('content-type', 'application/x-www-form-urlencoded')
-				.send({ password: 'password' })
+				.send({ username: 'paginationaccount', password: 'password' })
 
 			let thread = await agent
 				.post('/api/v1/thread')
@@ -352,7 +352,7 @@ describe('User', () => {
 					.send({ threadId: thread.body.id, content: `POST ${i}` })
 			}
 
-			let pageOne = await agent.get('/api/v1/thread/' + thread.body.id)
+			let pageOne = await agent.get('/api/v1/user/paginationaccount?posts=true')
 			let pageTwo = await agent.get(pageOne.body.meta.nextURL)
 			let pageThree = await agent.get(pageTwo.body.meta.nextURL)
 			let pageInvalid = await agent.get('/api/v1/thread/' + thread.body.id + '?lastId=' + 100)