ソースを参照

Fix tests and implement them to delete user

sbkwgh 8 年 前
コミット
a7ff1103ba
2 ファイル変更43 行追加9 行削除
  1. 30 0
      routes/user.js
  2. 13 9
      test/user.js

+ 30 - 0
routes/user.js

@@ -350,4 +350,34 @@ router.put('/:username', async (req, res) => {
 	}
 })
 
+router.delete('/:username', async (req, res) => {
+	let validationErrors = []
+
+	try {
+		if(req.session.username !== req.params.username) {
+			validationErrors.push(Errors.requestNotAuthorized)
+			throw validationErrors
+		}
+
+		let user = await User.findOne({ where: {
+			username: req.session.username
+		}})
+
+		await user.destroy()
+
+		res.json({ success: true })
+
+	} catch (e) {
+		if(validationErrors.length) {
+			res.status(400)
+			res.json({ errors: validationErrors })
+		} else {
+			console.log(e)
+
+			res.status(500)
+			res.json({errors: Errors.unknown })
+		}
+	}
+})
+
 module.exports = router

+ 13 - 9
test/user.js

@@ -744,22 +744,26 @@ describe('User', () => {
 			agent
 				.delete('/api/v1/user/adminaccount')
 				.then(res => {
-					deleteRes.should.be.json
-					deleteRes.body.should.have.property('success', true)
-					deleteRes.should.not.have.cookie('username')
+					res.should.be.json
+					res.body.should.have.property('success', true)
+					res.should.not.have.cookie('username')
 
-					return agent
+					agent
 						.post('/api/v1/user/adminaccount/login')
 						.set('content-type', 'application/json')
 						.send({
 							password: 'qwertyuiop'
 						})
-				})
-				.catch(res => {
-					res.should.have.status(401)
-					res.body.should.have.property('errors')
-					res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
+						.end((err, res) => {
+							res.should.have.status(401)
+							res.body.should.have.property('errors')
+							res.body.errors.should.contain.something.that.deep.equals(Errors.invalidLoginCredentials)
 
+							done()
+						})
+				})
+				.catch(err => {
+					console.log(err)
 					done()
 				})
 		})