Skip to content

Commit

Permalink
Add initial "Twitter" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
David Trasbo committed Dec 3, 2010
1 parent 8124770 commit c67d578
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.gem
Gemfile.lock
tmp
53 changes: 53 additions & 0 deletions features/step_definitions/app_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Given /^I have a Sinatra app with Cucumber and OmniAuth$/ do
FileUtils.rm_rf(App::ROOT)
FileUtils.cp_r(File.join(ROOT, 'fixtures/app'), App::ROOT)
end

When /^I add a "([^"]*)" feature with:$/ do |name, text|
App.open_file("features/#{name.downcase.gsub(' ', '_')}.feature", 'w') do |f|
f.write "Feature: #{name}\n\n"
f.write text.split("\n").collect { |l| " #{l}" }.join("\n")
end
end

When /^I add the "([^"]*)" strategy to the middleware stack$/ do |strategy|
app = File.read(File.join(App::ROOT, 'app.rb'))

App.open_file('app.rb', 'w') do |f|
f.write app.gsub(
/(enable :sessions)/,
"\\1\n\nuse OmniAuth::Strategies::Twitter, 'KEY', 'SECRET'"
)
end
end

When /^I add a link to "([^"]*)" with the text "([^"]*)" to the homepage$/ do |url, text|
When "I add the following to my Sinatra app:",
"get '/' do\n '<a href=\"#{url}\">#{text}</a>'\nend"
end

When /^I add the following step definition to the user steps:$/ do |text|
App.open_file('features/step_definitions/user_steps.rb', 'a') do |f|
f.write "#{text}\n\n"
end
end

When /^I add the following to my Sinatra app:$/ do |text|
App.open_file('app.rb', 'a') do |f|
f.write "\n#{text}\n"
end
end

Then /^the features should pass$/ do
RVM.gemset.use! 'oa-testing-test'

FileUtils.cd(App::ROOT) do
cucumber_output = `BUNDLE_GEMFILE="#{App::ROOT}/Gemfile" cucumber`
end

if $?.exitstatus != 0
raise cucumber_output
end

RVM.gemset.use! 'oa-testing'
end
10 changes: 10 additions & 0 deletions features/support/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module App
ROOT = File.join(::ROOT, 'tmp/app')

def open_file(file, mode)
File.open(File.join(ROOT, file), mode) do |f|
yield f
end
end
module_function :open_file
end
4 changes: 4 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
require 'bundler/setup'
$: << File.expand_path("~/.rvm/lib")
require 'rvm'

ROOT = File.expand_path(File.join(File.dirname(__FILE__), '../..'))
35 changes: 35 additions & 0 deletions features/twitter.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Feature: Twitter

Scenario:
Given I have a Sinatra app with Cucumber and OmniAuth
When I add a "User signs up" feature with:
"""
Scenario: The user chooses to authorize through Twitter
Given I am on the homepage
When I follow "Sign up using Twitter"
And I authorize the app to read my Twitter info
Then I should see an OmniAuth hash of my Twitter info
"""
And I add the "Twitter" strategy to the middleware stack
And I add a link to "/auth/twitter" with the text "Sign up using Twitter" to the homepage
And I add the following step definition to the user steps:
"""
When /^I authorize the app to read my Twitter info$/ do
visit '/auth/twitter/callback'
end
"""
And I add the following to my Sinatra app:
"""
get '/auth/twitter/callback' do
request.env['omniauth.auth'].to_yaml
end
"""
And I add the following step definition to the user steps:
"""
Then /^I should see an OmniAuth hash of my Twitter info$/ do
auth_hash = YAML.load(response.body)
auth_hash['provider'].should == 'twitter'
auth_hash['user_info']['name'].should == 'James Garcia'
end
"""
Then the features should pass
1 change: 1 addition & 0 deletions oa-testing.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Gem::Specification.new do |s|
s.files = ['README.md', 'LICENSE'] + Dir['lib/**/*']

s.add_development_dependency 'cucumber', '~> 0.9.0'
s.add_development_dependency 'rspec', '~> 1.3.0'
end
9 changes: 9 additions & 0 deletions spec/fixtures/app/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source 'http://rubygems.org/'

gem 'sinatra'
gem 'omniauth'
gem 'cucumber'
gem 'rspec'
gem 'rack-test'
gem 'webrat'
gem 'fakeweb'
5 changes: 5 additions & 0 deletions spec/fixtures/app/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'bundler/setup'
require 'sinatra'
require 'omniauth'

enable :sessions
35 changes: 35 additions & 0 deletions spec/fixtures/app/features/fixtures/twitter_user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"follow_request_sent": false,
"profile_use_background_image": true,
"id": 218183011,
"verified": false,
"profile_sidebar_fill_color": "DDEEF6",
"profile_text_color": "333333",
"followers_count": 1,
"profile_sidebar_border_color": "C0DEED",
"location": null,
"profile_background_color": "C0DEED",
"id_str": "218183011",
"utc_offset": null,
"statuses_count": 0,
"description": null,
"friends_count": 0,
"profile_link_color": "0084B4",
"profile_image_url": "http://s.twimg.com/a/1290206336/images/default_profile_4_normal.png",
"notifications": false,
"show_all_inline_media": false,
"geo_enabled": false,
"profile_background_image_url": "http://s.twimg.com/a/1290206336/images/themes/theme1/bg.png",
"screen_name": "jamesgarcia123",
"lang": "en",
"profile_background_tile": false,
"favourites_count": 0,
"name": "James Garcia",
"url": null,
"created_at": "Sun Nov 21 17:11:16 +0000 2010",
"contributors_enabled": false,
"time_zone": null,
"protected": false,
"following": false,
"listed_count": 0
}
7 changes: 7 additions & 0 deletions spec/fixtures/app/features/step_definitions/web_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Given /^I am on the homepage$/ do
visit '/'
end

When /^I follow "([^"]*)"$/ do |selector|
click_link selector
end
41 changes: 41 additions & 0 deletions spec/fixtures/app/features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require File.join(File.dirname(__FILE__), '../../app')

require 'rspec'
require 'rack/test'
require 'webrat'
require 'fakeweb'

FakeWeb.allow_net_connect = false

FakeWeb.register_uri(
:post,
'https://api.twitter.com/oauth/request_token',
body: 'oauth_token=fake&oauth_token_secret=fake'
)

FakeWeb.register_uri(
:post,
'https://api.twitter.com/oauth/access_token',
body: 'oauth_token=fake&oauth_token_secret=fake&user_id=218183011'
)

FakeWeb.register_uri(
:get,
'https://api.twitter.com/1/account/verify_credentials.json',
body: File.read(File.join(File.dirname(__FILE__), '../fixtures/twitter_user.json'))
)

Webrat.configure do |config|
config.mode = :rack
end

module AppWorld
include Rack::Test::Methods
include Webrat::Methods

def app
Sinatra::Application
end
end

World(AppWorld)

0 comments on commit c67d578

Please sign in to comment.