Class: SimpleCrowd::MockClient

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_crowd/mock_client.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.groupsObject

Returns the value of attribute groups.



5
6
7
# File 'lib/simple_crowd/mock_client.rb', line 5

def groups
  @groups
end

.tokensObject

Returns the value of attribute tokens.



5
6
7
# File 'lib/simple_crowd/mock_client.rb', line 5

def tokens
  @tokens
end

.usersObject

Returns the value of attribute users.



5
6
7
# File 'lib/simple_crowd/mock_client.rb', line 5

def users
  @users
end

Instance Attribute Details

#app_tokenObject

Returns the value of attribute app_token.



12
13
14
# File 'lib/simple_crowd/mock_client.rb', line 12

def app_token
  @app_token
end

Class Method Details

.resetObject



7
8
9
# File 'lib/simple_crowd/mock_client.rb', line 7

def reset
  @users, @tokens, @groups = [], [], []
end

Instance Method Details

#add_user(user, credential) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/simple_crowd/mock_client.rb', line 67

def add_user user, credential
  if user && user.username && !find_user_by_name(user.username)
    user.instance_variable_set('@password', credential)
    self.class.users << user
    user
  end
end

#authenticate_application(name = nil, password = nil) ⇒ Object



21
22
23
# File 'lib/simple_crowd/mock_client.rb', line 21

def authenticate_application(name = nil, password = nil)
  "MOCKAPPTOKEN"
end

#authenticate_user(name, password, factors = nil) ⇒ Object



25
26
27
# File 'lib/simple_crowd/mock_client.rb', line 25

def authenticate_user name, password, factors = nil
  (user = find_user_by_name(name)) && user.instance_variable_get('@password') && user.instance_variable_get('@password') == password ? new_user_token(name) : nil
end

#create_user_token(name) ⇒ Object



28
29
30
# File 'lib/simple_crowd/mock_client.rb', line 28

def create_user_token name
  new_user_token(name) if find_user_by_name(name)
end

#find_all_group_namesObject



110
111
112
# File 'lib/simple_crowd/mock_client.rb', line 110

def find_all_group_names
  groups.map{|g| g.name}
end

#find_all_user_namesObject



37
38
39
# File 'lib/simple_crowd/mock_client.rb', line 37

def find_all_user_names
  users.map{|u| u.username}
end

#find_group_by_name(name) ⇒ Object



107
108
109
# File 'lib/simple_crowd/mock_client.rb', line 107

def find_group_by_name name
  groups.detect{|g| g.name == name}
end

#find_user_by_email(email) ⇒ Object



51
52
53
# File 'lib/simple_crowd/mock_client.rb', line 51

def find_user_by_email email
  users.detect{|u| u.email == email}
end

#find_user_by_name(name) ⇒ Object Also known as: find_user_with_attributes_by_name



40
41
42
# File 'lib/simple_crowd/mock_client.rb', line 40

def find_user_by_name name
  users.detect{|u| u.username == name}
end

#find_user_by_token(token) ⇒ Object



45
46
47
# File 'lib/simple_crowd/mock_client.rb', line 45

def find_user_by_token token
  token && tokens.include?(token) && (name = /.+-TOKENFOR-(.+)$/.match(token)) && name[1] && find_user_by_name(name[1])
end

#find_username_by_token(token) ⇒ Object



48
49
50
# File 'lib/simple_crowd/mock_client.rb', line 48

def find_username_by_token token
  (user = find_user_by_token(token)) && user.username
end


18
19
20
# File 'lib/simple_crowd/mock_client.rb', line 18

def get_cookie_info
  {:secure=>false, :domain=>".twcrowdtest.com"}
end

#invalidate_user_token(token) ⇒ Object



31
32
33
# File 'lib/simple_crowd/mock_client.rb', line 31

def invalidate_user_token token
  tokens.delete token
end

#is_group_member?(group, user) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/simple_crowd/mock_client.rb', line 103

def is_group_member? group, user
  group = find_group_by_name(group) unless group.is_a?(SimpleCrowd::Group)
  !!(group && group.members.detect{|m| m == user})
end

#is_valid_user_token?(token, factors = nil) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/simple_crowd/mock_client.rb', line 34

def is_valid_user_token? token, factors = nil
  tokens.include? token
end

#remove_user(name) ⇒ Object



74
75
76
77
# File 'lib/simple_crowd/mock_client.rb', line 74

def remove_user name
  user = users.delete(find_user_by_name(name))
  tokens.reject!{|t| t =~ /.+-TOKENFOR-#{user.username}/} if user && user.username
end

#search_users(criteria, limit = 0, start = 0) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/simple_crowd/mock_client.rb', line 57

def search_users criteria, limit=0, start=0
  users = users()
  criteria.each do |search_key, search_val|
    users = users.select do |user|
      val = user[:"#{search_key}"]
      val && val.downcase.include?(search_val.downcase)
    end
  end
  users
end

#search_users_by_email(email) ⇒ Object



54
55
56
# File 'lib/simple_crowd/mock_client.rb', line 54

def search_users_by_email email
  users.select{|u| u.email =~ /#{email}/}
end

#update_user(user) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/simple_crowd/mock_client.rb', line 88

def update_user user
  return unless user.dirty?

  attrs_to_update = user.dirty_attributes
  return if attrs_to_update.empty?

  stored_user = find_user_by_name(user.username)
  return if stored_user.blank?

  attrs_to_update.each do |a|
    stored_user[a] = user.send(a)
  end
  user.clean
end

#update_user_attribute(user, name, value) ⇒ Object



83
84
85
86
87
# File 'lib/simple_crowd/mock_client.rb', line 83

def update_user_attribute user, name, value
  if user = find_user_by_name(user)
    user[name] = value
  end
end

#update_user_credential(name, credential, encrypted = false) ⇒ Object



78
79
80
81
82
# File 'lib/simple_crowd/mock_client.rb', line 78

def update_user_credential name, credential, encrypted = false
  if user = find_user_by_name(name)
    user.instance_variable_set('@password', credential)
  end
end