create_token added to before

This commit is contained in:
Tanisha Payne 2021-06-15 18:34:43 -04:00
parent 75661b6c86
commit aeff9e9d3f
2 changed files with 25 additions and 39 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'json' require 'json'
module Vmpooler module Vmpooler
@ -36,9 +38,9 @@ module Vmpooler
end end
get '/restart/?' do get '/restart/?' do
content_type :json
# token authentication # token authentication
need_token! need_token!
# restart operation # restart operation
exit_process exit_process
status 200 status 200

View file

@ -1,9 +1,13 @@
require 'spec_helper' require 'spec_helper'
require 'rack/test' require 'rack/test'
#require 'pry-byebug'
describe Vmpooler::API::Restart do describe Vmpooler::API::Restart do
include Rack::Test::Methods include Rack::Test::Methods
let(:backend) { MockRedis.new }
def app() def app()
Vmpooler::API Vmpooler::API
end end
@ -13,60 +17,40 @@ describe Vmpooler::API::Restart do
# https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method # https://rubydoc.info/gems/sinatra/Sinatra/Base#reset!-class_method
before(:each) do before(:each) do
app.reset! app.reset!
#allow_any_instance_of(Vmpooler::API::Restart).to receive(:exit_process)
end end
describe '/restart' do describe '/restart' do
let(:current_time) { Time.now }
let(:config) { {
config: {}
} }
before(:each) do before(:each) do
expect(app).to receive(:run!).once expect(app).to receive(:run!).once
app.execute([:api], config, redis, nil, nil) app.execute([:api], config, redis, nil, nil)
create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time)
end end
describe 'GET /restart' do
context '(auth not configured)' do
let(:config) { {
config: {},
auth: false
} }
it 'returns a 404' do
get "/restart"
expect_json(ok = false, http = 404)
end
end
context 'when restart endpoint is called' do
context '(auth configured)' do
let(:config) {
{
config: {},
auth: {
'provider' => 'dummy'
}
}
}
let(:username_str) { 'admin' }
let(:password_str) { 's3cr3t' }
it 'returns a 401 if no token is provided' do it 'returns a 401 if no token is provided' do
get "/restart" get "/restart/"
expect_json(ok = false, http = 401) expect(ok = false)
expect(last_response.status).to eq(401)
end end
it 'restarts if token is provided' do it 'vmpooler restarts when a token is provided' do
authorize 'admin', 's3cr3t'
get "/restart"
expect_json(ok = true, http = 200)
expect(JSON.parse(last_response.body).to eq JSON.parse(JSON.dump({ 'ok' => true, 'message' => 'Restarting ...' })))
get "/restart/"
expect(last_response.header['Content-Type']).to eq('application/json')
expect(last_response.status).to eq(200)
expect(last_response.body).to eq(JSON.pretty_generate({ 'ok' => true, 'message' => 'Restarting ...' }))
end end
end end
end
end end
end end