Class: EDH::Passport::TestUsers

Inherits:
Object
  • Object
show all
Defined in:
lib/edh/test_users.rb

Overview

Note:

the test user API is fairly slow compared to other interfaces (which makes sense – it’s creating whole new user accounts!).

Create and manage test users for your application.

A test user is a user account associated with an app created for the purpose of testing the functionality of that app. You can use test users for manual or automated testing – EDH’s live test suite uses test users to verify the library works with Passport.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ TestUsers

Create a new TestUsers instance.

If you don’t have your app’s access token, provide the app’s secret and EDH will make a request to Passport for the appropriate token.

Options Hash (options):

  • :app_id (Object)

    the application’s ID.

  • :app_access_token (Object)

    an application access token, if known.

  • :secret (Object)

    the application’s secret.

Raises:

  • ArgumentError if the application ID and one of the app access token or the secret are not provided.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/edh/test_users.rb', line 32

def initialize(options = {})
  @app_id = options[:app_id]
  @app_access_token = options[:app_access_token]
  @secret = options[:secret]
  unless @app_id && (@app_access_token || @secret) # make sure we have what we need
    raise ArgumentError, "Initialize must receive a hash with :app_id and either :app_access_token or :secret! (received #{options.inspect})"
  end

  # fetch the access token if we're provided a secret
  if @secret && !@app_access_token
    oauth = EDH::Passport::OAuth.new(@app_id, @secret)
    @app_access_token = oauth.get_app_access_token
  end
  
  @api = API.new(@app_access_token)
end

Instance Attribute Details

#apiEDH::Passport::API (readonly)

The application API interface used to communicate with Passport.



19
20
21
# File 'lib/edh/test_users.rb', line 19

def api
  @api
end

#app_access_tokenObject (readonly)

Returns the value of attribute app_access_token.



20
21
22
# File 'lib/edh/test_users.rb', line 20

def app_access_token
  @app_access_token
end

#app_idObject (readonly)

Returns the value of attribute app_id.



20
21
22
# File 'lib/edh/test_users.rb', line 20

def app_id
  @app_id
end

#secretObject (readonly)

Returns the value of attribute secret.



20
21
22
# File 'lib/edh/test_users.rb', line 20

def secret
  @secret
end

Instance Method Details

#befriend(user1_hash, user2_hash, options = {}) ⇒ Object

Note:

there’s no way to unfriend test users; you can always just create a new one.

Make two test users friends.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/edh/test_users.rb', line 130

def befriend(user1_hash, user2_hash, options = {})
  user1_id = user1_hash["id"] || user1_hash[:id]
  user2_id = user2_hash["id"] || user2_hash[:id]
  user1_token = user1_hash["access_token"] || user1_hash[:access_token]
  user2_token = user2_hash["access_token"] || user2_hash[:access_token]
  unless user1_id && user2_id && user1_token && user2_token
    # we explicitly raise an error here to minimize the risk of confusing output
    # if you pass in a string (as was previously supported) no local exception would be raised
    # but the Passport call would fail
    raise ArgumentError, "TestUsers#befriend requires hash arguments for both users with id and access_token"
  end

  u1_graph_api = API.new(:access_token => user1_token)
  u2_graph_api = API.new(:access_token => user2_token)

  u1_graph_api.graph_call("#{user1_id}/friends/#{user2_id}", {}, "post", options) &&
    u2_graph_api.graph_call("#{user2_id}/friends/#{user1_id}", {}, "post", options)
end

#create(installed, permissions = nil, args = {}, options = {}) ⇒ Object

Create a new test user.



57
58
59
60
61
62
# File 'lib/edh/test_users.rb', line 57

def create(installed, permissions = nil, args = {}, options = {})
  # Creates and returns a test user
  args['installed'] = installed
  args['permissions'] = (permissions.is_a?(Array) ? permissions.join(",") : permissions) if installed
  @api.graph_call(test_user_accounts_path, args, "post", options)
end

#create_network(network_size, installed = true, permissions = '', options = {}) ⇒ Object

Note:

this call slows down dramatically the more users you create (test user calls are slow, and more users => more 1-on-1 connections to be made). Use carefully.

Create a network of test users, all of whom are friends and have the same permissions.



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/edh/test_users.rb', line 161

def create_network(network_size, installed = true, permissions = '', options = {})
  users = (0...network_size).collect { create(installed, permissions, options) }
  friends = users.clone
  users.each do |user|
    # Remove this user from list of friends
    friends.delete_at(0)
    # befriend all the others
    friends.each do |friend|
      befriend(user, friend, options)
    end
  end
  return users
end

#delete(test_user, options = {}) ⇒ Object

Delete a test user.



79
80
81
82
# File 'lib/edh/test_users.rb', line 79

def delete(test_user, options = {})
  test_user = test_user["id"] if test_user.is_a?(Hash)
  @api.delete_object(test_user, options)
end

#delete_all(options = {}) ⇒ Object

Note:

if you have a lot of test users (> 20), this operation can take a long time.

Deletes all test users in batches of 50.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/edh/test_users.rb', line 91

def delete_all(options = {})
  # ideally we'd save a call by checking next_page_params, but at the time of writing
  # Passport isn't consistently returning full pages after the first one
  previous_list = nil
  while (test_user_list = list(options)).length > 0
    break if test_user_list == previous_list

    test_user_list.each_slice(50) do |users| 
      self.api.batch(options) {|batch_api| users.each {|u| batch_api.delete_object(u["id"]) }}
    end
    
    previous_list = test_user_list
  end
end

#list(options = {}) ⇒ Object

List all test users for the app.



69
70
71
# File 'lib/edh/test_users.rb', line 69

def list(options = {})
  @api.graph_call(test_user_accounts_path, {}, "get", options)
end

#test_user_accounts_pathObject

The Passport test users management URL for your application.



176
177
178
# File 'lib/edh/test_users.rb', line 176

def test_user_accounts_path
  @test_user_accounts_path ||= "/#{@app_id}/accounts/test-users"
end

#update(test_user, args = {}, options = {}) ⇒ Object

Note:

currently, only name and password can be changed; see / the Facebook documentation.

Updates a test user’s attributes.



116
117
118
119
# File 'lib/edh/test_users.rb', line 116

def update(test_user, args = {}, options = {})
  test_user = test_user["id"] if test_user.is_a?(Hash)
  @api.graph_call(test_user, args, "post", options)
end