From ff07764b398a495a8e12cdaa1c129354fa2a7923 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 26 Nov 2014 13:02:22 -0400 Subject: [PATCH] Move auth to resin module --- lib/actions/auth.coffee | 8 ++++---- lib/hooks/auth.coffee | 4 ++-- lib/hooks/auth.spec.coffee | 5 ++--- lib/{ => resin}/auth/auth.coffee | 13 +++++++------ lib/{ => resin}/auth/auth.spec.coffee | 12 ++++++------ lib/resin/index.coffee | 1 + 6 files changed, 22 insertions(+), 21 deletions(-) rename lib/{ => resin}/auth/auth.coffee (72%) rename lib/{ => resin}/auth/auth.spec.coffee (94%) diff --git a/lib/actions/auth.coffee b/lib/actions/auth.coffee index b1586396..bdd64d47 100644 --- a/lib/actions/auth.coffee +++ b/lib/actions/auth.coffee @@ -1,6 +1,6 @@ open = require('open') async = require('async') -auth = require('../auth/auth') +resin = require('../resin') authHooks = require('../hooks/auth') widgets = require('../widgets/widgets') config = require('../config') @@ -10,17 +10,17 @@ exports.login = (credentials) -> (callback) -> if credentials? - return auth.parseCredentials(credentials, callback) + return resin.auth.parseCredentials(credentials, callback) else return widgets.login(callback) (credentials, callback) -> - auth.login(credentials, callback) + resin.auth.login(credentials, callback) ], resin.errors.handle exports.logout = authHooks.failIfNotLoggedIn -> - auth.logout() + resin.auth.logout() exports.signup = -> open(config.urls.signup) diff --git a/lib/hooks/auth.coffee b/lib/hooks/auth.coffee index 2c1c7790..d27e3ce7 100644 --- a/lib/hooks/auth.coffee +++ b/lib/hooks/auth.coffee @@ -1,11 +1,11 @@ _ = require('lodash') -auth = require('../auth/auth') +resin = require('../resin') messages = require('../messages/messages') exports.failIfNotLoggedIn = (fn, onError) -> return -> args = arguments - auth.isLoggedIn (isLoggedIn) -> + resin.auth.isLoggedIn (isLoggedIn) -> if not isLoggedIn error = new Error(messages.errors.loginRequired) diff --git a/lib/hooks/auth.spec.coffee b/lib/hooks/auth.spec.coffee index a2b81b45..8a42b5b1 100644 --- a/lib/hooks/auth.spec.coffee +++ b/lib/hooks/auth.spec.coffee @@ -3,7 +3,6 @@ nock = require('nock') sinon = require('sinon') expect = require('chai').expect config = require('../config') -auth = require('../auth/auth') resin = require('../resin') authHooks = require('./auth') johnDoeFixture = require('../../tests/fixtures/johndoe') @@ -29,7 +28,7 @@ describe 'Auth Hooks:', -> describe 'if not logged in', -> beforeEach (done) -> - auth.logout(done) + resin.auth.logout(done) it 'should not call the function', (done) -> spy = sinon.spy() @@ -63,7 +62,7 @@ describe 'Auth Hooks:', -> .post('/login_', johnDoeFixture.credentials) .reply(200, johnDoeFixture.token) - auth.login(johnDoeFixture.credentials, done) + resin.auth.login(johnDoeFixture.credentials, done) it 'should call the function with the correct arguments', (done) -> args = [ 1, 2, 3, 'foo', 'bar' ] diff --git a/lib/auth/auth.coffee b/lib/resin/auth/auth.coffee similarity index 72% rename from lib/auth/auth.coffee rename to lib/resin/auth/auth.coffee index c8f58009..185bf0f0 100644 --- a/lib/auth/auth.coffee +++ b/lib/resin/auth/auth.coffee @@ -1,10 +1,11 @@ async = require('async') _ = require('lodash') -resin = require('../resin') +token = require('../token/token') +server = require('../server/server') exports.authenticate = (credentials, callback) -> - resin.server.post '/login_', credentials, (error, response) -> + server.post '/login_', credentials, (error, response) -> return callback(error, response?.body) exports.login = (credentials, callback) -> @@ -14,17 +15,17 @@ exports.login = (credentials, callback) -> exports.authenticate(credentials, callback) (authToken, callback) -> - resin.token.saveToken(authToken, callback) + token.saveToken(authToken, callback) ], callback) # Handy aliases -exports.isLoggedIn = resin.token.hasToken -exports.getToken = resin.token.getToken +exports.isLoggedIn = token.hasToken +exports.getToken = token.getToken # TODO: Maybe we should post to /logout or something # like that to invalidate the token on the server? -exports.logout = resin.token.clearToken +exports.logout = token.clearToken exports.parseCredentials = (credentials, callback) -> result = credentials.split(':') diff --git a/lib/auth/auth.spec.coffee b/lib/resin/auth/auth.spec.coffee similarity index 94% rename from lib/auth/auth.spec.coffee rename to lib/resin/auth/auth.spec.coffee index 95da7fc7..fd84f66d 100644 --- a/lib/auth/auth.spec.coffee +++ b/lib/resin/auth/auth.spec.coffee @@ -3,11 +3,11 @@ nock = require('nock') _ = require('lodash') async = require('async') auth = require('./auth') -resin = require('../resin') -config = require('../config') -mock = require('../../tests/utils/mock') -johnDoeFixture = require('../../tests/fixtures/johndoe') -janeDoeFixture = require('../../tests/fixtures/janedoe') +data = require('../data/data') +config = require('../../config') +mock = require('../../../tests/utils/mock') +johnDoeFixture = require('../../../tests/fixtures/johndoe') +janeDoeFixture = require('../../../tests/fixtures/janedoe') describe 'Auth:', -> @@ -19,7 +19,7 @@ describe 'Auth:', -> beforeEach (done) -> mock.fs.init() - resin.data.prefix.set(config.dataPrefix, done) + data.prefix.set(config.dataPrefix, done) afterEach -> mock.fs.restore() diff --git a/lib/resin/index.coffee b/lib/resin/index.coffee index 6b9e9597..ea92a1bc 100644 --- a/lib/resin/index.coffee +++ b/lib/resin/index.coffee @@ -5,3 +5,4 @@ module.exports = errors: require('./errors/errors') token: require('./token/token') data: require('./data/data') + auth: require('./auth/auth')