Permalink
Browse files

refactor a few tests

  • Loading branch information...
Sven Fuchs
Sven Fuchs committed Apr 1, 2010
1 parent 4473f2b commit de5fd8e77f7306efaefc8286fa5f2ad9d125ae2f
View
@@ -7,7 +7,7 @@ gem 'httparty'
gem 'mbbx6spp-twitter4r' #, :source => 'http://gems.github.com'
gem 'twibot'
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
gem 'mocha'
View
4 app.rb
@@ -1,13 +1,15 @@
$: << File.expand_path('../lib', __FILE__)
require 'rubygems'
require 'sinatra'
+
require 'command'
require 'identity'
+require 'identity/command'
# should be set through heroku config:add
ENV['couchdb_url'] ||= 'http://localhost:5984/rugb'
ENV['twitter_login'] ||= 'rugb_test'
-ENV['twitter_password'] ||= 'clubmate'
+ENV['twitter_password'] ||= 'password'
Dir[File.expand_path('../app/**/*', __FILE__)].each { |file| require file }
View
@@ -33,7 +33,7 @@ def initialize(profiles = {})
Sources.each_name do |name|
define_method(name) { profiles[name] || {} }
- define_method(:"#{name}=") { |profile| profiles[name] = profile }
+ define_method(:"#{name}=") { |profile| set_profile(name, profile) }
end
def handles
@@ -55,7 +55,7 @@ def name
@name ||= profiles.map { |name, profile| profile['name'] }.compact.first
end
- def claim
+ def claim_unclaimed
profiles.each { |name, profile| profile['claimed_at'] = Time.now unless profile['claimed_at'] }
end
@@ -69,9 +69,3 @@ def to_json(*args)
{ :handles => handles, :profiles => profiles, :groups => groups, :created_at => created_at }.to_json
end
end
-#
-# class Command
-# include Identity::Command
-# end
-require 'identity/command'
-
View
@@ -13,8 +13,9 @@ def create
desc 'update [sources]', 'update from all or given sources'
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)
- sender.claim
+ sender.claim_unclaimed
sender.save
end
View
@@ -15,7 +15,7 @@ def all
end
def [](name)
- all[name]
+ all[name.to_s]
end
def map(&block) # use Enumerable
@@ -35,19 +35,25 @@ def each_without(*names)
end
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
- 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) }
all['json'].update(identity, url)
end
- def update_named(identity, arg)
- ix = arg.index(':')
- name = arg[0..ix - 1]
- value = arg[ix + 1..-1]
- self[name].update(identity, value)
+ def update_from_named(identity, arg)
+ ix = arg.index(':')
+ source = arg[0..ix - 1]
+ handle = arg[ix + 1..-1]
+ update_from_source(identity, source, handle)
+ end
+
+ def update_from_source(identity, source, handle)
+ self[source].update(identity, handle) if handle
end
end
end
@@ -36,7 +36,8 @@ def fetch(url)
end
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
@@ -10,7 +10,7 @@ def initialize
'blog' => 'url',
'location' => 'location',
'company' => 'company',
- 'gravatar' => 'gravatar_id'
+ 'avatar' => 'avatar'
}
end
@@ -27,7 +27,9 @@ def source_url(handle)
end
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
@@ -1,11 +1,10 @@
module Identity::Sources
class Json < Base
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|
- source.update(identity, identity.json[name]) if identity.json[name]
- end if identity.json
+ # i.e. when the json defines a github handle, we'll pull the github profile
+ Identity::Sources.update_from_source(identity, 'github', json['github']) if json
end
def profile_url(profile)
@@ -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
@@ -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
@@ -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
+
Oops, something went wrong.

0 comments on commit de5fd8e

Please sign in to comment.