Skip to content

Commit

Permalink
refactor a few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Apr 1, 2010
1 parent 4473f2b commit de5fd8e
Show file tree
Hide file tree
Showing 20 changed files with 355 additions and 122 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gem 'httparty'
gem 'mbbx6spp-twitter4r' #, :source => 'http://gems.github.com' gem 'mbbx6spp-twitter4r' #, :source => 'http://gems.github.com'
gem 'twibot' gem 'twibot'
gem 'simply_stored' gem 'simply_stored'
gem 'command', '>= 0.0.2' #, :path => '/Volumes/Users/sven/Development/lab/command' gem 'command', '>= 0.0.2', :path => '/Volumes/Users/sven/Development/lab/command'


group :test do group :test do
gem 'mocha' gem 'mocha'
Expand Down
4 changes: 3 additions & 1 deletion app.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,13 +1,15 @@
$: << File.expand_path('../lib', __FILE__) $: << File.expand_path('../lib', __FILE__)
require 'rubygems' require 'rubygems'
require 'sinatra' require 'sinatra'

require 'command' require 'command'
require 'identity' require 'identity'
require 'identity/command'


# should be set through heroku config:add # should be set through heroku config:add
ENV['couchdb_url'] ||= 'http://localhost:5984/rugb' ENV['couchdb_url'] ||= 'http://localhost:5984/rugb'
ENV['twitter_login'] ||= 'rugb_test' ENV['twitter_login'] ||= 'rugb_test'
ENV['twitter_password'] ||= 'clubmate' ENV['twitter_password'] ||= 'password'


Dir[File.expand_path('../app/**/*', __FILE__)].each { |file| require file } Dir[File.expand_path('../app/**/*', __FILE__)].each { |file| require file }


Expand Down
10 changes: 2 additions & 8 deletions lib/identity.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def initialize(profiles = {})


Sources.each_name do |name| Sources.each_name do |name|
define_method(name) { profiles[name] || {} } define_method(name) { profiles[name] || {} }
define_method(:"#{name}=") { |profile| profiles[name] = profile } define_method(:"#{name}=") { |profile| set_profile(name, profile) }
end end


def handles def handles
Expand All @@ -55,7 +55,7 @@ def name
@name ||= profiles.map { |name, profile| profile['name'] }.compact.first @name ||= profiles.map { |name, profile| profile['name'] }.compact.first
end end


def claim def claim_unclaimed
profiles.each { |name, profile| profile['claimed_at'] = Time.now unless profile['claimed_at'] } profiles.each { |name, profile| profile['claimed_at'] = Time.now unless profile['claimed_at'] }
end end


Expand All @@ -69,9 +69,3 @@ def to_json(*args)
{ :handles => handles, :profiles => profiles, :groups => groups, :created_at => created_at }.to_json { :handles => handles, :profiles => profiles, :groups => groups, :created_at => created_at }.to_json
end end
end end
#
# class Command
# include Identity::Command
# end
require 'identity/command'

3 changes: 2 additions & 1 deletion lib/identity/command.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def create


desc 'update [sources]', 'update from all or given sources' desc 'update [sources]', 'update from all or given sources'
def update def update
self.args = sender.profiles.map { |name, profile| Identity::Sources[name].profile_url(profile['handle']) } if args.empty?
Identity::Sources.update_all(sender, args) Identity::Sources.update_all(sender, args)
sender.claim sender.claim_unclaimed
sender.save sender.save
end end


Expand Down
22 changes: 14 additions & 8 deletions lib/identity/sources.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def all
end end


def [](name) def [](name)
all[name] all[name.to_s]
end end


def map(&block) # use Enumerable def map(&block) # use Enumerable
Expand All @@ -35,19 +35,25 @@ def each_without(*names)
end end


def update_all(identity, arguments) def update_all(identity, arguments)
arguments.each { |arg| arg.starts_with?('http://') ? update_url(identity, arg) : update_named(identity, arg) } arguments.each do |arg|
arg.starts_with?('http://') ? update_from_url(identity, arg) : update_from_named(identity, arg)
end
end end


def update_url(identity, url) def update_from_url(identity, url)
each { |name, source| handle = source.recognize_url(url) and return source.update(identity, handle) } each { |name, source| handle = source.recognize_url(url) and return source.update(identity, handle) }
all['json'].update(identity, url) all['json'].update(identity, url)
end end


def update_named(identity, arg) def update_from_named(identity, arg)
ix = arg.index(':') ix = arg.index(':')
name = arg[0..ix - 1] source = arg[0..ix - 1]
value = arg[ix + 1..-1] handle = arg[ix + 1..-1]
self[name].update(identity, value) update_from_source(identity, source, handle)
end

def update_from_source(identity, source, handle)
self[source].update(identity, handle) if handle
end end
end end
end end
3 changes: 2 additions & 1 deletion lib/identity/sources/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def fetch(url)
end end


def remap(data) def remap(data)
Hash[*(data || {}).map { |key, value| [map[key], value] if map.key?(key) && value }.compact.flatten] mapped = (data || {}).map { |key, value| [map[key], value] if map.key?(key) && value }
Hash[*mapped.compact.flatten].merge(:source_url => data['source_url'])
end end
end end
end end
6 changes: 4 additions & 2 deletions lib/identity/sources/github.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize
'blog' => 'url', 'blog' => 'url',
'location' => 'location', 'location' => 'location',
'company' => 'company', 'company' => 'company',
'gravatar' => 'gravatar_id' 'avatar' => 'avatar'
} }
end end


Expand All @@ -27,7 +27,9 @@ def source_url(handle)
end end


def fetch(source_url) def fetch(source_url)
remap(Base.get(source_url)['user']) data = Base.get(source_url)['user']
data['avatar'] = "http:gravatar.com/avatar/#{data.delete('gravatar_id')}" if data.key?('gravatar_id')
remap(data)
end end
end end
end end
7 changes: 3 additions & 4 deletions lib/identity/sources/json.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,10 @@
module Identity::Sources module Identity::Sources
class Json < Base class Json < Base
def update(identity, url) def update(identity, url)
identity.set_profile('json', fetch(url)) json = identity.set_profile('json', fetch(url))


Identity::Sources.each_without('json') do |name, source| # i.e. when the json defines a github handle, we'll pull the github profile
source.update(identity, identity.json[name]) if identity.json[name] Identity::Sources.update_from_source(identity, 'github', json['github']) if json
end if identity.json
end end


def profile_url(profile) def profile_url(profile)
Expand Down
41 changes: 41 additions & 0 deletions test/commands/create_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,41 @@
require File.expand_path('../../test_helper', __FILE__)

class CommandCreateTest < Test::Unit::TestCase
def setup
setup_stubs
@now = Time.now
Time.stubs(:now).returns(@now)
end

test '!create (w/o arguments) polls the twitter profile' do
command('svenfuchs', '@rugb !create').run
assert_profile('svenfuchs', 'twitter', :claimed_at => @now)
end

test '!create twitter:svenfuchs polls the twitter profile, same shit' do
command('svenfuchs', '@rugb !create').run
assert_profile('svenfuchs', 'twitter', :claimed_at => @now)
end

test '!create github:svenfuchs polls the twitter and github profiles' do
command('svenfuchs', '@rugb !create github:svenfuchs').run
assert_profile('svenfuchs', 'twitter', :claimed_at => @now)
assert_profile('svenfuchs', 'github', :claimed_at => @now)
end

test '!create http://github.com/svenfuchs polls the twitter and github profiles' do
command('svenfuchs', '@rugb !create http://github.com/svenfuchs').run
assert_profile('svenfuchs', 'twitter', :claimed_at => @now)
assert_profile('svenfuchs', 'github', :claimed_at => @now)
end

test '!create http://tinyurl.com/yc7t8bv polls the twitter and json profiles AND updates from github BUT NOT twitter' do
source_url = Identity::Sources['github'].source_url('hax')
Identity::Sources::Base.expects(:get).with(source_url).never

command('svenfuchs', '@rugb !create http://tinyurl.com/yc7t8bv').run
assert_profile('svenfuchs', 'twitter', :claimed_at => @now)
assert_profile('svenfuchs', 'json', :claimed_at => @now)
assert_profile('svenphoox', 'github', :claimed_at => @now)
end
end
45 changes: 45 additions & 0 deletions test/commands/update_existing_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,45 @@
require File.expand_path('../../test_helper', __FILE__)

class CommandUpdateExistingTest < Test::Unit::TestCase
def setup
setup_stubs
@now = Time.now
Time.stubs(:now).returns(@now)

Identity.create(
:json => profile('json', 'svenfuchs').merge(:email => 'will_be_overwritten'),
:twitter => profile('twitter', 'svenfuchs').merge(:name => 'will_be_overwritten'),
:github => profile('github', 'svenfuchs').merge(:name => 'will_be_overwritten')
)
end

test '!update updates all known profiles' do
command('svenfuchs', '@rugb !update').run
assert_profiles('svenfuchs', 'twitter', 'json')
assert_profile('svenphoox', 'github')
end

test '!update twitter:svenfuchs updates the twitter profile' do
command('svenfuchs', '@rugb !update twitter:svenfuchs').run
assert_profile('svenfuchs', 'twitter')
end

test '!update github:svenfuchs updates the github profile' do
command('svenfuchs', '@rugb !update github:svenfuchs').run
assert_profile('svenfuchs', 'github')
end

test '!update http://github.com/svenfuchs updates the github profile' do
command('svenfuchs', '@rugb !update http://github.com/svenfuchs').run
assert_profile('svenfuchs', 'github')
end

test '!update http://tinyurl.com/yc7t8bv updates the json profiles AND updates from github BUT NOT twitter' do
source_url = Identity::Sources['github'].source_url('hax')
Identity::Sources::Base.expects(:get).with(source_url).never

command('svenfuchs', '@rugb !update http://tinyurl.com/yc7t8bv').run
assert_profiles('svenfuchs', 'json')
assert_profile('svenphoox', 'github')
end
end
39 changes: 39 additions & 0 deletions test/commands/update_new_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,39 @@
require File.expand_path('../../test_helper', __FILE__)

class CommandUpdateNewTest < Test::Unit::TestCase
def setup
setup_stubs
@now = Time.now
Time.stubs(:now).returns(@now)
end

test '!update (w/o arguments) polls the twitter profile' do
command('svenfuchs', '@rugb !update').run
assert_profile('svenfuchs', 'twitter')
end

test '!update twitter:svenfuchs polls the twitter profile' do
command('svenfuchs', '@rugb !update twitter:svenfuchs').run
assert_profile('svenfuchs', 'twitter')
end

test '!update github:svenfuchs polls the twitter and github profiles' do
command('svenfuchs', '@rugb !update github:svenfuchs').run
assert_profiles('svenfuchs', 'twitter', 'github')
end

test '!update http://github.com/svenfuchs polls the twitter and github profiles' do
command('svenfuchs', '@rugb !update http://github.com/svenfuchs').run
assert_profiles('svenfuchs', 'twitter', 'github')
end

test '!update http://tinyurl.com/yc7t8bv polls the twitter and json profiles AND updates from github BUT NOT twitter' do
source_url = Identity::Sources['github'].source_url('hax')
Identity::Sources::Base.expects(:get).with(source_url).never

command('svenfuchs', '@rugb !update http://tinyurl.com/yc7t8bv').run
assert_profiles('svenfuchs', 'twitter', 'json')
assert_profile('svenphoox', 'github')
end
end

Loading

0 comments on commit de5fd8e

Please sign in to comment.