Files
nairobi/spec/system/admin/activity_spec.rb
Javi Martín 5311daadfe Use a button for non-GET table actions
Links acting like buttons have a few disadvantages.

First, screen readers will announce them as "links". Screen reader users
usually associate links with "things that get you somewhere" and buttons
with "things that perform an action". So when something like "Delete,
link" is announced, they'll probably think this is a link which will
take them to another page where they can delete a record.

Furthermore, the URL of the link for the "destroy" action might be the
same as the URL for the "show" action (only one is accessed with a
DELETE request and the other one with a GET request). That means screen
readers could announce the link like "Delete, visited link", which is
very confusing.

They also won't work when opening links in a new tab, since opening
links in a new tab always results in a GET request to the URL the link
points to.

Finally, submit buttons work without JavaScript enabled, so they'll work
even if the JavaScript in the page hasn't loaded (for whatever reason).

For all these reasons (and probably many more), using a button to send
forms is IMHO superior to using links.

There's one disadvantage, though. Using `button_to` we create a <form>
tag, which means we'll generate invalid HTML if the table is inside
another form. If we run into this issue, we need to use `button_tag`
with a `form` attribute and then generate a form somewhere else inside
the HTML (with `content_for`).

Note we're using `button_to` with a block so it generates a <button>
tag. Using it in a different way the text would result in an <input />
tag, and input elements can't have pseudocontent added via CSS.

The following code could be a starting point to use the `button_tag`
with a `form` attribute. One advantage of this approach is screen
readers wouldn't announce "leaving form" while navigating through these
buttons. However, it doesn't work in Internet Explorer.

```
ERB:

<% content_for(:hidden_content, form_tag(path, form_options) {}) %>
<%= button_tag text, button_options %>

Ruby:

def form_id
  path.gsub("/", "_")
end

def form_options
  { id: form_id, method: options[:method] }
end

def button_options
  html_options.except(:method).merge(form: form_id)
end

Layout:

<%= content_for :hidden_content %> # Right before the `</body>`
```
2021-09-20 20:27:37 +02:00

377 lines
11 KiB
Ruby

require "rails_helper"
describe "Admin activity" do
let(:admin) { create(:administrator) }
before do
login_as(admin.user)
end
context "Proposals" do
scenario "Shows moderation activity on proposals" do
proposal = create(:proposal)
visit proposal_path(proposal)
within("#proposal_#{proposal.id}") do
accept_confirm { click_link "Hide" }
end
expect(page).to have_css("#proposal_#{proposal.id}.faded")
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content(proposal.title)
expect(page).to have_content("Hidden")
expect(page).to have_content(admin.user.username)
end
end
scenario "Shows moderation activity from moderation screen" do
proposal1 = create(:proposal)
proposal2 = create(:proposal)
proposal3 = create(:proposal)
visit moderation_proposals_path(filter: "all")
within("#proposal_#{proposal1.id}") do
check "proposal_#{proposal1.id}_check"
end
within("#proposal_#{proposal3.id}") do
check "proposal_#{proposal3.id}_check"
end
accept_confirm { click_button "Hide proposals" }
expect(page).not_to have_content(proposal1.title)
visit admin_activity_path
expect(page).to have_content(proposal1.title)
expect(page).not_to have_content(proposal2.title)
expect(page).to have_content(proposal3.title)
end
scenario "Shows admin restores" do
proposal = create(:proposal, :hidden)
visit admin_hidden_proposals_path
within("#proposal_#{proposal.id}") do
accept_confirm { click_button "Restore" }
end
expect(page).to have_content "There are no hidden proposals"
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content(proposal.title)
expect(page).to have_content("Restored")
expect(page).to have_content(admin.user.username)
end
end
end
context "Debates" do
scenario "Shows moderation activity on debates" do
debate = create(:debate)
visit debate_path(debate)
within("#debate_#{debate.id}") do
accept_confirm { click_link "Hide" }
end
expect(page).to have_css("#debate_#{debate.id}.faded")
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content(debate.title)
expect(page).to have_content("Hidden")
expect(page).to have_content(admin.user.username)
end
end
scenario "Shows moderation activity from moderation screen" do
debate1 = create(:debate)
debate2 = create(:debate)
debate3 = create(:debate)
visit moderation_debates_path(filter: "all")
within("#debate_#{debate1.id}") do
check "debate_#{debate1.id}_check"
end
within("#debate_#{debate3.id}") do
check "debate_#{debate3.id}_check"
end
accept_confirm { click_button "Hide debates" }
expect(page).not_to have_content(debate1.title)
visit admin_activity_path
expect(page).to have_content(debate1.title)
expect(page).not_to have_content(debate2.title)
expect(page).to have_content(debate3.title)
end
scenario "Shows admin restores" do
debate = create(:debate, :hidden)
visit admin_hidden_debates_path
within("#debate_#{debate.id}") do
accept_confirm { click_button "Restore" }
end
expect(page).to have_content "There are no hidden debates"
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content(debate.title)
expect(page).to have_content("Restored")
expect(page).to have_content(admin.user.username)
end
end
end
context "Comments" do
scenario "Shows moderation activity on comments" do
debate = create(:debate)
comment = create(:comment, commentable: debate)
visit debate_path(debate)
within("#comment_#{comment.id}") do
accept_confirm { click_link "Hide" }
expect(page).to have_css(".faded")
end
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content(comment.body)
expect(page).to have_content("Hidden")
expect(page).to have_content(admin.user.username)
end
end
scenario "Shows moderation activity from moderation screen" do
comment1 = create(:comment, body: "SPAM")
comment2 = create(:comment)
comment3 = create(:comment, body: "Offensive!")
visit moderation_comments_path(filter: "all")
within("#comment_#{comment1.id}") do
check "comment_#{comment1.id}_check"
end
within("#comment_#{comment3.id}") do
check "comment_#{comment3.id}_check"
end
accept_confirm { click_button "Hide comments" }
expect(page).not_to have_content(comment1.body)
visit admin_activity_path
expect(page).to have_content(comment1.body)
expect(page).not_to have_content(comment2.body)
expect(page).to have_content(comment3.body)
end
scenario "Shows admin restores" do
comment = create(:comment, :hidden)
visit admin_hidden_comments_path
within("#comment_#{comment.id}") do
accept_confirm { click_button "Restore" }
end
expect(page).to have_content "There are no hidden comments"
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content(comment.body)
expect(page).to have_content("Restored")
expect(page).to have_content(admin.user.username)
end
end
end
context "User" do
scenario "Shows moderation activity on users" do
proposal = create(:proposal)
visit proposal_path(proposal)
within("#proposal_#{proposal.id}") do
accept_confirm { click_link "Hide author" }
expect(page).to have_current_path(debates_path)
end
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content("Blocked")
expect(page).to have_content(proposal.author.username)
expect(page).to have_content(proposal.author.email)
expect(page).to have_content(admin.user.username)
expect(page).not_to have_content(proposal.title)
end
end
scenario "Shows moderation activity from moderation screen" do
user = create(:user)
visit moderation_users_path(search: user.username)
within("#moderation_users") do
click_link "Block"
end
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content(user.username)
expect(page).to have_content(user.email)
expect(page).to have_content(admin.user.username)
end
end
scenario "Shows moderation activity from proposals moderation screen" do
proposal1 = create(:proposal)
proposal2 = create(:proposal)
proposal3 = create(:proposal)
visit moderation_proposals_path(filter: "all")
within("#proposal_#{proposal1.id}") do
check "proposal_#{proposal1.id}_check"
end
within("#proposal_#{proposal3.id}") do
check "proposal_#{proposal3.id}_check"
end
accept_confirm { click_button "Block authors" }
expect(page).not_to have_content(proposal1.author.username)
visit admin_activity_path
expect(page).to have_content(proposal1.author.username)
expect(page).to have_content(proposal1.author.email)
expect(page).to have_content(proposal3.author.username)
expect(page).to have_content(proposal3.author.email)
expect(page).not_to have_content(proposal2.author.username)
end
scenario "Shows moderation activity from debates moderation screen" do
debate1 = create(:debate)
debate2 = create(:debate)
debate3 = create(:debate)
visit moderation_debates_path(filter: "all")
within("#debate_#{debate1.id}") do
check "debate_#{debate1.id}_check"
end
within("#debate_#{debate3.id}") do
check "debate_#{debate3.id}_check"
end
accept_confirm { click_button "Block authors" }
expect(page).not_to have_content(debate1.author.username)
visit admin_activity_path
expect(page).to have_content(debate1.author.username)
expect(page).to have_content(debate1.author.email)
expect(page).to have_content(debate3.author.username)
expect(page).to have_content(debate3.author.email)
expect(page).not_to have_content(debate2.author.username)
end
scenario "Shows moderation activity from comments moderation screen" do
comment1 = create(:comment, body: "SPAM")
comment2 = create(:comment)
comment3 = create(:comment, body: "Offensive!")
visit moderation_comments_path(filter: "all")
within("#comment_#{comment1.id}") do
check "comment_#{comment1.id}_check"
end
within("#comment_#{comment3.id}") do
check "comment_#{comment3.id}_check"
end
accept_confirm { click_button "Block authors" }
expect(page).not_to have_content comment1.author.username
visit admin_activity_path
expect(page).to have_content(comment1.author.username)
expect(page).to have_content(comment1.author.email)
expect(page).to have_content(comment3.author.username)
expect(page).to have_content(comment3.author.email)
expect(page).not_to have_content(comment2.author.username)
end
scenario "Shows admin restores" do
user = create(:user, :hidden)
visit admin_hidden_users_path
within("#user_#{user.id}") do
accept_confirm { click_button "Restore" }
end
expect(page).to have_content "There are no hidden users"
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content(user.username)
expect(page).to have_content(user.email)
expect(page).to have_content("Restored")
expect(page).to have_content(admin.user.username)
end
end
end
context "System emails" do
scenario "Shows moderation activity on system emails" do
proposal = create(:proposal, title: "Proposal A")
proposal_notification = create(:proposal_notification, proposal: proposal,
title: "Proposal A Title",
body: "Proposal A Notification Body")
proposal_notification.moderate_system_email(admin.user)
visit admin_activity_path
within first("tbody tr") do
expect(page).to have_content(proposal_notification.title)
expect(page).to have_content("Hidden")
expect(page).to have_content(admin.user.username)
end
end
end
end