Skip to content
Browse files

added new url handle /user/id/:id so people can go to their acconts f…

…rom the addon. fixes #46
  • Loading branch information...
1 parent 73fa7f6 commit 6e1b9c3cbddc8c3a2c0108783ca33145e585c0cb @AutomatedTester committed Jan 25, 2012
Showing with 69 additions and 0 deletions.
  1. +11 −0 routes/users.js
  2. +58 −0 tests/http_tests.js
View
11 routes/users.js
@@ -18,4 +18,15 @@ module.exports = function(app){
}
});
});
+
+ app.get('/user/id/:userId', function(req, res, next){
+ dataProvider.findUserById(req.params.userId, function(err, user){
+ if (err) console.error(err);
+ if (user){
+ res.redirect("/user/" + user.name, 301);
+ } else {
+ next();
+ }
+ });
+ });
};
View
58 tests/http_tests.js
@@ -140,6 +140,64 @@ describe('server', function(){
});
});
+ it('should find a user if we access /user/id/:userID', function(done){
+ var params = {
+ 'name': 'testsSearchById',
+ 'oauthAccessToken': 'req.session.oauthAccessToken',
+ 'oauthAccessTokenSecret': 'req.session.oauthAccessTokenSecret',
+ };
+ var mongoose = require('mongoose');
+ mongoose.connect('mongodb://localhost/powerball');
+
+ var post = new User({
+ name: params.name
+ , oauthAccessToken : params.oauthAccessToken
+ , oauthAccessTokenSecret: params.oauthAccessTokenSecret
+ , created_at: new Date()});
+ post.save(function (err) {
+ var req = http.get({ path: '/user/id/' + post._id, port: 3000 }, function(res) {
+ assert.ok(res.statusCode === 301, res.statusCode);
+ var buf = '';
+ res.on('data', function(chunk){
+ buf += chunk
+ });
+ res.on('end', function(){
+ assert.ok(buf.indexOf("testsUserWillExist"));
+ done();
+ });
+ });
+ });
+ });
+
+ it('should find not user if we access /user/id/:userID and the user ID does exist', function(done){
+ var params = {
+ 'name': 'testsSearchById',
+ 'oauthAccessToken': 'req.session.oauthAccessToken',
+ 'oauthAccessTokenSecret': 'req.session.oauthAccessTokenSecret',
+ };
+ var mongoose = require('mongoose');
+ mongoose.connect('mongodb://localhost/powerball');
+
+ var post = new User({
+ name: params.name
+ , oauthAccessToken : params.oauthAccessToken
+ , oauthAccessTokenSecret: params.oauthAccessTokenSecret
+ , created_at: new Date()});
+ post.save(function (err) {
+ var req = http.get({ path: '/user/id/' + post._id + "1", port: 3000 }, function(res) {
+ assert.ok(res.statusCode === 404, res.statusCode);
+ var buf = '';
+ res.on('data', function(chunk){
+ buf += chunk
+ });
+ res.on('end', function(){
+ assert.ok(buf.indexOf("testsUserWillExist"));
+ done();
+ });
+ });
+ });
+ });
+
it('should get 200 and a message saying it failed when calling /score', function(done){
var req = http.request({ path: '/score', port: 3000, method: "POST" }, function(res) {
assert.ok(res.statusCode === 200);

0 comments on commit 6e1b9c3

Please sign in to comment.
Something went wrong with that request. Please try again.