2013-08-13 21:10:41 +00:00
|
|
|
require 'test_helper'
|
2013-08-19 15:33:03 +00:00
|
|
|
# require 'sinatra/main'
|
2013-08-13 21:10:41 +00:00
|
|
|
|
|
|
|
# --- Tests for non-modular `Sinatra::Application` style ----
|
|
|
|
class Sinatra::Application
|
2014-03-14 20:11:10 +00:00
|
|
|
configs = YAML.load_file(File.expand_path('../../dummy/config/database.yml', __FILE__))
|
|
|
|
ActiveRecord::Base.configurations = configs
|
|
|
|
ActiveRecord::Base.establish_connection(:test)
|
2013-09-26 14:29:53 +00:00
|
|
|
register PaperTrail::Sinatra # we shouldn't actually need this line if I'm not mistaken but the tests seem to fail without it ATM
|
2013-08-13 21:10:41 +00:00
|
|
|
|
|
|
|
get '/test' do
|
2013-08-19 15:33:03 +00:00
|
|
|
Widget.create!(:name => 'bar')
|
2013-08-15 15:00:55 +00:00
|
|
|
'Hai'
|
2013-08-13 21:10:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_user
|
2014-02-11 16:51:28 +00:00
|
|
|
@current_user ||= OpenStruct.new(:id => 'raboof').tap do |obj|
|
|
|
|
# Invoking `id` returns the `object_id` value in Ruby18 unless specifically overwritten
|
|
|
|
def obj.id; 'raboof'; end if RUBY_VERSION.to_f < 1.9
|
|
|
|
end
|
2013-08-13 21:10:41 +00:00
|
|
|
end
|
2013-08-15 15:00:55 +00:00
|
|
|
|
2013-08-13 21:10:41 +00:00
|
|
|
end
|
|
|
|
|
2013-08-19 15:33:03 +00:00
|
|
|
class SinatraTest < ActionDispatch::IntegrationTest
|
2013-08-13 21:10:41 +00:00
|
|
|
include Rack::Test::Methods
|
|
|
|
|
|
|
|
def app
|
|
|
|
@app ||= Sinatra::Application
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'baseline' do
|
|
|
|
assert_nil Widget.create.versions.first.whodunnit
|
|
|
|
end
|
|
|
|
|
|
|
|
context "`PaperTrail::Sinatra` in a `Sinatra::Application` application" do
|
|
|
|
|
|
|
|
should "sets the `user_for_paper_trail` from the `current_user` method" do
|
|
|
|
get '/test'
|
2013-08-15 15:00:55 +00:00
|
|
|
assert_equal 'Hai', last_response.body
|
2014-05-09 15:05:58 +00:00
|
|
|
widget = Widget.last
|
2013-08-13 21:10:41 +00:00
|
|
|
assert_not_nil widget
|
2013-08-19 15:33:03 +00:00
|
|
|
assert_equal 'bar', widget.name
|
2013-08-13 21:10:41 +00:00
|
|
|
assert_equal 1, widget.versions.size
|
2013-08-13 22:02:46 +00:00
|
|
|
assert_equal 'raboof', widget.versions.first.whodunnit
|
2013-08-13 21:10:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|