facebook_test_users 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ module FacebookTestUsers
6
6
  attr_reader :name, :id, :secret
7
7
 
8
8
  def initialize(attrs)
9
- @name, @id, @secret = attrs[:name], attrs[:id], attrs[:secret]
9
+ @name, @id, @secret = attrs[:name].to_s, attrs[:id].to_s, attrs[:secret].to_s
10
10
  validate!
11
11
  end
12
12
 
@@ -58,27 +58,27 @@ module FacebookTestUsers
58
58
  all.find {|a| a.name == name}
59
59
  end
60
60
 
61
+ def access_token
62
+ @access_token ||= AccessToken.get(id, secret)
63
+ end
64
+
61
65
  private
62
66
 
63
67
  def users_url
64
68
  GRAPH_API_BASE + "/#{id}/accounts/test-users"
65
69
  end
66
70
 
67
- def access_token
68
- @access_token ||= AccessToken.get(id, secret)
69
- end
70
-
71
71
  def validate!
72
72
  unless name && name =~ /\S/
73
73
  raise ArgumentError, "App name must not be empty"
74
74
  end
75
75
 
76
76
  unless id && id =~ /^[0-9a-f]+$/i
77
- raise ArgumentError, "App id must be a nonempty hex string"
77
+ raise ArgumentError, "App id must be a nonempty hex string, but was #{id.inspect}"
78
78
  end
79
79
 
80
80
  unless secret && secret =~ /^[0-9a-f]+$/i
81
- raise ArgumentError, "App secret must be a nonempty hex string"
81
+ raise ArgumentError, "App secret must be a nonempty hex string, but was #{secret.inspect}"
82
82
  end
83
83
  end
84
84
 
@@ -60,6 +60,23 @@ module FacebookTestUsers
60
60
  puts "Login URL: #{user.login_url}"
61
61
  end
62
62
 
63
+ desc "friend", "Make two of an app's users friends"
64
+ method_option "app", :aliases => %w[-a], :type => :string, :required => true, :banner => "Name of the app"
65
+ method_option "user1", :aliases => %w[-1 -u1], :type => :string, :required => true, :banner => "ID of the first user"
66
+ method_option "user2", :aliases => %w[-2 -u2], :type => :string, :required => true, :banner => "ID of the second user"
67
+
68
+ def friend
69
+ app = find_app!(options[:app])
70
+ users = app.users
71
+ u1 = users.find {|u| u.id.to_s == options[:user1] } or raise ArgumentError, "No user found w/id #{options[:user1].inspect}"
72
+ u2 = users.find {|u| u.id.to_s == options[:user2] } or raise ArgumentError, "No user found w/id #{options[:user2].inspect}"
73
+
74
+ # the first request is just a request; the second request
75
+ # accepts the first request
76
+ u1.send_friend_request_to(u2)
77
+ u2.send_friend_request_to(u1)
78
+ end
79
+
63
80
  desc "rm", "Remove a test user from an application"
64
81
  method_option "app", :aliases => %w[-a], :type => :string, :required => true, :banner => "Name of the app"
65
82
  method_option "user", :banner => "ID of the user to remove", :aliases => %w[-u], :type => :string, :required => true
@@ -17,10 +17,20 @@ module FacebookTestUsers
17
17
  RestClient.delete(destroy_url)
18
18
  end
19
19
 
20
+ def send_friend_request_to(other)
21
+ RestClient.post(friend_request_url_for(other),
22
+ 'access_token' => access_token.to_s)
23
+ end
24
+
20
25
  private
21
26
 
22
27
  def destroy_url
23
28
  GRAPH_API_BASE + "/#{id}?access_token=#{URI.escape(access_token.to_s)}"
24
29
  end
30
+
31
+ def friend_request_url_for(other)
32
+ GRAPH_API_BASE + "/#{id}/friends/#{other.id}"
33
+ end
34
+
25
35
  end
26
36
  end
@@ -1,3 +1,3 @@
1
1
  module FacebookTestUsers
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -2,10 +2,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_hel
2
2
 
3
3
  describe "fbtu users add" do
4
4
  before(:each) do
5
- fbtu %w[apps add --name alpha --app-id 123456 --app-secret abcdef]
6
- FakeWeb.register_uri(:get,
7
- 'https://graph.facebook.com/oauth/access_token?client_id=123456&client_secret=abcdef&grant_type=client_credentials',
8
- :body => 'access_token=doublesecret')
5
+ alpha = add_app('alpha')
9
6
 
10
7
  new_user = {
11
8
  "id" => 60189,
@@ -14,7 +11,7 @@ describe "fbtu users add" do
14
11
  }
15
12
 
16
13
  FakeWeb.register_uri(:post,
17
- 'https://graph.facebook.com/123456/accounts/test-users',
14
+ "https://graph.facebook.com/#{alpha.id}/accounts/test-users",
18
15
  :body => new_user.to_json)
19
16
  end
20
17
 
@@ -0,0 +1,29 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper'))
2
+
3
+ describe "fbtu users friend" do
4
+ before(:each) do
5
+ alpha = add_app('alpha')
6
+
7
+ @alice = add_user_to(alpha)
8
+ @bob = add_user_to(alpha)
9
+
10
+ FakeWeb.register_uri(:post,
11
+ "https://graph.facebook.com/#{@alice.id}/friends/#{@bob.id}",
12
+ :body => "true")
13
+ FakeWeb.register_uri(:post,
14
+ "https://graph.facebook.com/#{@bob.id}/friends/#{@alice.id}",
15
+ :body => "true")
16
+ end
17
+
18
+ it "adds a user with the app installed" do
19
+ fbtu ['users', 'friend',
20
+ '--app', 'alpha',
21
+ '--user1', @alice.id,
22
+ '--user2', @bob.id]
23
+
24
+ FakeWeb.should have_requested(:post,
25
+ "https://graph.facebook.com/#{@alice.id}/friends/#{@bob.id}")
26
+ FakeWeb.should have_requested(:post,
27
+ "https://graph.facebook.com/#{@bob.id}/friends/#{@alice.id}")
28
+ end
29
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'pp'
2
2
  require 'stringio'
3
3
  require 'tempfile'
4
+ require 'digest/md5'
4
5
 
5
6
  require 'facebook_test_users'
6
7
  require 'facebook_test_users/cli'
@@ -43,7 +44,7 @@ module FBTU
43
44
  @err = @err.string
44
45
  end
45
46
  end
46
-
47
+
47
48
  def capture_stdout_into(io)
48
49
  $stdout = io
49
50
  yield
@@ -60,13 +61,110 @@ module FBTU
60
61
 
61
62
  end # CliTestMethods
62
63
 
64
+ module SetupHelpers
65
+
66
+ def self.reset!
67
+ UserList.reset!
68
+ end
69
+
70
+ class UserList
71
+
72
+ def self.reset!
73
+ @users = Hash.new {|h,k| h[k] = []}
74
+ end
75
+
76
+ def self.users
77
+ @users || reset!
78
+ end
79
+
80
+ def self.add(app, user)
81
+ users[app] << user
82
+ end
83
+
84
+ def self.for(app)
85
+ users[app]
86
+ end
87
+
88
+ end # UserList
89
+
90
+ def add_app(name, opts={})
91
+ appinfo = {
92
+ :name => name,
93
+ :app_id => opts[:app_id] || 123456,
94
+ :app_secret => opts[:app_secret] || 'abcdef'
95
+ }
96
+
97
+ appinfo[:access_token] = Digest::MD5.hexdigest(appinfo[:app_id].to_s + "::" + appinfo[:app_secret].to_s)
98
+
99
+ FakeWeb.register_uri(:get,
100
+ "https://graph.facebook.com/oauth/access_token?client_id=#{appinfo[:app_id]}&client_secret=#{appinfo[:app_secret]}&grant_type=client_credentials",
101
+ :body => "access_token=#{appinfo[:access_token]}")
102
+
103
+ fbtu (%w[apps add --name] << name <<
104
+ '--app-id' << appinfo[:app_id] <<
105
+ '--app-secret' << appinfo[:app_secret])
106
+
107
+ app = FacebookTestUsers::App.find_by_name(name)
108
+ fakeweb_register_users_url(app, [])
109
+ app
110
+ end
111
+
112
+ def add_user_to(app)
113
+ # real FB test users all have IDs starting with 10000 (or at
114
+ # least the ones that I've seen; N=20 or so)
115
+ user_id = "10000" + rand(9999999999).to_s
116
+ user_access_token = Digest::MD5.hexdigest(user_id)
117
+
118
+ user_data = {
119
+ "id" => user_id,
120
+ "access_token" => user_access_token,
121
+ "login_url" => "https://facebook.example.com/login/#{user_id}",
122
+ }
123
+
124
+ FakeWeb.register_uri(:post,
125
+ "https://graph.facebook.com/#{app.id}/accounts/test-users",
126
+ :body => user_data.to_json)
127
+
128
+
129
+ # XXX ideally we'd call fbtu, but then we're stuck parsing its
130
+ # textual output to get the fields to create a user object
131
+ user = app.create_user
132
+ UserList.add(app, user)
133
+ fakeweb_register_users_url(app, UserList.for(app))
134
+ user
135
+ end
136
+
137
+ protected
138
+
139
+ # If this looks like it's getting called a bunch, it's because
140
+ # the set of users changes all the time, and so their JSONified
141
+ # form has to be refreshed.
142
+ def fakeweb_register_users_url(app, users)
143
+ user_data = users.map do |u|
144
+ {
145
+ "id" => u.id,
146
+ "access_token" => u.access_token,
147
+ "login_url" => u.login_url
148
+ }
149
+ end
150
+
151
+ FakeWeb.register_uri(:get,
152
+ "https://graph.facebook.com/#{app.id}/accounts/test-users?access_token=#{app.access_token}",
153
+ :body => {"data" => user_data}.to_json)
154
+ end
155
+
156
+ end # SetupHelpers
157
+
63
158
  end
64
159
  end
65
160
 
66
161
  RSpec.configure do |config|
67
162
  config.include FBTU::SpecHelpers::CliTestMethods
163
+ config.include FBTU::SpecHelpers::SetupHelpers
68
164
  config.extend FBTU::SpecHelpers::SemanticNames
69
165
 
166
+ config.before(:each) { FBTU::SpecHelpers::SetupHelpers.reset! }
167
+
70
168
  config.before(:each) do
71
169
  @fbtu_dotfile = Tempfile.new('fbtu-prefs')
72
170
  FacebookTestUsers::DB.filename = @fbtu_dotfile.path
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: facebook_test_users
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sam Merritt
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-10 00:00:00 -08:00
18
+ date: 2011-02-14 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -142,6 +142,7 @@ files:
142
142
  - spec/fbtu/apps/list_spec.rb
143
143
  - spec/fbtu/apps_spec.rb
144
144
  - spec/fbtu/users/add_spec.rb
145
+ - spec/fbtu/users/friend_spec.rb
145
146
  - spec/fbtu/users/list_spec.rb
146
147
  - spec/fbtu/users/nuke_spec.rb
147
148
  - spec/fbtu/users/rm_spec.rb
@@ -185,6 +186,7 @@ test_files:
185
186
  - spec/fbtu/apps/list_spec.rb
186
187
  - spec/fbtu/apps_spec.rb
187
188
  - spec/fbtu/users/add_spec.rb
189
+ - spec/fbtu/users/friend_spec.rb
188
190
  - spec/fbtu/users/list_spec.rb
189
191
  - spec/fbtu/users/nuke_spec.rb
190
192
  - spec/fbtu/users/rm_spec.rb