/
user_spec.rb
229 lines (196 loc) · 9.53 KB
/
user_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
require "spec_helper"
describe "Intercom::User" do
it "to_hash'es itself" do
created_at = Time.now
user = Intercom::User.new("email" => "jim@example.com", :user_id => "12345", :created_at => created_at, :name => "Jim Bob")
as_hash = user.to_hash
as_hash["email"].must_equal "jim@example.com"
as_hash["user_id"].must_equal "12345"
as_hash["created_at"].must_equal created_at.to_i
as_hash["name"].must_equal "Jim Bob"
end
it "presents created_at and last_impression_at as Date" do
now = Time.now
user = Intercom::User.from_api(:created_at => now, :last_impression_at => now)
user.created_at.must_be_kind_of Time
user.created_at.to_s.must_equal now.to_s
user.last_impression_at.must_be_kind_of Time
user.last_impression_at.to_s.must_equal now.to_s
end
it "is ok on missing methods" do
user = Intercom::User.new
user.created_at.must_be_nil
user.last_impression_at.must_be_nil
user.email.must_be_nil
user.social_profiles.must_equal([])
end
it "presents a complete user record correctly" do
user = Intercom::User.from_api(test_user)
user.session_count.must_equal 123
user.social_profiles.size.must_equal 4
twitter_account = user.social_profiles.first
twitter_account.must_be_kind_of Intercom::SocialProfile
twitter_account.type.must_equal "twitter"
twitter_account.username.must_equal "abc"
twitter_account.url.must_equal "http://twitter.com/abc"
user.custom_data["a"].must_equal "b"
user.custom_data["b"].must_equal 2
user.relationship_score.must_equal 90
user.last_seen_ip.must_equal "1.2.3.4"
user.last_seen_user_agent.must_equal "Mozilla blah blah ie6"
end
it "has read-only social accounts" do
user = Intercom::User.new(:social_profiles => ["url" => "http://twitter.com/abc", "username" => "abc", "type" => "twitter"])
user.social_profiles.size.must_equal 1
twitter = user.social_profiles.first
twitter.type.must_equal "twitter"
twitter.url.must_equal "http://twitter.com/abc"
twitter.username.must_equal "abc"
user.to_hash["social_profiles"].must_equal nil
proc { user.social_profiles << "a" }.must_raise error_on_modify_frozen, "can't modify frozen array"
proc { Intercom::User.new.social_profiles << "a" }.must_raise error_on_modify_frozen, "can't modify frozen array"
end
it "has read-only location data" do
Intercom::User.new.location_data.must_equal({})
user = Intercom::User.new(:location_data => {"city" => "Dublin"})
user.location_data.must_equal({"city" => "Dublin"})
proc { user.location_data["change"] = "123" }.must_raise error_on_modify_frozen, "can't modify frozen hash"
user.to_hash["location_data"].must_equal nil
end
it "allows easy setting of custom data" do
now = Time.now
user = Intercom::User.new()
user.custom_data["mad"] = 123
user.custom_data["other"] = now.to_i
user.custom_data["thing"] = "yay"
user.to_hash["custom_data"].must_equal "mad" => 123, "other" => now.to_i, "thing" => "yay"
end
it "allows easy setting of company data" do
user = Intercom::User.new()
user.company["name"] = "Intercom"
user.company["id"] = 6
user.to_hash["company"].must_equal "name" => "Intercom", "id" => 6
end
it "allows easy setting of multiple companies" do
user = Intercom::User.new()
companies = [
{"name" => "Intercom", "id" => "6"},
{"name" => "Test", "id" => "9"}
]
user.companies = companies
user.to_hash["companies"].must_equal companies
end
it "rejects setting companies without an array of hashes" do
user = Intercom::User.new()
proc { user.companies = {"name" => "test"} }.must_raise ArgumentError
proc { user.companies = [ ["name", "test"] ]}.must_raise ArgumentError
end
it "rejects nested data structures in custom_data" do
user = Intercom::User.new()
proc { user.custom_data["thing"] = [1] }.must_raise ArgumentError
proc { user.custom_data["thing"] = {1 => 2} }.must_raise ArgumentError
proc { user.custom_data = {1 => {2 => 3}} }.must_raise ArgumentError
user = Intercom::User.from_api(test_user)
proc { user.custom_data["thing"] = [1] }.must_raise ArgumentError
end
it "fetches a user" do
Intercom.expects(:get).with("/v1/users", {"email" => "bo@example.com"}).returns(test_user)
user = Intercom::User.find("email" => "bo@example.com")
user.email.must_equal "bob@example.com"
user.name.must_equal "Joe Schmoe"
user.session_count.must_equal 123
end
it "saves a user" do
user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242")
Intercom.expects(:post).with("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242"}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
user.save
end
it "saves a user with a company" do
user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242", :company => {:id => 6, :name => "Intercom"})
Intercom.expects(:post).with("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242", "company" => {"id" => 6, "name" => "Intercom"}}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
user.save
end
it "saves a user with a companies" do
user = Intercom::User.new("email" => "jo@example.com", :user_id => "i-1224242", :companies => [{:id => 6, :name => "Intercom"}])
Intercom.expects(:post).with("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242", "companies" => [{"id" => 6, "name" => "Intercom"}]}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
user.save
end
it "deletes a user" do
Intercom.expects(:delete).with("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242"}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
Intercom::User.delete("email" => "jo@example.com", "user_id" => "i-1224242")
end
it "can use User.create for convenience" do
Intercom.expects(:post).with("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242"}).returns({"email" => "jo@example.com", "user_id" => "i-1224242"})
user = Intercom::User.create("email" => "jo@example.com", :user_id => "i-1224242")
user.email.must_equal "jo@example.com"
end
it "updates the @user with attributes as set by the server" do
Intercom.expects(:post).with("/v1/users", {"email" => "jo@example.com", "user_id" => "i-1224242"}).returns({"email" => "jo@example.com", "user_id" => "i-1224242", "session_count" => 4})
user = Intercom::User.create("email" => "jo@example.com", :user_id => "i-1224242")
user.session_count.must_equal 4
end
it "raises argument error when trying to set a read only attribute" do
read_only_attributes = %w(relationship_score session_count)
read_only_attributes.each do |read_only|
proc { Intercom::User.create("email" => "jo@example.com", read_only => "some value") }.must_raise NoMethodError
user = Intercom::User.new
user.public_methods.include?("#{read_only}=").must_equal false
end
end
it "allows setting dates to nil without converting them to 0" do
Intercom.expects(:post).with("/v1/users", {"email" => "jo@example.com", "created_at" => nil}).returns({"email" => "jo@example.com"})
user = Intercom::User.create("email" => "jo@example.com", "created_at" => nil)
user.created_at.must_equal nil
end
it "sets/gets rw keys" do
params = {"email" => "me@example.com", :user_id => "abc123", "name" => "Bob Smith", "last_seen_ip" => "1.2.3.4", "last_seen_user_agent" => "ie6", "created_at" => Time.now}
user = Intercom::User.new(params)
user.to_hash.keys.sort.must_equal params.keys.map(&:to_s).sort
params.keys.each do |key|
user.send(key).to_s.must_equal params[key].to_s
end
end
it "will allow extra attributes in response from api" do
user = Intercom::User.send(:from_api, {"new_param" => "some value"})
user.new_param.must_equal "some value"
end
it "returns a UserCollectionProxy for all without making any requests" do
Intercom.expects(:execute_request).never
all = Intercom::User.all
all.must_be_instance_of(Intercom::UserCollectionProxy)
end
describe "tagging and untagging" do
it "tags a group of users" do
Intercom.expects(:post).with("/v1/users/tag", {:user_ids => ["abc123", "def456"], :tag => "Test Tag"}).returns(tagging_response)
Intercom::User.tag(:user_ids => ["abc123", "def456"], :tag => "Test Tag")
end
it "untags a group of users" do
Intercom.expects(:post).with("/v1/users/untag", {:user_ids => ["abc123", "def456"], :tag => "Test Tag"}).returns(untagging_response)
Intercom::User.untag(:user_ids => ["abc123", "def456"], :tag => "Test Tag")
end
end
it "returns the total number of users" do
Intercom.expects(:get).with("/v1/users", {:per_page => 1}).returns(page_of_users)
Intercom::User.count.must_be_kind_of(Integer)
end
it "can find_by_email" do
Intercom::User.expects(:find).with(:email => "bob@example.com")
Intercom::User.find_by_email("bob@example.com")
end
it "can find_by_user_id" do
Intercom::User.expects(:find).with(:user_id => "abc123")
Intercom::User.find_by_user_id("abc123")
end
it "converts company created_at values to unix timestamps" do
time = Time.now
user = Intercom::User.new("companies" => [
{ "created_at" => time },
{ "created_at" => time.to_i }
])
as_hash = user.to_hash
first_company_as_hash = as_hash["companies"][0]
second_company_as_hash = as_hash["companies"][1]
first_company_as_hash["created_at"].must_equal time.to_i
second_company_as_hash["created_at"].must_equal time.to_i
end
end