• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-ruby / #5867

18 Apr 2024 09:24PM UTC coverage: 92.724% (-4.8%) from 97.485%
#5867

push

github

ramya-stripe
Bump version to 11.2.0

10067 of 10857 relevant lines covered (92.72%)

258.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.56
/test/stripe/api_operations_test.rb
1
# frozen_string_literal: true
2

3
require File.expand_path("../test_helper", __dir__)
1✔
4

5
module Stripe
1✔
6
  class ApiOperationsTest < Test::Unit::TestCase
1✔
7
    class UpdateableResource < APIResource
1✔
8
      include Stripe::APIOperations::Save
1✔
9

10
      OBJECT_NAME = "updateableresource"
1✔
11

12
      def self.object_name
1✔
13
        "updateableresource"
2✔
14
      end
15

16
      def self.protected_fields
1✔
17
        [:protected]
3✔
18
      end
19
    end
20

21
    context ".update" do
1✔
22
      should "post the correct parameters to the resource URL" do
1✔
23
        stub_request(:post, "#{Stripe.api_base}/v1/updateableresources/id")
1✔
24
          .with(body: { foo: "bar" })
×
25
          .to_return(body: JSON.generate(foo: "bar"))
×
26
        resource = UpdateableResource.update("id", foo: "bar")
1✔
27
        assert_equal("bar", resource.foo)
1✔
28
      end
29

30
      should "handle a frozen set of opts" do
1✔
31
        stub_request(:post, "#{Stripe.api_base}/v1/updateableresources/id")
1✔
32
          .with(body: { foo: "bar" })
×
33
          .to_return(body: JSON.generate(foo: "bar"))
×
34
        resource = UpdateableResource.update("id", { foo: "bar" }, {}.freeze)
1✔
35
        assert_equal("bar", resource.foo)
1✔
36
      end
37

38
      should "error on protected fields" do
1✔
39
        e = assert_raises do
1✔
40
          UpdateableResource.update("id", protected: "bar")
1✔
41
        end
42
        assert_equal "Cannot update protected field: protected", e.message
1✔
43
      end
44
    end
45

46
    context ".search" do
1✔
47
      should "warn that ._search is deprecated" do
1✔
48
        old_stderr = $stderr
1✔
49
        $stderr = StringIO.new
1✔
50
        begin
×
51
          stub_request(:post, "#{Stripe.api_base}/v1/customers/search?query=foo:bar")
1✔
52
            .to_return(body: JSON.generate(object: "customer"))
1✔
53

54
          client = StripeClient.new
1✔
55
          client.request { Customer._search("/v1/customers/search", query: "foo:bar") }
2✔
56

57
          message = "NOTE: Stripe::Customer._search is deprecated; use request_stripe_object " \
1✔
58
                    "instead. It will be removed on or after 2024-01."
59
          assert_match Regexp.new(message), $stderr.string
1✔
60
        ensure
61
          $stderr = old_stderr
1✔
62
        end
63
      end
64
    end
65

66
    context ".nested_resource_class_methods" do
1✔
67
      class MainResource < APIResource # rubocop:todo Lint/ConstantDefinitionInBlock
1✔
68
        extend Stripe::APIOperations::NestedResource
1✔
69
        OBJECT_NAME = "mainresource"
1✔
70
        def self.object_name
1✔
71
          "mainresource"
5✔
72
        end
73
        nested_resource_class_methods :nested,
1✔
74
                                      operations: %i[create retrieve update delete list]
75
      end
76

77
      should "define a create method" do
1✔
78
        stub_request(:post, "#{Stripe.api_base}/v1/mainresources/id/nesteds")
1✔
79
          .with(body: { foo: "bar" })
×
80
          .to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
×
81
        nested_resource = MainResource.create_nested("id", foo: "bar")
1✔
82
        assert_equal "bar", nested_resource.foo
1✔
83
      end
84

85
      should "define a retrieve method" do
1✔
86
        stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
1✔
87
          .to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "bar"))
×
88
        nested_resource = MainResource.retrieve_nested("id", "nested_id")
1✔
89
        assert_equal "bar", nested_resource.foo
1✔
90
      end
91

92
      should "define an update method" do
1✔
93
        stub_request(:post, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
1✔
94
          .with(body: { foo: "baz" })
×
95
          .to_return(body: JSON.generate(id: "nested_id", object: "nested", foo: "baz"))
×
96
        nested_resource = MainResource.update_nested("id", "nested_id", foo: "baz")
1✔
97
        assert_equal "baz", nested_resource.foo
1✔
98
      end
99

100
      should "define a delete method" do
1✔
101
        stub_request(:delete, "#{Stripe.api_base}/v1/mainresources/id/nesteds/nested_id")
1✔
102
          .to_return(body: JSON.generate(id: "nested_id", object: "nested", deleted: true))
×
103
        nested_resource = MainResource.delete_nested("id", "nested_id")
1✔
104
        assert_equal true, nested_resource.deleted
1✔
105
      end
106

107
      should "define a list method" do
1✔
108
        stub_request(:get, "#{Stripe.api_base}/v1/mainresources/id/nesteds")
1✔
109
          .to_return(body: JSON.generate(object: "list", data: []))
×
110
        nested_resources = MainResource.list_nesteds("id")
1✔
111
        assert nested_resources.data.is_a?(Array)
1✔
112
      end
113
    end
114
  end
115
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc