Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First part of install command, lots of spec setup

  * Checks api key against api.localeapp.com
  * Uses FakeWeb to fake out api endpoints
  * Uses ENV vars to communicate FakeWeb setup between cucumber/aruba
    and bin/localeapp
  * Data used for testing defined in
    spec/support/locale_app_integration_data
    so should only have to change there if api changes. Required in both
    cucumber and rspec tests
  * FakeWeb.allow_net_connect is false in both cucumber and rspec
  • Loading branch information...
commit 340a2a4cd878fd11ea02d46ee96b450f3031e105 1 parent 1a3fb53
@chrismcg chrismcg authored
View
32 bin/localeapp
@@ -1,3 +1,33 @@
#!/usr/bin/env ruby
+$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
-puts "Usage: localeapp COMMAND [ARGS]"
+require 'locale_app'
+
+# Don't connect to the net if we're running under cucumber for testing
+# Use FakeWeb to simulate api.localeapp.com
+if ENV['FAKE_WEB_DURING_CUCUMBER_RUN'] && fake_data_as_json = ENV['FAKE_WEB_FAKES']
+ require 'fakeweb'
+ FakeWeb.allow_net_connect = false
+ fakes = JSON.parse(fake_data_as_json)
+ fakes.each do |fake|
+ FakeWeb.register_uri fake['method'].to_sym, fake['uri'], :body => fake['body'], :status => fake['status']
+ end
+end
+
+args = ARGV.dup
+
+command = args.shift.strip rescue nil
+
+case command
+when 'install'
+ key = args.shift.strip rescue nil
+ installer = LocaleApp::CLI::Install.new
+ installer.execute(key)
+else
+puts <<-HELP
+Usage: localeapp COMMAND [ARGS]
+
+Commands:
+ install <key> - Creates new configuration files and confirms key works
+HELP
+end
View
8 cucumber.yml
@@ -0,0 +1,8 @@
+<%
+rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
+rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
+std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} --strict --tags ~@wip"
+%>
+default: <%= std_opts %> features
+wip: --tags @wip:3 --wip features
+rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
View
17 features/localeapp_binary.feature
@@ -6,4 +6,21 @@ Feature: localeapp executable
Then the output should contain:
"""
Usage: localeapp COMMAND [ARGS]
+
+ Commands:
+ install <key> - Creates new configuration files and confirms key works
+ """
+
+ Scenario: Running install
+ In order to configure my project and check my api key is correct
+ When I have a valid project on localeapp.com with api key "MYAPIKEY"
+ And I run `localeapp install MYAPIKEY`
+ Then the output should contain:
+ """
+ LocaleApp Install
+
+ Checking API key: MYAPIKEY
+ Success!
+ Project: Test Project
+ Default Locale: en (English)
"""
View
6 features/step_definitions/cli_steps.rb
@@ -0,0 +1,6 @@
+require 'net/http'
+When /^I have a valid project on localeapp\.com with api key "([^"]*)"$/ do |api_key|
+ uri = "http://api.localeapp.com/projects/#{api_key}.json"
+ body = valid_project_data.to_json
+ add_fake_web_uri(:get, uri, ['200', 'OK'], body)
+end
View
18 features/support/env.rb
@@ -1,2 +1,20 @@
require 'aruba/cucumber'
+require File.expand_path(File.join(File.dirname(__FILE__), '../../spec/support/locale_app_integration_data'))
+World(LocaleAppIntegrationData)
+
ENV['PATH'] = "#{File.expand_path(File.dirname(__FILE__) + '/../../bin')}#{File::PATH_SEPARATOR}#{ENV['PATH']}"
+
+module FakeWebHelper
+ def add_fake_web_uri(method, uri, status, body)
+ fakes = JSON.parse(ENV['FAKE_WEB_FAKES'] || '[]')
+ fakes << {
+ 'method' => method,
+ 'uri' => uri,
+ 'status' => status,
+ 'body' => body
+ }
+ ENV['FAKE_WEB_FAKES'] = fakes.to_json
+ end
+end
+World(FakeWebHelper)
+
View
3  features/support/hooks.rb
@@ -0,0 +1,3 @@
+Before do
+ ENV['FAKE_WEB_DURING_CUCUMBER_RUN'] = '1'
+end
View
2  lib/locale_app.rb
@@ -3,6 +3,8 @@
require 'locale_app/sender'
require 'locale_app/poller'
require 'locale_app/updater'
+require 'locale_app/key_checker'
+require 'locale_app/cli/install'
# AUDIT: Will this work on ruby 1.9.x
$KCODE="UTF8"
View
29 lib/locale_app/cli/install.rb
@@ -0,0 +1,29 @@
+module LocaleApp
+ module CLI
+ class Install
+ def execute(key, output = $stdout)
+ output.puts "LocaleApp Install"
+ output.puts ""
+ output.puts "Checking API key: #{key}"
+ if key.nil?
+ output.puts "ERROR: You must supply an API key"
+ return
+ end
+ valid_key, project_data = check_key(key)
+ if valid_key
+ output.puts "Success!"
+ output.puts "Project: #{project_data['name']}"
+ output.puts "Default Locale: #{project_data['default_locale']['code']} (#{project_data['default_locale']['name']})"
+ else
+ output.puts "ERROR: Project not found"
+ return
+ end
+ end
+
+ private
+ def check_key(key)
+ LocaleApp::KeyChecker.new.check(key)
+ end
+ end
+ end
+end
View
2  lib/locale_app/configuration.rb
@@ -40,7 +40,7 @@ class Configuration
attr_accessor :translation_data_directory
def initialize
- @host = 'localeapp.com'
+ @host = 'api.localeapp.com'
@port = 80
@disabled_environments = %w(test cucumber)
@poll_interval = 60
View
32 lib/locale_app/key_checker.rb
@@ -0,0 +1,32 @@
+require 'yaml'
+require 'rest-client'
+require 'time'
+
+module LocaleApp
+ class KeyChecker
+ def check(key)
+ begin
+ response = RestClient.get(project_resource_url(key))
+ [true, JSON.parse(response)]
+ rescue RestClient::ResourceNotFound => e
+ [false, {}]
+ end
+ end
+
+ private
+ def project_resource_url(key)
+ if LocaleApp.configuration.nil? # no config file yet
+ LocaleApp.configuration = LocaleApp::Configuration.new
+ end
+ uri_params = {
+ :host => LocaleApp.configuration.host,
+ :port => LocaleApp.configuration.port,
+ :path => "/projects/#{key}.json"
+ }
+ if LocaleApp.configuration.http_auth_username
+ uri_params[:userinfo] = "#{LocaleApp.configuration.http_auth_username}:#{LocaleApp.configuration.http_auth_password}"
+ end
+ URI::HTTP.build(uri_params).to_s
+ end
+ end
+end
View
1  locale_app.gemspec
@@ -30,4 +30,5 @@ Gem::Specification.new do |s|
s.add_development_dependency('yard')
s.add_development_dependency('RedCloth')
s.add_development_dependency('aruba')
+ s.add_development_dependency('fakeweb')
end
View
29 spec/locale_app/cli/install_spec.rb
@@ -0,0 +1,29 @@
+require 'spec_helper'
+require 'locale_app/cli/install'
+
+describe LocaleApp::CLI::Install, '.execute(key, output = $stdout)' do
+ before(:each) do
+ @output = StringIO.new
+ end
+
+ it "displays error if key is nil" do
+ command = LocaleApp::CLI::Install.new
+ command.execute(nil, @output)
+ @output.string.should match(/You must supply an API key/)
+ end
+
+ it "displays error if the key is there but isn't valid on localeapp.com" do
+ command = LocaleApp::CLI::Install.new
+ command.stub!(:check_key).and_return([false, {}])
+ command.execute('API_KEY', @output)
+ @output.string.should match(/Project not found/)
+ end
+
+ it "displays project name and base locale if the key is there and valid on localeapp.com" do
+ command = LocaleApp::CLI::Install.new
+ command.stub!(:check_key).and_return([true, valid_project_data])
+ command.execute('API_KEY', @output)
+ @output.string.should match(/Test Project/)
+ @output.string.should match(/en \(English\)/)
+ end
+end
View
19 spec/locale_app/key_checker_spec.rb
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe LocaleApp::KeyChecker, "#check(key)" do
+ it "returns false and an empty hash if the response from locale app is a 404" do
+ FakeWeb.register_uri(:get, 'http://api.localeapp.com/projects/TEST_KEY.json', :body => "", :status => ['404', 'Not Found'])
+ with_configuration do
+ @checker = LocaleApp::KeyChecker.new
+ end
+ @checker.check('TEST_KEY').should == [false, {}]
+ end
+
+ it "returns true and and the parsed json hash if the response from locale app is a 200" do
+ FakeWeb.register_uri(:get, 'http://api.localeapp.com/projects/TEST_KEY.json', :body => valid_project_data.to_json, :status => ['200', 'OK'])
+ with_configuration do
+ @checker = LocaleApp::KeyChecker.new
+ end
+ @checker.check('TEST_KEY').should == [true, valid_project_data]
+ end
+end
View
9 spec/spec_helper.rb
@@ -1,7 +1,9 @@
require 'i18n'
require 'locale_app'
+require 'fakeweb'
+require 'support/locale_app_integration_data'
-def with_configuration(options)
+def with_configuration(options = {})
LocaleApp.configuration = nil
LocaleApp.configure do |configuration|
options.each do |option, value|
@@ -12,5 +14,8 @@ def with_configuration(options)
end
RSpec.configure do |config|
-
+ config.include(LocaleAppIntegrationData)
+ config.before(:each) do
+ FakeWeb.allow_net_connect = false
+ end
end
View
14 spec/support/locale_app_integration_data.rb
@@ -0,0 +1,14 @@
+# This module is the source for all data that simulates what the real app would return.
+# It's included in the specs and cucumber tests, so if our format changes we
+# should only have to change here
+module LocaleAppIntegrationData
+ def valid_project_data
+ {
+ 'name' => "Test Project",
+ 'default_locale' => {
+ 'name' => "English",
+ 'code' => "en"
+ }
+ }
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.