acfs 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. metadata +8 -141
  3. data/CHANGELOG.md +0 -325
  4. data/LICENSE +0 -22
  5. data/README.md +0 -334
  6. data/acfs.gemspec +0 -37
  7. data/lib/acfs.rb +0 -49
  8. data/lib/acfs/adapter/base.rb +0 -26
  9. data/lib/acfs/adapter/typhoeus.rb +0 -60
  10. data/lib/acfs/collection.rb +0 -27
  11. data/lib/acfs/collections/paginatable.rb +0 -75
  12. data/lib/acfs/configuration.rb +0 -113
  13. data/lib/acfs/errors.rb +0 -106
  14. data/lib/acfs/global.rb +0 -99
  15. data/lib/acfs/location.rb +0 -78
  16. data/lib/acfs/middleware/base.rb +0 -20
  17. data/lib/acfs/middleware/json.rb +0 -27
  18. data/lib/acfs/middleware/logger.rb +0 -23
  19. data/lib/acfs/middleware/msgpack.rb +0 -30
  20. data/lib/acfs/middleware/print.rb +0 -21
  21. data/lib/acfs/middleware/serializer.rb +0 -39
  22. data/lib/acfs/operation.rb +0 -81
  23. data/lib/acfs/request.rb +0 -36
  24. data/lib/acfs/request/callbacks.rb +0 -50
  25. data/lib/acfs/resource.rb +0 -37
  26. data/lib/acfs/resource/attributes.rb +0 -268
  27. data/lib/acfs/resource/attributes/base.rb +0 -28
  28. data/lib/acfs/resource/attributes/boolean.rb +0 -37
  29. data/lib/acfs/resource/attributes/date_time.rb +0 -31
  30. data/lib/acfs/resource/attributes/dict.rb +0 -37
  31. data/lib/acfs/resource/attributes/float.rb +0 -31
  32. data/lib/acfs/resource/attributes/integer.rb +0 -27
  33. data/lib/acfs/resource/attributes/list.rb +0 -34
  34. data/lib/acfs/resource/attributes/string.rb +0 -24
  35. data/lib/acfs/resource/attributes/uuid.rb +0 -47
  36. data/lib/acfs/resource/dirty.rb +0 -35
  37. data/lib/acfs/resource/initialization.rb +0 -29
  38. data/lib/acfs/resource/loadable.rb +0 -33
  39. data/lib/acfs/resource/locatable.rb +0 -128
  40. data/lib/acfs/resource/operational.rb +0 -22
  41. data/lib/acfs/resource/persistence.rb +0 -257
  42. data/lib/acfs/resource/query_methods.rb +0 -264
  43. data/lib/acfs/resource/service.rb +0 -42
  44. data/lib/acfs/resource/validation.rb +0 -37
  45. data/lib/acfs/response.rb +0 -28
  46. data/lib/acfs/response/formats.rb +0 -25
  47. data/lib/acfs/response/status.rb +0 -31
  48. data/lib/acfs/rspec.rb +0 -11
  49. data/lib/acfs/runner.rb +0 -97
  50. data/lib/acfs/service.rb +0 -91
  51. data/lib/acfs/service/middleware.rb +0 -56
  52. data/lib/acfs/service/middleware/stack.rb +0 -63
  53. data/lib/acfs/singleton_resource.rb +0 -83
  54. data/lib/acfs/stub.rb +0 -172
  55. data/lib/acfs/util.rb +0 -20
  56. data/lib/acfs/version.rb +0 -14
  57. data/lib/acfs/yard.rb +0 -5
  58. data/spec/acfs/adapter/typhoeus_spec.rb +0 -28
  59. data/spec/acfs/collection_spec.rb +0 -155
  60. data/spec/acfs/configuration_spec.rb +0 -51
  61. data/spec/acfs/global_spec.rb +0 -137
  62. data/spec/acfs/location_spec.rb +0 -23
  63. data/spec/acfs/middleware/json_spec.rb +0 -63
  64. data/spec/acfs/middleware/msgpack_spec.rb +0 -60
  65. data/spec/acfs/operation_spec.rb +0 -10
  66. data/spec/acfs/request/callbacks_spec.rb +0 -46
  67. data/spec/acfs/request_spec.rb +0 -77
  68. data/spec/acfs/resource/attributes/boolean_spec.rb +0 -56
  69. data/spec/acfs/resource/attributes/date_time_spec.rb +0 -49
  70. data/spec/acfs/resource/attributes/dict_spec.rb +0 -75
  71. data/spec/acfs/resource/attributes/float_spec.rb +0 -59
  72. data/spec/acfs/resource/attributes/integer_spec.rb +0 -34
  73. data/spec/acfs/resource/attributes/list_spec.rb +0 -58
  74. data/spec/acfs/resource/attributes/uuid_spec.rb +0 -40
  75. data/spec/acfs/resource/attributes_spec.rb +0 -179
  76. data/spec/acfs/resource/dirty_spec.rb +0 -47
  77. data/spec/acfs/resource/initialization_spec.rb +0 -30
  78. data/spec/acfs/resource/loadable_spec.rb +0 -20
  79. data/spec/acfs/resource/locatable_spec.rb +0 -116
  80. data/spec/acfs/resource/persistance_spec.rb +0 -316
  81. data/spec/acfs/resource/query_methods_spec.rb +0 -541
  82. data/spec/acfs/resource/validation_spec.rb +0 -127
  83. data/spec/acfs/response/formats_spec.rb +0 -50
  84. data/spec/acfs/response/status_spec.rb +0 -69
  85. data/spec/acfs/runner_spec.rb +0 -97
  86. data/spec/acfs/service/middleware_spec.rb +0 -33
  87. data/spec/acfs/service_spec.rb +0 -46
  88. data/spec/acfs/singleton_resource_spec.rb +0 -15
  89. data/spec/acfs/stub_spec.rb +0 -343
  90. data/spec/acfs_spec.rb +0 -203
  91. data/spec/fixtures/config.yml +0 -14
  92. data/spec/spec_helper.rb +0 -41
  93. data/spec/support/hash.rb +0 -9
  94. data/spec/support/response.rb +0 -10
  95. data/spec/support/service.rb +0 -91
  96. data/spec/support/shared/find_callbacks.rb +0 -48
@@ -1,127 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Acfs::Resource::Validation do
4
- let(:params) { {name: 'john smith', age: 24} }
5
- let(:model) { MyUserWithValidations.new params }
6
-
7
- describe '#valid?' do
8
- context 'with valid attributes' do
9
- subject { model }
10
-
11
- it { should be_valid }
12
- end
13
-
14
- context 'with invalid attributes' do
15
- let(:params) { {name: 'invname'} }
16
- subject { model }
17
-
18
- it { should_not be_valid }
19
- end
20
-
21
- context 'on resource with service side errors' do
22
- before { Acfs::Stub.enable }
23
- after { Acfs::Stub.disable }
24
-
25
- before do
26
- Acfs::Stub.resource MyUser, :create, return: {errors: {name: ['can\'t be blank']}}, raise: 422
27
- end
28
-
29
- let(:params) { {} }
30
- let(:resource) { MyUser.create params }
31
- subject { resource }
32
-
33
- it { should_not be_valid }
34
-
35
- it 'should not override errors' do
36
- subject.valid?
37
- expect(subject.errors.to_hash).to eq(name: ['can\'t be blank'])
38
- end
39
- end
40
- end
41
-
42
- describe '#errors' do
43
- context 'with valid attributes' do
44
- let(:params) { {name: 'john smith', age: 24} }
45
- before { model.valid? }
46
- subject { model.errors }
47
-
48
- it { should be_empty }
49
- end
50
-
51
- context 'with invalid attributes' do
52
- let(:params) { {name: 'john'} }
53
- before { model.valid? }
54
- subject { model.errors }
55
-
56
- it { should_not be_empty }
57
- it { should have(2).items }
58
-
59
- it 'should contain a list of error messages' do
60
- expect(subject.to_hash).to eq age: ["can't be blank"], name: ['is invalid']
61
- end
62
- end
63
-
64
- context 'server side errors' do
65
- before { Acfs::Stub.enable }
66
- after { Acfs::Stub.disable }
67
-
68
- before do
69
- Acfs::Stub.resource MyUser, :create,
70
- with: {}, return: {errors: errors}, raise: 422
71
- end
72
-
73
- let(:params) { {} }
74
- let(:resource) { MyUser.create params }
75
- subject { resource.errors.to_hash }
76
-
77
- context 'with `field => [messages]` payload' do
78
- let(:errors) { {name: ['cannot be blank']} }
79
-
80
- it { is_expected.to eq(name: ['cannot be blank']) }
81
- end
82
-
83
- context 'with `field => message` payload' do
84
- let(:errors) { {name: 'cannot be blank'} }
85
-
86
- it { is_expected.to eq(name: ['cannot be blank']) }
87
- end
88
-
89
- context 'with `[messages]` payload' do
90
- let(:errors) { ['cannot be blank'] }
91
-
92
- it { is_expected.to eq(base: ['cannot be blank']) }
93
- end
94
- end
95
- end
96
-
97
- describe '#save!' do
98
- subject { -> { model.save! } }
99
- before { allow(model).to receive(:operation) }
100
-
101
- context 'with invalid attributes' do
102
- let(:params) { {name: 'john'} }
103
-
104
- it { expect { subject.call }.to raise_error Acfs::InvalidResource }
105
- end
106
-
107
- context 'on new resource' do
108
- it 'should validate with `create` context' do
109
- expect(model).to receive(:valid?).with(:create).and_call_original
110
- subject.call
111
- end
112
- end
113
-
114
- context 'on changed resource' do
115
- before { model.loaded! }
116
- let(:model) { super().tap {|m| m.id = 1 } }
117
-
118
- it 'should validate with `save` context' do
119
- expect(model).to receive(:valid?).with(:save).and_call_original
120
- subject.call
121
- end
122
- end
123
- end
124
-
125
- describe 'validates with context' do
126
- end
127
- end
@@ -1,50 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Acfs::Response::Formats do
4
- let(:status) { 200 }
5
- let(:mime_type) { 'application/unknown' }
6
- let(:headers) { {'Content-Type' => mime_type} }
7
- let(:request) { Acfs::Request.new 'fubar' }
8
- let(:body) { nil }
9
- let(:response) { Acfs::Response.new request, status: status, headers: headers, body: body }
10
-
11
- context 'without Content-Type header' do
12
- let(:headers) { {} }
13
-
14
- it "should fallback on 'text/plain'" do
15
- expect(response.content_type).to be == Mime[:text]
16
- end
17
- end
18
-
19
- context 'with JSON mimetype' do
20
- let(:mime_type) { 'application/json' }
21
-
22
- describe '#content_type' do
23
- it 'should return Mime::JSON' do
24
- expect(response.content_type).to be == Mime[:json]
25
- end
26
- end
27
-
28
- describe '#json?' do
29
- it 'should return true' do
30
- expect(response).to be_json
31
- end
32
- end
33
-
34
- context 'with charset option' do
35
- let(:mime_type) { 'application/json; charset=utf8' }
36
-
37
- describe '#content_type' do
38
- it 'should return Mime::JSON' do
39
- expect(response.content_type).to be == Mime[:json]
40
- end
41
- end
42
-
43
- describe '#json?' do
44
- it 'should return true' do
45
- expect(response).to be_json
46
- end
47
- end
48
- end
49
- end
50
- end
@@ -1,69 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Acfs::Response::Status do
4
- let(:status) { 200 }
5
- let(:mime_type) { 'application/unknown' }
6
- let(:headers) { {'Content-Type' => mime_type} }
7
- let(:request) { Acfs::Request.new 'fubar' }
8
- let(:body) { nil }
9
- let(:response) { Acfs::Response.new request, status: status, headers: headers, body: body }
10
-
11
- describe '#status_code alias #code' do
12
- context 'when given' do
13
- let(:status) { 200 }
14
-
15
- it 'should return status code' do
16
- expect(response.code).to be == 200
17
- expect(response.status_code).to be == 200
18
- end
19
- end
20
-
21
- context 'when nil' do
22
- let(:status) { nil }
23
-
24
- it 'should return zero' do
25
- expect(response.code).to be == 0
26
- expect(response.status_code).to be == 0
27
- end
28
- end
29
- end
30
-
31
- describe '#success?' do
32
- context 'with success status code' do
33
- let(:status) { 200 }
34
- it { expect(response).to be_success }
35
- end
36
-
37
- context 'with error status code' do
38
- let(:status) { 500 }
39
- it { expect(response).to_not be_success }
40
- end
41
-
42
- context 'with zero status code' do
43
- let(:status) { nil }
44
- it { expect(response).to_not be_success }
45
- end
46
- end
47
-
48
- describe '#modified?' do
49
- context 'with success status code' do
50
- let(:status) { 200 }
51
- it { expect(response).to be_modified }
52
- end
53
-
54
- context 'with not modified status code' do
55
- let(:status) { 304 }
56
- it { expect(response).to_not be_modified }
57
- end
58
-
59
- context 'with error status code' do
60
- let(:status) { 500 }
61
- it { expect(response).to be_modified }
62
- end
63
-
64
- context 'with zero status code' do
65
- let(:status) { nil }
66
- it { expect(response).to be_modified }
67
- end
68
- end
69
- end
@@ -1,97 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class NullAdapter < Acfs::Adapter::Base
4
- # Start processing queued requests.
5
- #
6
- def start
7
- end
8
-
9
- # Abort running and queued requests.
10
- #
11
- def abort
12
- end
13
-
14
- # Run request right now skipping queue.
15
- #
16
- def run(_)
17
- end
18
-
19
- # Enqueue request to be run later.
20
- #
21
- def queue(_)
22
- end
23
- end
24
-
25
- class NotificationCollector
26
- def call(*args)
27
- events << ActiveSupport::Notifications::Event.new(*args)
28
- end
29
-
30
- def events
31
- @events ||= []
32
- end
33
- end
34
-
35
- describe ::Acfs::Runner do
36
- let(:adapter) { ::NullAdapter.new }
37
- let(:runner) { ::Acfs::Runner.new adapter }
38
- let(:collector) { NotificationCollector.new }
39
- let(:collector2) { NotificationCollector.new }
40
-
41
- after do
42
- ::ActiveSupport::Notifications.notifier = \
43
- ::ActiveSupport::Notifications::Fanout.new
44
- end
45
-
46
- describe '#instrumentation' do
47
- before do
48
- ::ActiveSupport::Notifications.subscribe(/^acfs\.runner/, collector)
49
- ::ActiveSupport::Notifications.subscribe(/^acfs\.operation/, collector2)
50
- end
51
-
52
- describe '#process' do
53
- it 'should trigger event' do
54
- runner.process ::Acfs::Operation.new MyUser, :read, params: {id: 0}
55
- expect(collector.events).to have(1).items
56
- expect(collector2.events).to have(1).items
57
- end
58
- end
59
-
60
- describe '#run' do
61
- it 'should trigger event' do
62
- runner.run ::Acfs::Operation.new MyUser, :read, params: {id: 0}
63
- expect(collector.events).to have(1).items
64
- expect(collector2.events).to have(0).items
65
- end
66
- end
67
-
68
- describe '#enqueue' do
69
- it 'should trigger event' do
70
- runner.enqueue ::Acfs::Operation.new MyUser, :read, params: {id: 0}
71
- expect(collector.events).to have(1).items
72
- expect(collector2.events).to have(0).items
73
- end
74
- end
75
- end
76
-
77
- describe '#run' do
78
- before do
79
- expect_any_instance_of(UserService).to receive(:prepare).and_return nil
80
- end
81
- it 'it should not do requests when a middleware aborted' do
82
- expect(adapter).to_not receive :run
83
- runner.run ::Acfs::Operation.new MyUser, :read, params: {id: 0}
84
- end
85
- end
86
-
87
- describe '#enqueue' do
88
- before do
89
- expect_any_instance_of(UserService).to receive(:prepare).and_return nil
90
- end
91
- it 'it should not do requests when a middleware aborted' do
92
- expect(adapter).to_not receive :queue
93
- runner.enqueue ::Acfs::Operation.new MyUser, :read, params: {id: 0}
94
- runner.start
95
- end
96
- end
97
- end
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class TestMiddleware < Acfs::Middleware::Base
4
- end
5
-
6
- describe Acfs::Service::Middleware do
7
- let(:srv_class) { Class.new(Acfs::Service) }
8
- let(:options) { {} }
9
- let(:middleware) { TestMiddleware }
10
-
11
- describe '.use' do
12
- let(:options) { {abc: 'cde'} }
13
-
14
- it 'should add middleware to list' do
15
- srv_class.use middleware
16
-
17
- expect(srv_class.middleware).to include(middleware)
18
- end
19
-
20
- it 'should add middleware to stack' do
21
- srv_class.use middleware
22
-
23
- expect(srv_class.middleware.build(1)).to be_a(middleware)
24
- end
25
-
26
- it 'should instantiate middleware object' do
27
- expect(middleware).to receive(:new).with(anything, options)
28
-
29
- srv_class.use middleware, options
30
- srv_class.middleware.build
31
- end
32
- end
33
- end
@@ -1,46 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Acfs::Service do
4
- let(:srv_class) { Class.new(Acfs::Service) { identity :test } }
5
- let(:options) { {} }
6
- let(:service) { srv_class.new options }
7
-
8
- before do
9
- Acfs::Configuration.current.locate :test, ''
10
- end
11
-
12
- describe '#initialize' do
13
- let(:options) { {path: 'abc', key: 'value'} }
14
-
15
- it 'should set options' do
16
- expect(service.options).to eq(options)
17
- end
18
- end
19
-
20
- describe '#location' do
21
- let(:resource) { Class.new }
22
- before { allow(resource).to receive(:location_default_path, &proc{|_a, p| p}) }
23
-
24
- it 'should extract resource path name from given class' do
25
- expect(service.location(resource).to_s).to eq('/classes')
26
- end
27
-
28
- context 'with path options' do
29
- let(:options) { {path: 'abc'} }
30
-
31
- it 'should have custom resource path' do
32
- expect(service.location(resource).to_s).to eq('/abc')
33
- end
34
- end
35
- end
36
-
37
- describe '.base_url' do
38
- before do
39
- Acfs::Configuration.current.locate :test, 'http://abc.de/api/v1'
40
- end
41
-
42
- it 'should return configured URI for service' do
43
- expect(srv_class.base_url).to eq('http://abc.de/api/v1')
44
- end
45
- end
46
- end
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Acfs::SingletonResource do
4
- let(:model) { Single }
5
-
6
- describe '.find' do
7
- before do
8
- stub_request(:get, 'http://users.example.org/singles')
9
- .to_return response id: 1
10
- end
11
-
12
- let(:action) { ->(cb) { model.find(&cb) } }
13
- it_should_behave_like 'a query method with multi-callback support'
14
- end
15
- end
@@ -1,343 +0,0 @@
1
- require 'spec_helper'
2
-
3
- class SpecialCustomError < StandardError; end
4
-
5
- describe Acfs::Stub do
6
- let(:stub) { Class.new(Acfs::Stub) }
7
-
8
- before(:all) { Acfs::Stub.enable }
9
- after(:all) { Acfs::Stub.disable }
10
-
11
- before do
12
- Acfs::Stub.allow_requests = false
13
- end
14
-
15
- describe '#called?' do
16
- context 'without specified number' do
17
- let!(:operation) { Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32} }
18
-
19
- it 'should allow to test if stub was called' do
20
- MyUser.find 1
21
- Acfs.run
22
-
23
- expect(operation).to be_called
24
- end
25
-
26
- it 'should allow to test if stub was called a specific number of times' do
27
- MyUser.find 1
28
- Acfs.run
29
-
30
- MyUser.find 1
31
- Acfs.run
32
-
33
- expect(operation).to be_called 2.times
34
- end
35
- end
36
- end
37
-
38
- describe '.resource' do
39
- context 'with ambiguous stubs' do
40
- before do
41
- Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32}
42
- Acfs::Stub.resource MyUser, :read, with: {id: 1}, raise: :not_found
43
- end
44
-
45
- it 'should raise error' do
46
- MyUser.find 1
47
-
48
- expect { Acfs.run }.to raise_error(Acfs::AmbiguousStubError)
49
- end
50
- end
51
-
52
- context 'with read action' do
53
- before do
54
- Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32}
55
- Acfs::Stub.resource MyUser, :read, with: {id: 2}, raise: SpecialCustomError
56
- Acfs::Stub.resource MyUser, :read, with: {id: 3}, raise: :not_found
57
- end
58
-
59
- it 'should allow to stub resource reads' do
60
- user = MyUser.find 1
61
- Acfs.run
62
-
63
- expect(user.id).to be == 1
64
- expect(user.name).to be == 'John Smith'
65
- expect(user.age).to be == 32
66
- end
67
-
68
- context 'with error' do
69
- it 'should allow to raise errors' do
70
- MyUser.find 2
71
-
72
- expect { Acfs.run }.to raise_error(SpecialCustomError)
73
- end
74
-
75
- it 'should allow to raise symbolic errors' do
76
- MyUser.find 3
77
-
78
- expect { Acfs.run }.to raise_error(Acfs::ResourceNotFound)
79
- end
80
- end
81
-
82
- context 'with type parameter' do
83
- before do
84
- Acfs::Stub.resource Computer, :read, with: {id: 1}, return: {id: 1, type: 'PC'}
85
- Acfs::Stub.resource Computer, :read, with: {id: 2}, return: {id: 2, type: 'Mac'}
86
- end
87
-
88
- it 'should create inherited type' do
89
- pc = Computer.find 1
90
- mac = Computer.find 2
91
-
92
- Acfs.run
93
-
94
- expect(pc).to be_a PC
95
- expect(mac).to be_a Mac
96
- end
97
- end
98
- end
99
-
100
- context 'with create action' do
101
- before do
102
- Acfs::Stub.resource Session, :create, with: {ident: 'john@exmaple.org', password: 's3cr3t'}, return: {id: 'longhash', user: 1}
103
- Acfs::Stub.resource Session, :create, with: ->(op) { op.data[:ident] == 'john@exmaple.org' && op.data[:password] == 'wrong' }, raise: 422
104
- end
105
-
106
- it 'should allow stub resource creation' do
107
- session = Session.create! ident: 'john@exmaple.org', password: 's3cr3t'
108
-
109
- expect(session.id).to be == 'longhash'
110
- expect(session.user).to be == 1
111
- end
112
-
113
- it 'should allow to raise error' do
114
- expect do
115
- Session.create! ident: 'john@exmaple.org', password: 'wrong'
116
- end.to raise_error(::Acfs::InvalidResource)
117
- end
118
- end
119
-
120
- context 'with list action' do
121
- before do
122
- Acfs::Stub.resource MyUser, :list,
123
- return: [{id: 1, name: 'John Smith', age: 32}, {id: 2, name: 'Anon', age: 12}]
124
- end
125
-
126
- it 'should return collection' do
127
- users = MyUser.all
128
- Acfs.run
129
-
130
- expect(users).to have(2).items
131
- end
132
-
133
- it 'should return defined resources' do
134
- users = MyUser.all
135
- Acfs.run
136
-
137
- expect(users[0].id).to eq 1
138
- expect(users[1].id).to eq 2
139
- expect(users[0].name).to eq 'John Smith'
140
- expect(users[1].name).to eq 'Anon'
141
- end
142
-
143
- context 'with type parameter' do
144
- before do
145
- Acfs::Stub.resource Computer, :list,
146
- return: [{id: 1, type: 'PC'}, {id: 2, type: 'Mac'}]
147
- end
148
-
149
- it 'should create inherited type' do
150
- computers = Computer.all
151
- Acfs.run
152
-
153
- expect(computers.first).to be_a PC
154
- expect(computers.last).to be_a Mac
155
- end
156
- end
157
-
158
- context 'with header' do
159
- before do
160
- Acfs::Stub.resource Comment, :list,
161
- return: [{id: 1, text: 'Foo'}, {id: 2, text: 'Bar'}],
162
- headers: headers
163
- end
164
-
165
- let!(:comments) { Comment.all }
166
- let(:headers) do
167
- {
168
- 'X-Total-Pages' => '2',
169
- 'X-Total-Count' => '10'
170
- }
171
- end
172
- subject { Acfs.run; comments }
173
-
174
- its(:total_pages) { should eq 2 }
175
- its(:total_count) { should eq 10 }
176
- end
177
- end
178
-
179
- context 'with update action' do
180
- before do
181
- Acfs::Stub.resource MyUser, :read, with: {id: 1}, return: {id: 1, name: 'John Smith', age: 32}
182
- Acfs::Stub.resource MyUser, :update, with: {id: 1, name: 'John Smith', age: 22}, return: {id: 1, name: 'John Smith', age: 23}
183
- Acfs::Stub.resource MyUser, :update, with: {id: 1, name: 'John Smith', age: 0}, raise: 422
184
- end
185
-
186
- let!(:update_stub) do
187
- Acfs::Stub.resource MyUser, :update,
188
- with: {id: 1, name: 'Jane Smith'},
189
- return: ->(op) { Hash[op.data.map {|k, v| [k, v.to_s.upcase] }] }
190
- end
191
-
192
- it 'should allow stub resource update' do
193
- user = MyUser.find 1
194
- Acfs.run
195
-
196
- user.age = 22
197
- user.save!
198
-
199
- expect(user.age).to be == 23
200
- end
201
-
202
- it 'should allow to raise error' do
203
- user = MyUser.find 1
204
- Acfs.run
205
-
206
- user.age = 0
207
- user.save
208
-
209
- expect do
210
- user.save!
211
- end.to raise_error(::Acfs::InvalidResource)
212
- end
213
-
214
- it 'should match partial :with' do
215
- user = MyUser.find 1
216
- Acfs.run
217
-
218
- user.age = 5
219
- user.name = 'Jane Smith'
220
- user.save!
221
-
222
- expect(update_stub).to be_called
223
- end
224
-
225
- it 'should process response body' do
226
- user = MyUser.find 1
227
- Acfs.run
228
-
229
- user.age = 5
230
- user.name = 'Jane Smith'
231
- user.save!
232
-
233
- expect(user.name).to eq 'JANE SMITH'
234
- end
235
- end
236
-
237
- context 'with create action' do
238
- before do
239
- Acfs::Stub.resource MyUser, :create, with: {name: 'John Smith', age: 0}, raise: 422
240
- end
241
-
242
- it 'should allow to raise error' do
243
- expect do
244
- MyUser.create! name: 'John Smith', age: 0
245
- end.to raise_error(::Acfs::InvalidResource)
246
- end
247
- end
248
- end
249
-
250
- describe '.allow_requests=' do
251
- context 'when enabled' do
252
- before do
253
- Acfs::Stub.allow_requests = true
254
- stub_request(:get, 'http://users.example.org/users/2').to_return response(id: 2, name: 'John', age: 26)
255
- end
256
-
257
- it 'should allow real requests' do
258
- @user = MyUser.find 2
259
- expect { Acfs.run }.to_not raise_error
260
- end
261
- end
262
-
263
- context 'when disabled' do
264
- before do
265
- Acfs::Stub.allow_requests = false
266
- end
267
-
268
- it 'should not allow real requests' do
269
- @user = MyUser.find 2
270
- expect { Acfs.run }.to raise_error(Acfs::RealRequestsNotAllowedError)
271
- end
272
- end
273
- end
274
-
275
- describe 'accept?' do
276
- subject { stub.accept?(op) }
277
-
278
- context 'with a match in params' do
279
- let(:op) do
280
- double('operation').tap do |op|
281
- allow(op).to receive(:full_params).and_return(id: 1337, blub: 'abc')
282
- allow(op).to receive(:data).and_return({})
283
- end
284
- end
285
-
286
- let(:stub) { Acfs::Stub.resource MyUser, :read, with: {id: 1337} }
287
-
288
- it { is_expected.to be true }
289
- end
290
-
291
- context 'with a match in data' do
292
- let(:op) do
293
- double('operation').tap do |op|
294
- allow(op).to receive(:full_params).and_return({})
295
- allow(op).to receive(:data).and_return(id: 1337, blub: 'abc')
296
- end
297
- end
298
-
299
- let(:stub) { Acfs::Stub.resource MyUser, :read, with: {id: 1337} }
300
-
301
- it { is_expected.to be true }
302
- end
303
-
304
- context 'with no match in params nor data' do
305
- let(:op) do
306
- double('operation').tap do |op|
307
- allow(op).to receive(:full_params).and_return(id: 1337)
308
- allow(op).to receive(:data).and_return({})
309
- end
310
- end
311
-
312
- let(:stub) { Acfs::Stub.resource MyUser, :read, with: {abc: '123'} }
313
-
314
- it { is_expected.to be false }
315
- end
316
-
317
- context 'with a wrong match' do
318
- let(:op) do
319
- double('operation').tap do |op|
320
- allow(op).to receive(:full_params).and_return(id: 1337, blub: 'abc')
321
- allow(op).to receive(:data).and_return({})
322
- end
323
- end
324
-
325
- let(:stub) { Acfs::Stub.resource MyUser, :read, with: {id: 1337, blub: '123'} }
326
-
327
- it { is_expected.to be false }
328
- end
329
-
330
- context 'with a missing match' do
331
- let(:op) do
332
- double('operation').tap do |op|
333
- allow(op).to receive(:full_params).and_return(id: 1337, blub: 'abc')
334
- allow(op).to receive(:data).and_return({})
335
- end
336
- end
337
-
338
- let(:stub) { Acfs::Stub.resource MyUser, :read, with: {id: 1337, answer: 42} }
339
-
340
- it { is_expected.to be false }
341
- end
342
- end
343
- end