sift 1.1.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,193 @@
1
+ require_relative "../spec_helper"
2
+ require "sift"
3
+
4
+ describe Sift::Client do
5
+
6
+ before :each do
7
+ Sift.api_key = nil
8
+ end
9
+
10
+ def valid_transaction_properties
11
+ {
12
+ :$buyer_user_id => "123456",
13
+ :$seller_user_id => "654321",
14
+ :$amount => 1253200,
15
+ :$currency_code => "USD",
16
+ :$time => Time.now.to_i,
17
+ :$transaction_id => "my_transaction_id",
18
+ :$billing_name => "Mike Snow",
19
+ :$billing_bin => "411111",
20
+ :$billing_last4 => "1111",
21
+ :$billing_address1 => "123 Main St.",
22
+ :$billing_city => "San Francisco",
23
+ :$billing_region => "CA",
24
+ :$billing_country => "US",
25
+ :$billing_zip => "94131",
26
+ :$user_email => "mike@example.com"
27
+ }
28
+ end
29
+
30
+ def score_response_json
31
+ {
32
+ :user_id => "247019",
33
+ :score => 0.93,
34
+ :reasons => [{
35
+ :name => "UsersPerDevice",
36
+ :value => 4,
37
+ :details => {
38
+ :users => "a, b, c, d"
39
+ }
40
+ }],
41
+ :status => 0,
42
+ :error_message => "OK"
43
+ }
44
+ end
45
+
46
+ def action_response_json
47
+ {
48
+ :user_id => "247019",
49
+ :score => 0.93,
50
+ :actions => [{
51
+ :action_id => "1234567890abcdefghijklmn",
52
+ :time => 1437421587052,
53
+ :triggers => [{
54
+ :triggerType => "FORMULA",
55
+ :source => "synchronous_action",
56
+ :trigger_id => "12345678900987654321abcd"
57
+ }],
58
+ :entity => {
59
+ :type => "USER",
60
+ :id => "23056"
61
+ }
62
+ },
63
+ {
64
+ :action_id => "12345678901234567890abcd",
65
+ :time => 1437421587410,
66
+ :triggers => [{
67
+ :triggerType => "FORMULA",
68
+ :source => "synchronous_action",
69
+ :trigger_id => "abcd12345678901234567890"
70
+ }],
71
+ :entity => {
72
+ :type => "ORDER",
73
+ :id => "order_at_ 1437421587009"
74
+ }
75
+ }],
76
+ :status => 0,
77
+ :error_message => "OK"
78
+ }
79
+ end
80
+
81
+ def fully_qualified_api_endpoint
82
+ Sift::Client::API_ENDPOINT + Sift.rest_api_path
83
+ end
84
+
85
+ it "Successfully submits a v203 event with overridden key" do
86
+ response_json = { :status => 0, :error_message => "OK"}
87
+ stub_request(:post, "https://api.siftscience.com/v203/events").
88
+ with { | request|
89
+ parsed_body = JSON.parse(request.body)
90
+ expect(parsed_body).to include("$buyer_user_id" => "123456")
91
+ expect(parsed_body).to include("$api_key" => "overridden")
92
+ }.to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {})
93
+
94
+ api_key = "foobar"
95
+ event = "$transaction"
96
+ properties = valid_transaction_properties
97
+
98
+ response = Sift::Client.new(:api_key => api_key, :version => "203")
99
+ .track(event, properties, :api_key => "overridden")
100
+ expect(response.ok?).to eq(true)
101
+ expect(response.api_status).to eq(0)
102
+ expect(response.api_error_message).to eq("OK")
103
+ end
104
+
105
+
106
+ it "Successfully fetches a v203 score" do
107
+
108
+ api_key = "foobar"
109
+ response_json = score_response_json
110
+
111
+ stub_request(:get, "https://api.siftscience.com/v203/score/247019/?api_key=foobar")
112
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
113
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
114
+ "content-length"=> "74"})
115
+
116
+ response = Sift::Client.new(:api_key => api_key)
117
+ .score(score_response_json[:user_id], :version => 203)
118
+ expect(response.ok?).to eq(true)
119
+ expect(response.api_status).to eq(0)
120
+ expect(response.api_error_message).to eq("OK")
121
+
122
+ expect(response.body["score"]).to eq(0.93)
123
+ end
124
+
125
+
126
+ it "Successfully fetches a v203 score with an overridden key" do
127
+
128
+ api_key = "foobar"
129
+ response_json = score_response_json
130
+
131
+ stub_request(:get, "https://api.siftscience.com/v203/score/247019/?api_key=overridden")
132
+ .to_return(:status => 200, :body => MultiJson.dump(response_json), :headers => {})
133
+
134
+ response = Sift::Client.new(:api_key => api_key, :version => 203)
135
+ .score(score_response_json[:user_id], :api_key => "overridden")
136
+ expect(response.ok?).to eq(true)
137
+ expect(response.api_status).to eq(0)
138
+ expect(response.api_error_message).to eq("OK")
139
+
140
+ expect(response.body["score"]).to eq(0.93)
141
+ end
142
+
143
+
144
+ it "Successfuly make a v203 sync score request" do
145
+
146
+ api_key = "foobar"
147
+ response_json = {
148
+ :status => 0,
149
+ :error_message => "OK",
150
+ :score_response => score_response_json
151
+ }
152
+
153
+ stub_request(:post, "https://api.siftscience.com/v203/events?return_score=true")
154
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
155
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
156
+ "content-length"=> "74"})
157
+
158
+ event = "$transaction"
159
+ properties = valid_transaction_properties
160
+ response = Sift::Client.new(:api_key => api_key)
161
+ .track(event, properties, :return_score => true, :version => "203")
162
+ expect(response.ok?).to eq(true)
163
+ expect(response.api_status).to eq(0)
164
+ expect(response.api_error_message).to eq("OK")
165
+ expect(response.body["score_response"]["score"]).to eq(0.93)
166
+ end
167
+
168
+
169
+ it "Successfuly make a v203 sync action request" do
170
+
171
+ api_key = "foobar"
172
+ response_json = {
173
+ :status => 0,
174
+ :error_message => "OK",
175
+ :score_response => action_response_json
176
+ }
177
+
178
+ stub_request(:post, "https://api.siftscience.com/v203/events?return_action=true")
179
+ .to_return(:status => 200, :body => MultiJson.dump(response_json),
180
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
181
+ "content-length"=> "74"})
182
+
183
+ event = "$transaction"
184
+ properties = valid_transaction_properties
185
+ response = Sift::Client.new(:api_key => api_key, :version => "203")
186
+ .track(event, properties, :return_action => true)
187
+ expect(response.ok?).to eq(true)
188
+ expect(response.api_status).to eq(0)
189
+ expect(response.api_error_message).to eq("OK")
190
+ expect(response.body["score_response"]["actions"].first["entity"]["type"]).to eq("USER")
191
+ end
192
+
193
+ end
@@ -1,8 +1,17 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
1
+ require_relative "../spec_helper"
2
+ require "sift"
2
3
 
3
4
  describe Sift::Client do
4
5
 
5
6
  def valid_label_properties
7
+ {
8
+ :$abuse_type => 'content_abuse',
9
+ :$is_bad => true,
10
+ :$description => "Listed a fake item"
11
+ }
12
+ end
13
+
14
+ def valid_label_properties_203
6
15
  {
7
16
  :$reasons => [ "$fake" ],
8
17
  :$is_bad => true,
@@ -10,27 +19,74 @@ describe Sift::Client do
10
19
  }
11
20
  end
12
21
 
13
- def fully_qualified_users_labels_endpoint(user_id)
14
- Sift::Client::API_ENDPOINT + Sift.current_users_label_api_path(user_id)
22
+
23
+ it "Successfuly handles a $label and returns OK" do
24
+
25
+ response_json = { :status => 0, :error_message => "OK" }
26
+ user_id = "frodo_baggins"
27
+
28
+ stub_request(:post, "https://api.siftscience.com/v205/users/frodo_baggins/labels")
29
+ .with(:body => ('{"$abuse_type":"content_abuse","$is_bad":true,"$description":"Listed a fake item","$type":"$label","$api_key":"foobar"}'))
30
+ .to_return(:body => MultiJson.dump(response_json), :status => 200,
31
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
32
+ "content-length"=> "74"})
33
+
34
+ api_key = "foobar"
35
+ properties = valid_label_properties
36
+
37
+ response = Sift::Client.new(:api_key => api_key).label(user_id, properties)
38
+ expect(response.ok?).to eq(true)
39
+ expect(response.api_status).to eq(0)
40
+ expect(response.api_error_message).to eq("OK")
15
41
  end
16
42
 
43
+
44
+ it "Successfully handles an $unlabel and returns OK" do
45
+ user_id = "frodo_baggins"
46
+
47
+ stub_request(:delete,
48
+ "https://api.siftscience.com/v205/users/frodo_baggins/labels?api_key=foobar&abuse_type=payment_abuse")
49
+ .to_return(:status => 204)
50
+
51
+ api_key = "foobar"
52
+
53
+ response = Sift::Client.new(:api_key => api_key).unlabel(user_id, :abuse_type => 'payment_abuse')
54
+ expect(response.ok?).to eq(true)
55
+ end
56
+
57
+
17
58
  it "Successfuly handles a $label with the v203 API and returns OK" do
18
59
 
19
60
  response_json = { :status => 0, :error_message => "OK" }
20
61
  user_id = "frodo_baggins"
21
62
 
22
- FakeWeb.register_uri(:post, fully_qualified_users_labels_endpoint(user_id),
23
- :body => MultiJson.dump(response_json),
24
- :status => [Net::HTTPOK, "OK"],
25
- :content_type => "text/json")
63
+ stub_request(:post, "https://api.siftscience.com/v203/users/frodo_baggins/labels")
64
+ .with(:body => ('{"$reasons":["$fake"],"$is_bad":true,"$description":"Listed a fake item","$type":"$label","$api_key":"foobar"}'))
65
+ .to_return(:body => MultiJson.dump(response_json), :status => 200,
66
+ :headers => {"content-type"=>"application/json; charset=UTF-8",
67
+ "content-length"=> "74"})
68
+
69
+ api_key = "foobar"
70
+ properties = valid_label_properties_203
71
+
72
+ response = Sift::Client.new(:api_key => api_key, :version => 203).label(user_id, properties)
73
+ expect(response.ok?).to eq(true)
74
+ expect(response.api_status).to eq(0)
75
+ expect(response.api_error_message).to eq("OK")
76
+ end
77
+
78
+
79
+ it "Successfully handles an $unlabel with the v203 API endpoing and returns OK" do
80
+ user_id = "frodo_baggins"
81
+
82
+ stub_request(:delete,
83
+ "https://api.siftscience.com/v203/users/frodo_baggins/labels?api_key=foobar")
84
+ .to_return(:status => 204)
26
85
 
27
86
  api_key = "foobar"
28
- properties = valid_label_properties
29
87
 
30
- response = Sift::Client.new(api_key).label(user_id, properties)
31
- response.ok?.should eq(true)
32
- response.api_status.should eq(0)
33
- response.api_error_message.should eq("OK")
88
+ response = Sift::Client.new(:api_key => api_key).unlabel(user_id, :version => "203")
89
+ expect(response.ok?).to eq(true)
34
90
  end
35
91
 
36
92
  end