made the job, broke outthe save_users, still need to do lifecycle callbacks

This commit is contained in:
zhitomirskiyi 2011-03-18 13:48:14 -07:00
parent 3afa14d341
commit ede7d4e8e2
7 changed files with 160 additions and 129 deletions

View file

@ -7,7 +7,7 @@ module Job
class UpdateServiceUsers < Base
def self.perform_delegate(service_id)
service = Service.find(service_id)
response = RestClient.get("https://graph.facebook.com/me/friends", {:params => {:access_token => service.access_token}})
service.save_friends
end
end
end

View file

@ -62,4 +62,14 @@ class Services::Facebook < Service
data_h
end
def save_friends
response = RestClient.get("https://graph.facebook.com/me/friends", {:params =>
{:fields => ['name', 'id', 'picture'], :access_token => self.access_token}})
data = JSON.parse(response.body)['data']
data.each{ |p|
ServiceUser.create(:service_id => self.id, :name => p["name"],
:uid => p["id"], :picture_url => p["picture"])
}
end
end

View file

@ -1,14 +1,17 @@
class CreateServiceUsers < ActiveRecord::Migration
def self.up
create_table :service_users do |t|
t.string :uid
t.string :name
t.integer :service_id
t.string :uid, :null => false
t.string :name, :null => false
t.string :picture_url, :null => false
t.integer :service_id, :null => false
t.integer :person_id
t.integer :contact_id
t.integer :request_id
t.timestamps
end
add_index :service_users, :service_id
end
def self.down

View file

@ -274,9 +274,10 @@ ActiveRecord::Schema.define(:version => 20110319172136) do
add_index "requests", ["sender_id"], :name => "index_requests_on_sender_id"
create_table "service_users", :force => true do |t|
t.string "uid"
t.string "name"
t.integer "service_id"
t.string "uid", :null => false
t.string "name", :null => false
t.string "picture_url", :null => false
t.integer "service_id", :null => false
t.integer "person_id"
t.integer "contact_id"
t.integer "request_id"
@ -284,6 +285,8 @@ ActiveRecord::Schema.define(:version => 20110319172136) do
t.datetime "updated_at"
end
add_index "service_users", ["service_id"], :name => "index_service_users_on_service_id"
create_table "services", :force => true do |t|
t.string "type", :null => false
t.integer "user_id", :null => false

View file

@ -6,39 +6,13 @@ describe Job::UpdateServiceUsers do
@post = @user.post(:status_message, :text => "hello", :to =>@user.aspects.first.id)
@service = Services::Facebook.new(:access_token => "yeah")
@user.services << @service
@user2 = Factory.create(:user_with_aspect)
@user2_fb_id = '820651'
@user2_fb_name = 'Maxwell Salzberg'
@user2_service = Services::Facebook.new(:uid => @user2_fb_id, :access_token => "yo")
@user2.services << @user2_service
@fb_list_hash = <<JSON
{
"data": [
{
"name": "#{@user2_fb_name}",
"id": "#{@user2_fb_id}"
},
{
"name": "Person to Invite",
"id": "abc123"
}
]
}
JSON
@web_mock = mock()
@web_mock.stub!(:body).and_return(@fb_list_hash)
RestClient.stub!(:get).and_return(@web_mock)
end
it 'requests a friend list' do
RestClient.should_receive(:get).with("https://graph.facebook.com/me/friends", {:params => {:access_token => @service.access_token}}).and_return(@web_mock)
Job::UpdateServiceUsers.perform(@service.id)
end
it 'creates a service user objects' do
lambda{
Job::UpdateServiceUsers.perform(@service.id)
}.should change(ServiceUser, :count).by(2)
it 'calls the update_friends for the service' do
Service.stub!(:find).and_return(@service)
@service.should_receive(:save_friends)
Job::UpdateServiceUsers.perform(@service.id)
end
end

View file

@ -1,5 +1,122 @@
require 'spec_helper'
describe ServiceUser do
pending "add some examples to (or delete) #{__FILE__}"
describe '#finder' do
before do
@user = alice
@post = @user.post(:status_message, :text => "hello", :to =>@user.aspects.first.id)
@service = Services::Facebook.new(:access_token => "yeah")
@user.services << @service
@user2 = Factory.create(:user_with_aspect)
@user2_fb_id = '820651'
@user2_fb_name = 'Maxwell Salzberg'
@user2_service = Services::Facebook.new(:uid => @user2_fb_id, :access_token => "yo")
@user2.services << @user2_service
@fb_list_hash = <<JSON
{
"data": [
{
"name": "#{@user2_fb_name}",
"id": "#{@user2_fb_id}",
"picture": "http://cdn.fn.com/pic1.jpg"
},
{
"name": "Person to Invite",
"id": "abc123",
"picture": "http://cdn.fn.com/pic1.jpg"
}
]
}
JSON
@web_mock = mock()
@web_mock.stub!(:body).and_return(@fb_list_hash)
RestClient.stub!(:get).and_return(@web_mock)
end
context 'lifecycle callbacks' do
it 'contains a name' do
su = ServiceUser.new(:service_id => @service.id, :uid => @user2_fb_id)
su.save
su.name.should == @user2_fb_name
end
it 'contains a photo url' do
pending
end
it 'contains a FB id' do
@service.finder.include?(@user2_fb_id).should be_true
end
it 'contains a diaspora person object' do
@service.finder["#{@user2_fb_id}"][:person].should == @user2.person
end
it 'caches the profile' do
@service.finder["#{@user2_fb_id}"][:person].profile.loaded?.should be_true
end
it 'does not include the person if the search is disabled' do
p = @user2.person.profile
p.searchable = false
p.save
@service.finder["#{@user2_fb_id}"][:person].should be_nil
end
context "request" do
before do
@request = Request.diaspora_initialize(:from => @user2.person, :to => @user.person, :into => @user2.aspects.first)
Postzord::Receiver.new(@user, :object => @request, :person => @user2.person).receive_object
Request.count.should == 1
end
it 'contains a request object if one has been sent' do
@service.finder["#{@user2_fb_id}"][:request].should == @request
end
it 'caches the profile' do
@service.finder["#{@user2_fb_id}"][:request].sender.profile.loaded?.should be_true
end
it 'caches the sender' do
@service.finder["#{@user2_fb_id}"][:request].sender.loaded?.should be_true
end
end
it 'contains a contact object if connected' do
connect_users(@user, @user.aspects.first, @user2, @user2.aspects.first)
@service.finder["#{@user2_fb_id}"][:contact].should == @user.reload.contact_for(@user2.person)
end
context 'only local' do
it 'does not return people who are remote' do
@service.finder(:local => true)['abc123'].should be nil
@service.finder(:local => true)["#{@user2_fb_id}"].should_not be_nil
end
end
context 'only remote' do
it 'does not return people who are remote' do
@service.finder(:remote => true)['abc123'].should_not be nil
@service.finder(:remote => true)["#{@user2_fb_id}"].should be_nil
end
end
context 'already invited' do
before do
@user2.invitation_service = 'facebook'
@user2.invitation_identifier = @user2_fb_id
@user2.save!
end
it 'contains an invitation if invited' do
@inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first)
@service.finder["#{@user2_fb_id}"][:invitation_id].should == @inv.id
end
it 'does not find the user with a wrong identifier' do
@user2.invitation_identifier = 'dsaofhnadsoifnsdanf'
@user2.save
@inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first)
@service.finder["#{@user2_fb_id}"][:invitation_id].should be_nil
end
end
end
end
end

View file

@ -27,8 +27,8 @@ describe Services::Facebook do
end
end
describe '#finder' do
before do
describe '#cache_friends' do
before do
@user2 = Factory.create(:user_with_aspect)
@user2_fb_id = '820651'
@user2_fb_name = 'Maxwell Salzberg'
@ -39,11 +39,13 @@ describe Services::Facebook do
"data": [
{
"name": "#{@user2_fb_name}",
"id": "#{@user2_fb_id}"
"id": "#{@user2_fb_id}",
"picture": "http://cdn.fn.com/pic1.jpg"
},
{
"name": "Person to Invite",
"id": "abc123"
"id": "abc123",
"picture": "http://cdn.fn.com/pic1.jpg"
}
]
}
@ -54,93 +56,15 @@ JSON
end
it 'requests a friend list' do
RestClient.should_receive(:get).with("https://graph.facebook.com/me/friends", {:params => {:access_token => @service.access_token}}).and_return(@web_mock)
@service.finder
RestClient.should_receive(:get).with("https://graph.facebook.com/me/friends", {:params =>
{:fields => ['name', 'id', 'picture'], :access_token => @service.access_token}}).and_return(@web_mock)
@service.save_friends
end
context 'returns a hash' do
it 'returns a hash' do
@service.finder.class.should == Hash
end
it 'contains a name' do
@service.finder["#{@user2_fb_id}"][:name].should == @user2_fb_name
end
it 'contains a photo url' do
pending
end
it 'contains a FB id' do
@service.finder.include?(@user2_fb_id).should be_true
end
it 'contains a diaspora person object' do
@service.finder["#{@user2_fb_id}"][:person].should == @user2.person
end
it 'caches the profile' do
@service.finder["#{@user2_fb_id}"][:person].profile.loaded?.should be_true
end
it 'does not include the person if the search is disabled' do
p = @user2.person.profile
p.searchable = false
p.save
@service.finder["#{@user2_fb_id}"][:person].should be_nil
end
context "request" do
before do
@request = Request.diaspora_initialize(:from => @user2.person, :to => @user.person, :into => @user2.aspects.first)
Postzord::Receiver.new(@user, :object => @request, :person => @user2.person).receive_object
Request.count.should == 1
end
it 'contains a request object if one has been sent' do
@service.finder["#{@user2_fb_id}"][:request].should == @request
end
it 'caches the profile' do
@service.finder["#{@user2_fb_id}"][:request].sender.profile.loaded?.should be_true
end
it 'caches the sender' do
@service.finder["#{@user2_fb_id}"][:request].sender.loaded?.should be_true
end
end
it 'contains a contact object if connected' do
connect_users(@user, @user.aspects.first, @user2, @user2.aspects.first)
@service.finder["#{@user2_fb_id}"][:contact].should == @user.reload.contact_for(@user2.person)
end
context 'only local' do
it 'does not return people who are remote' do
@service.finder(:local => true)['abc123'].should be nil
@service.finder(:local => true)["#{@user2_fb_id}"].should_not be_nil
end
end
context 'only remote' do
it 'does not return people who are remote' do
@service.finder(:remote => true)['abc123'].should_not be nil
@service.finder(:remote => true)["#{@user2_fb_id}"].should be_nil
end
end
context 'already invited' do
before do
@user2.invitation_service = 'facebook'
@user2.invitation_identifier = @user2_fb_id
@user2.save!
end
it 'contains an invitation if invited' do
@inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first)
@service.finder["#{@user2_fb_id}"][:invitation_id].should == @inv.id
end
it 'does not find the user with a wrong identifier' do
@user2.invitation_identifier = 'dsaofhnadsoifnsdanf'
@user2.save
@inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first)
@service.finder["#{@user2_fb_id}"][:invitation_id].should be_nil
end
end
it 'creates a service user objects' do
lambda{
@service.save_friends
}.should change(ServiceUser, :count).by(2)
end
end
end