{"id":7956,"date":"2015-11-12T03:17:50","date_gmt":"2015-11-12T03:17:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/12\/nodejs-passport-testing-user-login-with-mocha-and-superagent-open-source-projects-visionmedia-superagent\/"},"modified":"2015-11-12T03:17:50","modified_gmt":"2015-11-12T03:17:50","slug":"nodejs-passport-testing-user-login-with-mocha-and-superagent-open-source-projects-visionmedia-superagent","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/12\/nodejs-passport-testing-user-login-with-mocha-and-superagent-open-source-projects-visionmedia-superagent\/","title":{"rendered":"NodeJS\/Passport &#8211; Testing user login with mocha and superagent-open source projects visionmedia\/superagent"},"content":{"rendered":"<p>I have built a login process with a basic MEAN stack and using passport for the authentication process.<\/p>\n<p>I am trying to set up a test to make sure the login process is working. To do the login part here is the code I used:<\/p>\n<pre><code>  it ('login', function(done) {\n    agent.post(config.web.vhost + '\/login')\n      .send({ email: 'bruce@wayne.inc', password: 'batman' })\n      .end(function(err, res) {\n      if (err) console.log('error' + err.message);\n\n        res.should.have.status(200);\n\n        done();\n      });\n  });\n<\/code><\/pre>\n<p>I get the following error:<\/p>\n<p>no error email: bruce@wayne.inc \u2024no error errorsocket hang up \u2024double callback!<\/p>\n<pre><code>  1 passing (2s)\n  1 failing\n\n  1) User login:\n     Uncaught TypeError: Cannot read property 'should' of undefined\n<\/code><\/pre>\n<p>I know my routes and credentials are good but I cant figure out whats not working here. This is my first steps with user testing so I am probably not getting something right.<\/p>\n<p>Here is the rest of my test:<\/p>\n<pre><code>var should = require(\"should\");\nvar mongoose = require('mongoose');\nvar request = require('superagent');\nvar agent = request.agent();\n\nvar config = require(\"..\/settings\/conf\");\nvar dbUrl = require(\"..\/config\/database.js\");\nvar User = require('..\/server\/models\/user');\n\nvar db;\n\ndescribe('User', function() {\n\nbefore(function(done) {\n  db = mongoose.connect(dbUrl.url);\n    done();\n  });\n\n  after(function(done) {\n     mongoose.connection.close()\n    done();\n  });\n\n  beforeEach(function(done) {\n    var user = new User({\n      email: 'bruce@wayne.inc',\n      password: 'batman',\n      firstName: 'Bruce',\n      lastName: 'Wayne'\n    });\n\n    user.save(function(err, user) {\n      if (err) console.log('error' + err.message);\n      else console.log('no error');\n\n      done();\n    });\n  });\n\n  it('find a user by username', function(done) {\n    User.findOne({ email: 'bruce@wayne.inc' }, function(err, user) {\n      user.email.should.eql('bruce@wayne.inc');\n      console.log(\"   email: \", user.email)\n      done();\n    });\n  });\n\n  it ('login', function(done) {\n    agent.post(config.web.vhost + '\/login')\n      .send({ email: 'bruce@wayne.inc', password: 'batman' })\n      .end(function(err, res) {\n      if (err) console.log('error' + err.message);\n\n        res.should.have.status(200);\n\n        done();\n      });\n  });\n\n  afterEach(function(done) {\n    User.remove({ email: 'bruce@wayne.inc' }, function() {\n      done();\n    });\n  });\n});\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have built a login process with a basic MEAN stack and using passport for the authentication process. I am trying to set up a test to make sure the login process is working. To do the login part here is the code I used: it (&#8216;login&#8217;, function(done) { agent.post(config.web.vhost + &#8216;\/login&#8217;) .send({ email: &#8216;bruce@wayne.inc&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7956","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7956"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7956\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}