authy 2.7.5 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,23 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Authy::OneTouch do
4
- describe ".send_approval_request" do
3
+ class Utils
4
+ include Authy::URL
5
+ end
5
6
 
6
- before do
7
- @email = generate_email
8
- @cellphone = generate_cellphone
9
- @user = Authy::API.register_user(:email => @email,
10
- :cellphone => @cellphone,
11
- :country_code => 1)
12
- expect(@user).to be_ok
13
- end
7
+ describe Authy::OneTouch do
8
+ let(:headers) { { "X-Authy-API-Key" => Authy.api_key, "User-Agent" => "AuthyRuby/#{Authy::VERSION} (#{RUBY_PLATFORM}, Ruby #{RUBY_VERSION})" } }
9
+ let(:user_id) { 81547 }
14
10
 
11
+ describe ".send_approval_request" do
12
+ let(:url) { "#{Authy.api_url}/onetouch/json/users/#{user_id}/approval_requests" }
15
13
  it 'creates a new approval_request for user' do
16
- pending('The Authy account this test uses needs to have OneTouch enabled. Another option is to mock the API')
17
- response = Authy::OneTouch.send_approval_request(
18
- id: @user.id,
14
+ response_json = {
15
+ "approval_request" => {
16
+ "uuid" => "550e8400-e29b-41d4-a716-446655440000"
17
+ },
18
+ "success" => true
19
+ }.to_json
20
+ params = {
19
21
  message: 'You are moving 10 BTC from your account',
20
22
  details: {
21
23
  'Bank account' => '23527922',
@@ -25,15 +27,27 @@ describe Authy::OneTouch do
25
27
  'IP Address' => '192.168.0.3'
26
28
  },
27
29
  seconds_to_expire: 150
28
- )
30
+ }
31
+
32
+ expect(Authy::API.http_client).to receive(:request)
33
+ .once
34
+ .with(:post, url, {
35
+ :body => Utils.escape_query(params),
36
+ :header => headers
37
+ })
38
+ .and_return(double(:status => 200, :body => response_json))
39
+
40
+ params[:id] = user_id
41
+ response = Authy::OneTouch.send_approval_request(params)
29
42
 
30
43
  expect(response).to be_kind_of(Authy::Response)
31
44
  expect(response).to be_ok
32
45
  end
33
46
 
34
47
  it 'requires message as mandatory' do
48
+ expect(Authy::API.http_client).to receive(:request).never
35
49
  response = Authy::OneTouch.send_approval_request(
36
- id: @user.id,
50
+ id: user_id,
37
51
  details: {
38
52
  'Bank account' => '23527922',
39
53
  'Amount' => '10 BTC',
@@ -49,20 +63,39 @@ describe Authy::OneTouch do
49
63
  end
50
64
 
51
65
  it 'does not require other fields as mandatory' do
52
- pending('The Authy account this test uses needs to have OneTouch enabled. Another option is to mock the API')
53
- response = Authy::OneTouch.send_approval_request(
54
- id: @user.id,
55
- message: 'Test message'
56
- )
66
+ response_json = {
67
+ "approval_request" => {
68
+ "uuid" => "550e8400-e29b-41d4-a716-446655440000"
69
+ },
70
+ "success" => true
71
+ }.to_json
72
+ params = {
73
+ message: 'You are moving 10 BTC from your account'
74
+ }
75
+
76
+ expect(Authy::API.http_client).to receive(:request)
77
+ .once
78
+ .with(:post, url, {
79
+ :body => Utils.escape_query(params),
80
+ :header => headers
81
+ })
82
+ .and_return(double(:status => 200, :body => response_json))
83
+
84
+ params[:id] = user_id
85
+ response = Authy::OneTouch.send_approval_request(params)
57
86
 
58
87
  expect(response).to be_kind_of(Authy::Response)
59
88
  expect(response).to be_ok
60
89
  end
61
90
 
62
91
  it 'checks logos format' do
63
- pending('The Authy account this test uses needs to have OneTouch enabled. Another option is to mock the API')
64
- response = Authy::OneTouch.send_approval_request(
65
- id: @user.id,
92
+ response_json = {
93
+ "approval_request" => {
94
+ "uuid" => "550e8400-e29b-41d4-a716-446655440000"
95
+ },
96
+ "success" => true
97
+ }.to_json
98
+ params = {
66
99
  message: 'You are moving 10 BTC from your account',
67
100
  details: {
68
101
  'Bank account' => '23527922',
@@ -71,9 +104,19 @@ describe Authy::OneTouch do
71
104
  hidden_details: {
72
105
  'IP Address' => '192.168.0.3'
73
106
  },
74
- seconds_to_expire: 150,
75
- logos: [{res: 'low', url: 'http://foo.bar'}]
76
- )
107
+ logos: [{res: 'low', url: 'http://foo.bar'}],
108
+ seconds_to_expire: 150
109
+ }
110
+ expect(Authy::API.http_client).to receive(:request)
111
+ .once
112
+ .with(:post, url, {
113
+ :body => Utils.escape_query(params),
114
+ :header => headers
115
+ })
116
+ .and_return(double(:status => 200, :body => response_json))
117
+
118
+ params[:id] = user_id
119
+ response = Authy::OneTouch.send_approval_request(params)
77
120
 
78
121
  expect(response).to be_kind_of(Authy::Response)
79
122
  expect(response).to be_ok
@@ -82,10 +125,23 @@ describe Authy::OneTouch do
82
125
 
83
126
  describe '.approval_request_status' do
84
127
  it 'returns approval request status' do
85
- pending('The Authy account this test uses needs to have OneTouch enabled. Another option is to mock the API')
86
- response = Authy::OneTouch.approval_request_status(
87
- uuid: '550e8400-e29b-41d4-a716-446655440000'
88
- )
128
+ uuid = '550e8400-e29b-41d4-a716-446655440000'
129
+ url = "#{Authy.api_url}/onetouch/json/approval_requests/#{uuid}"
130
+ response_json = {
131
+ "approval_request" => {
132
+ "status" => "pending"
133
+ },
134
+ "success" => true
135
+ }.to_json
136
+ expect(Authy::API.http_client).to receive(:request)
137
+ .once
138
+ .with(:get, url, {
139
+ :header => headers,
140
+ :query => {},
141
+ :follow_redirect => nil
142
+ })
143
+ .and_return(double(:status => 200, :body => response_json))
144
+ response = Authy::OneTouch.approval_request_status(uuid: uuid)
89
145
 
90
146
  expect(response).to be_kind_of(Authy::Response)
91
147
  expect(response).to be_ok
@@ -1,108 +1,239 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Utils
4
+ include Authy::URL
5
+ end
6
+
3
7
  describe "Authy::PhoneVerification" do
4
- describe "Sending the verification code" do
8
+ let(:valid_phone_number) { '201-555-0123' }
9
+ let(:invalid_phone_number) { '123' }
10
+ let(:headers) { { "X-Authy-API-Key" => Authy.api_key, "User-Agent" => "AuthyRuby/#{Authy::VERSION} (#{RUBY_PLATFORM}, Ruby #{RUBY_VERSION})" } }
11
+ let(:start_url) { "#{Authy.api_url}/protected/json/phones/verification/start" }
12
+ let(:check_url) { "#{Authy.api_url}/protected/json/phones/verification/check"}
5
13
 
14
+ describe "Sending the verification code" do
6
15
  it "should send the code via SMS" do
7
- pending("API is not returning expected response in this case. The test phone number is invalid.")
8
- response = Authy::PhoneVerification.start(
16
+ response_json = {
17
+ "carrier" => "Fixed Line Operators and Other Networks",
18
+ "is_cellphone" => true,
19
+ "is_ported" => false,
20
+ "message" => "Text message sent to +1 201-555-0123.",
21
+ "seconds_to_expire" => 0,
22
+ "uuid" => nil,
23
+ "success" => true
24
+ }.to_json
25
+ params = {
9
26
  via: "sms",
10
27
  country_code: "1",
11
- phone_number: "111-111-1111"
12
- )
13
-
28
+ phone_number: valid_phone_number
29
+ }
30
+ expect(Authy::API.http_client).to receive(:request)
31
+ .once
32
+ .with(:post, start_url, {
33
+ :body => Utils.escape_query(params),
34
+ :header => headers
35
+ })
36
+ .and_return(double(:status => 200, :body => response_json))
37
+ response = Authy::PhoneVerification.start(params)
14
38
  expect(response).to be_kind_of(Authy::Response)
15
39
  expect(response).to be_ok
16
- expect(response.message).to eq "Text message sent to +1 111-111-1111."
40
+ expect(response.message).to eq "Text message sent to +1 #{valid_phone_number}."
17
41
  end
18
42
 
19
- # it "should send the code via CALL" do
20
- # response = Authy::PhoneVerification.start(
21
- # via: "call",
22
- # country_code: "1",
23
- # phone_number: "111-111-1111"
24
- # )
25
-
26
- # response.should be_kind_of(Authy::Response)
27
- # response.success.should be_truthy
28
- # response.message.should == "Text message sent to +1 111-111-1111."
29
- # end
43
+ it "should send the code via SMS with code length" do
44
+ response_json = {
45
+ "carrier" => "Fixed Line Operators and Other Networks",
46
+ "is_cellphone" => true,
47
+ "is_ported" => false,
48
+ "message" => "Text message sent to +1 201-555-0123.",
49
+ "seconds_to_expire" => 0,
50
+ "uuid" => nil,
51
+ "success" => true
52
+ }.to_json
53
+ params = {
54
+ via: "sms",
55
+ country_code: "1",
56
+ phone_number: valid_phone_number,
57
+ code_length: "4"
58
+ }
59
+ expect(Authy::API.http_client).to receive(:request)
60
+ .once
61
+ .with(:post, start_url, {
62
+ :body => Utils.escape_query(params),
63
+ :header => headers
64
+ })
65
+ .and_return(double(:status => 200, :body => response_json))
66
+ response = Authy::PhoneVerification.start(params)
67
+
68
+ expect(response).to be_kind_of(Authy::Response)
69
+ expect(response).to be_ok
70
+ expect(response.message).to eq "Text message sent to +1 #{valid_phone_number}."
71
+ end
30
72
  end
31
73
 
32
74
  describe "validate the fields required" do
33
75
  it "should return an error. Country code is required" do
34
- response = Authy::PhoneVerification.start(
76
+ response_json = {
77
+ "error_code" => "60004",
78
+ "message" => "Invalid parameter: country_code - Parameter is required",
79
+ "errors" => {
80
+ "message" => "Invalid parameter: country_code - Parameter is required"
81
+ },
82
+ "success" => false
83
+ }.to_json
84
+ params = {
35
85
  via: "sms",
36
- phone_number: "111-111-1111"
37
- )
38
-
86
+ phone_number: valid_phone_number
87
+ }
88
+ expect(Authy::API.http_client).to receive(:request)
89
+ .once
90
+ .with(:post, start_url, {
91
+ :body => Utils.escape_query(params),
92
+ :header => headers
93
+ })
94
+ .and_return(double(:status => 400, :body => response_json))
95
+ response = Authy::PhoneVerification.start(params)
39
96
 
40
97
  expect(response).to_not be_ok
41
98
  expect(response.errors['message']).to match(/country_code - Parameter is required/)
42
99
  end
43
100
 
44
101
  it "should return an error. Cellphone is invalid" do
45
- response = Authy::PhoneVerification.start(
102
+ response_json = {
103
+ "error_code" => "60033",
104
+ "message" => "Phone number is invalid",
105
+ "errors" => {
106
+ "message" => "Phone number is invalid"
107
+ },
108
+ "success" => false
109
+ }.to_json
110
+ params = {
46
111
  via: "sms",
47
112
  country_code: "1",
48
- phone_number: "123"
49
- )
113
+ phone_number: invalid_phone_number
114
+ }
115
+ expect(Authy::API.http_client).to receive(:request)
116
+ .once
117
+ .with(:post, start_url, {
118
+ :body => Utils.escape_query(params),
119
+ :header => headers
120
+ })
121
+ .and_return(double(:status => 400, :body => response_json))
122
+ response = Authy::PhoneVerification.start(params)
50
123
 
51
124
  expect(response).to_not be_ok
52
125
  expect(response.errors['message']).to eq('Phone number is invalid')
53
126
  end
54
127
  end
55
128
 
56
- describe 'Check that a custom code request' do
57
- it "should return an error if not enabled" do
58
- pending("API is not returning expected response in this case. The test phone number is invalid")
59
-
60
- response = Authy::PhoneVerification.start(
61
- country_code: "1",
62
- phone_number: "111-111-1111",
63
- custom_code: "1234"
64
- )
65
- expect(response).not_to be_ok
66
- expect(response.message).to eq("Phone verification couldn't be created: custom codes are not allowed.")
67
- end
68
- end
69
-
70
129
  describe "Check the verification code" do
71
130
  it "should return success true if code is correct" do
72
- pending("API is not returning expected response in this case. The test phone number is invalid.")
73
-
74
- response = Authy::PhoneVerification.check(
131
+ response_json = {
132
+ "message" => "Verification code is correct.",
133
+ "success" => true
134
+ }.to_json
135
+ params = {
75
136
  country_code: "1",
76
- phone_number: "111-111-1111",
137
+ phone_number: valid_phone_number,
77
138
  verification_code: "0000"
78
- )
139
+ }
140
+ expect(Authy::API.http_client).to receive(:request)
141
+ .once
142
+ .with(:get, check_url, {
143
+ :query => params,
144
+ :header => headers,
145
+ :follow_redirect => nil
146
+ })
147
+ .and_return(double(:status => 200, :body => response_json))
148
+ response = Authy::PhoneVerification.check(params)
79
149
 
80
150
  expect(response).to be_ok
81
151
  expect(response.message).to eq('Verification code is correct.')
82
152
  end
83
153
 
84
154
  it "should return an error if code is incorrect" do
85
- pending("API is not returning expected response in this case. The test phone number is invalid")
86
-
87
- response = Authy::PhoneVerification.check(
155
+ response_json = {
156
+ "error_code" => "60022",
157
+ "message" => "Verification code is incorrect",
158
+ "errors" => {
159
+ "message" => "Verification code is incorrect"
160
+ },
161
+ "success" => false
162
+ }.to_json
163
+ params = {
88
164
  country_code: "1",
89
- phone_number: "111-111-1111",
165
+ phone_number: valid_phone_number,
90
166
  verification_code: "1234"
91
- )
167
+ }
168
+ expect(Authy::API.http_client).to receive(:request)
169
+ .once
170
+ .with(:get, check_url, {
171
+ :query => params,
172
+ :header => headers,
173
+ :follow_redirect => nil
174
+ })
175
+ .and_return(double(:status => 401, :body => response_json))
176
+ response = Authy::PhoneVerification.check(params)
177
+
178
+ expect(response).not_to be_ok
179
+ expect(response.message).to eq('Verification code is incorrect')
180
+ end
181
+
182
+ it "should return an error if there are no active verifications" do
183
+ response_json = {
184
+ "message" => "No pending verifications for #{valid_phone_number} found.",
185
+ "success" => false,
186
+ "errors" => {
187
+ "message" => "No pending verifications for #{valid_phone_number} found."
188
+ },
189
+ "error_code" => "60023"
190
+ }.to_json
191
+ params = {
192
+ :country_code => "1",
193
+ :phone_number => valid_phone_number,
194
+ :verification_code => "1234"
195
+ }
196
+ expect(Authy::API.http_client).to receive(:request)
197
+ .once
198
+ .with(:get, check_url, {
199
+ :query => params,
200
+ :header => headers,
201
+ :follow_redirect => nil
202
+ })
203
+ .and_return(double(:status => 404, :body => response_json))
204
+
205
+ response = Authy::PhoneVerification.check(params)
92
206
 
93
207
  expect(response).not_to be_ok
94
- expect(response.message).to eq('Verification code is incorrect.')
208
+ expect(response.message).to eq("No pending verifications for #{valid_phone_number} found.")
95
209
  end
96
210
  end
97
211
 
98
212
  describe 'Check the phone number' do
99
213
  it "should return an error if phone number is invalid" do
100
- response = Authy::PhoneVerification.check(
214
+ response_json = {
215
+ "error_code" => "60033",
216
+ "message" => "Phone number is invalid",
217
+ "errors" => {
218
+ "message" => "Phone number is invalid"
219
+ },
220
+ "success" => false
221
+ }.to_json
222
+ params = {
101
223
  country_code: "1",
102
- phone_number: "111-111-1111",
224
+ phone_number: invalid_phone_number,
103
225
  verification_code: "1234"
104
- )
105
-
226
+ }
227
+ expect(Authy::API.http_client).to receive(:request)
228
+ .once
229
+ .with(:get, check_url, {
230
+ :query => params,
231
+ :header => headers,
232
+ :follow_redirect => nil
233
+ })
234
+ .and_return(double(:status => 400, :body => response_json))
235
+
236
+ response = Authy::PhoneVerification.check(params)
106
237
  expect(response).not_to be_ok
107
238
  expect(response.message).to eq('Phone number is invalid')
108
239
  end