json-crud-api 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json-crud-api/api.rb +5 -65
- data/lib/json-crud-api/crud.rb +49 -51
- data/lib/json-crud-api/json_errors.rb +45 -0
- data/lib/json-crud-api/json_payload.rb +21 -0
- data/lib/json-crud-api/presenter.rb +26 -18
- data/lib/json-crud-api/session.rb +24 -0
- data/lib/json-crud-api.rb +5 -2
- data/spec/spec_helper.rb +9 -0
- data/spec/unit/api_spec.rb +11 -0
- data/spec/unit/auth_client_spec.rb +30 -30
- data/spec/unit/crud_spec.rb +331 -0
- data/spec/unit/json_errors_spec.rb +120 -0
- data/spec/unit/json_payload_spec.rb +65 -0
- data/spec/unit/presenter_spec.rb +88 -67
- data/spec/unit/service_spec.rb +54 -54
- data/spec/unit/session_spec.rb +120 -0
- metadata +22 -65
- data/spec/helper.rb +0 -12
- data/spec/helpers_spec.rb +0 -3
data/spec/unit/presenter_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe JsonCrudApi::Presenter do
|
4
4
|
before(:each) do
|
@@ -13,9 +13,9 @@ describe JsonCrudApi::Presenter do
|
|
13
13
|
|
14
14
|
describe '#initialize' do
|
15
15
|
it 'should inject dependencies correctly' do
|
16
|
-
@presenter.model.
|
17
|
-
@presenter.include.
|
18
|
-
@presenter.exclude.
|
16
|
+
expect(@presenter.model).to be @mock_model
|
17
|
+
expect(@presenter.include).to eq({ :no_operation => 10 })
|
18
|
+
expect(@presenter.exclude).to eq({ :no_operation => 11 })
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'should throw an exception if model is not set' do
|
@@ -24,195 +24,216 @@ describe JsonCrudApi::Presenter do
|
|
24
24
|
|
25
25
|
describe '#render' do
|
26
26
|
it 'should output a single property in data based on model properties' do
|
27
|
-
@mock_model.
|
27
|
+
expect(@mock_model).to receive(:properties)
|
28
28
|
.and_return([OpenStruct.new(:name => :one)])
|
29
29
|
data = OpenStruct.new(:one => "Test")
|
30
30
|
|
31
|
-
@presenter.render(data).
|
31
|
+
expect(@presenter.render(data)).to eq({ :one => "Test" })
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should not return data properties that do not have model properties' do
|
35
|
-
@mock_model.
|
35
|
+
expect(@mock_model).to receive(:properties)
|
36
36
|
.and_return([OpenStruct.new(:name => :one)])
|
37
37
|
data = OpenStruct.new(:one => "YES", :two => "OK")
|
38
38
|
|
39
|
-
@presenter.render(data).
|
39
|
+
expect(@presenter.render(data)).to eq({ :one => "YES" })
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'should return nil for model properties that do not have data' do
|
43
|
-
@mock_model.
|
43
|
+
expect(@mock_model).to receive(:properties)
|
44
44
|
.and_return([
|
45
45
|
OpenStruct.new(:name => :one),
|
46
46
|
OpenStruct.new(:name => :two),
|
47
47
|
])
|
48
48
|
data = OpenStruct.new(:two => "OK")
|
49
49
|
|
50
|
-
@presenter.render(data).
|
50
|
+
expect(@presenter.render(data)).to eq({ :one => nil, :two => 'OK' })
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'should call itself when supplied with an array and return an array of the results' do
|
54
|
-
@mock_model.
|
54
|
+
expect(@mock_model).to receive(:properties).and_return(
|
55
55
|
[OpenStruct.new(:name => :one)]
|
56
|
-
|
56
|
+
)
|
57
|
+
|
57
58
|
data = [OpenStruct.new(:one => "Test"), OpenStruct.new(:one => "TEST2")]
|
58
59
|
|
59
|
-
@presenter.render(data).
|
60
|
+
expect(@presenter.render(data)).to eq([{ :one => "Test" }, { :one => "TEST2" }])
|
60
61
|
end
|
61
62
|
|
62
63
|
it 'should include render:all properties' do
|
63
64
|
@presenter.include = { :render => { :all => [:five] } }
|
64
|
-
@mock_model.
|
65
|
+
expect(@mock_model).to receive(:properties).and_return(
|
65
66
|
[OpenStruct.new(:name => :two)]
|
66
|
-
|
67
|
+
)
|
68
|
+
|
67
69
|
data = OpenStruct.new(:two => "Two",:five=>"Five")
|
68
70
|
|
69
|
-
@presenter.render(data).
|
71
|
+
expect(@presenter.render(data)).to eq({ :two => "Two", :five=>"Five" })
|
70
72
|
end
|
71
73
|
|
72
74
|
it 'should include global:all properties' do
|
73
75
|
@presenter.include = { :all => [:five] }
|
74
|
-
@mock_model.
|
75
|
-
[
|
76
|
-
|
76
|
+
expect(@mock_model).to receive(:properties).and_return(
|
77
|
+
[OpenStruct.new(:name => :two)]
|
78
|
+
)
|
79
|
+
|
77
80
|
data = OpenStruct.new(:two => "Two",:five=>"Five")
|
78
81
|
|
79
|
-
@presenter.render(data).
|
82
|
+
expect(@presenter.render(data)).to eq({ :two => "Two", :five=>"Five" })
|
80
83
|
end
|
81
84
|
|
82
85
|
it 'should include global:operation properties' do
|
83
86
|
@presenter.include = { :test => [:five] }
|
84
|
-
@mock_model.
|
85
|
-
[
|
86
|
-
|
87
|
+
expect(@mock_model).to receive(:properties).and_return(
|
88
|
+
[OpenStruct.new(:name => :two)]
|
89
|
+
)
|
90
|
+
|
87
91
|
data = OpenStruct.new(:two => "Two",:five=>"Five")
|
88
92
|
|
89
|
-
@presenter.render(data, :test).
|
93
|
+
expect(@presenter.render(data, :test)).to eq({ :two => "Two", :five=>"Five" })
|
90
94
|
end
|
91
95
|
|
92
96
|
it 'should include render:operation properties' do
|
93
97
|
@presenter.include = { :render => { :test => [:five] } }
|
94
|
-
@mock_model.
|
95
|
-
[
|
96
|
-
|
98
|
+
expect(@mock_model).to receive(:properties).and_return(
|
99
|
+
[OpenStruct.new(:name => :two)]
|
100
|
+
)
|
101
|
+
|
97
102
|
data = OpenStruct.new(:two => "Two",:five=>"Five")
|
98
103
|
|
99
|
-
@presenter.render(data, :test).
|
104
|
+
expect(@presenter.render(data, :test)).to eq({ :two => "Two", :five=>"Five" })
|
100
105
|
end
|
101
106
|
|
102
107
|
it 'should exclude render:all properties' do
|
103
108
|
@presenter.exclude = { :render => { :all => [:one] } }
|
104
|
-
@mock_model.
|
105
|
-
[OpenStruct.new(:name => :
|
106
|
-
|
109
|
+
expect(@mock_model).to receive(:properties).and_return(
|
110
|
+
[OpenStruct.new(:name => :two)]
|
111
|
+
)
|
112
|
+
|
107
113
|
data = OpenStruct.new(:one => "Test",:two => "Two")
|
108
114
|
|
109
|
-
@presenter.render(data).
|
115
|
+
expect(@presenter.render(data)).to eq({ :two => "Two" })
|
110
116
|
end
|
111
117
|
|
112
118
|
it 'should exclude global:all properties' do
|
113
119
|
@presenter.exclude = { :all => [:one] }
|
114
|
-
@mock_model.
|
120
|
+
expect(@mock_model).to receive(:properties).and_return(
|
115
121
|
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two)]
|
116
|
-
|
122
|
+
)
|
123
|
+
|
117
124
|
data = OpenStruct.new(:one => "Test",:two => "Two")
|
118
125
|
|
119
|
-
@presenter.render(data).
|
126
|
+
expect(@presenter.render(data)).to eq({ :two => "Two" })
|
120
127
|
end
|
121
128
|
|
122
129
|
it 'should exclude render:operation properties' do
|
123
130
|
@presenter.exclude = { :render => { :test => [:one] } }
|
124
|
-
@mock_model.
|
131
|
+
expect(@mock_model).to receive(:properties).and_return(
|
125
132
|
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two)]
|
126
|
-
|
133
|
+
)
|
134
|
+
|
127
135
|
data = OpenStruct.new(:one => "Test",:two => "Two")
|
128
136
|
|
129
|
-
@presenter.render(data, :test).
|
137
|
+
expect(@presenter.render(data, :test)).to eq({ :two => "Two" })
|
130
138
|
end
|
131
139
|
|
132
140
|
it 'should exclude global:operation properties' do
|
133
141
|
@presenter.exclude = { :test => [:one] }
|
134
|
-
@mock_model.
|
142
|
+
expect(@mock_model).to receive(:properties).and_return(
|
135
143
|
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two)]
|
136
|
-
|
144
|
+
)
|
145
|
+
|
137
146
|
data = OpenStruct.new(:one => "Test",:two => "Two")
|
138
147
|
|
139
|
-
@presenter.render(data, :test).
|
148
|
+
expect(@presenter.render(data, :test)).to eq({ :two => "Two" })
|
140
149
|
end
|
141
150
|
|
142
151
|
it 'should exclude combinations of render:all and render:operation properties' do
|
143
152
|
@presenter.exclude = { :render => { :all => [:two] , :test => [:one] } }
|
144
|
-
@mock_model.
|
153
|
+
expect(@mock_model).to receive(:properties).and_return(
|
145
154
|
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two), OpenStruct.new(:name => :three)]
|
146
|
-
|
155
|
+
)
|
147
156
|
data = OpenStruct.new(:one => "Test",:two => "Two",:three => "Three")
|
148
157
|
|
149
|
-
@presenter.render(data, :test).
|
158
|
+
expect(@presenter.render(data, :test)).to eq({ :three => "Three" })
|
150
159
|
end
|
151
160
|
end
|
152
161
|
|
153
162
|
describe '#parse' do
|
154
163
|
it 'should output a single property in data based on model properties' do
|
155
|
-
@mock_model.
|
164
|
+
expect(@mock_model).to receive(:properties).and_return(
|
156
165
|
[OpenStruct.new(:name => :one)]
|
157
|
-
|
166
|
+
)
|
158
167
|
data = { :one => 1 }
|
159
|
-
@presenter.parse(data).
|
168
|
+
expect(@presenter.parse(data)).to eq({:one => 1})
|
160
169
|
end
|
161
170
|
|
162
171
|
it 'should not output properties of data that are not in model properties' do
|
163
|
-
@mock_model.
|
172
|
+
expect(@mock_model).to receive(:properties).and_return(
|
164
173
|
[OpenStruct.new(:name => :one)]
|
165
|
-
|
174
|
+
)
|
166
175
|
data = { :one => 1, :two => 2 }
|
167
|
-
@presenter.parse(data).
|
176
|
+
expect(@presenter.parse(data)).to eq({:one => 1})
|
168
177
|
end
|
169
178
|
|
170
179
|
it 'should not output model properties with no data property' do
|
171
|
-
@mock_model.
|
172
|
-
[OpenStruct.new(:name => :one)]
|
173
|
-
|
174
|
-
end
|
180
|
+
expect(@mock_model).to receive(:properties).and_return(
|
181
|
+
[OpenStruct.new(:name => :one),OpenStruct.new(:name => :two)]
|
182
|
+
)
|
175
183
|
data = { :two => 2 }
|
176
|
-
@presenter.parse(data).
|
184
|
+
expect(@presenter.parse(data)).to eq({:two => 2})
|
177
185
|
end
|
178
186
|
|
179
187
|
it 'should call itself when supplied with an array and return an array of the results' do
|
180
|
-
@mock_model.
|
188
|
+
expect(@mock_model).to receive(:properties).and_return(
|
181
189
|
[OpenStruct.new(:name => :one)]
|
182
|
-
|
190
|
+
)
|
191
|
+
|
183
192
|
data = [{ :one => 1 }]
|
184
193
|
|
185
|
-
@presenter.parse(data).
|
194
|
+
expect(@presenter.parse(data)).to eq([{ :one => 1 }])
|
186
195
|
end
|
187
196
|
|
188
197
|
it 'should exclude parse:all properties' do
|
189
198
|
@presenter.exclude = { :parse => { :all => [:one] } }
|
190
|
-
@mock_model.
|
199
|
+
expect(@mock_model).to receive(:properties).and_return(
|
191
200
|
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two)]
|
192
|
-
|
201
|
+
)
|
202
|
+
|
193
203
|
data = OpenStruct.new(:one => "Test",:two => "Two")
|
194
204
|
|
195
|
-
@presenter.parse(data).
|
205
|
+
expect(@presenter.parse(data)).to eq({ :two => "Two" })
|
196
206
|
end
|
197
207
|
|
198
208
|
it 'should exclude parse:operation properties' do
|
199
209
|
@presenter.exclude = { :parse => { :test => [:one] } }
|
200
|
-
@mock_model.
|
210
|
+
expect(@mock_model).to receive(:properties).and_return(
|
201
211
|
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two)]
|
202
|
-
|
212
|
+
)
|
213
|
+
|
203
214
|
data = OpenStruct.new(:one => "Test",:two => "Two")
|
204
215
|
|
205
|
-
@presenter.parse(data, :test).
|
216
|
+
expect(@presenter.parse(data, :test)).to eq({ :two => "Two" })
|
206
217
|
end
|
207
218
|
|
208
219
|
it 'should exclude combinations of parse:all and parse:operation properties' do
|
209
220
|
@presenter.exclude = { :parse => { :all => [:two] , :test => [:one] } }
|
210
|
-
@mock_model.
|
221
|
+
expect(@mock_model).to receive(:properties).and_return(
|
211
222
|
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two), OpenStruct.new(:name => :three)]
|
212
|
-
|
223
|
+
)
|
213
224
|
data = OpenStruct.new(:one => "Test",:two => "Two",:three => "Three")
|
214
225
|
|
215
|
-
@presenter.parse(data, :test).
|
226
|
+
expect(@presenter.parse(data, :test)).to eq({ :three => "Three" })
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'should not supply keys that are not in the supplied data' do
|
230
|
+
expect(@mock_model).to receive(:properties).and_return(
|
231
|
+
[OpenStruct.new(:name => :one), OpenStruct.new(:name => :two), OpenStruct.new(:name => :three)]
|
232
|
+
)
|
233
|
+
|
234
|
+
data = OpenStruct.new(:one => "Test",:two => "Two")
|
235
|
+
|
236
|
+
expect(@presenter.parse(data, :test).keys).to_not include :three
|
216
237
|
end
|
217
238
|
end
|
218
239
|
end
|
data/spec/unit/service_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe JsonCrudApi::Service do
|
4
4
|
before(:each) do
|
@@ -15,215 +15,215 @@ describe JsonCrudApi::Service do
|
|
15
15
|
|
16
16
|
describe '#initialize' do
|
17
17
|
it 'should inject dependencies correctly' do
|
18
|
-
@service.log_service.
|
19
|
-
@service.model.
|
20
|
-
@service.scope_map.
|
18
|
+
expect(@service.log_service).to be @mock_log
|
19
|
+
expect(@service.model).to be @mock_model
|
20
|
+
expect(@service.scope_map).to be @mock_map
|
21
21
|
end
|
22
22
|
|
23
23
|
it 'should initialize user and scopes to nil' do
|
24
|
-
@service.user.
|
25
|
-
@service.user_scopes.
|
24
|
+
expect(@service.user).to be nil
|
25
|
+
expect(@service.user_scopes).to be nil
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
describe '#create' do
|
30
30
|
it 'should call create on model with params' do
|
31
31
|
params = { :one => 'one', :two => 'two' }
|
32
|
-
@mock_model.
|
33
|
-
@service.create(params).
|
32
|
+
expect(@mock_model).to receive(:create).with(params).and_return(2)
|
33
|
+
expect(@service.create(params)).to eq 2
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
describe '#exists?' do
|
38
38
|
it 'should call all on model with correct id' do
|
39
39
|
query_object = OpenStruct.new :count => 1
|
40
|
-
@mock_model.
|
40
|
+
expect(@mock_model).to receive(:key)
|
41
41
|
.and_return(OpenStruct.new(:first => OpenStruct.new(:name => :id)))
|
42
|
-
@mock_model.
|
42
|
+
expect(@mock_model).to receive(:all).with(:id => 3)
|
43
43
|
.and_return(query_object)
|
44
|
-
@service.exists?(3).
|
44
|
+
expect(@service.exists?(3)).to eq true
|
45
45
|
end
|
46
46
|
|
47
47
|
it 'should return false when count is zero' do
|
48
48
|
query_object = OpenStruct.new :count => 0
|
49
|
-
@mock_model.
|
49
|
+
expect(@mock_model).to receive(:key)
|
50
50
|
.and_return(OpenStruct.new(:first => OpenStruct.new(:name => :id)))
|
51
|
-
@mock_model.
|
51
|
+
expect(@mock_model).to receive(:all).with(:id => 3)
|
52
52
|
.and_return(query_object)
|
53
|
-
@service.exists?(3).
|
53
|
+
expect(@service.exists?(3)).to eq false
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'should return true when count is one' do
|
57
57
|
query_object = OpenStruct.new :count => 1
|
58
|
-
@mock_model.
|
58
|
+
expect(@mock_model).to receive(:key)
|
59
59
|
.and_return(OpenStruct.new(:first => OpenStruct.new(:name => :id)))
|
60
|
-
@mock_model.
|
60
|
+
expect(@mock_model).to receive(:all).with(:id => 3)
|
61
61
|
.and_return(query_object)
|
62
|
-
@service.exists?(3).
|
62
|
+
expect(@service.exists?(3)).to eq true
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'should return true when count is more than one' do
|
66
66
|
query_object = OpenStruct.new :count => 2
|
67
|
-
@mock_model.
|
67
|
+
expect(@mock_model).to receive(:key)
|
68
68
|
.and_return(OpenStruct.new(:first => OpenStruct.new(:name => :id)))
|
69
|
-
@mock_model.
|
69
|
+
expect(@mock_model).to receive(:all).with(:id => 3)
|
70
70
|
.and_return(query_object)
|
71
|
-
@service.exists?(3).
|
71
|
+
expect(@service.exists?(3)).to eq true
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
75
|
describe '#get_all' do
|
76
76
|
it 'should call all on model and return output' do
|
77
|
-
@mock_model.
|
78
|
-
@service.get_all.
|
77
|
+
expect(@mock_model).to receive(:all).and_return(67)
|
78
|
+
expect(@service.get_all).to eq 67
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
82
|
describe '#get' do
|
83
83
|
it 'should call first on model with correct id and return result' do
|
84
|
-
@mock_model.
|
84
|
+
expect(@mock_model).to receive(:key)
|
85
85
|
.and_return(OpenStruct.new(:first => OpenStruct.new(:name => :id)))
|
86
|
-
@mock_model.
|
87
|
-
@service.get(8).
|
86
|
+
expect(@mock_model).to receive(:first).with({:id=>8}).and_return(123)
|
87
|
+
expect(@service.get(8)).to eq 123
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
91
|
describe '#update' do
|
92
92
|
it 'should call get on service with correct id' do
|
93
|
-
@service.
|
93
|
+
expect(@service).to receive(:get).with(5).and_return(nil)
|
94
94
|
@service.update(5,nil)
|
95
95
|
end
|
96
96
|
|
97
97
|
it 'should return false if get returns nil' do
|
98
|
-
@service.
|
99
|
-
@service.update(5,nil).
|
98
|
+
expect(@service).to receive(:get).with(5).and_return(nil)
|
99
|
+
expect(@service.update(5,nil)).to eq false
|
100
100
|
end
|
101
101
|
|
102
102
|
it 'should call update on record with correct params' do
|
103
103
|
params = { :one => 'one', :two => 'two' }
|
104
104
|
record = double('entity')
|
105
|
-
@service.
|
105
|
+
expect(@service).to receive(:get).with(5)
|
106
106
|
.and_return(record)
|
107
|
-
record.
|
107
|
+
expect(record).to receive(:update).with(params)
|
108
108
|
.and_return(789)
|
109
|
-
@service.update(5,params).
|
109
|
+
expect(@service.update(5,params)).to eq 789
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
113
113
|
describe '#delete' do
|
114
114
|
it 'should call get on service with correct id' do
|
115
|
-
@service.
|
115
|
+
expect(@service).to receive(:get).with(5).and_return(nil)
|
116
116
|
@service.delete(5)
|
117
117
|
end
|
118
118
|
|
119
119
|
it 'should return false if get returns nil' do
|
120
|
-
@service.
|
121
|
-
@service.delete(5).
|
120
|
+
expect(@service).to receive(:get).with(5).and_return(nil)
|
121
|
+
expect(@service.delete(5)).to eq false
|
122
122
|
end
|
123
123
|
|
124
124
|
it 'should call delete on record' do
|
125
125
|
record = double('entity')
|
126
|
-
@service.
|
126
|
+
expect(@service).to receive(:get).with(5)
|
127
127
|
.and_return(record)
|
128
|
-
record.
|
129
|
-
@service.delete(5).
|
128
|
+
expect(record).to receive(:destroy).and_return(109)
|
129
|
+
expect(@service.delete(5)).to eq 109
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
133
|
describe '#set_user' do
|
134
134
|
it 'should set user in service to param' do
|
135
135
|
@service.set_user(nil)
|
136
|
-
@service.user.
|
136
|
+
expect(@service.user).to eq nil
|
137
137
|
end
|
138
138
|
|
139
139
|
it 'should not call set_user_scopes if user is nil' do
|
140
|
-
@service.
|
140
|
+
expect(@service).not_to receive(:set_user_scopes)
|
141
141
|
@service.set_user(nil)
|
142
|
-
@service.user.
|
142
|
+
expect(@service.user).to eq nil
|
143
143
|
end
|
144
144
|
|
145
145
|
it 'should call set_user_scopes if user is not' do
|
146
146
|
user = { :scopes => [1,2] }
|
147
|
-
@service.
|
147
|
+
expect(@service).to receive(:set_user_scopes).with([1,2])
|
148
148
|
@service.set_user(user)
|
149
|
-
@service.user.
|
149
|
+
expect(@service.user).to eq user
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
153
153
|
describe '#set_user_scopes' do
|
154
154
|
it 'should set user_scopes in service to param' do
|
155
155
|
@service.set_user_scopes(nil)
|
156
|
-
@service.user_scopes.
|
156
|
+
expect(@service.user_scopes).to eq nil
|
157
157
|
|
158
158
|
@service.set_user_scopes(234234)
|
159
|
-
@service.user_scopes.
|
159
|
+
expect(@service.user_scopes).to eq 234234
|
160
160
|
end
|
161
161
|
end
|
162
162
|
|
163
163
|
describe '#user_authorized_for?' do
|
164
164
|
it 'should return true if scope_map is nil' do
|
165
165
|
@service.scope_map = nil
|
166
|
-
@service.user_authorized_for?(:one).
|
166
|
+
expect(@service.user_authorized_for?(:one)).to be true
|
167
167
|
end
|
168
168
|
|
169
169
|
it 'should return true if scope_map is not nil but no map for operation' do
|
170
170
|
@service.scope_map = { :two => 'TWO' }
|
171
|
-
@service.user_authorized_for?(:one).
|
171
|
+
expect(@service.user_authorized_for?(:one)).to be true
|
172
172
|
end
|
173
173
|
|
174
174
|
it 'should return false if user is nil' do
|
175
175
|
@service.scope_map = { :two => 'TWO' }
|
176
176
|
@service.user = nil
|
177
|
-
@service.user_authorized_for?(:two).
|
177
|
+
expect(@service.user_authorized_for?(:two)).to be false
|
178
178
|
end
|
179
179
|
|
180
180
|
it 'should return false if user has nil scopes' do
|
181
181
|
@service.scope_map = { :two => 'TWO' }
|
182
182
|
@service.user = { :name => "Tom" }
|
183
183
|
@service.user_scopes = nil
|
184
|
-
@service.user_authorized_for?(:two).
|
184
|
+
expect(@service.user_authorized_for?(:two)).to be false
|
185
185
|
end
|
186
186
|
|
187
187
|
it 'should return false if user has empty scopes' do
|
188
188
|
@service.scope_map = { :two => 'TWO' }
|
189
189
|
@service.user = { :name => "Tom" }
|
190
190
|
@service.user_scopes = []
|
191
|
-
@service.user_authorized_for?(:two).
|
191
|
+
expect(@service.user_authorized_for?(:two)).to be false
|
192
192
|
end
|
193
193
|
|
194
194
|
it 'should return true if scope map exists in user scopes' do
|
195
195
|
@service.scope_map = { :two => 'FIVE'}
|
196
196
|
@service.user = { :name => "Tom" }
|
197
197
|
@service.user_scopes = [ 'ONE', 'TWO', 'FIVE']
|
198
|
-
@service.user_authorized_for?(:two).
|
198
|
+
expect(@service.user_authorized_for?(:two)).to be true
|
199
199
|
end
|
200
200
|
|
201
201
|
it 'should return false if scope map does not exist in user scopes' do
|
202
202
|
@service.scope_map = { :two => 'SEVEN'}
|
203
203
|
@service.user = { :name => "Tom" }
|
204
204
|
@service.user_scopes = [ 'ONE', 'TWO', 'FIVE']
|
205
|
-
@service.user_authorized_for?(:two).
|
205
|
+
expect(@service.user_authorized_for?(:two)).to be false
|
206
206
|
end
|
207
207
|
|
208
208
|
it 'should return true if scope map is array and shares one scope with user' do
|
209
209
|
@service.scope_map = { :two => ['TWO'] }
|
210
210
|
@service.user = { :name => "Tom" }
|
211
211
|
@service.user_scopes = [ 'ONE', 'TWO', 'THREE']
|
212
|
-
@service.user_authorized_for?(:two).
|
212
|
+
expect(@service.user_authorized_for?(:two)).to be true
|
213
213
|
end
|
214
214
|
|
215
215
|
it 'should return true if scope map is array and shares more than one scope with user' do
|
216
216
|
@service.scope_map = { :two => ['TWO','THREE'] }
|
217
217
|
@service.user = { :name => "Tom" }
|
218
218
|
@service.user_scopes = [ 'ONE', 'TWO', 'THREE']
|
219
|
-
@service.user_authorized_for?(:two).
|
219
|
+
expect(@service.user_authorized_for?(:two)).to be true
|
220
220
|
end
|
221
221
|
|
222
222
|
it 'should return false if scope map is array and does not share scopes with user' do
|
223
223
|
@service.scope_map = { :two => ['FOUR'] }
|
224
224
|
@service.user = { :name => "Tom" }
|
225
225
|
@service.user_scopes = [ 'ONE', 'TWO', 'THREE']
|
226
|
-
@service.user_authorized_for?(:two).
|
226
|
+
expect(@service.user_authorized_for?(:two)).to be false
|
227
227
|
end
|
228
228
|
end
|
229
229
|
end
|