Extend SysController to allow WS to check / update storage

This commit is contained in:
Oliver Günther
2015-07-30 09:13:37 +02:00
parent 8b5a120052
commit c16c1e75d4
7 changed files with 204 additions and 32 deletions
+34
View File
@@ -32,6 +32,8 @@ require 'open_project/repository_authentication'
class SysController < ActionController::Base
before_filter :check_enabled
before_filter :require_basic_auth, only: [:repo_auth]
before_filter :find_project, only: [:update_required_storage]
before_filter :find_repository_with_storage, only: [:update_required_storage]
def projects
p = Project.active.has_module(:repository).find(:all, include: :repository, order: 'identifier')
@@ -58,6 +60,11 @@ class SysController < ActionController::Base
end
end
def update_required_storage
update_storage_information(@repository, params[:force] == '1')
render nothing: true, status: 200
end
def fetch_changesets
projects = []
if params[:id]
@@ -100,6 +107,33 @@ class SysController < ActionController::Base
private
def update_storage_information(repository, force = false)
if force
Delayed::Job.enqueue ::Scm::StorageUpdaterJob.new(repository)
else
repository.required_disk_storage
end
end
def find_project
@project = Project.find(params[:id])
rescue ActiveRecord::RecordNotFound
render text: "Could not find project ##{params[:id]}.", status: 404
end
def find_repository_with_storage
@repository = @project.repository
if @repository.nil?
render text: "Project ##{@project.id} does not have a repository.", status: 404
else
return true if @repository.scm.storage_available?
render text: 'repositories.storage.not_available', status: 400
end
false
end
def require_basic_auth
authenticate_with_http_basic do |username, password|
@authenticated_user = cached_user_login(username, password)
+1 -1
View File
@@ -170,7 +170,7 @@ class Repository < ActiveRecord::Base
end
def required_disk_storage
if scm.storage_countable?
if scm.storage_available?
if storage_updated_at.nil? ||
storage_updated_at < 12.hours.ago
+1 -1
View File
@@ -31,7 +31,7 @@ class Scm::StorageUpdaterJob
def initialize(repository)
@id = repository.id
unless repository.scm.storage_countable?
unless repository.scm.storage_available?
raise OpenProject::Scm::Exceptions::ScmError.new(
I18n.t('repositories.storage.not_available')
)
+1
View File
@@ -520,6 +520,7 @@ OpenProject::Application.routes.draw do
scope controller: 'sys' do
match '/sys/projects.:format', action: 'projects', via: :get
match '/sys/projects/:id/repository/storage', action: 'update_required_storage', via: :get
match '/sys/projects/:id/repository.:format', action: 'create_project_repository', via: :post
end
+1 -1
View File
@@ -52,7 +52,7 @@ module OpenProject
# Overriden by descendants when
# they are able to retrieve current
# storage usage.
def storage_countable?
def storage_available?
false
end
@@ -56,7 +56,7 @@ module OpenProject
##
# Determines whether this repository is eligible
# to count storage.
def storage_countable?
def storage_available?
local? && File.directory?(local_repository_path)
end
@@ -64,7 +64,7 @@ module OpenProject
# Counts the repository storage requirement immediately
# or raises an exception if this is impossible for the current repository.
def count_repository!
if storage_countable?
if storage_available?
count_required_storage
else
raise ScmError.new I18n.t('repositories.storage.not_available')
+164 -27
View File
@@ -39,8 +39,8 @@ module OpenProjectRepositoryAuthenticationSpecs
let(:valid_user_password) { 'Top Secret Password' }
let(:valid_user) {
FactoryGirl.create(:user, login: 'johndoe',
password: valid_user_password,
password_confirmation: valid_user_password)
password: valid_user_password,
password_confirmation: valid_user_password)
}
before(:each) do
@@ -49,8 +49,8 @@ module OpenProjectRepositoryAuthenticationSpecs
random_project = FactoryGirl.create(:project, is_public: false)
@member = FactoryGirl.create(:member, user: valid_user,
roles: [browse_role],
project: random_project)
roles: [browse_role],
project: random_project)
allow(Setting).to receive(:sys_api_key).and_return('12345678')
allow(Setting).to receive(:sys_api_enabled?).and_return(true)
allow(Setting).to receive(:repository_authentication_caching_enabled?).and_return(true)
@@ -60,7 +60,7 @@ module OpenProjectRepositoryAuthenticationSpecs
before(:each) do
@key = Setting.sys_api_key
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(valid_user.login, valid_user_password)
post 'repo_auth', key: @key, repository: 'without-access', method: 'GET'
post 'repo_auth', key: @key, repository: 'without-access', method: 'GET'
end
it 'should respond 403 not allowed' do
@@ -74,18 +74,18 @@ module OpenProjectRepositoryAuthenticationSpecs
@key = Setting.sys_api_key
@project = FactoryGirl.create(:project, is_public: false)
@member = FactoryGirl.create(:member, user: valid_user,
roles: [browse_role],
project: @project)
roles: [browse_role],
project: @project)
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(valid_user.login, valid_user_password)
end
it 'should respond 200 okay dokay for GET' do
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
expect(response.code).to eq('200')
end
it 'should respond 403 not allowed for POST' do
post 'repo_auth', key: @key, repository: @project.identifier, method: 'POST'
post 'repo_auth', key: @key, repository: @project.identifier, method: 'POST'
expect(response.code).to eq('403')
end
end
@@ -95,19 +95,19 @@ module OpenProjectRepositoryAuthenticationSpecs
@key = Setting.sys_api_key
@project = FactoryGirl.create(:project, is_public: false)
@member = FactoryGirl.create(:member, user: valid_user,
roles: [commit_role],
project: @project)
roles: [commit_role],
project: @project)
valid_user.save
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(valid_user.login, valid_user_password)
end
it 'should respond 200 okay dokay for GET' do
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
expect(response.code).to eq('200')
end
it 'should respond 200 okay dokay for POST' do
post 'repo_auth', key: @key, repository: @project.identifier, method: 'POST'
post 'repo_auth', key: @key, repository: @project.identifier, method: 'POST'
expect(response.code).to eq('200')
end
end
@@ -117,10 +117,10 @@ module OpenProjectRepositoryAuthenticationSpecs
@key = Setting.sys_api_key
@project = FactoryGirl.create(:project, is_public: false)
@member = FactoryGirl.create(:member, user: valid_user,
roles: [commit_role],
project: @project)
roles: [commit_role],
project: @project)
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(valid_user.login, valid_user_password + 'made invalid')
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
end
it 'should respond 401 auth required' do
@@ -133,7 +133,7 @@ module OpenProjectRepositoryAuthenticationSpecs
@key = Setting.sys_api_key
@project = FactoryGirl.create(:project, is_public: false)
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(valid_user.login, valid_user_password)
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
end
it 'should respond 403 not allowed' do
@@ -148,11 +148,11 @@ module OpenProjectRepositoryAuthenticationSpecs
random_project = FactoryGirl.create(:project, is_public: false)
@member = FactoryGirl.create(:member, user: valid_user,
roles: [browse_role],
project: random_project)
roles: [browse_role],
project: random_project)
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(valid_user.login, valid_user_password)
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
post 'repo_auth', key: @key, repository: @project.identifier, method: 'GET'
end
it 'should respond 200 OK' do
@@ -163,7 +163,7 @@ module OpenProjectRepositoryAuthenticationSpecs
describe '#repo_auth', 'for invalid credentials' do
before(:each) do
@key = Setting.sys_api_key
post 'repo_auth', key: @key, repository: 'any-repo', method: 'GET'
post 'repo_auth', key: @key, repository: 'any-repo', method: 'GET'
end
it 'should respond 401 auth required' do
@@ -179,14 +179,14 @@ module OpenProjectRepositoryAuthenticationSpecs
it 'should respond 403 for valid username/password' do
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(valid_user.login, valid_user_password)
post 'repo_auth', key: @key, repository: 'any-repo', method: 'GET'
post 'repo_auth', key: @key, repository: 'any-repo', method: 'GET'
expect(response.code).to eq('403')
expect(response.body).to eq('Access denied. Repository management WS is disabled or key is invalid.')
end
it 'should respond 403 for invalid username/password' do
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials('invalid', 'invalid')
post 'repo_auth', key: @key, repository: 'any-repo', method: 'GET'
post 'repo_auth', key: @key, repository: 'any-repo', method: 'GET'
expect(response.code).to eq('403')
expect(response.body).to eq('Access denied. Repository management WS is disabled or key is invalid.')
end
@@ -211,14 +211,14 @@ module OpenProjectRepositoryAuthenticationSpecs
it 'should return the same as user_login for valid creds' do
expect(controller.send(:cached_user_login, valid_user.login, valid_user_password)).to eq(
controller.send(:user_login, valid_user.login, valid_user_password)
)
controller.send(:user_login, valid_user.login, valid_user_password)
)
end
it 'should return the same as user_login for invalid creds' do
expect(controller.send(:cached_user_login, 'invalid', 'invalid')).to eq(
controller.send(:user_login, 'invalid', 'invalid')
)
controller.send(:user_login, 'invalid', 'invalid')
)
end
it 'should use cache' do
@@ -261,5 +261,142 @@ module OpenProjectRepositoryAuthenticationSpecs
end
end
end
describe 'update_required_storage' do
let(:force) { nil }
let(:apikey) { Setting.sys_api_key }
let(:last_updated) { nil }
def request_storage
get 'update_required_storage', key: apikey, id: id, force: force
end
context 'missing project' do
let(:id) { 1234 }
it 'returns 404' do
request_storage
expect(response.code).to eq('404')
expect(response.body).to include('Could not find project #1234')
end
end
context 'available project, but missing repository' do
let(:project) { FactoryGirl.build_stubbed(:project) }
let(:id) { project.id }
before do
allow(Project).to receive(:find).and_return(project)
request_storage
end
it 'returns 404' do
expect(response.code).to eq('404')
expect(response.body).to include("Project ##{project.id} does not have a repository.")
end
end
context 'stubbed repository' do
let(:project) { FactoryGirl.build_stubbed(:project) }
let(:id) { project.id }
let(:repository) {
FactoryGirl.build_stubbed(:repository_subversion, url: url, root_url: url)
}
before do
allow(Project).to receive(:find).and_return(project)
allow(project).to receive(:repository).and_return(repository)
allow(repository).to receive(:storage_updated_at).and_return(last_updated)
request_storage
end
context 'local non-existing repository' do
let(:root_url) { '/tmp/does/not/exist/svn/foo.svn' }
let(:url) { "file://#{root_url}" }
it 'does not have storage available' do
expect(repository.scm.storage_available?).to be false
expect(response.code).to eq('400')
end
end
context 'remote stubbed repository' do
let(:root_url) { '' }
let(:url) { 'https://foo.example.org/svn/bar' }
it 'has no storage available' do
request_storage
expect(repository.scm.storage_available?).to be false
expect(response.code).to eq('400')
end
end
end
context 'local existing repository' do
with_subversion_repository do |repo_dir|
let(:root_url) { repo_dir }
let(:url) { "file://#{root_url}" }
let(:project) { FactoryGirl.create(:project) }
let(:id) { project.id }
let(:repository) {
FactoryGirl.create(:repository_subversion, project: project, url: url, root_url: url)
}
before do
allow(Project).to receive(:find).and_return(project)
allow(project).to receive(:repository).and_return(repository)
allow(repository).to receive(:storage_updated_at).and_return(last_updated)
end
it 'has storage available' do
expect(repository.scm.storage_available?).to be true
end
context 'storage never updated before' do
it 'updates the storage' do
expect(repository.required_storage_bytes).to be == 0
request_storage
repository.reload
expect(repository.required_storage_bytes).to be > 0
end
end
context 'outdated storage' do
let(:last_updated) { 2.days.ago }
it 'updates the storage' do
expect(Delayed::Job)
.to receive(:enqueue).with(instance_of(::Scm::StorageUpdaterJob))
request_storage
end
end
context 'valid storage time' do
let(:last_updated) { 10.minutes.ago }
it 'does not update to storage' do
expect(Delayed::Job)
.not_to receive(:enqueue).with(instance_of(::Scm::StorageUpdaterJob))
request_storage
end
end
context 'valid storage time and force' do
let(:force) { '1' }
let(:last_updated) { 10.minutes.ago }
it 'does update to storage' do
expect(Delayed::Job)
.to receive(:enqueue).with(instance_of(::Scm::StorageUpdaterJob))
request_storage
end
end
end
end
end
end
end