Merge pull request #123 from sschneid/user_token_list

User token list
This commit is contained in:
Justin Stoller 2015-08-20 19:15:20 -07:00
commit ce158d9fec
3 changed files with 119 additions and 36 deletions

View file

@ -15,6 +15,10 @@ module Vmpooler
call env.merge("PATH_INFO" => "/api/v#{api_version}/summary/#{params[:route]}/#{params[:key]}") call env.merge("PATH_INFO" => "/api/v#{api_version}/summary/#{params[:route]}/#{params[:key]}")
end end
get '/token/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/token")
end
post '/token/?' do post '/token/?' do
call env.merge("PATH_INFO" => "/api/v#{api_version}/token") call env.merge("PATH_INFO" => "/api/v#{api_version}/token")
end end

View file

@ -162,6 +162,40 @@ module Vmpooler
JSON.pretty_generate(result) JSON.pretty_generate(result)
end end
get "#{api_prefix}/token/?" do
content_type :json
status 404
result = { 'ok' => false }
if Vmpooler::API.settings.config[:auth]
status 401
need_auth!
backend.keys('vmpooler__token__*').each do |key|
data = backend.hgetall(key)
if data['user'] == Rack::Auth::Basic::Request.new(request.env).username
token = key.split('__').last
result[token] ||= {}
result[token]['created'] = data['timestamp']
result['ok'] = true
end
end
if result['ok']
status 200
else
status 404
end
end
JSON.pretty_generate(result)
end
get "#{api_prefix}/token/:token/?" do get "#{api_prefix}/token/:token/?" do
content_type :json content_type :json

View file

@ -27,6 +27,87 @@ describe Vmpooler::API::V1 do
app.settings.set :redis, redis app.settings.set :redis, redis
end end
describe 'GET /token' do
context '(auth not configured)' do
let(:config) { { auth: false } }
it 'returns a 404' do
get "#{prefix}/token"
expect_json(ok = false, http = 404)
end
end
context '(auth configured)' do
let(:config) { { auth: true } }
it 'returns a 401 if not authed' do
get "#{prefix}/token"
expect_json(ok = false, http = 401)
end
it 'returns a list of tokens if authed' do
expect(redis).to receive(:keys).with('vmpooler__token__*').and_return(["vmpooler__token__abc"])
expect(redis).to receive(:hgetall).with('vmpooler__token__abc').and_return({"user" => "admin", "timestamp" => "now"})
authorize 'admin', 's3cr3t'
get "#{prefix}/token"
expect(JSON.parse(last_response.body)['abc']['created']).to eq('now')
expect_json(ok = true, http = 200)
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
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 +144,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 } }