Browse Source

Add test and update route to login in user (i.e. set the session variable) when creating account

sbkwgh 8 years ago
parent
commit
d7d7a2964f
2 changed files with 25 additions and 0 deletions
  1. 2 0
      routes/user.js
  2. 23 0
      test/user.js

+ 2 - 0
routes/user.js

@@ -43,6 +43,8 @@ router.post('/', async (req, res) => {
 			hash: hash
 			hash: hash
 		})
 		})
 
 
+		req.session.loggedIn = true
+		req.session.username = user.username
 		res.json(user.toJSON())
 		res.json(user.toJSON())
 	} catch (err) {
 	} catch (err) {
 		if(err === Errors.VALIDATION_ERROR) {
 		if(err === Errors.VALIDATION_ERROR) {

+ 23 - 0
test/user.js

@@ -42,6 +42,29 @@ describe('User', () => {
 				})
 				})
 		})
 		})
 
 
+		it('should log in the user after creating an account', (done) => {
+			let agent = chai.request.agent(server)
+
+			agent
+				.post('/api/v1/user')
+				.set('content-type', 'application/x-www-form-urlencoded')
+				.send({
+					username: 'username1',
+					password: 'password'
+				})
+				.end((err, res) => {
+					
+					agent
+						.get('/api/v1/user/username1')
+						.then((res) => {
+							res.should.have.status(200)
+
+							done()
+						})
+						.catch(done)
+				})
+		})
+
 		it('should throw an error if account already created', (done) => {
 		it('should throw an error if account already created', (done) => {
 			chai.request(server)
 			chai.request(server)
 				.post('/api/v1/user')
 				.post('/api/v1/user')