Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Prepare service for twitter.

  • Loading branch information...
commit 77c8086a9be010c16eecf4fe5fa780f97ed87dda 1 parent 073955c
@llun authored
View
2  app.js
@@ -131,7 +131,7 @@ app.get('/cards/tag/:tag.html', routes.tag);
app.get('/cards/:cid/comments', comments.list);
app.post('/cards/:cid/comments', comments.create);
-app.post('/admin/services', services.create);
+app.post('/admin/services', services.authorize);
app.get('/admin/services/:service/callback', services.callback);
// server codes
View
27 routes/services.js
@@ -5,32 +5,31 @@ var services = require('../services');
var Service = require('../models/service');
module.exports = {
- create: function (req, res) {
- services.get(req.body.service, req.body.oauth_key, req.body.oauth_secret,
- function (err, service) {
- if (service) {
- var url = service.authorizeUrl();
- res.redirect(url);
- }
- else {
- // Redirect back to admin
- res.redirect('/admin');
- }
+ authorize: function (req, res) {
+ q.nfcall(services.get.bind(services), req.body.service, req.body.oauth_key, req.body.oauth_secret)
+ .then(function (service) {
+ service.authorize(req, res);
+ })
+ .fail(function (err) {
+ res.redirect('/admin');
});
},
callback: function (req, res) {
q.nfcall(services.get.bind(services), req.params.service)
.then(function (service) {
- return q.nfcall(service.accessToken.bind(service),
- service.authorizeUrl(), req.query.code);
+ return q.nfcall(service.callback.bind(service), req);
})
.then(function (response) {
return q.nfcall(services.save.bind(services), req.params.service, response);
})
.then (function (service) {
return res.redirect('/admin');
- });
+ })
+ .fail (function (err) {
+ res.redirect('/admin');
+ })
+ .done();
},
list: function (req, res) {
View
11 services/facebook.js
@@ -26,6 +26,17 @@ var Facebook = function (key, secret, url, scope) {
*/
Facebook.states = {};
+Facebook.prototype.authorize = function (req, res) {
+ res.redirect(this.authorizeUrl());
+}
+
+Facebook.prototype.callback = function (req, cb) {
+ this.accessToken(this.authorizeUrl(), req.query.code,
+ function (err, token) {
+ cb(err, token);
+ });
+}
+
/**
* Return authorize url for redirect by browser
*
View
1  services/index.js
@@ -10,6 +10,7 @@ var Service = function (key, secret, authorizeUrl) {
// Supported services
Service.facebook = require('./facebook');
+Service.twitter = require('./twitter');
/**
* Get service from database or create it if not found.
View
25 services/twitter.js
@@ -0,0 +1,25 @@
+var OAuth = require('oauth').OAuth;
+
+var requestTokenURL = 'https://api.twitter.com/oauth/request_token',
+ accessTokenURL = 'https://api.twitter.com/oauth/access_token';
+
+
+var Twitter = function (key, secret, url) {
+ this.key = key;
+ this.secret = secret;
+
+ this.oauth = new OAuth(
+ requestTokenURL,
+ accessTokenURL,
+ key,
+ secret,
+ '1.0A',
+ null,
+ 'HMAC-SHA1');
+}
+
+Twitter.prototype.authorizeUrl = function () {
+
+}
+
+module.exports = Twitter;
View
4 spec/remote/test_twitter.js
@@ -0,0 +1,4 @@
+describe('Twitter', function () {
+
+
+});
View
134 spec/test_routes_services.js
@@ -18,93 +18,77 @@ describe('routes/services.js', function () {
this.timeout(500);
- describe('Facebook', function () {
- describe('#create', function () {
-
- it ('should redirect to authorizeUrl', function (done) {
-
- var stub = sinon.stub(Services, 'get', function (name, key, secret, cb) {
- cb(null, {
- authorizeUrl: function () {
- return 'http://sample.com'
- }
- });
- });
-
- var deferred = q.defer();
-
- var req = {
- body: {
- oauth_key: 'key',
- oauth_secret: 'secret',
- service: 'service'
- }
- };
- var res = {
- redirect: function (url) {
- deferred.resolve(url);
+ describe('#authorize', function () {
+
+ it ('should send req and res to service to authorize', function (done) {
+ var deferred = q.defer();
+ var stub = sinon.stub(Services, 'get', function (service, key, secret, cb) {
+ cb(null, {
+ authorize: function (_req, _res) {
+ _res.redirect();
}
- };
-
- routes.create(req, res);
-
- deferred.promise.should.be.fulfilled.then(function (url) {
- url.should.equal('http://sample.com');
+ })
+ });
+ var req = {
+ body: {
+ oauth_key: 'key',
+ oauth_secret: 'secret',
+ service: 'name'
+ }
+ };
- stub.restore();
- }).should.notify(done);
+ var res = {
+ redirect: function () {
+ deferred.resolve();
+ }
+ };
+
+ routes.authorize(req, res);
- });
+ deferred.promise.should.be.fulfilled.then(function () {
+ stub.restore();
+ }).should.notify(done);
});
- describe('#callback', function () {
-
- it ('should add service to database', function (done) {
- var stub = sinon.stub(Services, 'get', function (name, cb) {
- cb(null, {
- authorizeUrl: function () {
- return 'http://google.com';
- },
- accessToken: function (url, code, cb) {
- cb(null, 'access_token');
- }
- });
+ });
+
+ describe('#callback', function () {
+
+ it('should send req to callback function and get result and save to database', function (done) {
+ var deferred = q.defer();
+ var getStub = sinon.stub(Services, 'get', function (service, cb) {
+ cb(null, {
+ callback: function (_req, cb) {
+ cb(null, 'token');
+ }
});
+ });
+ var saveStub = sinon.stub(Services, 'save', function (service, token, cb) {
+ cb(null);
+ });
- var deferred = q.defer();
-
- var req = {
- app: {
- get: function () { return 'http://local.llun.in.th' }
- },
- params: {
- service: 'facebook'
- },
- query: {
- state: 'timestamp',
- code: 'code'
- }
+ var req = {
+ params: {
+ service: 'name'
}
- var res = {
- redirect: function () {
- deferred.resolve();
- },
- json: function () {
- deferred.resolve();
- }
+ };
+ var res = {
+ redirect: function () {
+ deferred.resolve();
}
-
- routes.callback(req, res);
-
- deferred.promise.should.be.fulfilled.then(function(){
- stub.restore();
- }).should.notify(done);
- });
-
+ };
+
+ routes.callback(req, res);
+
+ deferred.promise.should.be.fulfilled.then(function () {
+ getStub.restore();
+ saveStub.restore();
+ }).should.notify(done);
});
+
+
});
-
});
Please sign in to comment.
Something went wrong with that request. Please try again.