Skip to content

Commit 06423f9

Browse files
feat(count): add deactivate count support (#547)
1 parent f9312e5 commit 06423f9

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

app/controllers/forest_liana/resources_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ class ResourcesController < ForestLiana::ApplicationController
88
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found
99

1010
if Rails::VERSION::MAJOR < 4
11-
before_filter :find_resource
11+
before_filter :find_resource, except: :count
1212
else
13-
before_action :find_resource
13+
before_action :find_resource, except: :count
14+
end
15+
16+
def deactivate_count_response
17+
render serializer: nil, json: { meta: { count: 'deactivated '} }
1418
end
1519

1620
def index
@@ -54,6 +58,7 @@ def index
5458
end
5559

5660
def count
61+
find_resource
5762
begin
5863
checker = ForestLiana::PermissionsChecker.new(
5964
@resource,

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Rails.application.routes.draw do
22
namespace :forest do
33
post '/actions/test' => 'islands#test'
4+
get '/Owner/count' , to: 'owners#count'
45
end
56

67
mount ForestLiana::Engine => "/forest"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Forest::OwnersController < ForestLiana::ResourcesController
2+
def count
3+
if (params[:search])
4+
deactivate_count_response
5+
else
6+
params[:collection] = 'Owner'
7+
super
8+
end
9+
end
10+
end

spec/requests/count_spec.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
require 'rails_helper'
2+
3+
describe 'Requesting Owner', :type => :request do
4+
before(:each) do
5+
1.upto(10) do |i|
6+
Owner.create(name: "Owner #{i}")
7+
end
8+
end
9+
10+
after(:each) do
11+
Owner.destroy_all
12+
end
13+
14+
before(:each) do
15+
allow(ForestLiana::IpWhitelist).to receive(:retrieve) { true }
16+
allow(ForestLiana::IpWhitelist).to receive(:is_ip_whitelist_retrieved) { true }
17+
allow(ForestLiana::IpWhitelist).to receive(:is_ip_valid) { true }
18+
19+
allow_any_instance_of(ForestLiana::PermissionsChecker).to receive(:is_authorized?) { true }
20+
21+
allow(ForestLiana::ScopeManager).to receive(:fetch_scopes).and_return({})
22+
end
23+
24+
token = JWT.encode({
25+
id: 38,
26+
email: 'michael.kelso@that70.show',
27+
first_name: 'Michael',
28+
last_name: 'Kelso',
29+
team: 'Operations',
30+
rendering_id: 16,
31+
exp: Time.now.to_i + 2.weeks.to_i
32+
}, ForestLiana.auth_secret, 'HS256')
33+
34+
headers = {
35+
'Accept' => 'application/json',
36+
'Content-Type' => 'application/json',
37+
'Authorization' => "Bearer #{token}"
38+
}
39+
describe 'count' do
40+
params = {
41+
fields: { 'Owner' => 'id,name' },
42+
page: { 'number' => '1', 'size' => '10' },
43+
searchExtended: '0',
44+
sort: '-id',
45+
timezone: 'Europe/Paris'
46+
}
47+
48+
it 'should respond 200' do
49+
get '/forest/Owner/count', params: params, headers: headers
50+
expect(response.status).to eq(200)
51+
end
52+
53+
it 'should equal to 10' do
54+
get '/forest/Owner/count', params: params, headers: headers
55+
expect(response.status).to eq(200)
56+
end
57+
end
58+
59+
describe 'deactivate_count_response' do
60+
params = {
61+
fields: { 'Product' => 'id,name' },
62+
page: { 'number' => '1', 'size' => '10' },
63+
search: 'foo',
64+
searchExtended: '0',
65+
sort: '-id',
66+
timezone: 'Europe/Paris'
67+
}
68+
69+
it 'should respond 200' do
70+
get '/forest/Owner/count', params: params, headers: headers
71+
expect(response.status).to eq(200)
72+
end
73+
74+
it 'should equal to deactivated response' do
75+
get '/forest/Owner/count', params: params, headers: headers
76+
expect(response.body).to eq('{"meta":{"count":"deactivated "}}')
77+
end
78+
end
79+
end

0 commit comments

Comments
 (0)