# Offense count: 63
# Configuration parameters: CountComments.
Metrics/ClassLength:
- Max: 1801
+ Max: 1627
# Offense count: 72
Metrics/CyclomaticComplexity:
--- /dev/null
+class ChangesetCommentsController < ApplicationController
+ before_action :authorize_web, :only => [:index]
+ before_action :set_locale, :only => [:index]
+ before_action :authorize, :only => [:create, :destroy, :restore]
+ before_action :require_moderator, :only => [:destroy, :restore]
+ before_action :require_allow_write_api, :only => [:create, :destroy, :restore]
+ before_action :require_public_data, :only => [:create]
+ before_action :check_api_writable, :only => [:create, :destroy, :restore]
+ before_action :check_api_readable, :except => [:create, :index]
+ before_action(:only => [:index]) { |c| c.check_database_readable(true) }
+ around_action :api_call_handle_error, :except => [:index]
+ around_action :api_call_timeout, :except => [:index]
+ around_action :web_timeout, :only => [:index]
+
+ ##
+ # Add a comment to a changeset
+ def create
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput, "No id was given" unless params[:id]
+ raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
+
+ # Extract the arguments
+ id = params[:id].to_i
+ body = params[:text]
+
+ # Find the changeset and check it is valid
+ changeset = Changeset.find(id)
+ raise OSM::APIChangesetNotYetClosedError, changeset if changeset.is_open?
+
+ # Add a comment to the changeset
+ comment = changeset.comments.create(:changeset => changeset,
+ :body => body,
+ :author => current_user)
+
+ # Notify current subscribers of the new comment
+ changeset.subscribers.visible.each do |user|
+ Notifier.changeset_comment_notification(comment, user).deliver_later if current_user != user
+ end
+
+ # Add the commenter to the subscribers if necessary
+ changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
+
+ # Return a copy of the updated changeset
+ render :xml => changeset.to_xml.to_s
+ end
+
+ ##
+ # Sets visible flag on comment to false
+ def destroy
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput, "No id was given" unless params[:id]
+
+ # Extract the arguments
+ id = params[:id].to_i
+
+ # Find the changeset
+ comment = ChangesetComment.find(id)
+
+ # Hide the comment
+ comment.update(:visible => false)
+
+ # Return a copy of the updated changeset
+ render :xml => comment.changeset.to_xml.to_s
+ end
+
+ ##
+ # Sets visible flag on comment to true
+ def restore
+ # Check the arguments are sane
+ raise OSM::APIBadUserInput, "No id was given" unless params[:id]
+
+ # Extract the arguments
+ id = params[:id].to_i
+
+ # Find the changeset
+ comment = ChangesetComment.find(id)
+
+ # Unhide the comment
+ comment.update(:visible => true)
+
+ # Return a copy of the updated changeset
+ render :xml => comment.changeset.to_xml.to_s
+ end
+
+ ##
+ # Get a feed of recent changeset comments
+ def index
+ if params[:id]
+ # Extract the arguments
+ id = params[:id].to_i
+
+ # Find the changeset
+ changeset = Changeset.find(id)
+
+ # Return comments for this changeset only
+ @comments = changeset.comments.includes(:author, :changeset).limit(comments_limit)
+ else
+ # Return comments
+ @comments = ChangesetComment.includes(:author, :changeset).where(:visible => true).order("created_at DESC").limit(comments_limit).preload(:changeset)
+ end
+
+ # Render the result
+ respond_to do |format|
+ format.rss
+ end
+ rescue OSM::APIBadUserInput
+ head :bad_request
+ end
+
+ private
+
+ ##
+ # Get the maximum number of comments to return
+ def comments_limit
+ if params[:limit]
+ if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
+ params[:limit].to_i
+ else
+ raise OSM::APIBadUserInput, "Comments limit must be between 1 and 10000"
+ end
+ else
+ 100
+ end
+ end
+end
require "xml/libxml"
skip_before_action :verify_authenticity_token, :except => [:index]
- before_action :authorize_web, :only => [:index, :feed, :comments_feed]
- before_action :set_locale, :only => [:index, :feed, :comments_feed]
- before_action :authorize, :only => [:create, :update, :upload, :close, :comment, :subscribe, :unsubscribe, :hide_comment, :unhide_comment]
- before_action :require_moderator, :only => [:hide_comment, :unhide_comment]
- before_action :require_allow_write_api, :only => [:create, :update, :upload, :close, :comment, :subscribe, :unsubscribe, :hide_comment, :unhide_comment]
- before_action :require_public_data, :only => [:create, :update, :upload, :close, :comment, :subscribe, :unsubscribe]
- before_action :check_api_writable, :only => [:create, :update, :upload, :comment, :subscribe, :unsubscribe, :hide_comment, :unhide_comment]
- before_action :check_api_readable, :except => [:create, :update, :upload, :download, :query, :index, :feed, :comment, :subscribe, :unsubscribe, :comments_feed]
- before_action(:only => [:index, :feed, :comments_feed]) { |c| c.check_database_readable(true) }
- around_action :api_call_handle_error, :except => [:index, :feed, :comments_feed]
- around_action :api_call_timeout, :except => [:index, :feed, :comments_feed, :upload]
- around_action :web_timeout, :only => [:index, :feed, :comments_feed]
+ before_action :authorize_web, :only => [:index, :feed]
+ before_action :set_locale, :only => [:index, :feed]
+ before_action :authorize, :only => [:create, :update, :upload, :close, :subscribe, :unsubscribe]
+ before_action :require_allow_write_api, :only => [:create, :update, :upload, :close, :subscribe, :unsubscribe]
+ before_action :require_public_data, :only => [:create, :update, :upload, :close, :subscribe, :unsubscribe]
+ before_action :check_api_writable, :only => [:create, :update, :upload, :subscribe, :unsubscribe]
+ before_action :check_api_readable, :except => [:create, :update, :upload, :download, :query, :index, :feed, :subscribe, :unsubscribe]
+ before_action(:only => [:index, :feed]) { |c| c.check_database_readable(true) }
+ around_action :api_call_handle_error, :except => [:index, :feed]
+ around_action :api_call_timeout, :except => [:index, :feed, :upload]
+ around_action :web_timeout, :only => [:index, :feed]
# Helper methods for checking consistency
include ConsistencyValidations
index
end
- ##
- # Add a comment to a changeset
- def comment
- # Check the arguments are sane
- raise OSM::APIBadUserInput, "No id was given" unless params[:id]
- raise OSM::APIBadUserInput, "No text was given" if params[:text].blank?
-
- # Extract the arguments
- id = params[:id].to_i
- body = params[:text]
-
- # Find the changeset and check it is valid
- changeset = Changeset.find(id)
- raise OSM::APIChangesetNotYetClosedError, changeset if changeset.is_open?
-
- # Add a comment to the changeset
- comment = changeset.comments.create(:changeset => changeset,
- :body => body,
- :author => current_user)
-
- # Notify current subscribers of the new comment
- changeset.subscribers.visible.each do |user|
- Notifier.changeset_comment_notification(comment, user).deliver_later if current_user != user
- end
-
- # Add the commenter to the subscribers if necessary
- changeset.subscribers << current_user unless changeset.subscribers.exists?(current_user.id)
-
- # Return a copy of the updated changeset
- render :xml => changeset.to_xml.to_s
- end
-
##
# Adds a subscriber to the changeset
def subscribe
render :xml => changeset.to_xml.to_s
end
- ##
- # Sets visible flag on comment to false
- def hide_comment
- # Check the arguments are sane
- raise OSM::APIBadUserInput, "No id was given" unless params[:id]
-
- # Extract the arguments
- id = params[:id].to_i
-
- # Find the changeset
- comment = ChangesetComment.find(id)
-
- # Hide the comment
- comment.update(:visible => false)
-
- # Return a copy of the updated changeset
- render :xml => comment.changeset.to_xml.to_s
- end
-
- ##
- # Sets visible flag on comment to true
- def unhide_comment
- # Check the arguments are sane
- raise OSM::APIBadUserInput, "No id was given" unless params[:id]
-
- # Extract the arguments
- id = params[:id].to_i
-
- # Find the changeset
- comment = ChangesetComment.find(id)
-
- # Unhide the comment
- comment.update(:visible => true)
-
- # Return a copy of the updated changeset
- render :xml => comment.changeset.to_xml.to_s
- end
-
- ##
- # Get a feed of recent changeset comments
- def comments_feed
- if params[:id]
- # Extract the arguments
- id = params[:id].to_i
-
- # Find the changeset
- changeset = Changeset.find(id)
-
- # Return comments for this changeset only
- @comments = changeset.comments.includes(:author, :changeset).limit(comments_limit)
- else
- # Return comments
- @comments = ChangesetComment.includes(:author, :changeset).where(:visible => true).order("created_at DESC").limit(comments_limit).preload(:changeset)
- end
-
- # Render the result
- respond_to do |format|
- format.rss
- end
- rescue OSM::APIBadUserInput
- head :bad_request
- end
-
private
#------------------------------------------------------------
def conditions_nonempty(changesets)
changesets.where("num_changes > 0")
end
-
- ##
- # Get the maximum number of comments to return
- def comments_limit
- if params[:limit]
- if params[:limit].to_i.positive? && params[:limit].to_i <= 10000
- params[:limit].to_i
- else
- raise OSM::APIBadUserInput, "Comments limit must be between 1 and 10000"
- end
- else
- 100
- end
- end
end
-<h2><%= t "changeset.rss.comment", :author => comment.author.display_name,
+<h2><%= t ".comment", :author => comment.author.display_name,
:changeset_id => comment.changeset.id.to_s %></h2>
<div class="changeset-comment" style="margin-top: 5px">
- <div class="changeset-comment-description" style="font-size: smaller; color: #999999"><%= t "changeset.rss.commented_at_by_html", :when => friendly_date(comment.created_at), :user => comment.author.display_name %></div>
+ <div class="changeset-comment-description" style="font-size: smaller; color: #999999"><%= t ".commented_at_by_html", :when => friendly_date(comment.created_at), :user => comment.author.display_name %></div>
<div class="changeset-comment-text"><%= comment.body %></div>
</div>
comments.each do |comment|
xml.item do
- xml.title t("changeset.rss.comment", :author => comment.author.display_name, :changeset_id => comment.changeset.id.to_s)
+ xml.title t(".comment", :author => comment.author.display_name, :changeset_id => comment.changeset.id.to_s)
xml.link url_for(:controller => "browse", :action => "changeset", :id => comment.changeset.id, :anchor => "c#{comment.id}", :only_path => false)
xml.guid url_for(:controller => "browse", :action => "changeset", :id => comment.changeset.id, :anchor => "c#{comment.id}", :only_path => false)
"xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
xml.channel do
if @changeset
- xml.title t("changeset.rss.title_particular", :changeset_id => @changeset.id)
+ xml.title t(".title_particular", :changeset_id => @changeset.id)
else
- xml.title t("changeset.rss.title_all")
+ xml.title t(".title_all")
end
xml.link url_for(:controller => "site", :action => "index", :only_path => false)
--- /dev/null
+atom_feed(:language => I18n.locale, :schema_date => 2009,
+ :id => url_for(params.merge(:only_path => false)),
+ :root_url => url_for(params.merge(:only_path => false, :format => nil)),
+ "xmlns:georss" => "http://www.georss.org/georss") do |feed|
+ feed.title @title
+
+ feed.subtitle :type => "xhtml" do |xhtml|
+ xhtml.p do |p|
+ p << t(".sorry")
+ end
+ end
+end
--- /dev/null
+<p><%= t '.sorry' %></p>
load_more: "Load more"
timeout:
sorry: "Sorry, the list of changesets you requested took too long to retrieve."
- rss:
- title_all: OpenStreetMap changeset discussion
- title_particular: "OpenStreetMap changeset #%{changeset_id} discussion"
+ changeset_comments:
+ comment:
comment: "New comment on changeset #%{changeset_id} by %{author}"
- commented_at_html: "Updated %{when} ago"
commented_at_by_html: "Updated %{when} ago by %{user}"
- full: Full discussion
+ comments:
+ comment: "New comment on changeset #%{changeset_id} by %{author}"
+ index:
+ title_all: OpenStreetMap changeset discussion
+ title_particular: "OpenStreetMap changeset #%{changeset_id} discussion"
+ timeout:
+ sorry: "Sorry, the list of changeset comments you requested took too long to retrieve."
diary_entry:
new:
title: New Diary Entry
put "changeset/:id" => "changeset#update", :id => /\d+/
put "changeset/:id/close" => "changeset#close", :id => /\d+/
get "changesets" => "changeset#query"
- post "changeset/:id/comment" => "changeset#comment", :as => :changeset_comment, :id => /\d+/
- post "changeset/comment/:id/hide" => "changeset#hide_comment", :as => :changeset_comment_hide, :id => /\d+/
- post "changeset/comment/:id/unhide" => "changeset#unhide_comment", :as => :changeset_comment_unhide, :id => /\d+/
+ post "changeset/:id/comment" => "changeset_comments#create", :as => :changeset_comment, :id => /\d+/
+ post "changeset/comment/:id/hide" => "changeset_comments#destroy", :as => :changeset_comment_hide, :id => /\d+/
+ post "changeset/comment/:id/unhide" => "changeset_comments#restore", :as => :changeset_comment_unhide, :id => /\d+/
put "node/create" => "node#create"
get "node/:id/ways" => "way#ways_for_node", :id => /\d+/
get "/relation/:id" => "browse#relation", :id => /\d+/, :as => :relation
get "/relation/:id/history" => "browse#relation_history", :id => /\d+/
get "/changeset/:id" => "browse#changeset", :as => :changeset, :id => /\d+/
- get "/changeset/:id/comments/feed" => "changeset#comments_feed", :as => :changeset_comments_feed, :id => /\d*/, :defaults => { :format => "rss" }
+ get "/changeset/:id/comments/feed" => "changeset_comments#index", :as => :changeset_comments_feed, :id => /\d*/, :defaults => { :format => "rss" }
get "/note/:id" => "browse#note", :id => /\d+/, :as => "browse_note"
get "/note/new" => "browse#new_note"
get "/user/:display_name/history" => "changeset#index"
get "/about" => "site#about"
get "/history" => "changeset#index"
get "/history/feed" => "changeset#feed", :defaults => { :format => :atom }
- get "/history/comments/feed" => "changeset#comments_feed", :as => :changesets_comments_feed, :defaults => { :format => "rss" }
+ get "/history/comments/feed" => "changeset_comments#index", :as => :changesets_comments_feed, :defaults => { :format => "rss" }
get "/export" => "site#export"
match "/login" => "users#login", :via => [:get, :post]
match "/logout" => "users#logout", :via => [:get, :post]
--- /dev/null
+require "test_helper"
+
+class ChangesetCommentsControllerTest < ActionController::TestCase
+ ##
+ # test all routes which lead to this controller
+ def test_routes
+ assert_routing(
+ { :path => "/api/0.6/changeset/1/comment", :method => :post },
+ { :controller => "changeset_comments", :action => "create", :id => "1" }
+ )
+ assert_routing(
+ { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
+ { :controller => "changeset_comments", :action => "destroy", :id => "1" }
+ )
+ assert_routing(
+ { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
+ { :controller => "changeset_comments", :action => "restore", :id => "1" }
+ )
+ assert_routing(
+ { :path => "/changeset/1/comments/feed", :method => :get },
+ { :controller => "changeset_comments", :action => "index", :id => "1", :format => "rss" }
+ )
+ assert_routing(
+ { :path => "/history/comments/feed", :method => :get },
+ { :controller => "changeset_comments", :action => "index", :format => "rss" }
+ )
+ end
+
+ ##
+ # create comment success
+ def test_create_comment_success
+ user = create(:user)
+ user2 = create(:user)
+ private_user = create(:user, :data_public => false)
+ suspended_user = create(:user, :suspended)
+ deleted_user = create(:user, :deleted)
+ private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
+
+ basic_authorization user.email, "test"
+
+ assert_difference "ChangesetComment.count", 1 do
+ assert_no_difference "ActionMailer::Base.deliveries.size" do
+ perform_enqueued_jobs do
+ post :create, :params => { :id => private_user_closed_changeset.id, :text => "This is a comment" }
+ end
+ end
+ end
+ assert_response :success
+
+ changeset = create(:changeset, :closed, :user => private_user)
+ changeset.subscribers.push(private_user)
+ changeset.subscribers.push(user)
+ changeset.subscribers.push(suspended_user)
+ changeset.subscribers.push(deleted_user)
+
+ assert_difference "ChangesetComment.count", 1 do
+ assert_difference "ActionMailer::Base.deliveries.size", 1 do
+ perform_enqueued_jobs do
+ post :create, :params => { :id => changeset.id, :text => "This is a comment" }
+ end
+ end
+ end
+ assert_response :success
+
+ email = ActionMailer::Base.deliveries.first
+ assert_equal 1, email.to.length
+ assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
+ assert_equal private_user.email, email.to.first
+
+ ActionMailer::Base.deliveries.clear
+
+ basic_authorization user2.email, "test"
+
+ assert_difference "ChangesetComment.count", 1 do
+ assert_difference "ActionMailer::Base.deliveries.size", 2 do
+ perform_enqueued_jobs do
+ post :create, :params => { :id => changeset.id, :text => "This is a comment" }
+ end
+ end
+ end
+ assert_response :success
+
+ email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
+ assert_not_nil email
+ assert_equal 1, email.to.length
+ assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
+
+ email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
+ assert_not_nil email
+ assert_equal 1, email.to.length
+ assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
+
+ ActionMailer::Base.deliveries.clear
+ end
+
+ ##
+ # create comment fail
+ def test_create_comment_fail
+ # unauthorized
+ post :create, :params => { :id => create(:changeset, :closed).id, :text => "This is a comment" }
+ assert_response :unauthorized
+
+ basic_authorization create(:user).email, "test"
+
+ # bad changeset id
+ assert_no_difference "ChangesetComment.count" do
+ post :create, :params => { :id => 999111, :text => "This is a comment" }
+ end
+ assert_response :not_found
+
+ # not closed changeset
+ assert_no_difference "ChangesetComment.count" do
+ post :create, :params => { :id => create(:changeset).id, :text => "This is a comment" }
+ end
+ assert_response :conflict
+
+ # no text
+ assert_no_difference "ChangesetComment.count" do
+ post :create, :params => { :id => create(:changeset, :closed).id }
+ end
+ assert_response :bad_request
+
+ # empty text
+ assert_no_difference "ChangesetComment.count" do
+ post :create, :params => { :id => create(:changeset, :closed).id, :text => "" }
+ end
+ assert_response :bad_request
+ end
+
+ ##
+ # test hide comment fail
+ def test_destroy_comment_fail
+ # unauthorized
+ comment = create(:changeset_comment)
+ assert_equal true, comment.visible
+
+ post :destroy, :params => { :id => comment.id }
+ assert_response :unauthorized
+ assert_equal true, comment.reload.visible
+
+ basic_authorization create(:user).email, "test"
+
+ # not a moderator
+ post :destroy, :params => { :id => comment.id }
+ assert_response :forbidden
+ assert_equal true, comment.reload.visible
+
+ basic_authorization create(:moderator_user).email, "test"
+
+ # bad comment id
+ post :destroy, :params => { :id => 999111 }
+ assert_response :not_found
+ assert_equal true, comment.reload.visible
+ end
+
+ ##
+ # test hide comment succes
+ def test_hide_comment_success
+ comment = create(:changeset_comment)
+ assert_equal true, comment.visible
+
+ basic_authorization create(:moderator_user).email, "test"
+
+ post :destroy, :params => { :id => comment.id }
+ assert_response :success
+ assert_equal false, comment.reload.visible
+ end
+
+ ##
+ # test unhide comment fail
+ def test_restore_comment_fail
+ # unauthorized
+ comment = create(:changeset_comment, :visible => false)
+ assert_equal false, comment.visible
+
+ post :restore, :params => { :id => comment.id }
+ assert_response :unauthorized
+ assert_equal false, comment.reload.visible
+
+ basic_authorization create(:user).email, "test"
+
+ # not a moderator
+ post :restore, :params => { :id => comment.id }
+ assert_response :forbidden
+ assert_equal false, comment.reload.visible
+
+ basic_authorization create(:moderator_user).email, "test"
+
+ # bad comment id
+ post :restore, :params => { :id => 999111 }
+ assert_response :not_found
+ assert_equal false, comment.reload.visible
+ end
+
+ ##
+ # test unhide comment succes
+ def test_unhide_comment_success
+ comment = create(:changeset_comment, :visible => false)
+ assert_equal false, comment.visible
+
+ basic_authorization create(:moderator_user).email, "test"
+
+ post :restore, :params => { :id => comment.id }
+ assert_response :success
+ assert_equal true, comment.reload.visible
+ end
+
+ ##
+ # test comments feed
+ def test_feed
+ changeset = create(:changeset, :closed)
+ create_list(:changeset_comment, 3, :changeset => changeset)
+
+ get :index, :params => { :format => "rss" }
+ assert_response :success
+ assert_equal "application/rss+xml", @response.content_type
+ assert_select "rss", :count => 1 do
+ assert_select "channel", :count => 1 do
+ assert_select "item", :count => 3
+ end
+ end
+
+ get :index, :params => { :format => "rss", :limit => 2 }
+ assert_response :success
+ assert_equal "application/rss+xml", @response.content_type
+ assert_select "rss", :count => 1 do
+ assert_select "channel", :count => 1 do
+ assert_select "item", :count => 2
+ end
+ end
+
+ get :index, :params => { :id => changeset.id, :format => "rss" }
+ assert_response :success
+ assert_equal "application/rss+xml", @response.content_type
+ assert_select "rss", :count => 1 do
+ assert_select "channel", :count => 1 do
+ assert_select "item", :count => 3
+ end
+ end
+ end
+
+ ##
+ # test comments feed
+ def test_feed_bad_limit
+ get :index, :params => { :format => "rss", :limit => 0 }
+ assert_response :bad_request
+
+ get :index, :params => { :format => "rss", :limit => 100001 }
+ assert_response :bad_request
+ end
+end
{ :path => "/api/0.6/changeset/1/close", :method => :put },
{ :controller => "changeset", :action => "close", :id => "1" }
)
- assert_routing(
- { :path => "/api/0.6/changeset/1/comment", :method => :post },
- { :controller => "changeset", :action => "comment", :id => "1" }
- )
- assert_routing(
- { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
- { :controller => "changeset", :action => "hide_comment", :id => "1" }
- )
- assert_routing(
- { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
- { :controller => "changeset", :action => "unhide_comment", :id => "1" }
- )
assert_routing(
{ :path => "/api/0.6/changesets", :method => :get },
{ :controller => "changeset", :action => "query" }
)
- assert_routing(
- { :path => "/changeset/1/comments/feed", :method => :get },
- { :controller => "changeset", :action => "comments_feed", :id => "1", :format => "rss" }
- )
assert_routing(
{ :path => "/user/name/history", :method => :get },
{ :controller => "changeset", :action => "index", :display_name => "name" }
{ :path => "/history/feed", :method => :get },
{ :controller => "changeset", :action => "feed", :format => :atom }
)
- assert_routing(
- { :path => "/history/comments/feed", :method => :get },
- { :controller => "changeset", :action => "comments_feed", :format => "rss" }
- )
end
# -----------------------
assert_select "osmChange node[id='#{node.id}'][version='1']", 0
end
- ##
- # create comment success
- def test_create_comment_success
- user = create(:user)
- user2 = create(:user)
- private_user = create(:user, :data_public => false)
- suspended_user = create(:user, :suspended)
- deleted_user = create(:user, :deleted)
- private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
-
- basic_authorization user.email, "test"
-
- assert_difference "ChangesetComment.count", 1 do
- assert_no_difference "ActionMailer::Base.deliveries.size" do
- perform_enqueued_jobs do
- post :comment, :params => { :id => private_user_closed_changeset.id, :text => "This is a comment" }
- end
- end
- end
- assert_response :success
-
- changeset = create(:changeset, :closed, :user => private_user)
- changeset.subscribers.push(private_user)
- changeset.subscribers.push(user)
- changeset.subscribers.push(suspended_user)
- changeset.subscribers.push(deleted_user)
-
- assert_difference "ChangesetComment.count", 1 do
- assert_difference "ActionMailer::Base.deliveries.size", 1 do
- perform_enqueued_jobs do
- post :comment, :params => { :id => changeset.id, :text => "This is a comment" }
- end
- end
- end
- assert_response :success
-
- email = ActionMailer::Base.deliveries.first
- assert_equal 1, email.to.length
- assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
- assert_equal private_user.email, email.to.first
-
- ActionMailer::Base.deliveries.clear
-
- basic_authorization user2.email, "test"
-
- assert_difference "ChangesetComment.count", 1 do
- assert_difference "ActionMailer::Base.deliveries.size", 2 do
- perform_enqueued_jobs do
- post :comment, :params => { :id => changeset.id, :text => "This is a comment" }
- end
- end
- end
- assert_response :success
-
- email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
- assert_not_nil email
- assert_equal 1, email.to.length
- assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
-
- email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
- assert_not_nil email
- assert_equal 1, email.to.length
- assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
-
- ActionMailer::Base.deliveries.clear
- end
-
- ##
- # create comment fail
- def test_create_comment_fail
- # unauthorized
- post :comment, :params => { :id => create(:changeset, :closed).id, :text => "This is a comment" }
- assert_response :unauthorized
-
- basic_authorization create(:user).email, "test"
-
- # bad changeset id
- assert_no_difference "ChangesetComment.count" do
- post :comment, :params => { :id => 999111, :text => "This is a comment" }
- end
- assert_response :not_found
-
- # not closed changeset
- assert_no_difference "ChangesetComment.count" do
- post :comment, :params => { :id => create(:changeset).id, :text => "This is a comment" }
- end
- assert_response :conflict
-
- # no text
- assert_no_difference "ChangesetComment.count" do
- post :comment, :params => { :id => create(:changeset, :closed).id }
- end
- assert_response :bad_request
-
- # empty text
- assert_no_difference "ChangesetComment.count" do
- post :comment, :params => { :id => create(:changeset, :closed).id, :text => "" }
- end
- assert_response :bad_request
- end
-
##
# test subscribe success
def test_subscribe_success
assert_response :not_found
end
- ##
- # test hide comment fail
- def test_hide_comment_fail
- # unauthorized
- comment = create(:changeset_comment)
- assert_equal true, comment.visible
-
- post :hide_comment, :params => { :id => comment.id }
- assert_response :unauthorized
- assert_equal true, comment.reload.visible
-
- basic_authorization create(:user).email, "test"
-
- # not a moderator
- post :hide_comment, :params => { :id => comment.id }
- assert_response :forbidden
- assert_equal true, comment.reload.visible
-
- basic_authorization create(:moderator_user).email, "test"
-
- # bad comment id
- post :hide_comment, :params => { :id => 999111 }
- assert_response :not_found
- assert_equal true, comment.reload.visible
- end
-
- ##
- # test hide comment succes
- def test_hide_comment_success
- comment = create(:changeset_comment)
- assert_equal true, comment.visible
-
- basic_authorization create(:moderator_user).email, "test"
-
- post :hide_comment, :params => { :id => comment.id }
- assert_response :success
- assert_equal false, comment.reload.visible
- end
-
- ##
- # test unhide comment fail
- def test_unhide_comment_fail
- # unauthorized
- comment = create(:changeset_comment, :visible => false)
- assert_equal false, comment.visible
-
- post :unhide_comment, :params => { :id => comment.id }
- assert_response :unauthorized
- assert_equal false, comment.reload.visible
-
- basic_authorization create(:user).email, "test"
-
- # not a moderator
- post :unhide_comment, :params => { :id => comment.id }
- assert_response :forbidden
- assert_equal false, comment.reload.visible
-
- basic_authorization create(:moderator_user).email, "test"
-
- # bad comment id
- post :unhide_comment, :params => { :id => 999111 }
- assert_response :not_found
- assert_equal false, comment.reload.visible
- end
-
- ##
- # test unhide comment succes
- def test_unhide_comment_success
- comment = create(:changeset_comment, :visible => false)
- assert_equal false, comment.visible
-
- basic_authorization create(:moderator_user).email, "test"
-
- post :unhide_comment, :params => { :id => comment.id }
- assert_response :success
- assert_equal true, comment.reload.visible
- end
-
- ##
- # test comments feed
- def test_comments_feed
- changeset = create(:changeset, :closed)
- create_list(:changeset_comment, 3, :changeset => changeset)
-
- get :comments_feed, :params => { :format => "rss" }
- assert_response :success
- assert_equal "application/rss+xml", @response.content_type
- assert_select "rss", :count => 1 do
- assert_select "channel", :count => 1 do
- assert_select "item", :count => 3
- end
- end
-
- get :comments_feed, :params => { :format => "rss", :limit => 2 }
- assert_response :success
- assert_equal "application/rss+xml", @response.content_type
- assert_select "rss", :count => 1 do
- assert_select "channel", :count => 1 do
- assert_select "item", :count => 2
- end
- end
-
- get :comments_feed, :params => { :id => changeset.id, :format => "rss" }
- assert_response :success
- assert_equal "application/rss+xml", @response.content_type
- assert_select "rss", :count => 1 do
- assert_select "channel", :count => 1 do
- assert_select "item", :count => 3
- end
- end
- end
-
- ##
- # test comments feed
- def test_comments_feed_bad_limit
- get :comments_feed, :params => { :format => "rss", :limit => 0 }
- assert_response :bad_request
-
- get :comments_feed, :params => { :format => "rss", :limit => 100001 }
- assert_response :bad_request
- end
-
private
##