kintone-oauth-extension 0.2.1

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.
Files changed (75) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/gem-push.yml +37 -0
  3. data/.github/workflows/test.yml +21 -0
  4. data/.gitignore +21 -0
  5. data/.rspec +1 -0
  6. data/.rubocop.yml +47 -0
  7. data/CHANGELOG.md +13 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +374 -0
  12. data/bin/console +4 -0
  13. data/bin/setup +6 -0
  14. data/kintone.gemspec +38 -0
  15. data/lib/kintone.rb +8 -0
  16. data/lib/kintone/api.rb +121 -0
  17. data/lib/kintone/api/guest.rb +44 -0
  18. data/lib/kintone/command.rb +12 -0
  19. data/lib/kintone/command/accessor.rb +109 -0
  20. data/lib/kintone/command/apis.rb +22 -0
  21. data/lib/kintone/command/app.rb +11 -0
  22. data/lib/kintone/command/app_acl.rb +11 -0
  23. data/lib/kintone/command/apps.rb +12 -0
  24. data/lib/kintone/command/bulk_request.rb +12 -0
  25. data/lib/kintone/command/field_acl.rb +11 -0
  26. data/lib/kintone/command/file.rb +15 -0
  27. data/lib/kintone/command/form.rb +11 -0
  28. data/lib/kintone/command/guests.rb +17 -0
  29. data/lib/kintone/command/preview_form.rb +11 -0
  30. data/lib/kintone/command/record.rb +23 -0
  31. data/lib/kintone/command/record_acl.rb +11 -0
  32. data/lib/kintone/command/records.rb +29 -0
  33. data/lib/kintone/command/space.rb +15 -0
  34. data/lib/kintone/command/space_body.rb +11 -0
  35. data/lib/kintone/command/space_guests.rb +11 -0
  36. data/lib/kintone/command/space_members.rb +16 -0
  37. data/lib/kintone/command/space_thread.rb +14 -0
  38. data/lib/kintone/command/template_space.rb +12 -0
  39. data/lib/kintone/kintone_error.rb +12 -0
  40. data/lib/kintone/oauth_api.rb +91 -0
  41. data/lib/kintone/query.rb +152 -0
  42. data/lib/kintone/query/extension.rb +23 -0
  43. data/lib/kintone/type.rb +6 -0
  44. data/lib/kintone/type/extension/enumerable.rb +5 -0
  45. data/lib/kintone/type/extension/hash.rb +5 -0
  46. data/lib/kintone/type/extension/object.rb +5 -0
  47. data/lib/kintone/type/record.rb +11 -0
  48. data/lib/kintone/version.rb +3 -0
  49. data/spec/kintone/api/guest_spec.rb +289 -0
  50. data/spec/kintone/api_spec.rb +566 -0
  51. data/spec/kintone/command/apis_spec.rb +179 -0
  52. data/spec/kintone/command/app_acl_spec.rb +43 -0
  53. data/spec/kintone/command/app_spec.rb +54 -0
  54. data/spec/kintone/command/apps_spec.rb +90 -0
  55. data/spec/kintone/command/bulk_request_spec.rb +92 -0
  56. data/spec/kintone/command/field_acl_spec.rb +47 -0
  57. data/spec/kintone/command/file_spec.rb +65 -0
  58. data/spec/kintone/command/form_spec.rb +47 -0
  59. data/spec/kintone/command/guests_spec.rb +107 -0
  60. data/spec/kintone/command/preview_form_spec.rb +30 -0
  61. data/spec/kintone/command/record_acl_spec.rb +48 -0
  62. data/spec/kintone/command/record_spec.rb +210 -0
  63. data/spec/kintone/command/records_spec.rb +463 -0
  64. data/spec/kintone/command/space_body_spec.rb +47 -0
  65. data/spec/kintone/command/space_guests_spec.rb +55 -0
  66. data/spec/kintone/command/space_members_spec.rb +117 -0
  67. data/spec/kintone/command/space_spec.rb +86 -0
  68. data/spec/kintone/command/space_thread_spec.rb +77 -0
  69. data/spec/kintone/command/template_space_spec.rb +59 -0
  70. data/spec/kintone/kintone_error_spec.rb +93 -0
  71. data/spec/kintone/oauth_api_spec.rb +164 -0
  72. data/spec/kintone/query_spec.rb +506 -0
  73. data/spec/kintone/type/record_spec.rb +38 -0
  74. data/spec/spec_helper.rb +10 -0
  75. metadata +302 -0
@@ -0,0 +1,23 @@
1
+ class Kintone::Query
2
+ module Extention
3
+ refine Object do
4
+ def query_format
5
+ self
6
+ end
7
+ end
8
+
9
+ refine Symbol do
10
+ def query_format
11
+ "\"#{self}\""
12
+ end
13
+ end
14
+
15
+ refine String do
16
+ def query_format
17
+ if self =~ /\A".+"\z/ then self
18
+ else "\"#{self}\""
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,6 @@
1
+ require 'kintone/type/extension/object'
2
+ require 'kintone/type/extension/enumerable'
3
+ require 'kintone/type/extension/hash'
4
+ require 'kintone/type/record'
5
+
6
+ module Kintone::Type; end
@@ -0,0 +1,5 @@
1
+ module Enumerable
2
+ def to_kintone
3
+ map(&:to_kintone)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def to_kintone
3
+ map { |k, v| [k, v.to_kintone] }.to_h
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def to_kintone
3
+ self
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ module Kintone::Type
2
+ class Record < Hash
3
+ def initialize(default = nil)
4
+ default.each { |k, v| store(k, v) } if default.is_a?(Hash)
5
+ end
6
+
7
+ def to_kintone
8
+ map { |k, v| [k, { value: v }] }.to_h
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Kintone
2
+ VERSION = '0.2.1'.freeze
3
+ end
@@ -0,0 +1,289 @@
1
+ require 'spec_helper'
2
+ require 'kintone/api/guest'
3
+ require 'kintone/api'
4
+
5
+ describe Kintone::Api::Guest do
6
+ let(:target) { Kintone::Api::Guest.new(space, api) }
7
+ let(:space) { 1 }
8
+ let(:api) { Kintone::Api.new('example.cybozu.com', 'Administrator', 'cybozu') }
9
+
10
+ describe '#get_url' do
11
+ subject { target.get_url(command) }
12
+
13
+ let(:command) { 'path' }
14
+
15
+ it { expect(subject).to eq('/k/guest/1/v1/path.json') }
16
+ end
17
+
18
+ describe '#get' do
19
+ before(:each) do
20
+ stub_request(
21
+ :get,
22
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
23
+ )
24
+ .with(
25
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
26
+ body: params.to_json
27
+ )
28
+ .to_return(
29
+ body: '{"abc":"def"}',
30
+ status: 200,
31
+ headers: { 'Content-type' => 'application/json' }
32
+ )
33
+ end
34
+
35
+ subject { target.get(path, params) }
36
+
37
+ let(:path) { '/k/guest/1/v1/path.json' }
38
+ let(:params) { { p1: 'abc', p2: 'def' } }
39
+
40
+ it { expect(subject).to eq 'abc' => 'def' }
41
+
42
+ context 'fail to request' do
43
+ before(:each) do
44
+ stub_request(
45
+ :get,
46
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
47
+ )
48
+ .with(
49
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
50
+ body: params.to_json
51
+ )
52
+ .to_return(
53
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
54
+ status: 500
55
+ )
56
+ end
57
+
58
+ it { expect { subject }.to raise_error Kintone::KintoneError }
59
+ end
60
+ end
61
+
62
+ describe '#post' do
63
+ before(:each) do
64
+ stub_request(
65
+ :post,
66
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
67
+ )
68
+ .with(
69
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
70
+ body: '{"p1":"abc","p2":"def"}'
71
+ )
72
+ .to_return(
73
+ body: '{"abc":"def"}',
74
+ status: 200,
75
+ headers: { 'Content-type' => 'application/json' }
76
+ )
77
+ end
78
+
79
+ subject { target.post(path, body) }
80
+
81
+ let(:path) { '/k/guest/1/v1/path.json' }
82
+ let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
83
+
84
+ it { expect(subject).to eq 'abc' => 'def' }
85
+
86
+ context 'fail to request' do
87
+ before(:each) do
88
+ stub_request(
89
+ :post,
90
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
91
+ )
92
+ .with(
93
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
94
+ body: '{"p1":"abc","p2":"def"}'
95
+ )
96
+ .to_return(
97
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
98
+ status: 500
99
+ )
100
+ end
101
+
102
+ it { expect { subject }.to raise_error Kintone::KintoneError }
103
+ end
104
+ end
105
+
106
+ describe '#put' do
107
+ before(:each) do
108
+ stub_request(
109
+ :put,
110
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
111
+ )
112
+ .with(
113
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
114
+ body: '{"p1":"abc","p2":"def"}'
115
+ )
116
+ .to_return(
117
+ body: '{"abc":"def"}',
118
+ status: 200,
119
+ headers: { 'Content-type' => 'application/json' }
120
+ )
121
+ end
122
+
123
+ subject { target.put(path, body) }
124
+
125
+ let(:path) { '/k/guest/1/v1/path.json' }
126
+ let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
127
+
128
+ it { expect(subject).to eq 'abc' => 'def' }
129
+
130
+ context 'fail to request' do
131
+ before(:each) do
132
+ stub_request(
133
+ :put,
134
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
135
+ )
136
+ .with(
137
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' },
138
+ body: '{"p1":"abc","p2":"def"}'
139
+ )
140
+ .to_return(
141
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
142
+ status: 500
143
+ )
144
+ end
145
+
146
+ it { expect { subject }.to raise_error Kintone::KintoneError }
147
+ end
148
+ end
149
+
150
+ describe '#delete' do
151
+ before(:each) do
152
+ stub_request(
153
+ :delete,
154
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
155
+ )
156
+ .with(
157
+ body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
158
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
159
+ )
160
+ .to_return(
161
+ body: '{"abc":"def"}',
162
+ status: 200,
163
+ headers: { 'Content-type' => 'application/json' }
164
+ )
165
+ end
166
+
167
+ subject { target.delete(path, params) }
168
+
169
+ let(:path) { '/k/guest/1/v1/path.json' }
170
+ let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }
171
+
172
+ it { expect(subject).to eq 'abc' => 'def' }
173
+
174
+ context 'fail to request' do
175
+ before(:each) do
176
+ stub_request(
177
+ :delete,
178
+ 'https://example.cybozu.com/k/guest/1/v1/path.json'
179
+ )
180
+ .with(
181
+ body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
182
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
183
+ )
184
+ .to_return(
185
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
186
+ status: 500
187
+ )
188
+ end
189
+
190
+ it { expect { subject }.to raise_error Kintone::KintoneError }
191
+ end
192
+ end
193
+
194
+ describe '#record' do
195
+ subject { target.record }
196
+
197
+ it { expect(subject).to be_a_kind_of(Kintone::Command::Record) }
198
+ end
199
+
200
+ describe '#records' do
201
+ subject { target.records }
202
+
203
+ it { expect(subject).to be_a_kind_of(Kintone::Command::Records) }
204
+ end
205
+
206
+ describe '#form' do
207
+ subject { target.form }
208
+
209
+ it { expect(subject).to be_a_kind_of(Kintone::Command::Form) }
210
+ end
211
+
212
+ describe '#app_acl' do
213
+ subject { target.app_acl }
214
+
215
+ it { expect(subject).to be_a_kind_of(Kintone::Command::AppAcl) }
216
+ end
217
+
218
+ describe '#record_acl' do
219
+ subject { target.record_acl }
220
+
221
+ it { expect(subject).to be_a_kind_of(Kintone::Command::RecordAcl) }
222
+ end
223
+
224
+ describe '#field_acl' do
225
+ subject { target.field_acl }
226
+
227
+ it { expect(subject).to be_a_kind_of(Kintone::Command::FieldAcl) }
228
+ end
229
+
230
+ describe '#space' do
231
+ subject { target.space }
232
+
233
+ it { expect(subject).to be_a_kind_of(Kintone::Command::Space) }
234
+ end
235
+
236
+ describe '#space_body' do
237
+ subject { target.space_body }
238
+
239
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceBody) }
240
+ end
241
+
242
+ describe '#space_thread' do
243
+ subject { target.space_thread }
244
+
245
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceThread) }
246
+ end
247
+
248
+ describe '#space_members' do
249
+ subject { target.space_members }
250
+
251
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceMembers) }
252
+ end
253
+
254
+ describe '#space_guests' do
255
+ subject { target.space_guests }
256
+
257
+ it { expect(subject).to be_a_kind_of(Kintone::Command::SpaceGuests) }
258
+ end
259
+
260
+ describe '#app' do
261
+ subject { target.app }
262
+
263
+ it { is_expected.to be_a_kind_of(Kintone::Command::App) }
264
+ end
265
+
266
+ describe '#apps' do
267
+ subject { target.apps }
268
+
269
+ it { is_expected.to be_a_kind_of(Kintone::Command::Apps) }
270
+ end
271
+
272
+ describe '#bulk_request' do
273
+ subject { target.bulk_request }
274
+
275
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
276
+ end
277
+
278
+ describe '#bulk' do
279
+ subject { target.bulk }
280
+
281
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
282
+ end
283
+
284
+ describe '#preview_form' do
285
+ subject { target.preview_form }
286
+
287
+ it { is_expected.to be_a_kind_of(Kintone::Command::PreviewForm) }
288
+ end
289
+ end
@@ -0,0 +1,566 @@
1
+ require 'spec_helper'
2
+ require 'kintone/api'
3
+ require 'kintone/api/guest'
4
+ require 'kintone/command/record'
5
+ require 'kintone/command/records'
6
+
7
+ describe Kintone::Api do
8
+ let(:target) { Kintone::Api.new(domain, user, password) }
9
+ let(:domain) { 'www.example.com' }
10
+
11
+ context 'ユーザー認証の時' do
12
+ let(:user) { 'Administrator' }
13
+ let(:password) { 'cybozu' }
14
+
15
+ describe '#get_url' do
16
+ subject { target.get_url(command) }
17
+
18
+ let(:command) { 'path' }
19
+
20
+ it { is_expected.to eq('/k/v1/path.json') }
21
+ end
22
+
23
+ describe '#guest' do
24
+ subject { target.guest(space) }
25
+
26
+ context '引数が数値の1の時' do
27
+ let(:space) { 1 }
28
+
29
+ it { is_expected.to be_a_kind_of(Kintone::Api::Guest) }
30
+ it { expect(subject.instance_variable_get(:@guest_path)).to eq('/k/guest/1/v1/') }
31
+ end
32
+
33
+ context '引数がnilの時' do
34
+ let(:space) { nil }
35
+ it { expect(subject.instance_variable_get(:@guest_path)).to eq('/k/guest/0/v1/') }
36
+ end
37
+
38
+ context '引数が数字の時' do
39
+ let(:space) { '2.1' }
40
+ it { expect(subject.instance_variable_get(:@guest_path)).to eq('/k/guest/2/v1/') }
41
+ end
42
+
43
+ context '引数が数値に変換できる文字列の時' do
44
+ let(:space) { '21No' }
45
+ it { expect(subject.instance_variable_get(:@guest_path)).to eq('/k/guest/21/v1/') }
46
+ end
47
+
48
+ context '引数が数値に変換できる文字列でないとき時' do
49
+ let(:space) { 'No21' }
50
+ it { expect(subject.instance_variable_get(:@guest_path)).to eq('/k/guest/0/v1/') }
51
+ end
52
+ end
53
+
54
+ describe '#get' do
55
+ before(:each) do
56
+ stub_request(
57
+ :get,
58
+ 'https://www.example.com/k/v1/path'
59
+ )
60
+ .with(
61
+ body: params.to_h.to_json,
62
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
63
+ )
64
+ .to_return(
65
+ body: '{"abc":"def"}',
66
+ status: 200,
67
+ headers: { 'Content-type' => 'application/json' }
68
+ )
69
+ end
70
+
71
+ subject { target.get(path, params) }
72
+
73
+ let(:path) { '/k/v1/path' }
74
+
75
+ context 'with some params' do
76
+ let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }
77
+
78
+ it { is_expected.to eq 'abc' => 'def' }
79
+ end
80
+
81
+ context 'with empty params' do
82
+ let(:params) { {} }
83
+
84
+ it { is_expected.to eq 'abc' => 'def' }
85
+ end
86
+
87
+ context 'with nil' do
88
+ let(:params) { nil }
89
+
90
+ it { is_expected.to eq 'abc' => 'def' }
91
+ end
92
+
93
+ context 'fail to request' do
94
+ before(:each) do
95
+ stub_request(
96
+ :get,
97
+ 'https://www.example.com/k/v1/path'
98
+ )
99
+ .with(
100
+ body: params.to_json,
101
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
102
+ )
103
+ .to_return(
104
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
105
+ status: 500
106
+ )
107
+ end
108
+
109
+ let(:params) { {} }
110
+
111
+ it { expect { subject }.to raise_error Kintone::KintoneError }
112
+ end
113
+ end
114
+
115
+ describe '#post' do
116
+ before(:each) do
117
+ stub_request(
118
+ :post,
119
+ 'https://www.example.com/k/v1/path'
120
+ )
121
+ .with(
122
+ headers: {
123
+ 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=',
124
+ 'Content-Type' => 'application/json'
125
+ },
126
+ body: '{"p1":"abc","p2":"def"}'
127
+ )
128
+ .to_return(
129
+ body: '{"abc":"def"}',
130
+ status: 200,
131
+ headers: { 'Content-type' => 'application/json' }
132
+ )
133
+ end
134
+
135
+ subject { target.post(path, body) }
136
+ let(:path) { '/k/v1/path' }
137
+ let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
138
+
139
+ it { is_expected.to eq 'abc' => 'def' }
140
+
141
+ context 'fail to request' do
142
+ before(:each) do
143
+ stub_request(
144
+ :post,
145
+ 'https://www.example.com/k/v1/path'
146
+ )
147
+ .with(
148
+ headers: {
149
+ 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=',
150
+ 'Content-Type' => 'application/json'
151
+ },
152
+ body: '{"p1":"abc","p2":"def"}'
153
+ )
154
+ .to_return(
155
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
156
+ status: 500
157
+ )
158
+ end
159
+
160
+ it { expect { subject }.to raise_error Kintone::KintoneError }
161
+ end
162
+ end
163
+
164
+ describe '#put' do
165
+ before(:each) do
166
+ stub_request(
167
+ :put,
168
+ 'https://www.example.com/k/v1/path'
169
+ )
170
+ .with(
171
+ headers: {
172
+ 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=',
173
+ 'Content-Type' => 'application/json'
174
+ },
175
+ body: '{"p1":"abc","p2":"def"}'
176
+ )
177
+ .to_return(
178
+ body: '{"abc":"def"}',
179
+ status: 200,
180
+ headers: { 'Content-type' => 'application/json' }
181
+ )
182
+ end
183
+
184
+ subject { target.put(path, body) }
185
+ let(:path) { '/k/v1/path' }
186
+ let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
187
+
188
+ it { is_expected.to eq 'abc' => 'def' }
189
+
190
+ context 'fail to request' do
191
+ before(:each) do
192
+ stub_request(
193
+ :put,
194
+ 'https://www.example.com/k/v1/path'
195
+ )
196
+ .with(
197
+ headers: {
198
+ 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=',
199
+ 'Content-Type' => 'application/json'
200
+ },
201
+ body: '{"p1":"abc","p2":"def"}'
202
+ )
203
+ .to_return(
204
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
205
+ status: 500
206
+ )
207
+ end
208
+
209
+ it { expect { subject }.to raise_error Kintone::KintoneError }
210
+ end
211
+ end
212
+
213
+ describe '#delete' do
214
+ before(:each) do
215
+ stub_request(
216
+ :delete,
217
+ 'https://www.example.com/k/v1/path'
218
+ )
219
+ .with(
220
+ body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
221
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
222
+ )
223
+ .to_return(
224
+ body: '{"abc":"def"}',
225
+ status: 200,
226
+ headers: { 'Content-type' => 'application/json' }
227
+ )
228
+ end
229
+
230
+ subject { target.delete(path, params) }
231
+ let(:path) { '/k/v1/path' }
232
+ let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }
233
+
234
+ it { is_expected.to eq 'abc' => 'def' }
235
+
236
+ context 'fail to request' do
237
+ before(:each) do
238
+ stub_request(
239
+ :delete,
240
+ 'https://www.example.com/k/v1/path'
241
+ )
242
+ .with(
243
+ body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
244
+ headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }
245
+ )
246
+ .to_return(
247
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
248
+ status: 500
249
+ )
250
+ end
251
+
252
+ it { expect { subject }.to raise_error Kintone::KintoneError }
253
+ end
254
+ end
255
+
256
+ describe '#post_file' do
257
+ before(:each) do
258
+ stub_request(
259
+ :post,
260
+ 'https://www.example.com/k/v1/path'
261
+ )
262
+ .with(headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }) { attachment } # rubocop:disable Metrics/LineLength
263
+ .to_return(
264
+ body: '{"fileKey":"abc"}',
265
+ status: 200,
266
+ headers: { 'Content-type' => 'application/json' }
267
+ )
268
+
269
+ expect(Faraday::UploadIO).to receive(:new).with(path, content_type, original_filename).and_return(attachment) # rubocop:disable Metrics/LineLength
270
+ end
271
+
272
+ subject { target.post_file(url, path, content_type, original_filename) }
273
+ let(:attachment) { double('attachment') }
274
+ let(:url) { '/k/v1/path' }
275
+ let(:path) { '/path/to/file.txt' }
276
+ let(:content_type) { 'text/plain' }
277
+ let(:original_filename) { 'fileName.txt' }
278
+
279
+ it { is_expected.to eq 'abc' }
280
+
281
+ context 'fail to request' do
282
+ before(:each) do
283
+ stub_request(
284
+ :post,
285
+ 'https://www.example.com/k/v1/path'
286
+ )
287
+ .with(headers: { 'X-Cybozu-Authorization' => 'QWRtaW5pc3RyYXRvcjpjeWJvenU=' }) { attachment } # rubocop:disable Metrics/LineLength
288
+ .to_return(
289
+ body: '{"message":"不正なJSON文字列です。","id":"1505999166-897850006","code":"CB_IJ01"}',
290
+ status: 500
291
+ )
292
+ end
293
+
294
+ it { expect { subject }.to raise_error Kintone::KintoneError }
295
+ end
296
+ end
297
+
298
+ describe '#record' do
299
+ subject { target.record }
300
+
301
+ it { is_expected.to be_a_kind_of(Kintone::Command::Record) }
302
+ end
303
+
304
+ describe '#records' do
305
+ subject { target.records }
306
+
307
+ it { is_expected.to be_a_kind_of(Kintone::Command::Records) }
308
+ end
309
+
310
+ describe '#form' do
311
+ subject { target.form }
312
+
313
+ it { is_expected.to be_a_kind_of(Kintone::Command::Form) }
314
+ end
315
+
316
+ describe '#app_acl' do
317
+ subject { target.app_acl }
318
+
319
+ it { is_expected.to be_a_kind_of(Kintone::Command::AppAcl) }
320
+ end
321
+
322
+ describe '#record_acl' do
323
+ subject { target.record_acl }
324
+
325
+ it { is_expected.to be_a_kind_of(Kintone::Command::RecordAcl) }
326
+ end
327
+
328
+ describe '#field_acl' do
329
+ subject { target.field_acl }
330
+
331
+ it { is_expected.to be_a_kind_of(Kintone::Command::FieldAcl) }
332
+ end
333
+
334
+ describe '#template_space' do
335
+ subject { target.template_space }
336
+
337
+ it { is_expected.to be_a_kind_of(Kintone::Command::TemplateSpace) }
338
+ end
339
+
340
+ describe '#space' do
341
+ subject { target.space }
342
+
343
+ it { is_expected.to be_a_kind_of(Kintone::Command::Space) }
344
+ end
345
+
346
+ describe '#space_body' do
347
+ subject { target.space_body }
348
+
349
+ it { is_expected.to be_a_kind_of(Kintone::Command::SpaceBody) }
350
+ end
351
+
352
+ describe '#space_thread' do
353
+ subject { target.space_thread }
354
+
355
+ it { is_expected.to be_a_kind_of(Kintone::Command::SpaceThread) }
356
+ end
357
+
358
+ describe '#space_members' do
359
+ subject { target.space_members }
360
+
361
+ it { is_expected.to be_a_kind_of(Kintone::Command::SpaceMembers) }
362
+ end
363
+
364
+ describe '#guests' do
365
+ subject { target.guests }
366
+
367
+ it { is_expected.to be_a_kind_of(Kintone::Command::Guests) }
368
+ end
369
+
370
+ describe '#app' do
371
+ subject { target.app }
372
+
373
+ it { is_expected.to be_a_kind_of(Kintone::Command::App) }
374
+ end
375
+
376
+ describe '#apps' do
377
+ subject { target.apps }
378
+
379
+ it { is_expected.to be_a_kind_of(Kintone::Command::Apps) }
380
+ end
381
+
382
+ describe '#apis' do
383
+ subject { target.apis }
384
+
385
+ it { is_expected.to be_a_kind_of(Kintone::Command::Apis) }
386
+ end
387
+
388
+ describe '#bulk_request' do
389
+ subject { target.bulk_request }
390
+
391
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
392
+ end
393
+
394
+ describe '#bulk' do
395
+ subject { target.bulk }
396
+
397
+ it { is_expected.to be_a_kind_of(Kintone::Command::BulkRequest) }
398
+ end
399
+
400
+ describe '#file' do
401
+ subject { target.file }
402
+
403
+ it { is_expected.to be_a_kind_of(Kintone::Command::File) }
404
+ end
405
+
406
+ describe '#preview_form' do
407
+ subject { target.preview_form }
408
+
409
+ it { is_expected.to be_a_kind_of(Kintone::Command::PreviewForm) }
410
+ end
411
+ end
412
+
413
+ context 'APIトークン認証の時' do
414
+ let(:user) { 'token-api' }
415
+ let(:password) { nil }
416
+
417
+ describe '#get' do
418
+ before(:each) do
419
+ stub_request(
420
+ :get,
421
+ 'https://www.example.com/k/v1/path'
422
+ )
423
+ .with(query: query, headers: { 'X-Cybozu-API-Token' => 'token-api' })
424
+ .to_return(
425
+ body: '{"abc":"def"}',
426
+ status: 200,
427
+ headers: { 'Content-type' => 'application/json' }
428
+ )
429
+ end
430
+
431
+ subject { target.get(path, params) }
432
+
433
+ let(:path) { '/k/v1/path' }
434
+
435
+ context 'with no params' do
436
+ subject { target.get(path) }
437
+
438
+ let(:query) { nil }
439
+
440
+ it { is_expected.to eq 'abc' => 'def' }
441
+ end
442
+ end
443
+
444
+ describe '#post' do
445
+ before(:each) do
446
+ stub_request(
447
+ :post,
448
+ 'https://www.example.com/k/v1/path'
449
+ )
450
+ .with(
451
+ headers: { 'X-Cybozu-API-Token' => 'token-api', 'Content-Type' => 'application/json' },
452
+ body: '{"p1":"abc","p2":"def"}'
453
+ )
454
+ .to_return(
455
+ body: '{"abc":"def"}',
456
+ status: 200,
457
+ headers: { 'Content-type' => 'application/json' }
458
+ )
459
+ end
460
+
461
+ subject { target.post(path, body) }
462
+ let(:path) { '/k/v1/path' }
463
+ let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
464
+
465
+ it { is_expected.to eq 'abc' => 'def' }
466
+ end
467
+
468
+ describe '#put' do
469
+ before(:each) do
470
+ stub_request(
471
+ :put,
472
+ 'https://www.example.com/k/v1/path'
473
+ )
474
+ .with(
475
+ headers: { 'X-Cybozu-API-Token' => 'token-api', 'Content-Type' => 'application/json' },
476
+ body: '{"p1":"abc","p2":"def"}'
477
+ )
478
+ .to_return(
479
+ body: '{"abc":"def"}',
480
+ status: 200,
481
+ headers: { 'Content-type' => 'application/json' }
482
+ )
483
+ end
484
+
485
+ subject { target.put(path, body) }
486
+ let(:path) { '/k/v1/path' }
487
+ let(:body) { { 'p1' => 'abc', 'p2' => 'def' } }
488
+
489
+ it { is_expected.to eq 'abc' => 'def' }
490
+ end
491
+
492
+ describe '#delete' do
493
+ before(:each) do
494
+ stub_request(
495
+ :delete,
496
+ 'https://www.example.com/k/v1/path'
497
+ )
498
+ .with(
499
+ body: { 'p1' => 'abc', 'p2' => 'def' }.to_json,
500
+ headers: { 'X-Cybozu-API-Token' => 'token-api' }
501
+ )
502
+ .to_return(
503
+ body: '{"abc":"def"}',
504
+ status: 200,
505
+ headers: { 'Content-type' => 'application/json' }
506
+ )
507
+ end
508
+
509
+ subject { target.delete(path, params) }
510
+ let(:path) { '/k/v1/path' }
511
+ let(:params) { { 'p1' => 'abc', 'p2' => 'def' } }
512
+
513
+ it { is_expected.to eq 'abc' => 'def' }
514
+ end
515
+
516
+ describe '#post_file' do
517
+ before(:each) do
518
+ stub_request(
519
+ :post,
520
+ 'https://www.example.com/k/v1/path'
521
+ )
522
+ .with(headers: { 'X-Cybozu-API-Token' => 'token-api' })
523
+ .to_return(
524
+ body: '{"fileKey":"abc"}',
525
+ status: 200,
526
+ headers: { 'Content-type' => 'application/json' }
527
+ )
528
+
529
+ expect(Faraday::UploadIO).to receive(:new).with(path, content_type, original_filename).and_return(attachment) # rubocop:disable Metrics/LineLength
530
+ end
531
+
532
+ subject { target.post_file(url, path, content_type, original_filename) }
533
+ let(:attachment) { double('attachment') }
534
+ let(:url) { '/k/v1/path' }
535
+ let(:path) { '/path/to/file.txt' }
536
+ let(:content_type) { 'text/plain' }
537
+ let(:original_filename) { 'fileName.txt' }
538
+
539
+ it { is_expected.to eq 'abc' }
540
+ end
541
+ end
542
+
543
+ describe '#new' do
544
+ let(:domain) { 'www.example.com' }
545
+ let(:user) { 'Administrator' }
546
+ let(:password) { 'cybozu' }
547
+
548
+ context 'ブロック引数が与えられていない時' do
549
+ subject { api.instance_variable_get(:@connection).proxy }
550
+ let(:api) { Kintone::Api.new(domain, user, password) }
551
+
552
+ it { is_expected.to be_nil }
553
+ end
554
+
555
+ context 'ブロック引数が与えられている時' do
556
+ subject { api.instance_variable_get(:@connection).proxy }
557
+ let(:api) do
558
+ Kintone::Api.new(domain, user, password) do |connection|
559
+ connection.proxy = 'http://127.0.0.1'
560
+ end
561
+ end
562
+
563
+ it { is_expected.to be_a Faraday::ProxyOptions }
564
+ end
565
+ end
566
+ end