From e4f2777889f2f6652eff7d56ac5c8e1013dc87b7 Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Tue, 18 Aug 2015 19:05:47 -0700 Subject: [PATCH] Spec tests for GET /token --- spec/vmpooler/api/v1_spec.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/spec/vmpooler/api/v1_spec.rb b/spec/vmpooler/api/v1_spec.rb index 56fd2e3..f85d712 100644 --- a/spec/vmpooler/api/v1_spec.rb +++ b/spec/vmpooler/api/v1_spec.rb @@ -27,6 +27,41 @@ describe Vmpooler::API::V1 do app.settings.set :redis, redis 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 } }