bigcommerce-oauth-api 1.1.5 → 1.2.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.
@@ -9,7 +9,10 @@ module BigcommerceOAuthAPI
9
9
  :adapter,
10
10
  :client_id,
11
11
  :access_token,
12
- :format
12
+ :format,
13
+ # legacy authentication
14
+ :user_name,
15
+ :api_key
13
16
  ].freeze
14
17
 
15
18
  DEFAULT_STORE_HASH = nil
@@ -18,6 +21,8 @@ module BigcommerceOAuthAPI
18
21
  DEFAULT_ACCESS_TOKEN = nil
19
22
  DEFAULT_FORMAT = :json
20
23
  DEFAULT_ADAPTER = Faraday.default_adapter
24
+ DEFAULT_USER_NAME = nil
25
+ DEFAULT_API_KEY = nil
21
26
 
22
27
  attr_accessor *VALID_OPTIONS_KEYS
23
28
 
@@ -32,6 +37,8 @@ module BigcommerceOAuthAPI
32
37
  self.client_id = DEFAULT_CLIENT_ID
33
38
  self.access_token = DEFAULT_ACCESS_TOKEN
34
39
  self.adapter = DEFAULT_ADAPTER
40
+ self.user_name = DEFAULT_USER_NAME
41
+ self.api_key = DEFAULT_API_KEY
35
42
  end
36
43
 
37
44
  def configure
@@ -7,16 +7,24 @@ module BigcommerceOAuthAPI
7
7
 
8
8
  def connection
9
9
  options = {
10
- :headers => {
11
- 'Accept' => "application/#{format}; charset=utf-8",
12
- 'X-Auth-Client' => client_id,
13
- 'X-Auth-Token' => access_token
14
- },
15
- :url => "#{endpoint}/#{store_hash}/v2/"
10
+ :headers => {
11
+ 'Accept' => "application/#{format}; charset=utf-8"
12
+ }
16
13
  }
14
+ if is_legacy?
15
+ options[:url] = "#{endpoint}/api/v2/"
16
+ else
17
+ options[:headers]['X-Auth-Client'] = client_id
18
+ options[:headers]['X-Auth-Token'] = access_token
19
+ options[:url] = "#{endpoint}/#{store_hash}/v2/"
20
+ end
17
21
 
18
22
  Faraday::Connection.new(options) do |connection|
19
23
  connection.use Faraday::Request::UrlEncoded
24
+ if is_legacy?
25
+ connection.use Faraday::Request::BasicAuthentication, user_name, api_key
26
+ end
27
+
20
28
  case format.to_s.downcase
21
29
  when 'json' then connection.use Faraday::Response::ParseJson
22
30
  end
@@ -53,5 +53,8 @@ module BigcommerceOAuthAPI
53
53
  # Raised if the client attempts to define an api method that already is defined elsewhere.
54
54
  # Specs will catch this type of error since it will be thrown upon initialization.
55
55
  class MethodAlreadyDefinedError < Error; end
56
+
57
+ # Raised if the client attempts to use a non-legacy api with a legacy configuration.
58
+ class NonLegacyApi < Error; end
56
59
  end
57
60
 
@@ -1,3 +1,3 @@
1
1
  module BigcommerceOAuthAPI
2
- VERSION = '1.1.5'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
@@ -35,7 +35,9 @@ module BigcommerceOAuthAPI
35
35
  :client_id => 'c',
36
36
  :access_token => 'd',
37
37
  :adapter => 'e',
38
- :store_hash => 'f'
38
+ :store_hash => 'f',
39
+ :user_name => 'g',
40
+ :api_key => 'h',
39
41
  }
40
42
  end
41
43
 
@@ -51,5 +53,62 @@ module BigcommerceOAuthAPI
51
53
  end
52
54
  end
53
55
  end
56
+
57
+ describe '#is_legacy?' do
58
+
59
+ after do
60
+ BigcommerceOAuthAPI.reset
61
+ end
62
+
63
+ context 'given the default configuration' do
64
+ it 'should return false' do
65
+ expect(described_class.new.is_legacy?).to eql(false)
66
+ end
67
+ end
68
+
69
+ context 'given a legacy configuration' do
70
+ describe 'with module configuration' do
71
+ before do
72
+ BigcommerceOAuthAPI.configure do |config|
73
+ [:endpoint, :user_name, :api_key].each do |key|
74
+ config.send("#{key}=", key)
75
+ end
76
+ end
77
+ end
78
+
79
+ it 'should return true' do
80
+ expect(described_class.new.is_legacy?).to eql(true)
81
+ end
82
+ end
83
+
84
+ describe 'with class configuration' do
85
+ it 'should return true' do
86
+ expect(described_class.new({ :endpoint => 'c', :user_name => 'd', :api_key => 'f' }).is_legacy?).to eql(true)
87
+ end
88
+ end
89
+ end
90
+
91
+ context 'given an oauth configuration' do
92
+ describe 'with module configuration' do
93
+ before do
94
+ BigcommerceOAuthAPI.configure do |config|
95
+ [:client_id, :access_token, :store_hash].each do |key|
96
+ config.send("#{key}=", key)
97
+ end
98
+ end
99
+ end
100
+
101
+ it 'should return true' do
102
+ expect(described_class.new.is_legacy?).to eql(false)
103
+ end
104
+ end
105
+
106
+ describe 'with class configuration' do
107
+ it 'should return true' do
108
+ expect(described_class.new({ :client_id => 'c', :access_token => 'd', :store_hash => 'f' }).is_legacy?).to eql(false)
109
+ end
110
+ end
111
+ end
112
+ end
54
113
  end
55
114
  end
@@ -7,116 +7,203 @@ describe BigcommerceOAuthAPI::Client do
7
7
  [
8
8
  { api_module: 'post', methods: [:all, :select, :create, :update, :delete], prefix_paths: 'blog', prefix_methods: 'blog'},
9
9
  { api_module: 'tag', methods: [:all], prefix_paths: 'blog', prefix_methods: 'blog'},
10
- { api_module: 'brand', methods: [:all, :select, :create, :update, :delete]},
10
+ { api_module: 'brand', methods: [:all, :select, :create, :update, :delete, :count]},
11
11
  { api_module: 'category', methods: [:all, :select, :create, :update, :delete]},
12
12
  { api_module: 'customer', methods: [:all, :select, :create, :update, :delete, :count]},
13
13
  { api_module: 'customer_group', methods: [:all, :select, :create, :update, :delete]},
14
- { api_module: 'country', scope: :self, methods: [:all, :select]},
15
- { api_module: 'state', scope: :self, methods: [:all, :select]},
14
+ { api_module: 'country', scope: :self, methods: [:all, :select, :count]},
15
+ { api_module: 'order_status', methods: [:all, :select]},
16
16
  { api_module: 'order', methods: [:all, :select, :create, :update, :delete, :count]},
17
17
  { api_module: 'method', methods: [:all], prefix_paths: 'payments', prefix_methods: 'payment'},
18
- { api_module: 'option', methods: [:all, :select, :create, :update, :delete]},
19
- { api_module: 'option_set', methods: [:all, :select, :create, :update, :delete]},
18
+ { api_module: 'option', methods: [:all, :select, :create, :update, :delete, :count]},
19
+ { api_module: 'option_set', methods: [:all, :select, :create, :update, :delete, :count]},
20
20
  { api_module: 'product', methods: [:all, :select, :create, :update, :delete, :count]},
21
- { api_module: 'coupon', methods: [:all, :select, :create, :update, :delete]},
22
- { api_module: 'redirect', methods: [:all, :select, :create, :update, :delete]},
21
+ { api_module: 'coupon', methods: [:all, :select, :create, :update, :delete, :count]},
22
+ { api_module: 'redirect', methods: [:all, :select, :create, :update, :delete, :count]},
23
23
  { api_module: 'method', methods: [:all, :select], prefix_paths: 'shipping', prefix_methods: 'shipping'},
24
24
  { api_module: 'tax_class', scope: :self, methods: [:all, :select]},
25
- { api_module: 'hook', methods: [:all, :select, :create, :update, :delete]},
25
+ { api_module: 'hook', methods: [:all, :select, :create, :update, :delete], legacy: false}
26
26
  ]. each do |api_description|
27
27
  api_module = api_description[:api_module]
28
28
  api_module_pluralized = api_module.pluralize
29
29
  path_prefix = (api_description.has_key?(:prefix_paths) ? "#{api_description[:prefix_paths]}/" : nil)
30
30
  method_prefix = (api_description.has_key?(:prefix_methods) ? "#{api_description[:prefix_methods]}_" : nil)
31
+ has_legacy_support = (api_description.has_key?(:legacy) ? api_description[:legacy] : true)
31
32
 
32
- before do
33
- @client = BigcommerceOAuthAPI::Client.new(:store_hash => 'TEST_STORE',
34
- :client_id => 'SECRET_ID',
35
- :access_token => 'SECRET_TOKEN')
36
- end
33
+ [:legacy, :oauth].each do |config_type|
34
+ context "given a #{config_type} configuration" do
35
+ before do
36
+ @client = case config_type
37
+ when :legacy
38
+ BigcommerceOAuthAPI::Client.new(:endpoint => 'http://example.bigcommerce.com',
39
+ :user_name => 'USER',
40
+ :api_key => 'API_KEY')
41
+ when :oauth
42
+ BigcommerceOAuthAPI::Client.new(:store_hash => 'TEST_STORE',
43
+ :client_id => 'SECRET_ID',
44
+ :access_token => 'SECRET_TOKEN')
45
+ end
46
+ end
37
47
 
38
- if api_description[:methods].include?(:all)
39
- describe ".#{method_prefix}#{api_module_pluralized}" do
40
- it "should get a list of #{api_module_pluralized}" do
41
- stub_get(@client, "#{path_prefix}#{api_module_pluralized}").
42
- to_return(:headers => { :content_type => "application/#{@client.format}" })
43
- @client.send("#{method_prefix}#{api_module_pluralized}".to_sym)
44
- expect(a_get(@client, "#{path_prefix}#{api_module_pluralized}").
45
- with(:headers => {'X-Auth-Client' => 'SECRET_ID',
46
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
48
+ if api_description[:methods].include?(:all)
49
+ describe ".#{method_prefix}#{api_module_pluralized}" do
50
+ if config_type == :legacy && !has_legacy_support
51
+ it 'should raise a non legacy api error' do
52
+ expect { @client.send("#{method_prefix}#{api_module_pluralized}".to_sym) }.
53
+ to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
54
+ end
55
+ else
56
+ it "should get a list of #{api_module_pluralized}" do
57
+ stub_get(@client, "#{path_prefix}#{api_module_pluralized}").
58
+ to_return(:headers => { :content_type => "application/#{@client.format}" })
59
+ @client.send("#{method_prefix}#{api_module_pluralized}".to_sym)
60
+ if config_type == :legacy
61
+ expect(a_get(@client, "#{path_prefix}#{api_module_pluralized}")).to have_been_made
62
+ else
63
+ expect(a_get(@client, "#{path_prefix}#{api_module_pluralized}").
64
+ with(:headers => {'X-Auth-Client' => 'SECRET_ID',
65
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
66
+ end
67
+ end
68
+
69
+ end
70
+ end
47
71
  end
48
- end
49
- end
50
72
 
51
- if api_description[:methods].include?(:select)
52
- describe ".#{method_prefix}#{api_module}" do
53
- it "gets the #{api_module} with the given id" do
54
- id = 10
55
- stub_get(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
56
- to_return(:headers => { :content_type => "application/#{@client.format}" })
57
- @client.send("#{method_prefix}#{api_module}".to_sym, id)
58
- expect(a_get(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
59
- with(:headers => {'X-Auth-Client' => 'SECRET_ID',
60
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
73
+ if api_description[:methods].include?(:select)
74
+ describe ".#{method_prefix}#{api_module}" do
75
+ if config_type == :legacy && !has_legacy_support
76
+ it 'should raise a non legacy api error' do
77
+ id = 10
78
+ expect { @client.send("#{method_prefix}#{api_module}".to_sym, id) }.
79
+ to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
80
+ end
81
+ else
82
+ it "gets the #{api_module} with the given id" do
83
+ id = 10
84
+ stub_get(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
85
+ to_return(:headers => { :content_type => "application/#{@client.format}" })
86
+ @client.send("#{method_prefix}#{api_module}".to_sym, id)
87
+ if config_type == :legacy
88
+ expect(a_get(@client, "#{path_prefix}#{api_module_pluralized}/#{id}")).to have_been_made
89
+ else
90
+ expect(a_get(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
91
+ with(:headers => {'X-Auth-Client' => 'SECRET_ID',
92
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
93
+ end
94
+ end
95
+ end
96
+ end
61
97
  end
62
- end
63
- end
64
98
 
65
- if api_description[:methods].include?(:create)
66
- describe ".create_#{method_prefix}#{api_module}" do
67
- it "creates a #{api_module} with the given attributes" do
68
- options = { name: 'A', description: 'B'}
69
- stub_post(@client, "#{path_prefix}#{api_module_pluralized}").
70
- to_return(:body => options.to_json, :headers => { :content_type => "application/#{@client.format}" })
71
- @client.send("create_#{method_prefix}#{api_module}".to_sym, options)
72
- expect(a_post(@client, "#{path_prefix}#{api_module_pluralized}").
73
- with(:body => options,
74
- :headers => {'X-Auth-Client' => 'SECRET_ID',
75
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
99
+ if api_description[:methods].include?(:create)
100
+ describe ".create_#{method_prefix}#{api_module}" do
101
+ if config_type == :legacy && !has_legacy_support
102
+ it 'should raise a non legacy api error' do
103
+ options = { name: 'A', description: 'B'}
104
+ expect { @client.send("create_#{method_prefix}#{api_module}".to_sym, options) }.
105
+ to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
106
+ end
107
+ else
108
+ it "creates a #{api_module} with the given attributes" do
109
+ options = { name: 'A', description: 'B'}
110
+ stub_post(@client, "#{path_prefix}#{api_module_pluralized}").
111
+ to_return(:body => options.to_json, :headers => { :content_type => "application/#{@client.format}" })
112
+
113
+ @client.send("create_#{method_prefix}#{api_module}".to_sym, options)
114
+ if config_type == :legacy
115
+ expect(a_post(@client, "#{path_prefix}#{api_module_pluralized}")).to have_been_made
116
+ else
117
+ expect(a_post(@client, "#{path_prefix}#{api_module_pluralized}").
118
+ with(:body => options,
119
+ :headers => {'X-Auth-Client' => 'SECRET_ID',
120
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
121
+ end
122
+ end
123
+ end
124
+ end
76
125
  end
77
- end
78
- end
79
126
 
80
- if api_description[:methods].include?(:update)
81
- describe ".update_#{method_prefix}#{api_module}" do
82
- it "update the attributes of the #{api_module} with the given id" do
83
- id = 10
84
- options = { name: 'A', description: 'B'}
85
- stub_put(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
86
- with(:body => options).
87
- to_return(:body => '', :headers => { :content_type => "application/#{@client.format}" })
88
- @client.send("update_#{method_prefix}#{api_module}".to_sym, id, options)
89
- expect(a_put(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
90
- with(:body => options,
91
- :headers => {'X-Auth-Client' => 'SECRET_ID',
92
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
127
+ if api_description[:methods].include?(:update)
128
+ describe ".update_#{method_prefix}#{api_module}" do
129
+ if config_type == :legacy && !has_legacy_support
130
+ it 'should raise a non legacy api error' do
131
+ id = 10
132
+ options = { name: 'A', description: 'B'}
133
+ expect { @client.send("update_#{method_prefix}#{api_module}".to_sym, id, options) }.
134
+ to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
135
+ end
136
+ else
137
+ it "update the attributes of the #{api_module} with the given id" do
138
+ id = 10
139
+ options = { name: 'A', description: 'B'}
140
+ stub_put(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
141
+ with(:body => options).
142
+ to_return(:body => '', :headers => { :content_type => "application/#{@client.format}" })
143
+
144
+ @client.send("update_#{method_prefix}#{api_module}".to_sym, id, options)
145
+ if config_type == :legacy
146
+ expect(a_put(@client, "#{path_prefix}#{api_module_pluralized}/#{id}")).
147
+ to have_been_made
148
+ else
149
+ expect(a_put(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
150
+ with(:body => options,
151
+ :headers => {'X-Auth-Client' => 'SECRET_ID',
152
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
153
+ end
154
+ end
155
+ end
156
+ end
93
157
  end
94
- end
95
- end
96
158
 
97
- if api_description[:methods].include?(:delete)
98
- describe ".delete_#{method_prefix}#{api_module}" do
99
- it "deletes the #{api_module} with the given id" do
100
- id = 10
101
- stub_delete(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
102
- to_return(:headers => { :content_type => "application/#{@client.format}" })
103
- @client.send("delete_#{method_prefix}#{api_module}".to_sym, id)
104
- expect(a_delete(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
105
- with(:headers => {'X-Auth-Client' => 'SECRET_ID',
106
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
159
+ if api_description[:methods].include?(:delete)
160
+ describe ".delete_#{method_prefix}#{api_module}" do
161
+ if config_type == :legacy && !has_legacy_support
162
+ it 'should raise a non legacy api error' do
163
+ id = 10
164
+ expect { @client.send("delete_#{method_prefix}#{api_module}".to_sym, id) }.
165
+ to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
166
+ end
167
+ else
168
+ it "deletes the #{api_module} with the given id" do
169
+ id = 10
170
+ stub_delete(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
171
+ to_return(:headers => { :content_type => "application/#{@client.format}" })
172
+ @client.send("delete_#{method_prefix}#{api_module}".to_sym, id)
173
+ if config_type == :legacy
174
+ expect(a_delete(@client, "#{path_prefix}#{api_module_pluralized}/#{id}")).to have_been_made
175
+ else
176
+ expect(a_delete(@client, "#{path_prefix}#{api_module_pluralized}/#{id}").
177
+ with(:headers => {'X-Auth-Client' => 'SECRET_ID',
178
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
179
+ end
180
+ end
181
+ end
182
+ end
107
183
  end
108
- end
109
- end
110
184
 
111
- if api_description[:methods].include?(:count)
112
- describe ".#{method_prefix}#{api_module_pluralized}_count" do
113
- it "returns the number of #{api_module_pluralized}" do
114
- stub_get(@client, "#{path_prefix}#{api_module_pluralized}/count").
115
- to_return(:body => '', :headers => { :content_type => "application/#{@client.format}" })
116
- @client.send("#{method_prefix}#{api_module_pluralized}_count".to_sym)
117
- expect(a_get(@client, "#{path_prefix}#{api_module_pluralized}/count").
118
- with(:headers => {'X-Auth-Client' => 'SECRET_ID',
119
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
185
+ if api_description[:methods].include?(:count)
186
+ describe ".#{method_prefix}#{api_module_pluralized}_count" do
187
+ if config_type == :legacy && !has_legacy_support
188
+ it 'should raise a non legacy api error' do
189
+ expect { @client.send("#{method_prefix}#{api_module_pluralized}_count".to_sym) }.
190
+ to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
191
+ end
192
+ else
193
+ it "returns the number of #{api_module_pluralized}" do
194
+ stub_get(@client, "#{path_prefix}#{api_module_pluralized}/count").
195
+ to_return(:body => '', :headers => { :content_type => "application/#{@client.format}" })
196
+ @client.send("#{method_prefix}#{api_module_pluralized}_count".to_sym)
197
+ if config_type == :legacy
198
+ expect(a_get(@client, "#{path_prefix}#{api_module_pluralized}/count")).to have_been_made
199
+ else
200
+ expect(a_get(@client, "#{path_prefix}#{api_module_pluralized}/count").
201
+ with(:headers => {'X-Auth-Client' => 'SECRET_ID',
202
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
203
+ end
204
+ end
205
+ end
206
+ end
120
207
  end
121
208
  end
122
209
  end
@@ -5,21 +5,24 @@ require 'bigcommerce-oauth-api/client'
5
5
 
6
6
  describe BigcommerceOAuthAPI::Client do
7
7
  [
8
- { api_module: 'product', api_parent_module: 'order', methods: [:all, :select]},
8
+ { api_module: 'product', api_parent_module: 'order', methods: [:all, :select, :count]},
9
9
  { api_module: 'shipping_address', api_parent_module: 'order', methods: [:all, :select]},
10
+ { api_module: 'coupon', api_parent_module: 'order', methods: [:all, :select]},
10
11
  { api_module: 'message', api_parent_module: 'order', methods: [:all, :select]},
11
12
  { api_module: 'shipment', api_parent_module: 'order', methods: [:all, :select, :create, :update, :delete]},
12
- { api_module: 'configurable_field', api_parent_module: 'product', methods: [:all, :select, :delete]},
13
+ { api_module: 'tax', api_parent_module: 'order', methods: [:all, :select]},
14
+ { api_module: 'configurable_field', api_parent_module: 'product', methods: [:all, :select, :delete, :count]},
13
15
  { api_module: 'custom_field', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete]},
14
16
  { api_module: 'option', api_parent_module: 'option_set', methods: [:all, :select, :create, :update, :delete]},
15
- { api_module: 'value', api_parent_module: 'option_set', methods: [:all, :select, :create, :update, :delete]},
16
- { api_module: 'discount_rule', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete]},
17
- { api_module: 'image', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete]},
17
+ { api_module: 'value', api_parent_module: 'option', methods: [:all, :select, :create, :update, :delete]},
18
+ { api_module: 'discount_rule', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete, :count]},
19
+ { api_module: 'image', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete, :count]},
18
20
  { api_module: 'option', api_parent_module: 'product', methods: [:all, :select]},
19
21
  { api_module: 'review', api_parent_module: 'product', methods: [:all]},
20
- { api_module: 'rule', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete]},
21
- { api_module: 'video', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete]},
22
- { api_module: 'sku', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete]},
22
+ { api_module: 'rule', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete, :count]},
23
+ { api_module: 'video', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete, :count]},
24
+ { api_module: 'sku', api_parent_module: 'product', methods: [:all, :select, :create, :update, :delete, :count]},
25
+ { api_module: 'state', api_parent_module: 'country', methods: [:all, :select, :count]},
23
26
  { api_module: 'address', api_parent_module: 'customer', methods: [:all, :select, :create, :update, :delete]}
24
27
  ]. each do |nested_module|
25
28
  api_parent_module = nested_module[:api_parent_module]
@@ -28,101 +31,202 @@ describe BigcommerceOAuthAPI::Client do
28
31
  api_module_pluralized = api_module.pluralize
29
32
  path_prefix = (nested_module.has_key?(:prefix_paths) ? "#{nested_module[:prefix_paths]}/" : nil)
30
33
  method_prefix = (nested_module.has_key?(:prefix_methods) ? "#{nested_module[:prefix_methods]}_" : nil)
34
+ has_legacy_support = (nested_module.has_key?(:legacy) ? nested_module[:legacy] : true)
31
35
 
32
- before do
33
- @client = BigcommerceOAuthAPI::Client.new(:store_hash => 'TEST_STORE',
34
- :client_id => 'SECRET_ID',
35
- :access_token => 'SECRET_TOKEN')
36
- end
36
+ [:legacy, :oauth].each do |config_type|
37
+ context "given a #{config_type} configuration" do
38
+ before do
39
+ @client = case config_type
40
+ when :legacy
41
+ BigcommerceOAuthAPI::Client.new(:endpoint => 'http://example.bigcommerce.com',
42
+ :user_name => 'USER',
43
+ :api_key => 'API_KEY')
44
+ when :oauth
45
+ BigcommerceOAuthAPI::Client.new(:store_hash => 'TEST_STORE',
46
+ :client_id => 'SECRET_ID',
47
+ :access_token => 'SECRET_TOKEN')
48
+ end
49
+ end
37
50
 
38
- if nested_module[:methods].include?(:all)
39
- describe ".#{method_prefix}#{api_parent_module}_#{api_module_pluralized}" do
40
- it "should get a list of #{api_module_pluralized} for the given #{api_parent_module}" do
41
- parent_id = 10
42
- stub_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}").
43
- to_return(:headers => { :content_type => "application/#{@client.format}" })
44
- @client.send("#{method_prefix}#{api_parent_module}_#{api_module_pluralized}".to_sym, parent_id)
45
- expect(a_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}").
46
- with(:headers => {'X-Auth-Client' => 'SECRET_ID',
47
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
51
+ if nested_module[:methods].include?(:all)
52
+ describe ".#{method_prefix}#{api_parent_module}_#{api_module_pluralized}" do
53
+ # NOTICE! Currently all the nested APIs have legacy support
54
+ # if config_type == :legacy && !has_legacy_support
55
+ # it 'should raise a non legacy api error' do
56
+ # parent_id = 10
57
+ # expect { @client.send("#{method_prefix}#{api_parent_module}_#{api_module_pluralized}".to_sym, parent_id) }.
58
+ # to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
59
+ # end
60
+ # else
61
+ it "should get a list of #{api_module_pluralized} for the given #{api_parent_module}" do
62
+ parent_id = 10
63
+ stub_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}").
64
+ to_return(:headers => { :content_type => "application/#{@client.format}" })
65
+ @client.send("#{method_prefix}#{api_parent_module}_#{api_module_pluralized}".to_sym, parent_id)
66
+ if config_type == :legacy
67
+ expect(a_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}"))
68
+ .to have_been_made
69
+ else
70
+ expect(a_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}").
71
+ with(:headers => {'X-Auth-Client' => 'SECRET_ID',
72
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
73
+ end
74
+ end
75
+ # end
76
+ end
48
77
  end
49
- end
50
- end
51
78
 
52
- if nested_module[:methods].include?(:select)
53
- describe ".#{method_prefix}#{api_parent_module}_#{api_module}" do
54
- it "gets the #{api_module} with the given id for the given #{api_parent_module}" do
55
- id = 10
56
- parent_id = 5
57
- stub_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
58
- to_return(:headers => { :content_type => "application/#{@client.format}" })
59
- @client.send("#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, id)
60
- expect(a_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
61
- with(:headers => {'X-Auth-Client' => 'SECRET_ID',
62
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
79
+ if nested_module[:methods].include?(:select)
80
+ describe ".#{method_prefix}#{api_parent_module}_#{api_module}" do
81
+ # NOTICE! Currently all the nested APIs have legacy support
82
+ # if config_type == :legacy && !has_legacy_support
83
+ # it 'should raise a non legacy api error' do
84
+ # id = 10
85
+ # parent_id = 5
86
+ # expect { @client.send("#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, id) }.
87
+ # to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
88
+ # end
89
+ # else
90
+ it "gets the #{api_module} with the given id for the given #{api_parent_module}" do
91
+ id = 10
92
+ parent_id = 5
93
+ stub_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
94
+ to_return(:headers => { :content_type => "application/#{@client.format}" })
95
+ @client.send("#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, id)
96
+ if config_type == :legacy
97
+ expect(a_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}"))
98
+ .to have_been_made
99
+ else
100
+ expect(a_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
101
+ with(:headers => {'X-Auth-Client' => 'SECRET_ID',
102
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
103
+ end
104
+ end
105
+ # end
106
+ end
63
107
  end
64
- end
65
- end
66
108
 
67
- if nested_module[:methods].include?(:create)
68
- describe ".create_#{method_prefix}#{api_parent_module}_#{api_module}" do
69
- it "creates a #{api_module} with the given attributes for the given #{api_parent_module}" do
70
- options = { name: 'A', description: 'B'}
71
- parent_id = 5
72
- stub_post(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}").
73
- to_return(:body => options.to_json, :headers => { :content_type => "application/#{@client.format}" })
74
- @client.send("create_#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, options)
75
- expect(a_post(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}").
76
- with(:body => options,
77
- :headers => {'X-Auth-Client' => 'SECRET_ID',
78
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
109
+ if nested_module[:methods].include?(:create)
110
+ describe ".create_#{method_prefix}#{api_parent_module}_#{api_module}" do
111
+ # NOTICE! Currently all the nested APIs have legacy support
112
+ # if config_type == :legacy && !has_legacy_support
113
+ # it 'should raise a non legacy api error' do
114
+ # options = { name: 'A', description: 'B'}
115
+ # parent_id = 5
116
+ # expect { @client.send("create_#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, options) }.
117
+ # to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
118
+ # end
119
+ # else
120
+ it "creates a #{api_module} with the given attributes for the given #{api_parent_module}" do
121
+ options = { name: 'A', description: 'B'}
122
+ parent_id = 5
123
+ stub_post(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}").
124
+ to_return(:body => options.to_json, :headers => { :content_type => "application/#{@client.format}" })
125
+ @client.send("create_#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, options)
126
+ if config_type == :legacy
127
+ expect(a_post(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}"))
128
+ .to have_been_made
129
+ else
130
+ expect(a_post(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}").
131
+ with(:body => options,
132
+ :headers => {'X-Auth-Client' => 'SECRET_ID',
133
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
134
+ end
135
+ end
136
+ # end
137
+ end
79
138
  end
80
- end
81
- end
82
139
 
83
- if nested_module[:methods].include?(:update)
84
- describe ".update_#{method_prefix}#{api_parent_module}_#{api_module}" do
85
- it "update the attributes of the #{api_module} with the given id for the #{api_parent_module}" do
86
- id = 10
87
- parent_id = 5
88
- options = { name: 'A', description: 'B'}
89
- stub_put(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
90
- with(:body => options).
91
- to_return(:body => '', :headers => { :content_type => "application/#{@client.format}" })
92
- @client.send("update_#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, id, options)
93
- expect(a_put(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
94
- with(:body => options,
95
- :headers => {'X-Auth-Client' => 'SECRET_ID',
96
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
140
+ if nested_module[:methods].include?(:update)
141
+ describe ".update_#{method_prefix}#{api_parent_module}_#{api_module}" do
142
+ # NOTICE! Currently all the nested APIs have legacy support
143
+ # if config_type == :legacy && !has_legacy_support
144
+ # it 'should raise a non legacy api error' do
145
+ # options = { name: 'A', description: 'B'}
146
+ # id = 10
147
+ # parent_id = 5
148
+ # expect { @client.send("update_#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, id, options) }.
149
+ # to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
150
+ # end
151
+ # else
152
+ it "update the attributes of the #{api_module} with the given id for the #{api_parent_module}" do
153
+ id = 10
154
+ parent_id = 5
155
+ options = { name: 'A', description: 'B'}
156
+ stub_put(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
157
+ with(:body => options).
158
+ to_return(:body => '', :headers => { :content_type => "application/#{@client.format}" })
159
+ @client.send("update_#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, id, options)
160
+ if config_type == :legacy
161
+ expect(a_put(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}"))
162
+ .to have_been_made
163
+ else
164
+ expect(a_put(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
165
+ with(:body => options,
166
+ :headers => {'X-Auth-Client' => 'SECRET_ID',
167
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
168
+ end
169
+ end
170
+ # end
171
+ end
97
172
  end
98
- end
99
- end
100
173
 
101
- if nested_module[:methods].include?(:delete)
102
- describe ".delete_#{method_prefix}#{api_parent_module}_#{api_module}" do
103
- it "deletes the #{api_module} with the given id for the #{api_parent_module}" do
104
- id = 10
105
- parent_id = 5
106
- stub_delete(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
107
- to_return(:headers => { :content_type => "application/#{@client.format}" })
108
- @client.send("delete_#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, id)
109
- expect(a_delete(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
110
- with(:headers => {'X-Auth-Client' => 'SECRET_ID',
111
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
174
+ if nested_module[:methods].include?(:delete)
175
+ describe ".delete_#{method_prefix}#{api_parent_module}_#{api_module}" do
176
+ # NOTICE! Currently all the nested APIs have legacy support
177
+ # if config_type == :legacy && !has_legacy_support
178
+ # it 'should raise a non legacy api error' do
179
+ # id = 10
180
+ # parent_id = 5
181
+ # expect { @client.send("delete_#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, id) }.
182
+ # to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
183
+ # end
184
+ # else
185
+ it "deletes the #{api_module} with the given id for the #{api_parent_module}" do
186
+ id = 10
187
+ parent_id = 5
188
+ stub_delete(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
189
+ to_return(:headers => { :content_type => "application/#{@client.format}" })
190
+ @client.send("delete_#{method_prefix}#{api_parent_module}_#{api_module}".to_sym, parent_id, id)
191
+ if config_type == :legacy
192
+ expect(a_delete(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}"))
193
+ .to have_been_made
194
+ else
195
+ expect(a_delete(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/#{id}").
196
+ with(:headers => {'X-Auth-Client' => 'SECRET_ID',
197
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
198
+ end
199
+ end
200
+ # end
201
+ end
112
202
  end
113
- end
114
- end
115
203
 
116
- if nested_module[:methods].include?(:count)
117
- describe ".#{method_prefix}#{api_module_pluralized}_count" do
118
- it "returns the number of #{api_module_pluralized} for the #{api_parent_module}" do
119
- parent_id = 5
120
- stub_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/count").
121
- to_return(:body => '', :headers => { :content_type => "application/#{@client.format}" })
122
- @client.send("#{method_prefix}#{api_module_pluralized}_count".to_sym)
123
- expect(a_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/count").
124
- with(:headers => {'X-Auth-Client' => 'SECRET_ID',
125
- 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
204
+ if nested_module[:methods].include?(:count)
205
+ describe ".#{method_prefix}#{api_parent_module}_#{api_module_pluralized}_count" do
206
+ # NOTICE! Currently all the nested APIs have legacy support
207
+ # if config_type == :legacy && !has_legacy_support
208
+ # it 'should raise a non legacy api error' do
209
+ # parent_id = 5
210
+ # expect { @client.send("#{method_prefix}#{api_parent_module}_#{api_module_pluralized}_count".to_sym, parent_id) }.
211
+ # to raise_error(BigcommerceOAuthAPI::NonLegacyApi)
212
+ # end
213
+ # else
214
+ it "returns the number of #{api_module_pluralized} for the #{api_parent_module}" do
215
+ parent_id = 5
216
+ stub_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/count").
217
+ to_return(:body => '', :headers => { :content_type => "application/#{@client.format}" })
218
+ @client.send("#{method_prefix}#{api_parent_module}_#{api_module_pluralized}_count".to_sym, parent_id)
219
+ if config_type == :legacy
220
+ expect(a_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/count"))
221
+ .to have_been_made
222
+ else
223
+ expect(a_get(@client, "#{path_prefix}#{api_parent_module_pluralized}/#{parent_id}/#{api_module_pluralized}/count").
224
+ with(:headers => {'X-Auth-Client' => 'SECRET_ID',
225
+ 'X-Auth-Token' => 'SECRET_TOKEN'})).to have_been_made
226
+ end
227
+ end
228
+ # end
229
+ end
126
230
  end
127
231
  end
128
232
  end