diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/controllers/node_actions_controller_test.rb | 51 | ||||
| -rw-r--r-- | test/models/helpers/node_actions_helper_test.rb | 85 |
2 files changed, 136 insertions, 0 deletions
diff --git a/test/controllers/node_actions_controller_test.rb b/test/controllers/node_actions_controller_test.rb new file mode 100644 index 0000000..8bc68ec --- /dev/null +++ b/test/controllers/node_actions_controller_test.rb | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class NodeActionsControllerTest < ActionController::TestCase | ||
| 4 | |||
| 5 | def setup | ||
| 6 | login_as :quentin | ||
| 7 | end | ||
| 8 | |||
| 9 | def logged_node slug, title | ||
| 10 | node = Node.root.children.create!(:slug => slug) | ||
| 11 | Globalize.with_locale(I18n.default_locale) { node.draft.update!(:title => title) } | ||
| 12 | node | ||
| 13 | end | ||
| 14 | |||
| 15 | test "index renders" do | ||
| 16 | get :index | ||
| 17 | assert_response :success | ||
| 18 | end | ||
| 19 | |||
| 20 | test "index filters by node" do | ||
| 21 | node_a = logged_node("log_filter_a", "Alpha Subject") | ||
| 22 | node_b = logged_node("log_filter_b", "Beta Subject") | ||
| 23 | NodeAction.record!(:node => node_a, :action => "create", :user => users(:quentin)) | ||
| 24 | NodeAction.record!(:node => node_b, :action => "create", :user => users(:quentin)) | ||
| 25 | |||
| 26 | get :index, params: { :node_id => node_a.id } | ||
| 27 | |||
| 28 | assert_response :success | ||
| 29 | assert_includes @response.body, "Alpha Subject" | ||
| 30 | assert_not_includes @response.body, "Beta Subject" | ||
| 31 | end | ||
| 32 | |||
| 33 | test "index filters by user" do | ||
| 34 | node = logged_node("log_filter_user", "Gamma Subject") | ||
| 35 | NodeAction.record!(:node => node, :action => "create", :user => users(:quentin)) | ||
| 36 | |||
| 37 | get :index, params: { :user_id => users(:quentin).id } | ||
| 38 | assert_includes @response.body, "Gamma Subject" | ||
| 39 | |||
| 40 | other = User.where.not(:id => users(:quentin).id).first | ||
| 41 | get :index, params: { :user_id => other.id } | ||
| 42 | assert_not_includes @response.body, "Gamma Subject" | ||
| 43 | end | ||
| 44 | |||
| 45 | test "index requires login" do | ||
| 46 | session[:user_id] = nil | ||
| 47 | @controller.instance_variable_set(:@current_user, nil) | ||
| 48 | get :index | ||
| 49 | assert_response :redirect | ||
| 50 | end | ||
| 51 | end | ||
diff --git a/test/models/helpers/node_actions_helper_test.rb b/test/models/helpers/node_actions_helper_test.rb new file mode 100644 index 0000000..93bbdf6 --- /dev/null +++ b/test/models/helpers/node_actions_helper_test.rb | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | require 'test_helper' | ||
| 2 | |||
| 3 | class NodeActionsHelperTest < ActionView::TestCase | ||
| 4 | def setup | ||
| 5 | @original_locale = I18n.locale | ||
| 6 | I18n.locale = :en | ||
| 7 | end | ||
| 8 | |||
| 9 | def default_url_options | ||
| 10 | { :locale => nil } | ||
| 11 | end | ||
| 12 | |||
| 13 | def teardown | ||
| 14 | I18n.locale = @original_locale | ||
| 15 | end | ||
| 16 | |||
| 17 | def entry action, metadata = {}, node: nil, user: nil, page: nil | ||
| 18 | NodeAction.create!(:node => node, :user => user, :page => page, | ||
| 19 | :action => action, :occurred_at => Time.now, | ||
| 20 | :metadata => { "username" => "quentin", | ||
| 21 | "human_readable_node_name" => "Subject" }.merge(metadata)) | ||
| 22 | end | ||
| 23 | |||
| 24 | test "publish renders the title pair" do | ||
| 25 | out = action_summary(entry("publish", | ||
| 26 | { "via" => "draft", "title" => { "from" => "Old", "to" => "New" } })) | ||
| 27 | |||
| 28 | assert_includes out, "quentin" | ||
| 29 | assert_includes out, "Old" | ||
| 30 | end | ||
| 31 | |||
| 32 | test "first publish uses its own sentence" do | ||
| 33 | out = action_summary(entry("publish", | ||
| 34 | { "via" => "draft", "title" => { "from" => nil, "to" => "New" } })) | ||
| 35 | |||
| 36 | assert_includes out, "for the first time" | ||
| 37 | end | ||
| 38 | |||
| 39 | test "rollback publishes get the rollback sentence" do | ||
| 40 | out = action_summary(entry("publish", | ||
| 41 | { "via" => "revision", "title" => { "from" => "Now", "to" => "Then" } })) | ||
| 42 | |||
| 43 | assert_includes out, "rolled" | ||
| 44 | end | ||
| 45 | |||
| 46 | test "move renders the path pair" do | ||
| 47 | out = action_summary(entry("move", | ||
| 48 | { "path" => { "from" => "a/b", "to" => "a/c" } })) | ||
| 49 | |||
| 50 | assert_includes out, "a/b" | ||
| 51 | assert_includes out, "a/c" | ||
| 52 | end | ||
| 53 | |||
| 54 | test "unknown verbs degrade to a generic sentence, never an error" do | ||
| 55 | out = action_summary(entry("frobnicate")) | ||
| 56 | |||
| 57 | assert_includes out, "frobnicate" | ||
| 58 | assert_includes out, "quentin" | ||
| 59 | end | ||
| 60 | |||
| 61 | test "dead references render as plain names from metadata, no links" do | ||
| 62 | out = action_summary(entry("publish", | ||
| 63 | { "title" => { "from" => "Old", "to" => "New" } })) | ||
| 64 | |||
| 65 | assert_not_includes out, "<a " | ||
| 66 | assert_includes out, "Subject" | ||
| 67 | end | ||
| 68 | |||
| 69 | test "metadata values are escaped" do | ||
| 70 | out = action_summary(entry("publish", | ||
| 71 | { "human_readable_node_name" => "<b>bold</b>", | ||
| 72 | "title" => { "from" => "<script>alert(1)</script>", "to" => "x" } })) | ||
| 73 | |||
| 74 | assert_not_includes out, "<script>" | ||
| 75 | assert_not_includes out, "<b>" | ||
| 76 | end | ||
| 77 | |||
| 78 | test "live associations upgrade names to links" do | ||
| 79 | out = action_summary(entry("publish", | ||
| 80 | { "title" => { "from" => "Old", "to" => "New" } }, | ||
| 81 | :user => users(:quentin))) | ||
| 82 | |||
| 83 | assert_includes out, "<a " | ||
| 84 | end | ||
| 85 | end | ||
