-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from joehand/auth
Add auth
- Loading branch information
Showing
11 changed files
with
367 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var ui = require('../../ui') | ||
var TownshipClient = require('../../township') | ||
|
||
module.exports = { | ||
name: 'login', | ||
command: login, | ||
options: [ | ||
{ | ||
name: 'config', | ||
boolean: false, | ||
default: '.datrc' | ||
} | ||
] | ||
} | ||
|
||
function login (opts) { | ||
var email = opts.email | ||
if (!email) ui.exitErr(new Error('Email is required.')) | ||
var password = opts.password | ||
if (!password) ui.exitErr(new Error('Password is required.')) | ||
|
||
var client = TownshipClient({ | ||
server: opts.server, | ||
config: { | ||
filename: opts.config | ||
} | ||
}) | ||
|
||
client.login({ | ||
email: email, | ||
password: password | ||
}, function (err) { | ||
if (err) ui.exitErr(err) | ||
process.stdout.write('Logged in successfully.') | ||
ui.exit() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var ui = require('../../ui') | ||
var TownshipClient = require('../../township') | ||
|
||
module.exports = { | ||
name: 'logout', | ||
command: logout, | ||
options: [ | ||
{ | ||
name: 'config', | ||
boolean: false, | ||
default: '.datrc' | ||
} | ||
] | ||
} | ||
|
||
function logout (opts) { | ||
var client = TownshipClient({ | ||
server: opts.server, | ||
config: { | ||
filename: opts.config | ||
} | ||
}) | ||
|
||
var config = client.config.get() | ||
var login = config.currentLogin | ||
if (!login) { | ||
process.stdout.write('Not logged in.') | ||
ui.exit() | ||
} else { | ||
client.logout(function (err) { | ||
if (err) ui.exitErr(err) | ||
process.stdout.write('Logged out.') | ||
ui.exit() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var ui = require('../../ui') | ||
var TownshipClient = require('../../township') | ||
|
||
module.exports = { | ||
name: 'register', | ||
command: register, | ||
options: [ | ||
{ | ||
name: 'config', | ||
boolean: false, | ||
default: '.datrc' | ||
} | ||
] | ||
} | ||
|
||
function register (opts) { | ||
var email = opts.email | ||
if (!email) ui.exitErr(new Error('Email is required.')) | ||
var password = opts.password | ||
if (!password) ui.exitErr(new Error('Password is required.')) | ||
|
||
var client = TownshipClient({ | ||
server: opts.server, | ||
config: { | ||
filename: opts.config | ||
} | ||
}) | ||
|
||
client.register({ | ||
email: email, | ||
password: password | ||
}, function (err) { | ||
if (err) ui.exitErr(err) | ||
process.stdout.write('Registered successfully.') | ||
ui.exit() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var ui = require('../../ui') | ||
var TownshipClient = require('../../township') | ||
|
||
module.exports = { | ||
name: 'whoami', | ||
command: whoami, | ||
options: [ | ||
{ | ||
name: 'config', | ||
boolean: false, | ||
default: '.datrc' | ||
} | ||
] | ||
} | ||
|
||
function whoami (opts) { | ||
var client = TownshipClient({ | ||
server: opts.server, | ||
config: { | ||
filename: opts.config | ||
} | ||
}) | ||
var config = client.config.get() | ||
var login = config.currentLogin | ||
if (!login) process.stdout.write('Not logged in.') | ||
else process.stdout.write(login.email) | ||
ui.exit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var xtend = require('xtend') | ||
var TownshipClient = require('township-client') | ||
|
||
module.exports = function (opts) { | ||
var defaults = { | ||
server: 'https://auth.dat.land', | ||
config: { | ||
filename: '.datrc' | ||
} | ||
} | ||
var options = xtend(defaults, opts) | ||
return TownshipClient(options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
var test = require('tape') | ||
var path = require('path') | ||
var fs = require('fs') | ||
var homedir = require('homedir') | ||
var spawn = require('./helpers/spawn') | ||
var help = require('./helpers') | ||
var authServer = require('./helpers/auth-server') | ||
|
||
var dat = path.resolve(path.join(__dirname, '..', 'bin', 'cli.js')) | ||
var baseTestDir = help.testFolder() | ||
|
||
var port = process.env.PORT || 3000 | ||
var SERVER = 'http://localhost:' + port | ||
var config = '.datrc-test' | ||
var opts = ' --server=' + SERVER + ' --config=' + config | ||
|
||
dat += opts | ||
|
||
authServer(port, function (server) { | ||
test('auth - whoami works when not logged in', function (t) { | ||
var cmd = dat + ' whoami ' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stdout.match(function (output) { | ||
t.same('Not logged in.', output, 'printed correct output') | ||
return true | ||
}) | ||
st.stderr.empty() | ||
st.end() | ||
}) | ||
|
||
test('auth - register requires email', function (t) { | ||
var cmd = dat + ' register ' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stderr.match(function (output) { | ||
t.ok(output.indexOf('Email is required') > -1, 'outputs correct error message') | ||
st.kill() | ||
return true | ||
}) | ||
st.stdout.empty() | ||
st.fails() | ||
st.end() | ||
}) | ||
|
||
test('auth - register requires email and password', function (t) { | ||
var cmd = dat + ' register [email protected]' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stderr.match(function (output) { | ||
t.ok(output.indexOf('Password is required') > -1, 'outputs correct error message') | ||
st.kill() | ||
return true | ||
}) | ||
st.stdout.empty() | ||
st.fails() | ||
st.end() | ||
}) | ||
|
||
test('auth - register works', function (t) { | ||
var cmd = dat + ' register [email protected] --password=joe' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stdout.match(function (output) { | ||
t.same(output.trim(), 'Registered successfully.', 'output success message') | ||
return true | ||
}) | ||
st.stderr.empty() | ||
st.end() | ||
}) | ||
|
||
test('auth - login requires email', function (t) { | ||
var cmd = dat + ' login --password=joe' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stderr.match(function (output) { | ||
t.ok(output.indexOf('Email is required') > -1, 'outputs correct error message') | ||
st.kill() | ||
return true | ||
}) | ||
st.stdout.empty() | ||
st.end() | ||
}) | ||
|
||
test('auth - login requires email and password', function (t) { | ||
var cmd = dat + ' login [email protected]' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stderr.match(function (output) { | ||
t.ok(output.indexOf('Password is required') > -1, 'outputs correct error message') | ||
st.kill() | ||
return true | ||
}) | ||
st.stdout.empty() | ||
st.end() | ||
}) | ||
|
||
test('auth - login works', function (t) { | ||
var cmd = dat + ' login [email protected] --password=joe' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stdout.match(function (output) { | ||
t.same(output.trim(), 'Logged in successfully.', 'output success message') | ||
return true | ||
}) | ||
st.stderr.empty() | ||
st.end() | ||
}) | ||
|
||
test('auth - whoami works', function (t) { | ||
var cmd = dat + ' whoami' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stdout.match(function (output) { | ||
t.same('[email protected]', output, 'email printed') | ||
return true | ||
}) | ||
st.stderr.empty() | ||
st.end() | ||
}) | ||
|
||
test('auth - logout works', function (t) { | ||
var cmd = dat + ' logout' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stdout.match(function (output) { | ||
t.same('Logged out.', output, 'output correct') | ||
return true | ||
}) | ||
st.stderr.empty() | ||
st.end() | ||
}) | ||
|
||
test('auth - logout prints correctly when trying to log out twice', function (t) { | ||
var cmd = dat + ' logout' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stdout.match(function (output) { | ||
t.same('Not logged in.', output, 'output correct') | ||
return true | ||
}) | ||
st.stderr.empty() | ||
st.end() | ||
}) | ||
|
||
test('auth - whoami works after logging out', function (t) { | ||
var cmd = dat + ' whoami' | ||
var st = spawn(t, cmd, {cwd: baseTestDir}) | ||
st.stdout.match(function (output) { | ||
t.same('Not logged in.', output) | ||
return true | ||
}) | ||
st.stderr.empty() | ||
st.end() | ||
}) | ||
|
||
test.onFinish(function () { | ||
server.close(function () { | ||
fs.unlink(path.join(homedir(), config), function () { | ||
// done! | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.