Split API spec into /token and /token/:token

This commit is contained in:
Scott Schneider 2015-08-18 18:48:38 -07:00
parent 492cfb06a3
commit 47deddcc46

View file

@ -27,6 +27,52 @@ describe Vmpooler::API::V1 do
app.settings.set :redis, redis app.settings.set :redis, redis
end end
describe 'POST /token' do
context '(auth not configured)' do
let(:config) { { auth: false } }
it 'returns a 404' do
post "#{prefix}/token"
expect_json(ok = false, http = 404)
end
end
context '(auth configured)' do
before do
allow(redis).to receive(:hset).and_return '1'
end
let(:config) { { auth: true } }
it 'returns a 401 if not authed' do
post "#{prefix}/token"
expect_json(ok = false, http = 401)
end
it 'returns a token if authed' do
authorize 'admin', 's3cr3t'
post "#{prefix}/token"
expect(JSON.parse(last_response.body)['token'].length).to be(32)
expect_json(ok = true, http = 200)
end
end
end
end
describe '/token/:token' do
let(:redis) { double('redis') }
let(:prefix) { '/api/v1' }
before do
app.settings.set :config, config
app.settings.set :redis, redis
end
describe 'GET /token/:token' do describe 'GET /token/:token' do
context '(auth not configured)' do context '(auth not configured)' do
let(:config) { { auth: false } } let(:config) { { auth: false } }
@ -63,42 +109,6 @@ describe Vmpooler::API::V1 do
end end
end end
describe 'POST /token' do
context '(auth not configured)' do
let(:config) { { auth: false } }
it 'returns a 404' do
post "#{prefix}/token"
expect_json(ok = false, http = 404)
end
end
context '(auth configured)' do
before do
allow(redis).to receive(:hset).and_return '1'
end
let(:config) { { auth: true } }
it 'returns a 401 if not authed' do
post "#{prefix}/token"
expect_json(ok = false, http = 401)
end
it 'returns a token if authed' do
authorize 'admin', 's3cr3t'
post "#{prefix}/token"
expect(JSON.parse(last_response.body)['token'].length).to be(32)
expect_json(ok = true, http = 200)
end
end
end
describe 'DELETE /token/:token' do describe 'DELETE /token/:token' do
context '(auth not configured)' do context '(auth not configured)' do
let(:config) { { auth: false } } let(:config) { { auth: false } }