braintree 4.24.0 → 4.25.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. checksums.yaml +4 -4
  2. data/lib/braintree/customer_session_gateway.rb +194 -0
  3. data/lib/braintree/error_result.rb +1 -1
  4. data/lib/braintree/errors.rb +2 -1
  5. data/lib/braintree/gateway.rb +4 -0
  6. data/lib/braintree/graphql/enums/recommendations.rb +7 -0
  7. data/lib/braintree/graphql/enums/recommended_payment_option.rb +8 -0
  8. data/lib/braintree/graphql/inputs/create_customer_session_input.rb +35 -0
  9. data/lib/braintree/graphql/inputs/customer_recommendations_input.rb +41 -0
  10. data/lib/braintree/graphql/inputs/customer_session_input.rb +39 -0
  11. data/lib/braintree/graphql/inputs/phone_input.rb +32 -0
  12. data/lib/braintree/graphql/inputs/update_customer_session_input.rb +37 -0
  13. data/lib/braintree/graphql/types/customer_recommendations_payload.rb +32 -0
  14. data/lib/braintree/graphql/types/payment_options.rb +33 -0
  15. data/lib/braintree/graphql/unions/customer_recommendations.rb +34 -0
  16. data/lib/braintree/graphql_client.rb +16 -0
  17. data/lib/braintree/successful_result.rb +2 -0
  18. data/lib/braintree/version.rb +1 -1
  19. data/lib/braintree.rb +11 -0
  20. data/spec/integration/braintree/customer_session_spec.rb +143 -0
  21. data/spec/unit/braintree/customer_session_gateway_spec.rb +120 -0
  22. data/spec/unit/braintree/graphql/create_customer_session_input_spec.rb +81 -0
  23. data/spec/unit/braintree/graphql/customer_recommendations_input_spec.rb +110 -0
  24. data/spec/unit/braintree/graphql/customer_session_input_spec.rb +81 -0
  25. data/spec/unit/braintree/graphql/phone_input_spec.rb +51 -0
  26. data/spec/unit/braintree/graphql/update_customer_session_input_spec.rb +93 -0
  27. data/spec/unit/braintree/graphql_client_spec.rb +37 -0
  28. metadata +21 -2
@@ -0,0 +1,143 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+ require File.expand_path(File.dirname(__FILE__) + "/client_api/spec_helper")
3
+
4
+ describe Braintree::CustomerSessionGateway do
5
+ let(:gateway) do
6
+ Braintree::Gateway.new(
7
+ :environment => :development,
8
+ :merchant_id => "pwpp_multi_account_merchant",
9
+ :public_key => "pwpp_multi_account_merchant_public_key",
10
+ :private_key => "pwpp_multi_account_merchant_private_key",
11
+ )
12
+ end
13
+
14
+ describe "create" do
15
+ it "can create customer session without email and phone" do
16
+ input = Braintree::CreateCustomerSessionInput.new(merchant_account_id: "usd_pwpp_multi_account_merchant_account")
17
+ result = gateway.customer_session.create_customer_session(input)
18
+ expect(result.success?).to eq(true)
19
+ expect(result.session_id).not_to be_nil
20
+ end
21
+
22
+ it "can can create customer session with merchant provided session id" do
23
+ merchant_session_id = "11EF-A1E7-A5F5EE5C-A2E5-AFD2801469FC"
24
+ input = Braintree::CreateCustomerSessionInput.new(
25
+ session_id: merchant_session_id,
26
+ )
27
+ result = gateway.customer_session.create_customer_session(input)
28
+ expect(result.success?).to eq(true)
29
+ expect(result.session_id).to eq(merchant_session_id)
30
+ end
31
+
32
+ it "can create customer session with API-derived session id" do
33
+ input = Braintree::CreateCustomerSessionInput.new({})
34
+ result = gateway.customer_session.create_customer_session(input)
35
+ expect(result.success?).to eq(true)
36
+ expect(result.session_id).not_to be_nil
37
+ end
38
+
39
+ it "cannot create duplicate customer session" do
40
+ existing_session_id = "11EF-34BC-2702904B-9026-C3ECF4BAC765"
41
+ input = Braintree::CreateCustomerSessionInput.new(
42
+ session_id: existing_session_id,
43
+ )
44
+ result = gateway.customer_session.create_customer_session(input)
45
+ expect(result.success?).to eq(false)
46
+ expect(result.errors.first.message).to include("Session IDs must be unique per merchant.")
47
+ end
48
+ end
49
+
50
+ describe "update" do
51
+ it "can update a customer session" do
52
+ session_id = "11EF-A1E7-A5F5EE5C-A2E5-AFD2801469FC"
53
+ create_input = Braintree::CreateCustomerSessionInput.new(
54
+ merchant_account_id: "usd_pwpp_multi_account_merchant_account",
55
+ session_id: session_id,
56
+ )
57
+
58
+ gateway.customer_session.create_customer_session(create_input)
59
+
60
+ customer = build_customer_session_input("PR5_test@example.com", "4085005005")
61
+
62
+ update_input = Braintree::UpdateCustomerSessionInput.new(
63
+ session_id: session_id,
64
+ customer: customer,
65
+ )
66
+
67
+ result = gateway.customer_session.update_customer_session(update_input)
68
+
69
+ expect(result.success?).to eq(true)
70
+ expect(result.session_id).to eq(session_id)
71
+
72
+ end
73
+
74
+ it "cannot update a non-existent customer session" do
75
+ session_id = "11EF-34BC-2702904B-9026-C3ECF4BAC765"
76
+ customer = build_customer_session_input("PR9_test@example.com", "4085005009")
77
+ update_input = Braintree::UpdateCustomerSessionInput.new(
78
+ session_id: session_id,
79
+ customer: customer,
80
+ )
81
+
82
+ result = gateway.customer_session.update_customer_session(update_input)
83
+
84
+ expect(result.success?).to eq(false)
85
+ expect(result.errors.first.message).to include("does not exist")
86
+ end
87
+ end
88
+
89
+ describe "customer recommendations" do
90
+ it "can get customer recommendations" do
91
+ customer = build_customer_session_input("PR5_test@example.com", "4085005005")
92
+ recommendations = [Braintree::Recommendations::PAYMENT_RECOMMENDATIONS]
93
+ customer_recommendations_input = Braintree::CustomerRecommendationsInput.new(
94
+ session_id: "11EF-A1E7-A5F5EE5C-A2E5-AFD2801469FC",
95
+ recommendations: recommendations,
96
+ customer: customer,
97
+ )
98
+ result = gateway.customer_session.get_customer_recommendations(customer_recommendations_input)
99
+
100
+ expect(result.success?).to eq(true)
101
+ payload = result.customer_recommendations
102
+ expect(payload.is_in_paypal_network).to eq(true)
103
+ payment_options = payload.recommendations.payment_options
104
+ expect(payment_options[0].payment_option).to eq(Braintree::RecommendedPaymentOption::PAYPAL)
105
+ expect(payment_options[0].recommended_priority).to equal(1)
106
+ end
107
+
108
+ it "cannot get customer recommendations for non-existent session" do
109
+ customer = build_customer_session_input("PR9_test@example.com", "4085005009")
110
+ recommendations = [Braintree::Recommendations::PAYMENT_RECOMMENDATIONS]
111
+ customer_recommendations_input = Braintree::CustomerRecommendationsInput.new(
112
+ session_id: "11EF-34BC-2702904B-9026-C3ECF4BAC765",
113
+ recommendations: recommendations,
114
+ customer: customer,
115
+ )
116
+ result = gateway.customer_session.get_customer_recommendations(customer_recommendations_input)
117
+
118
+ expect(result.success?).to eq(false)
119
+ expect(result.errors.first.message).to include("does not exist")
120
+ end
121
+ end
122
+
123
+ private
124
+
125
+ def build_customer_session(session_id = nil)
126
+ customer = build_customer_session_input("PR1_test@example.com", "4085005002")
127
+
128
+ input = session_id ? CreateCustomerSessionInput.new(customer: customer, session_id: session_id) : CreateCustomerSessionInput.new(customer => customer)
129
+ @gateway.customer_session.create_customer_session(input)
130
+ end
131
+
132
+ def build_customer_session_input(email, phone_number)
133
+ {
134
+ email: email,
135
+ device_fingerprint_id: "test",
136
+ phone: {country_phone_code: "1", phone_number: phone_number},
137
+ paypal_app_installed: true,
138
+ venmo_app_installed: true,
139
+ user_agent: "Mozilla"
140
+ }
141
+ end
142
+
143
+ end
@@ -0,0 +1,120 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe Braintree::CustomerSessionGateway do
4
+ let(:gateway) { double(:gateway) }
5
+ let(:graphql_client) { double(:graphql_client) }
6
+ let(:customer_session_gateway) { Braintree::CustomerSessionGateway.new(gateway, graphql_client) }
7
+
8
+ describe "#create_customer_session" do
9
+ let(:input) do
10
+ {
11
+ :merchant_account_id => "merchant-account-id",
12
+ }
13
+ end
14
+
15
+ let(:response) do
16
+ {
17
+ data: {
18
+ createCustomerSession: {
19
+ sessionId: "session-id"
20
+ }
21
+ }
22
+ }
23
+ end
24
+
25
+
26
+ it "executes the createCustomerSession mutation" do
27
+ create_input = Braintree::CreateCustomerSessionInput.new(input)
28
+ expect(graphql_client).to receive(:query).with(Braintree::CustomerSessionGateway::CREATE_CUSTOMER_SESSION,
29
+ {
30
+ "input" => create_input.to_graphql_variables
31
+ }).and_return(response)
32
+
33
+ expect(Braintree::GraphQLClient).to receive(:get_validation_errors).with(response).and_return(nil)
34
+ result = customer_session_gateway.create_customer_session(create_input)
35
+ expect(result).to be_a(Braintree::SuccessfulResult)
36
+ expect(result.session_id).to eq("session-id")
37
+ end
38
+
39
+ it "returns an error result if there are validation errors" do
40
+ create_input = Braintree::CreateCustomerSessionInput.new(input)
41
+ errors = {:errors =>
42
+ [ {:attribute => "", :code => "123", :message => "error"} ]
43
+ }
44
+ expect(graphql_client).to receive(:query).and_return(response)
45
+ expect(Braintree::GraphQLClient).to receive(:get_validation_errors).with(response).and_return(errors)
46
+ result = customer_session_gateway.create_customer_session(create_input)
47
+ expect(result).to be_a(Braintree::ErrorResult)
48
+ expect(result.errors.first.message).to eq("error")
49
+ end
50
+
51
+ it "raises an UnexpectedError if there is a problem parsing the response" do
52
+ create_input = Braintree::CreateCustomerSessionInput.new(input)
53
+ badResonse = {:data => {}}
54
+ expect(graphql_client).to receive(:query).and_return(badResonse)
55
+ expect(Braintree::GraphQLClient).to receive(:get_validation_errors).with(badResonse).and_return(nil)
56
+ expect { customer_session_gateway.create_customer_session(create_input) }.to raise_error(Braintree::UnexpectedError)
57
+ end
58
+ end
59
+
60
+
61
+ describe "#update_customer_session" do
62
+ let(:input) do
63
+ {
64
+ :merchant_account_id => "merchant-account-id", :session_id => "session-id"
65
+ }
66
+ end
67
+ let(:response) do
68
+ {
69
+ data: {
70
+ updateCustomerSession: {
71
+ sessionId: "session-id"
72
+ }
73
+ }
74
+ }
75
+ end
76
+
77
+ it "executes the updateCustomerSession mutation" do
78
+ update_input = Braintree::UpdateCustomerSessionInput.new(input)
79
+ expect(graphql_client).to receive(:query).with(Braintree::CustomerSessionGateway::UPDATE_CUSTOMER_SESSION,
80
+ {
81
+ "input" => update_input.to_graphql_variables
82
+ }).and_return(response)
83
+ expect(Braintree::GraphQLClient).to receive(:get_validation_errors).with(response).and_return(nil)
84
+ result = customer_session_gateway.update_customer_session(update_input)
85
+ expect(result).to be_a(Braintree::SuccessfulResult)
86
+ expect(result.session_id).to eq("session-id")
87
+ end
88
+ end
89
+
90
+ describe "#get_customer_recommendations" do
91
+ let(:customer_recommendations_input) { double(:customer_recommendations_input, to_graphql_variables: {"sessionId" => "session_id", recommendations: ["PAYMENT_RECOMMENDATIONS"]}) }
92
+ let(:response) do
93
+ {
94
+ data: {
95
+ customerRecommendations: {
96
+ isInPayPalNetwork: true,
97
+ recommendations: {
98
+ paymentOptions: [
99
+ {paymentOption: "PAYPAL", recommendedPriority: 1}
100
+ ]
101
+ }
102
+ }
103
+ }
104
+ }
105
+ end
106
+
107
+ it "fetches customer recommendations" do
108
+ expected_variables = {"input" => {"sessionId" => "session_id", recommendations: ["PAYMENT_RECOMMENDATIONS"]}}
109
+ expect(graphql_client).to receive(:query).with(Braintree::CustomerSessionGateway::GET_CUSTOMER_RECOMMENDATIONS, expected_variables).and_return(response)
110
+ expect(Braintree::GraphQLClient).to receive(:get_validation_errors).with(response).and_return(nil)
111
+
112
+ result = customer_session_gateway.get_customer_recommendations(customer_recommendations_input)
113
+ expect(result).to be_a(Braintree::SuccessfulResult)
114
+ expect(result.customer_recommendations.is_in_paypal_network).to eq(true)
115
+ expect(result.customer_recommendations.recommendations.payment_options[0].payment_option).to eq("PAYPAL")
116
+ expect(result.customer_recommendations.recommendations.payment_options[0].recommended_priority).to eq(1)
117
+ end
118
+ end
119
+
120
+ end
@@ -0,0 +1,81 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+
4
+ describe Braintree::CreateCustomerSessionInput do
5
+ let(:input_data) do
6
+ {
7
+ merchant_account_id: "merchant-account-id",
8
+ session_id: "session-id",
9
+ customer: {
10
+ email: "test@example.com" ,
11
+ },
12
+ domain: "example.com"
13
+ }
14
+ end
15
+
16
+ describe "#initialize" do
17
+ it "initializes with attributes" do
18
+ input = Braintree::CreateCustomerSessionInput.new(input_data)
19
+
20
+ expect(input.attrs).to eq(input_data.keys)
21
+ expect(input.merchant_account_id).to eq("merchant-account-id")
22
+ expect(input.session_id).to eq("session-id")
23
+ expect(input.customer).to be_a(Braintree::CustomerSessionInput)
24
+ expect(input.customer.email).to eq("test@example.com")
25
+ expect(input.domain).to eq("example.com")
26
+ end
27
+
28
+ it "handles nil customer" do
29
+ attributes = {merchant_account_id: "merchant-account-id"}
30
+ input = Braintree::CreateCustomerSessionInput.new(attributes)
31
+ expect(input.customer).to be_nil
32
+ end
33
+ end
34
+
35
+
36
+ describe "#inspect" do
37
+ it "returns a string representation of the object" do
38
+ input = Braintree::CreateCustomerSessionInput.new(input_data)
39
+ expected_string = "#<Braintree::CreateCustomerSessionInput merchant_account_id:\"merchant-account-id\" session_id:\"session-id\" customer:#<Braintree::CustomerSessionInput email:\"test@example.com\"> domain:\"example.com\">"
40
+ expect(input.inspect).to eq(expected_string)
41
+
42
+ end
43
+
44
+ it "handles nil values" do
45
+ attributes = {
46
+ merchant_account_id: "merchant-account-id",
47
+ session_id: nil,
48
+ customer: nil
49
+ }
50
+ input = Braintree::CreateCustomerSessionInput.new(attributes)
51
+ expected_string = "#<Braintree::CreateCustomerSessionInput merchant_account_id:\"merchant-account-id\" session_id:nil customer:nil>"
52
+
53
+ expect(input.inspect).to eq(expected_string)
54
+ end
55
+
56
+ end
57
+
58
+ describe "#to_graphql_variables" do
59
+ it "converts the input to graphql variables" do
60
+ input = Braintree::CreateCustomerSessionInput.new(input_data)
61
+ expected_variables = {
62
+ "merchantAccountId" => "merchant-account-id",
63
+ "sessionId" => "session-id",
64
+ "domain" => "example.com",
65
+ "customer" => {"email" => "test@example.com"}
66
+ }
67
+
68
+ expect(input.to_graphql_variables).to eq(expected_variables)
69
+
70
+
71
+ end
72
+
73
+ it "handles nil values" do
74
+ attributes = {merchant_account_id: "merchant-account-id"}
75
+ input = Braintree::CreateCustomerSessionInput.new(attributes)
76
+ expected_variables = {"merchantAccountId" => "merchant-account-id"}
77
+
78
+ expect(input.to_graphql_variables).to eq(expected_variables)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,110 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+
4
+ describe Braintree::CustomerRecommendationsInput do
5
+ let(:input_data) do
6
+ {
7
+ merchant_account_id: "merchant-account-id",
8
+ session_id: "session-id",
9
+ recommendations: ["PAYMENT_RECOMMENDATIONS"],
10
+ customer: {email: "test@example.com"}
11
+ }
12
+ end
13
+ describe "#initialize" do
14
+ it "initializes with attributes" do
15
+ input = Braintree::CustomerRecommendationsInput.new(input_data)
16
+
17
+ expect(input.attrs).to eq(input_data.keys)
18
+ expect(input.merchant_account_id).to eq("merchant-account-id")
19
+ expect(input.session_id).to eq("session-id")
20
+ expect(input.customer).to be_a(Braintree::CustomerSessionInput)
21
+ expect(input.recommendations[0]).to eq("PAYMENT_RECOMMENDATIONS")
22
+ end
23
+
24
+ it "disallows nil session id" do
25
+ attributes = {
26
+ merchant_account_id: "merchant-account-id",
27
+ recommendations: ["PAYMENT_RECOMMENDATIONS"],
28
+ }
29
+ expect do
30
+ Braintree::CustomerRecommendationsInput.new(attributes)
31
+ end.to raise_error(ArgumentError, "Expected hash to contain a :session_id")
32
+ end
33
+
34
+ it "disallows nil recommendations" do
35
+ attributes = {
36
+ merchant_account_id: "merchant-account-id",
37
+ session_id: "session-id",
38
+ }
39
+ expect do
40
+ Braintree::CustomerRecommendationsInput.new(attributes)
41
+ end.to raise_error(ArgumentError, "Expected hash to contain a :recommendations")
42
+ end
43
+
44
+ it "handles nil customer" do
45
+ attributes = {
46
+ merchant_account_id: "merchant-account-id",
47
+ session_id: "session-id",
48
+ recommendations: ["PAYMENT_RECOMMENDATIONS"],
49
+ }
50
+ input = Braintree::CustomerRecommendationsInput.new(attributes)
51
+
52
+ expect(input.customer).to be_nil
53
+ end
54
+ end
55
+
56
+
57
+ describe "#inspect" do
58
+ it "returns a string representation of the object" do
59
+ input = Braintree::CustomerRecommendationsInput.new(input_data)
60
+ expected_string = "#<Braintree::CustomerRecommendationsInput merchant_account_id:\"merchant-account-id\" session_id:\"session-id\" recommendations:[\"PAYMENT_RECOMMENDATIONS\"] customer:#<Braintree::CustomerSessionInput email:\"test@example.com\">>"
61
+ expect(input.inspect).to eq(expected_string)
62
+
63
+ end
64
+
65
+ it "handles nil values" do
66
+ attributes = {
67
+ merchant_account_id: "merchant-account-id",
68
+ session_id: "session-id",
69
+ recommendations: ["PAYMENT_RECOMMENDATIONS"],
70
+ }
71
+ input = Braintree::CustomerRecommendationsInput.new(attributes)
72
+ expected_string = "#<Braintree::CustomerRecommendationsInput merchant_account_id:\"merchant-account-id\" session_id:\"session-id\" recommendations:[\"PAYMENT_RECOMMENDATIONS\"]>"
73
+
74
+ expect(input.inspect).to eq(expected_string)
75
+ end
76
+
77
+ end
78
+
79
+ describe "#to_graphql_variables" do
80
+ it "converts the input to graphql variables" do
81
+ input = Braintree::CustomerRecommendationsInput.new(input_data)
82
+ expected_variables = {
83
+ "merchantAccountId" => "merchant-account-id",
84
+ "sessionId" => "session-id",
85
+ "customer" => {"email" => "test@example.com"},
86
+ "recommendations" => ["PAYMENT_RECOMMENDATIONS"],
87
+ }
88
+
89
+ expect(input.to_graphql_variables).to eq(expected_variables)
90
+ end
91
+
92
+ it "handles nil values" do
93
+ attributes = {
94
+ merchant_account_id: "merchant-account-id",
95
+ session_id: "session-id",
96
+ recommendations: ["PAYMENT_RECOMMENDATIONS"],
97
+ }
98
+ input = Braintree::CustomerRecommendationsInput.new(attributes)
99
+
100
+ expected_variables = {
101
+ "merchantAccountId" => "merchant-account-id",
102
+ "sessionId" => "session-id",
103
+ "recommendations" => ["PAYMENT_RECOMMENDATIONS"],
104
+ }
105
+
106
+ expect(input.to_graphql_variables).to eq(expected_variables)
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,81 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+
4
+ describe Braintree::CustomerSessionInput do
5
+ let(:input_data) do
6
+ {
7
+ email: "test@example.com" ,
8
+ phone: {
9
+ country_phone_code: "1",
10
+ phone_number: "555-123-4567",
11
+ extension_number: "123",
12
+ },
13
+ user_agent: "Mozilla"
14
+ }
15
+ end
16
+
17
+ describe "#initialize" do
18
+ it "initializes with attributes" do
19
+ input = Braintree::CustomerSessionInput.new(input_data)
20
+
21
+ expect(input.attrs).to eq(input_data.keys)
22
+ expect(input.email).to eq("test@example.com")
23
+ expect(input.phone.country_phone_code).to eq("1")
24
+ expect(input.phone.phone_number).to eq("555-123-4567")
25
+ expect(input.phone.extension_number).to eq("123")
26
+ expect(input.user_agent).to eq("Mozilla")
27
+ end
28
+
29
+ it "handles nil email" do
30
+ attributes = {}
31
+ input = Braintree::CustomerSessionInput.new(attributes)
32
+ expect(input.email).to be_nil
33
+ end
34
+ end
35
+
36
+
37
+ describe "#inspect" do
38
+ it "returns a string representation of the object" do
39
+ input = Braintree::CustomerSessionInput.new(input_data)
40
+ expected_string = "#<Braintree::CustomerSessionInput email:\"test@example.com\" phone:#<Braintree::PhoneInput country_phone_code:\"1\" phone_number:\"555-123-4567\" extension_number:\"123\"> user_agent:\"Mozilla\">"
41
+ expect(input.inspect).to eq(expected_string)
42
+
43
+ end
44
+
45
+ it "handles nil values" do
46
+ attributes = {}
47
+ input = Braintree::CustomerSessionInput.new(attributes)
48
+ expected_string = "#<Braintree::CustomerSessionInput >"
49
+
50
+ expect(input.inspect).to eq(expected_string)
51
+ end
52
+
53
+ end
54
+
55
+ describe "#to_graphql_variables" do
56
+ it "converts the input to graphql variables" do
57
+ input = Braintree::CustomerSessionInput.new(input_data)
58
+ expected_variables = {
59
+ "email" => "test@example.com",
60
+ "phone" => {
61
+ "countryPhoneCode" => "1",
62
+ "phoneNumber" => "555-123-4567",
63
+ "extensionNumber" => "123"
64
+ },
65
+ "userAgent" => "Mozilla"
66
+ }
67
+
68
+ expect(input.to_graphql_variables).to eq(expected_variables)
69
+
70
+
71
+ end
72
+
73
+ it "handles nil values" do
74
+ attributes = {}
75
+ input = Braintree::CustomerSessionInput.new(attributes)
76
+ expected_variables = {}
77
+
78
+ expect(input.to_graphql_variables).to eq(expected_variables)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+
4
+ describe Braintree::PhoneInput do
5
+ let(:phone_input_data) do
6
+ {
7
+ country_phone_code: "1",
8
+ phone_number: "555-123-4567",
9
+ extension_number: "123"
10
+ }
11
+ end
12
+
13
+ let(:partial_phone_input_data) do
14
+ {
15
+ country_phone_code: "1",
16
+ phone_number: "555-123-4567"
17
+ }
18
+ end
19
+
20
+
21
+ describe "#initialize" do
22
+ it "initialize and sets the input keys to attrs variable" do
23
+ phone_input = described_class.new(phone_input_data)
24
+
25
+ expect(phone_input.attrs).to include(:country_phone_code)
26
+ expect(phone_input.attrs).to include(:phone_number)
27
+ expect(phone_input.attrs).to include(:extension_number)
28
+ expect(phone_input.attrs.length).to eq(3)
29
+ end
30
+ end
31
+
32
+ describe "inspect" do
33
+ it "includes all phone input attributes" do
34
+ phone_input = described_class.new(phone_input_data)
35
+ output = phone_input.inspect
36
+
37
+ expect(output).to include("country_phone_code:\"1\"")
38
+ expect(output).to include("phone_number:\"555-123-4567\"")
39
+ expect(output).to include("extension_number:\"123\"")
40
+ end
41
+
42
+ it "includes only specified phone input attributes" do
43
+ phone_input = described_class.new(partial_phone_input_data)
44
+ output = phone_input.inspect
45
+
46
+ expect(output).to include("country_phone_code:\"1\"")
47
+ expect(output).to include("phone_number:\"555-123-4567\"")
48
+ expect(output).not_to include("extension_number")
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,93 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
2
+
3
+
4
+ describe Braintree::UpdateCustomerSessionInput do
5
+ let(:input_data) do
6
+ {
7
+ merchant_account_id: "merchant-account-id",
8
+ session_id: "session-id",
9
+ customer: {email: "test@example.com"}
10
+ }
11
+ end
12
+ describe "#initialize" do
13
+ it "initializes with attributes" do
14
+ input = Braintree::UpdateCustomerSessionInput.new(input_data)
15
+
16
+ expect(input.attrs).to eq(input_data.keys)
17
+ expect(input.merchant_account_id).to eq("merchant-account-id")
18
+ expect(input.session_id).to eq("session-id")
19
+ expect(input.customer).to be_a(Braintree::CustomerSessionInput)
20
+ expect(input.customer.email).to eq("test@example.com")
21
+ end
22
+
23
+ it "disallows nil session id" do
24
+ attributes = {
25
+ merchant_account_id: "merchant-account-id",
26
+ }
27
+ expect do
28
+ Braintree::UpdateCustomerSessionInput.new(attributes)
29
+ end.to raise_error(ArgumentError, "Expected hash to contain a :session_id")
30
+ end
31
+
32
+ it "handles nil customer" do
33
+ attributes = {
34
+ merchant_account_id: "merchant-account-id",
35
+ session_id: "session-id",
36
+ }
37
+ input = Braintree::UpdateCustomerSessionInput.new(attributes)
38
+
39
+ expect(input.customer).to be_nil
40
+ end
41
+ end
42
+
43
+
44
+ describe "#inspect" do
45
+ it "returns a string representation of the object" do
46
+ input = Braintree::UpdateCustomerSessionInput.new(input_data)
47
+ expected_string = "#<Braintree::UpdateCustomerSessionInput merchant_account_id:\"merchant-account-id\" session_id:\"session-id\" customer:#<Braintree::CustomerSessionInput email:\"test@example.com\">>"
48
+ expect(input.inspect).to eq(expected_string)
49
+
50
+ end
51
+
52
+ it "handles nil values" do
53
+ attributes = {
54
+ merchant_account_id: "merchant-account-id",
55
+ session_id: "session-id",
56
+ }
57
+ input = Braintree::UpdateCustomerSessionInput.new(attributes)
58
+ expected_string = "#<Braintree::UpdateCustomerSessionInput merchant_account_id:\"merchant-account-id\" session_id:\"session-id\">"
59
+
60
+ expect(input.inspect).to eq(expected_string)
61
+ end
62
+
63
+ end
64
+
65
+ describe "#to_graphql_variables" do
66
+ it "converts the input to graphql variables" do
67
+ input = Braintree::UpdateCustomerSessionInput.new(input_data)
68
+ expected_variables = {
69
+ "merchantAccountId" => "merchant-account-id",
70
+ "sessionId" => "session-id",
71
+ "customer" => {"email" => "test@example.com"},
72
+ }
73
+
74
+ expect(input.to_graphql_variables).to eq(expected_variables)
75
+ end
76
+
77
+ it "handles nil values" do
78
+ attributes = {
79
+ merchant_account_id: "merchant-account-id",
80
+ session_id: "session-id",
81
+ }
82
+ input = Braintree::UpdateCustomerSessionInput.new(attributes)
83
+
84
+ expected_variables = {
85
+ "merchantAccountId" => "merchant-account-id",
86
+ "sessionId" => "session-id",
87
+ }
88
+
89
+ expect(input.to_graphql_variables).to eq(expected_variables)
90
+ end
91
+ end
92
+
93
+ end