Improve testing setup

This commit is contained in:
Brandon Robins
2018-03-11 16:38:50 -05:00
parent 19290d3832
commit 2ce668b9ea
9 changed files with 163 additions and 76 deletions

View File

@@ -1,4 +1,6 @@
Rails.application.routes.draw do Rails.application.routes.draw do
calligraphy_resource :test calligraphy_resource :test
calligraphy_resource :webdav, resource_class: Calligraphy::FileResource, resource_root_path: File.expand_path('../../../../tmp/webdav', __FILE__) calligraphy_resource :webdav,
resource_class: Calligraphy::FileResource,
resource_root_path: File.expand_path('../../../../tmp/webdav', __FILE__)
end end

View File

@@ -22,7 +22,7 @@ require 'rspec/rails'
# directory. Alternatively, in the individual `*_spec.rb` files, manually # directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary. # require only the support files necessary.
# #
Dir[Rails.root.join('../support/**/*.rb')].each { |f| require f } # Dir[Rails.root.join('../support/**/*.rb')].each { |f| require f }
# Checks for pending migrations and applies them before tests are run. # Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line. # If you are not using ActiveRecord, you can remove this line.
@@ -57,3 +57,5 @@ RSpec.configure do |config|
# arbitrary gems may also be filtered via: # arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name") # config.filter_gems_from_backtrace("gem name")
end end
require 'support/resource_helpers'

View File

@@ -5,20 +5,19 @@ require 'support/request_helpers'
require 'support/examples/ext_mkcol' require 'support/examples/ext_mkcol'
RSpec.describe 'mkcol', type: :request do RSpec.describe 'mkcol', type: :request do
before(:all) do before(:context) do
tmp_dir = Rails.root.join('../../tmp').to_path Calligraphy::FileResource.setup
Dir.mkdir tmp_dir unless File.exist? tmp_dir
webdav_dir = Rails.root.join('../../tmp/webdav').to_path
FileUtils.rm_r webdav_dir if File.exist? webdav_dir
Dir.mkdir webdav_dir
end end
before(:each) do before(:each) do
allow(Calligraphy).to receive(:enable_digest_authentication) skip_authentication
.and_return(false)
end end
after(:context) do
Calligraphy::FileResource.cleanup
end
context "for #{Calligraphy::FileResource}" do
it 'creates a collection with additional properties' do it 'creates a collection with additional properties' do
allow_any_instance_of(Calligraphy::FileResource).to receive( allow_any_instance_of(Calligraphy::FileResource).to receive(
:valid_resourcetypes :valid_resourcetypes
@@ -48,4 +47,5 @@ RSpec.describe 'mkcol', type: :request do
expect(response.body).to include('valid-resourcetype') expect(response.body).to include('valid-resourcetype')
end end
end end
end
end end

View File

@@ -4,11 +4,19 @@ require 'rails_helper'
require 'support/request_helpers' require 'support/request_helpers'
RSpec.describe 'OPTIONS', type: :request do RSpec.describe 'OPTIONS', type: :request do
before(:each) do before(:context) do
allow(Calligraphy).to receive(:enable_digest_authentication) Calligraphy::FileResource.setup
.and_return(false)
end end
before(:each) do
skip_authentication
end
after(:context) do
Calligraphy::FileResource.cleanup
end
context "for #{Calligraphy::FileResource}" do
context 'when not using extended MKCOL support' do context 'when not using extended MKCOL support' do
before(:each) do before(:each) do
allow_any_instance_of(Calligraphy::FileResource).to receive( allow_any_instance_of(Calligraphy::FileResource).to receive(
@@ -48,4 +56,5 @@ RSpec.describe 'OPTIONS', type: :request do
expect(response.headers['DAV']).to include('extended-mkcol') expect(response.headers['DAV']).to include('extended-mkcol')
end end
end end
end
end end

View File

@@ -6,25 +6,22 @@ require 'support/examples/propfind'
require 'support/examples/proppatch' require 'support/examples/proppatch'
RSpec.describe 'PROPFIND', type: :request do RSpec.describe 'PROPFIND', type: :request do
before(:all) do before(:context) do
tmp_dir = Rails.root.join('../../tmp').to_path Calligraphy::FileResource.setup
Dir.mkdir tmp_dir unless File.exist? tmp_dir
webdav_dir = Rails.root.join('../../tmp/webdav').to_path
FileUtils.rm_r webdav_dir if File.exist? webdav_dir
Dir.mkdir webdav_dir
end end
before(:each) do before(:each) do
allow(Calligraphy).to receive(:enable_digest_authentication) skip_authentication
.and_return(false) end
after(:context) do
Calligraphy::FileResource.cleanup
end end
context 'with xml defintiion' do context 'with xml defintiion' do
before(:each) do before(:each) do
put '/webdav/bar.html', headers: { Calligraphy::FileResource.create resource: 'bar.html'
RAW_POST_DATA: 'hello world'
}
proppatch '/webdav/bar.html', headers: { proppatch '/webdav/bar.html', headers: {
RAW_POST_DATA: Support::Examples::Proppatch.rfc4918_9_2_2 RAW_POST_DATA: Support::Examples::Proppatch.rfc4918_9_2_2
} }

View File

@@ -46,5 +46,19 @@ RSpec.describe 'Resource' do
expect(resource.dav_compliance).to eq('1, 2, 3') expect(resource.dav_compliance).to eq('1, 2, 3')
end end
end end
describe '#enable_extended_mkcol?' do
it 'is not enabled by default' do
resource = Calligraphy::Resource.new
expect(resource.enable_extended_mkcol?).to eq(false)
end
end
describe '#valid_resourcetypes' do
it 'returns only a collection resourcetype by default' do
resource = Calligraphy::Resource.new
expect(resource.valid_resourcetypes).to match_array(['collection'])
end
end
end end
end end

View File

@@ -113,5 +113,15 @@ RSpec.describe 'calligraphy_resource', type: :routing do
) )
end end
end end
context 'for HEAD requests' do
it do
expect(head: '/test/twelve').to route_to(
controller: 'calligraphy/rails/web_dav_requests',
action: 'invoke_method',
resource: 'twelve'
)
end
end
end end
end end

View File

@@ -15,3 +15,7 @@ module ActionDispatch
end end
end end
end end
def skip_authentication
allow(Calligraphy).to receive(:enable_digest_authentication).and_return(false)
end

View File

@@ -0,0 +1,49 @@
# frozen_string_literal: true
RESOURCES_TO_TEST_AGAINST = [
Calligraphy::FileResource
].freeze
module RSpecMethods
def setup; end
def cleanup; end
def create(resource:, content: 'Hi hi!'); end
end
module FileResourceHelpers
def setup
tmp_dir = Rails.root.join('../../tmp').to_path
Dir.mkdir tmp_dir unless File.exist? tmp_dir
FileUtils.rm_r resource_root if File.exist? resource_root
Dir.mkdir resource_root
end
def cleanup
FileUtils.rm_r resource_root if File.exist? resource_root
end
def create(resource:, content: 'Hello world')
resource = Calligraphy::FileResource.new(
resource: resource,
root_dir: resource_root
)
resource.write content
end
def resource_root
Rails.root.join('../../tmp/webdav').to_path
end
end
RESOURCES_TO_TEST_AGAINST.each do |resource_class|
resource_class.send :extend, RSpecMethods
end
module Calligraphy
class FileResource
extend FileResourceHelpers
end
end