wrote the first spec for the CLI class + working on the authenticate command (wip)

did committed Apr 06, 2015
commit 6e977d3bd6a5fe6496cb34c8c772a8292e4b268c
Showing 8 changed files with 127 additions and 61 deletions
Gemfile +11 -4
@@ @@ -14,14 +14,21 @@ gem 'rb-fsevent', '~> 0.9.1'
gem 'therubyracer'
gem 'locomotivecms_steam', github: 'locomotivecms/steam', ref: 'b2de77e', require: false
- gem 'locomotivecms_coal', github: 'locomotivecms/coal', ref: '6ae10e3684', require: false
+ # gem 'locomotivecms_coal', github: 'locomotivecms/coal', ref: '6ae10e3684', require: false
gem 'locomotivecms_common', github: 'locomotivecms/common', ref: '1895573', require: false
+ gem 'locomotivecms_coal', path: '../in_progress/coal', require: false
# gem 'locomotivecms_steam', path: '../in_progress/steam', require: false
# gem 'locomotivecms_common', path: '../in_progress/common', require: false
-
group :test do
- gem 'pry'
- gem 'coveralls', require: false
+ gem 'rspec', '~> 3.2.0'
+ gem 'json_spec', '~> 1.1.4'
+
+ gem 'pry-byebug', '~> 3.1.0'
+
+ gem 'webmock'
+ gem 'vcr'
+
+ gem 'coveralls', '~> 0.7.11', require: false
end
locomotive/wagon.rb b/lib/locomotive/wagon.rb +5 -37
@@ @@ -3,6 +3,8 @@ require_relative 'wagon/exceptions'
module Locomotive
module Wagon
+ DEFAULT_PLATFORM_URL = 'http://www.lvh.me:3000'.freeze
+
# Authenticate an user to the Hosting platform.
# If the user does not exist, then create an account for her/him.
# At the end, store the API key in the ~/.netrc file
@@ @@ -11,43 +13,9 @@ module Locomotive
# @param [ String ] password The password of the user
# @param [ Object ] shell Used to ask for/prompt information
#
- def self.authenticate(email, password, shell)
- require 'locomotive/wagon/misc/hosting_api'
- require 'netrc'
-
- api_key = nil
- api = Locomotive::HostingAPI.new(email: email, password: password)
-
- if api.authenticated?
- # existing account
- api_key = api.api_key
- shell.say "You have been successfully authenticated.", :green
- else
- # new account?
- shell.say "No account found for #{email} or invalid credentials", :yellow
-
- if shell.yes?('Do you want to create a new account? [Y/N]')
- name = shell.ask 'What is your name?'
-
- account = api.create_account(name: name, email: email, password: password)
-
- if account.success?
- shell.say "Your account has been successfully created.", :green
- api_key = account['api_key']
- else
- shell.say "We were unable to create your account, reason(s): #{account.error_messages.join(', ')}", :red
- end
- end
- end
-
- if api_key
- # record the credentials
- netrc = Netrc.read
- netrc[api.domain_with_port] = email, api_key
- netrc.save
- else
- shell.say "We were unable to authenticate you on our platform.", :red
- end
+ def self.authenticate(url, email, password, shell)
+ require_relative 'wagon/commands/authenticate_command'
+ Locomotive::Wagon::AuthenticateCommand.authenticate(url, email, password, shell)
end
# Create a site from a site generator.
locomotive/wagon/cli.rb b/lib/locomotive/wagon/cli.rb +8 -6
@@ @@ -177,14 +177,16 @@ module Locomotive
say Locomotive::Wagon::VERSION
end
- desc 'auth [EMAIL] [PASSWORD]', 'Log into the LocomotiveHosting platform'
- def auth(email = nil, password = nil)
- say "LocomotiveHosting Sign in/up\n\n", :bold
+ desc 'auth [EMAIL] [PASSWORD] [PLATFORM_URL]', 'Log into your Locomotive platform'
+ def auth(email = nil, password = nil, platform_url = nil)
+ say "Locomotive Sign in/up\n\n", :bold
- email ||= ask('Enter your e-mail?')
- password ||= ask('Enter your password?')
+ platform_url ||= ask("Enter the URL of your platform? (default: #{Locomotive::Wagon::DEFAULT_PLATFORM_URL})")
+ platform_url = Locomotive::Wagon::DEFAULT_PLATFORM_URL if platform_url.strip == ''
+ while email.to_s.strip == ''; email = ask('Enter your e-mail?'); end
+ while password.to_s.strip == ''; password = ask('Enter your password?', echo: false); end
- Locomotive::Wagon.authenticate(email, password, shell)
+ Locomotive::Wagon.authenticate(platform_url, email, password, shell)
end
desc 'init NAME [PATH] [GENERATOR_OPTIONS]', 'Create a brand new site'
locomotive/wagon/commands/authenticate_command.rb b/lib/locomotive/wagon/commands/authenticate_command.rb +60 -0
@@ @@ -0,0 +1,60 @@
+ module Locomotive::Wagon
+
+ class AuthenticateCommand < Struct.new(:platform_url, :email, :password, :shell)
+
+ def self.authenticate(platform_url, email, password, shell)
+ self.new(platform_url, email, password, shell).authenticate
+ end
+
+ def authenticate
+ puts "[auth] #{platform_url.inspect} / #{email.inspect} / #{password.inspect}"
+ puts 'YOUPI'
+
+ # require 'locomotive/wagon/misc/hosting_api'
+ # require 'locomotive/coal'
+ # require 'netrc'
+
+ # api_key = nil
+ # api = Locomotive::HostingAPI.new(email: email, password: password)
+
+ # if api.authenticated?
+ # # existing account
+ # api_key = api.api_key
+ # shell.say "You have been successfully authenticated.", :green
+ # else
+ # # new account?
+ # shell.say "No account found for #{email} or invalid credentials", :yellow
+
+ # if shell.yes?('Do you want to create a new account? [Y/N]')
+ # name = shell.ask 'What is your name?'
+
+ # account = api.create_account(name: name, email: email, password: password)
+
+ # if account.success?
+ # shell.say "Your account has been successfully created.", :green
+ # api_key = account['api_key']
+ # else
+ # shell.say "We were unable to create your account, reason(s): #{account.error_messages.join(', ')}", :red
+ # end
+ # end
+ # end
+
+ # if api_key
+ # # record the credentials
+ # netrc = Netrc.read
+ # netrc[api.domain_with_port] = email, api_key
+ # netrc.save
+ # else
+ # shell.say "We were unable to authenticate you on our platform.", :red
+ # end
+ end
+
+ private
+
+ def api_key_from_credentials
+ # client = Locomotive::Coal::Client.new('http://www.myengine.dev/locomotive/api', { email: <EMAIL>, api_key: <API KEY> })
+ end
+
+ end
+
+ end
locomotivecms_wagon.gemspec +2 -7
@@ @@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.executables = ['wagon']
+ gem.add_development_dependency 'rake', '~> 10.0.4'
+
gem.add_dependency 'thor', '~> 0.19.1'
gem.add_dependency 'thin', '~> 1.6.3'
gem.add_dependency 'rubyzip', '~> 1.1.7'
@@ @@ -27,11 +29,4 @@ Gem::Specification.new do |gem|
gem.add_dependency 'rack-livereload', '~> 0.3.15'
gem.add_dependency 'faker', '~> 1.4.3'
-
- gem.add_development_dependency 'rake', '~> 10.0.4'
- gem.add_development_dependency 'rspec', '~> 2.6.0'
- gem.add_development_dependency 'vcr'
- gem.add_development_dependency 'webmock', '~> 1.8.0'
- gem.add_development_dependency 'rack-test'
- gem.add_development_dependency 'launchy'
end
spec/integration/cli_spec.rb +26 -3
@@ @@ -1,7 +1,30 @@
- # # encoding: utf-8
+ # encoding: utf-8
- # require File.dirname(__FILE__) + '/integration_helper'
- # require 'locomotive/wagon/cli'
+ require File.dirname(__FILE__) + '/integration_helper'
+ require 'locomotive/wagon/cli'
+
+ describe Locomotive::Wagon::CLI do
+
+ subject { capture(:stdout) { command } }
+
+ describe '#version' do
+
+ let(:command) { Locomotive::Wagon::CLI::Main.start(['version']) }
+ it { is_expected.to eq '2.0.0-alpha' }
+
+ end
+
+ # describe '#auth' do
+
+ # subject { }
+ # expect(capture(:stdout) { A.new.invoke(:two) }).to eq("2\n3\n") }
+
+ # it ''
+ # let(:output) { }
+
+ # end
+
+ end
# describe Locomotive::Wagon::CLI do
# it "overrides subdomains and domains in different environments" do
spec/spec_helper.rb +3 -4
@@ @@ -1,6 +1,5 @@
- require "locomotive/wagon"
- require "rspec"
- require "launchy"
+ require 'locomotive/wagon'
+ require 'rspec'
require 'coveralls'
Coveralls.wear!
@@ @@ -14,4 +13,4 @@ RSpec.configure do |c|
c.before(:all) { remove_logs }
c.before { reset! }
c.after { reset! }
- end
\ No newline at end of file
+ end
spec/support/thor.rb +12 -0
@@ @@ -0,0 +1,12 @@
+ def capture(stream)
+ begin
+ stream = stream.to_s
+ eval "$#{stream} = StringIO.new"
+ yield
+ result = eval("$#{stream}").string
+ ensure
+ eval("$#{stream} = #{stream.upcase}")
+ end
+
+ result.strip
+ end