Skip to content

Commit

Permalink
better implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mose committed Jan 28, 2016
1 parent c432d3b commit be6949d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
12 changes: 6 additions & 6 deletions app/apiv1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,41 @@ def check_authorization
end
end

get '/:base/nodes' do |base|
get %r{^/?([-_\.a-zA-Z0-9]+)?/nodes} do |base|
check_authorization
hieracles_config = prepare_config(base)
json Hieracles::Registry.nodes(hieracles_config)
end

get '/:base/node/:n/info' do |base, node|
get %r{^/?([-_\.a-zA-Z0-9]+)?/node/([-_\.a-zA-Z0-9]+)/info} do |base, node|
check_authorization
hieracles_config = prepare_config(base)
node = Hieracles::Node.new(node, hieracles_config)
json node.info
end

get '/:base/node/:n/params' do |base, node|
get %r{^/?([-_\.a-zA-Z0-9]+)?/node/([-_\.a-zA-Z0-9]+)/params} do |base, node|
check_authorization
hieracles_config = prepare_config(base)
node = Hieracles::Node.new(node, hieracles_config)
json node.params
end

get '/:base/node/:n/allparams' do |base, node|
get %r{^/?([-_\.a-zA-Z0-9]+)?/node/([-_\.a-zA-Z0-9]+)/allparams} do |base, node|
check_authorization
hieracles_config = prepare_config(base)
node = Hieracles::Node.new(node, hieracles_config)
json node.params(false)
end

get '/:base/node/:n' do |base, node|
get %r{^/?([-_\.a-zA-Z0-9]+)?/node/([-_\.a-zA-Z0-9]+)$} do |base, node|
check_authorization
hieracles_config = prepare_config(base)
node = Hieracles::Node.new(node, hieracles_config)
json node.params
end

get '/:base/farms' do |base|
get %r{^/?([-_\.a-zA-Z0-9]+)?/farms} do |base|
check_authorization
hieracles_config = prepare_config(base)
json Hieracles::Registry.farms(hieracles_config)
Expand Down
30 changes: 15 additions & 15 deletions spec/app/apiv1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
end

context "when no creds" do
describe "GET /v1/puppet/nodes" do
describe "GET /v1/nodes" do
let(:expected) { 'http://example.org/v1/not_logged' }
before do
get '/puppet/nodes'
get '/nodes'
end
it { expect(last_response).not_to be_ok }
it { expect(last_response.header['Location']).to eq expected }
Expand All @@ -34,15 +34,15 @@
end

context "with creds but no perms" do
describe "GET /v1/puppet/nodes" do
describe "GET /v1/nodes" do
let(:expected) { 'http://example.org/v1/unauthorized' }
before do
current_session.rack_session[:access_token] = 'sada'
allow(Hieraviz::Store).
to receive(:get).
with('sada', 3600).
and_return(false)
get '/puppet/nodes'
get '/nodes'
end
it { expect(last_response).not_to be_ok }
it { expect(last_response.header['Location']).to eq expected }
Expand All @@ -65,10 +65,10 @@
with('sada', 3600).
and_return({})
end
describe "GET /v1/puppet/nodes" do
describe "GET /v1/nodes" do
let(:expected) { ['node1.example.com'] }
before do
get '/puppet/nodes'
get '/nodes'
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
Expand All @@ -84,12 +84,12 @@
}
}
before do
get '/puppet/node/node1.example.com/info'
get '/node/node1.example.com/info'
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
end
describe "GET /v1/puppet/node/node1.example.com/params" do
describe "GET /v1/node/node1.example.com/params" do
let(:expected) {
{
"param1.subparam1" => {
Expand All @@ -106,12 +106,12 @@
}
}
before do
get '/puppet/node/node1.example.com/params'
get '/node/node1.example.com/params'
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
end
describe "GET /v1/puppet/node/node1.example.com" do
describe "GET /v1/node/node1.example.com" do
let(:expected) {
{
"param1.subparam1" => {
Expand All @@ -128,12 +128,12 @@
}
}
before do
get '/puppet/node/node1.example.com'
get '/node/node1.example.com'
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
end
describe "GET /v1/puppet/node/node1.example.com/allparams" do
describe "GET /v1/node/node1.example.com/allparams" do
let(:expected) {
{
"param1.subparam1" => {
Expand Down Expand Up @@ -165,15 +165,15 @@
}
}
before do
get '/puppet/node/node1.example.com/allparams'
get '/node/node1.example.com/allparams'
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
end
describe "GET /v1/puppet/farms" do
describe "GET /v1/farms" do
let(:expected) { ['farm1'] }
before do
get '/puppet/farms'
get '/farms'
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
Expand Down

0 comments on commit be6949d

Please sign in to comment.