Skip to content

Commit

Permalink
cover more api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mose committed Jan 19, 2016
1 parent 8a43d93 commit d0992bd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
4 changes: 0 additions & 4 deletions app/apiv1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ def check_authorization
end
end

get '/test' do
json data: Time.new
end

get '/nodes' do
check_authorization
json Hieracles::Registry.nodes(settings.config)
Expand Down
65 changes: 58 additions & 7 deletions spec/app/apiv1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,83 @@

describe HieravizApp::ApiV1 do

def app()
described_class
context "page not found" do
describe "GET /v1/blahblah" do
let(:expected) { { 'error' => "data not found" } }
before do
get '/blahblah'
end
it { expect(last_response).not_to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
end
end

context "when no creds" do
describe "GET /v1/nodes" do
it "replies error" do
let(:expected) { 'http://example.org/v1/not_logged' }
before do
get '/nodes'
end
it { expect(last_response).not_to be_ok }
it { expect(last_response.header['Location']).to eq expected }
end
describe 'GET /v1/not_logged' do
let(:expected) { { 'error' => "Not connected." } }
before do
get '/not_logged'
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
end
end

context "with creds but no perms" 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 '/nodes'
expect(last_response).not_to be_ok
end
it { expect(last_response).not_to be_ok }
it { expect(last_response.header['Location']).to eq expected }
end
describe 'GET /v1/unauthorized' do
let(:expected) { { 'error' => "Unauthorized" } }
before do
get '/unauthorized'
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
end
end

context "with proper creds" do
before do
current_session.rack_session[:access_token] = 'sada'
allow(Hieraviz::Store).
to receive(:get).
with('sada', 3600).
and_return({})
end
describe "GET /v1/nodes" do
it "replies error" do
current_session.rack_session[:access_token] = 'sada'
let(:expected) { ['node1.example.com'] }
before do
get '/nodes'
expect(last_response).to be_ok
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
end
describe "GET /v1/farms" do
let(:expected) { ['farm1'] }
before do
get '/farms'
end
it { expect(last_response).to be_ok }
it { expect(JSON.parse last_response.body).to eq expected }
end
end

Expand Down
5 changes: 0 additions & 5 deletions spec/app/web_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
require 'spec_helper'
ENV['HIERAVIZ_CONFIG_FILE'] = File.expand_path '../../files/config.yml', __FILE__
require 'sinatra_helper'

describe HieravizApp::Web do

def app()
described_class
end

describe "GET /" do
it "replies 401" do
get '/'
Expand Down

0 comments on commit d0992bd

Please sign in to comment.