2016-08-23 01:00:59 -04:00
|
|
|
# PaperTrail is not compatible with sinatra 2 yet. Contributions welcome.
|
2016-01-04 23:53:52 -05:00
|
|
|
if Gem::Version.new(Rack.release) < Gem::Version.new("2.0.0.alpha")
|
2016-03-05 17:07:32 -05:00
|
|
|
require "test_helper"
|
2016-01-04 23:53:52 -05:00
|
|
|
# require 'sinatra/main'
|
|
|
|
|
|
|
|
# --- Tests for non-modular `Sinatra::Application` style ----
|
2016-02-15 22:07:48 -05:00
|
|
|
module Sinatra
|
|
|
|
class Application
|
2016-03-05 17:07:32 -05:00
|
|
|
configs = YAML.load_file(File.expand_path("../../dummy/config/database.yml", __FILE__))
|
2016-02-15 22:07:48 -05:00
|
|
|
ActiveRecord::Base.configurations = configs
|
|
|
|
ActiveRecord::Base.establish_connection(:test)
|
|
|
|
|
|
|
|
# We shouldn't actually need this line if I'm not mistaken but the tests
|
|
|
|
# seem to fail without it ATM
|
|
|
|
register PaperTrail::Sinatra
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
get "/test" do
|
|
|
|
Widget.create!(name: "bar")
|
|
|
|
"Hai"
|
2016-02-15 22:07:48 -05:00
|
|
|
end
|
2016-01-04 23:53:52 -05:00
|
|
|
|
2016-02-15 22:07:48 -05:00
|
|
|
def current_user
|
2016-03-05 17:07:32 -05:00
|
|
|
@current_user ||= OpenStruct.new(id: "raboof")
|
2016-02-15 22:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
2013-08-13 17:10:41 -04:00
|
|
|
end
|
2013-08-15 11:00:55 -04:00
|
|
|
|
2016-01-04 23:53:52 -05:00
|
|
|
class SinatraTest < ActionDispatch::IntegrationTest
|
|
|
|
include Rack::Test::Methods
|
2013-08-13 17:10:41 -04:00
|
|
|
|
2016-01-04 23:53:52 -05:00
|
|
|
def app
|
|
|
|
@app ||= Sinatra::Application
|
|
|
|
end
|
2013-08-13 17:10:41 -04:00
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
test "baseline" do
|
2016-01-04 23:53:52 -05:00
|
|
|
assert_nil Widget.create.versions.first.whodunnit
|
|
|
|
end
|
2013-08-13 17:10:41 -04:00
|
|
|
|
2016-01-04 23:53:52 -05:00
|
|
|
context "`PaperTrail::Sinatra` in a `Sinatra::Application` application" do
|
|
|
|
should "sets the `user_for_paper_trail` from the `current_user` method" do
|
2016-03-05 17:07:32 -05:00
|
|
|
get "/test"
|
|
|
|
assert_equal "Hai", last_response.body
|
2016-01-04 23:53:52 -05:00
|
|
|
widget = Widget.last
|
|
|
|
assert_not_nil widget
|
2016-03-05 17:07:32 -05:00
|
|
|
assert_equal "bar", widget.name
|
2016-01-04 23:53:52 -05:00
|
|
|
assert_equal 1, widget.versions.size
|
2016-03-05 17:07:32 -05:00
|
|
|
assert_equal "raboof", widget.versions.first.whodunnit
|
2016-01-04 23:53:52 -05:00
|
|
|
end
|
2013-08-13 17:10:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|