From 976a25a6c649b70b10f2de3e7706e2a61317ff43 Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 1 Dec 2010 18:50:04 -0500 Subject: [PATCH] additional functional tests, some controller fixes --- app/controllers/artists_controller.rb | 5 +- app/controllers/bans_controller.rb | 3 + app/controllers/comments_controller.rb | 3 +- app/models/artist.rb | 1 + app/models/ban.rb | 8 +++ app/views/artists/index.html.erb | 2 +- app/views/bans/edit.html.erb | 2 +- app/views/bans/index.html.erb | 2 +- app/views/bans/new.html.erb | 2 +- app/views/bans/show.html.erb | 2 +- app/views/comment_votes/create.js.erb | 3 + app/views/comment_votes/create.js.rjs | 3 - app/views/comments/index.html.erb | 2 +- test/factories/ban.rb | 2 +- test/factories/comment.rb | 2 +- test/functional/artists_controller_test.rb | 57 ++++++++++++++++++- test/functional/bans_controller_test.rb | 57 ++++++++++++++++++- .../comment_votes_controller_test.rb | 22 ++++++- test/functional/comments_controller_test.rb | 34 ++++++++++- 19 files changed, 185 insertions(+), 27 deletions(-) create mode 100644 app/views/comment_votes/create.js.erb delete mode 100644 app/views/comment_votes/create.js.rjs diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 10c53b165..883e7de43 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -1,5 +1,5 @@ class ArtistsController < ApplicationController - before_filter :member_only + before_filter :member_only, :except => [:index, :show] def new @artist = Artist.new_with_defaults(params) @@ -48,7 +48,8 @@ class ArtistsController < ApplicationController def revert @artist = Artist.find(params[:id]) - @artist.revert_to!(params[:version]) + @version = ArtistVersion.find(params[:version_id]) + @artist.revert_to!(@version) redirect_to artist_path(@artist), :notice => "Artist updated" end end diff --git a/app/controllers/bans_controller.rb b/app/controllers/bans_controller.rb index 21228da6f..ff9f2443e 100644 --- a/app/controllers/bans_controller.rb +++ b/app/controllers/bans_controller.rb @@ -20,6 +20,8 @@ class BansController < ApplicationController def create @ban = Ban.new(params[:ban]) + @ban.banner_id = CurrentUser.id + if @ban.save redirect_to ban_path(@ban), :notice => "Ban created" else @@ -39,5 +41,6 @@ class BansController < ApplicationController def destroy @ban = Ban.find(params[:id]) @ban.destroy + redirect_to bans_path, :notice => "Ban destroyed" end end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 828199938..3de1d1d15 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -19,8 +19,7 @@ class CommentsController < ApplicationController @comment.save respond_with(@comment) do |format| format.html do - flash[:notice] = "Comment posted" - redirect_to posts_path(@comment.post) + redirect_to post_path(@comment.post), :notice => "Comment posted" end format.js diff --git a/app/models/artist.rb b/app/models/artist.rb index 842e5607d..8a597e0d9 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -7,6 +7,7 @@ class Artist < ActiveRecord::Base belongs_to :creator, :class_name => "User" has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name" has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl" + has_many :versions, :order => "artist_versions.id", :class_name => "ArtistVersion" has_one :wiki_page, :foreign_key => "title", :primary_key => "name" has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name" accepts_nested_attributes_for :wiki_page diff --git a/app/models/ban.rb b/app/models/ban.rb index ed598b7c1..e2f836d6d 100644 --- a/app/models/ban.rb +++ b/app/models/ban.rb @@ -38,6 +38,14 @@ class Ban < ActiveRecord::Base end end + def user_name + user ? user.name : nil + end + + def user_name=(username) + self.user_id = User.name_to_id(username) + end + def duration=(dur) self.expires_at = dur.to_i.days.from_now @duration = dur diff --git a/app/views/artists/index.html.erb b/app/views/artists/index.html.erb index 2a1cdb355..2d82f1d21 100644 --- a/app/views/artists/index.html.erb +++ b/app/views/artists/index.html.erb @@ -11,7 +11,7 @@ <% @artists.each do |artist| %> - <% content_tag(:tr, :id => "artist-#{artist.id}") do %> + <%= content_tag(:tr, :id => "artist-#{artist.id}") do %> <%= link_to "P", posts_path(:tags => artist.name), :title => "Find posts for artist" %> <%= link_to "E", edit_artist_path(artist), :title => "Edit artist" %> diff --git a/app/views/bans/edit.html.erb b/app/views/bans/edit.html.erb index 3efcdde33..0e92665d9 100644 --- a/app/views/bans/edit.html.erb +++ b/app/views/bans/edit.html.erb @@ -1,7 +1,7 @@

Edit Ban

- <%= render "form", :locals => {:ban => @ban} %> + <%= render :partial => "form", :locals => {:ban => @ban} %>
diff --git a/app/views/bans/index.html.erb b/app/views/bans/index.html.erb index e6d02af8e..11f8196fa 100644 --- a/app/views/bans/index.html.erb +++ b/app/views/bans/index.html.erb @@ -12,7 +12,7 @@ <% @bans.each do |ban| %> - <%= ban.user_name %> + <%= ban.user.name %> <%= ban.expires_at %> <%= ban.reason %> diff --git a/app/views/bans/new.html.erb b/app/views/bans/new.html.erb index 3f8b1a03a..8c5eaa8e9 100644 --- a/app/views/bans/new.html.erb +++ b/app/views/bans/new.html.erb @@ -1,7 +1,7 @@

New Ban

- <%= render "form", :locals => {:ban => @ban} %> + <%= render :partial => "form", :locals => {:ban => @ban} %>
diff --git a/app/views/bans/show.html.erb b/app/views/bans/show.html.erb index a75e157ce..7e2f4f594 100644 --- a/app/views/bans/show.html.erb +++ b/app/views/bans/show.html.erb @@ -2,7 +2,7 @@

Show Ban

diff --git a/app/views/comment_votes/create.js.erb b/app/views/comment_votes/create.js.erb new file mode 100644 index 000000000..20f465b7e --- /dev/null +++ b/app/views/comment_votes/create.js.erb @@ -0,0 +1,3 @@ +<% if @error %> + alert(<%= escape_javascript(@error.to_s) %>); +<% end %> diff --git a/app/views/comment_votes/create.js.rjs b/app/views/comment_votes/create.js.rjs deleted file mode 100644 index 450cfa6f7..000000000 --- a/app/views/comment_votes/create.js.rjs +++ /dev/null @@ -1,3 +0,0 @@ -if @error - page.alert(@error.to_s) -end diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb index 282a244c0..d2fb5dbf9 100644 --- a/app/views/comments/index.html.erb +++ b/app/views/comments/index.html.erb @@ -5,7 +5,7 @@
<%= link_to(image_tag(post.preview_file_url), post_path(post)) %>
- <%= render "comments/partials/index/list", :locals => {:post => post, :comments => post.comments.recent.reverse, :show_header => true} %> + <%= render :partial => "comments/partials/index/list", :locals => {:post => post, :comments => post.comments.recent.reverse, :show_header => true} %>
<% end %> diff --git a/test/factories/ban.rb b/test/factories/ban.rb index c7a37f859..9ef20b80e 100644 --- a/test/factories/ban.rb +++ b/test/factories/ban.rb @@ -1,6 +1,6 @@ Factory.define(:ban) do |f| f.user {|x| x.association(:user)} f.banner {|x| x.association(:admin_user)} - f.reason {Faker::Lorem.words} + f.reason {Faker::Lorem.words.join(" ")} f.duration 60 end diff --git a/test/factories/comment.rb b/test/factories/comment.rb index 54afc3b76..173ed472e 100644 --- a/test/factories/comment.rb +++ b/test/factories/comment.rb @@ -1,5 +1,5 @@ Factory.define(:comment) do |f| f.post {|x| x.association(:post)} - f.body {Faker::Lorem.sentences} + f.body {Faker::Lorem.sentences.join(" ")} f.score 0 end diff --git a/test/functional/artists_controller_test.rb b/test/functional/artists_controller_test.rb index 9a8ec84ac..d181ee7a2 100644 --- a/test/functional/artists_controller_test.rb +++ b/test/functional/artists_controller_test.rb @@ -1,8 +1,59 @@ require 'test_helper' class ArtistsControllerTest < ActionController::TestCase - # Replace this with your real tests. - test "the truth" do - assert true + context "An artists controller" do + setup do + CurrentUser.user = Factory.create(:user) + CurrentUser.ip_addr = "127.0.0.1" + @artist = Factory.create(:artist) + @user = Factory.create(:user) + end + + teardown do + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end + + should "render the new page" do + get :new, {}, {:user_id => @user.id} + assert_response :success + end + + should "render the edit page" do + get :edit, {:id => @artist.id}, {:user_id => @user.id} + assert_response :success + end + + should "render the show page" do + get :show, {:id => @artist.id} + assert_response :success + end + + should "render the index page" do + get :index + assert_response :success + end + + should "create an artist" do + assert_difference("Artist.count", 1) do + post :create, {:artist => Factory.attributes_for(:artist)}, {:user_id => @user.id} + end + artist = Artist.last + assert_redirected_to(artist_path(artist)) + end + + should "update an artist" do + post :update, {:id => @artist.id, :artist => {:name => "xxx"}}, {:user_id => @user.id} + artist = Artist.last + assert_equal("xxx", artist.name) + assert_redirected_to(artist_path(artist)) + end + + should "revert an artist" do + @artist.update_attributes(:name => "xyz") + @artist.update_attributes(:name => "abc") + version = @artist.versions.first + post :revert, {:id => @artist.id, :version_id => version.id} + end end end diff --git a/test/functional/bans_controller_test.rb b/test/functional/bans_controller_test.rb index 31c8c3c93..a9dd3a875 100644 --- a/test/functional/bans_controller_test.rb +++ b/test/functional/bans_controller_test.rb @@ -1,8 +1,59 @@ require 'test_helper' class BansControllerTest < ActionController::TestCase - # Replace this with your real tests. - test "the truth" do - assert true + context "A bans controller" do + setup do + CurrentUser.user = Factory.create(:user) + CurrentUser.ip_addr = "127.0.0.1" + @ban = Factory.create(:ban) + @user = Factory.create(:moderator_user) + end + + teardown do + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end + + should "render the new page" do + get :new, {}, {:user_id => @user.id} + assert_response :success + end + + should "render the edit page" do + get :edit, {:id => @ban.id}, {:user_id => @user.id} + assert_response :success + end + + should "render the show page" do + get :show, {:id => @ban.id} + assert_response :success + end + + should "render the index page" do + get :index + assert_response :success + end + + should "create a ban" do + assert_difference("Ban.count", 1) do + post :create, {:ban => Factory.attributes_for(:ban)}, {:user_id => @user.id} + end + ban = Ban.last + assert_redirected_to(ban_path(ban)) + end + + should "update a ban" do + post :update, {:id => @ban.id, :ban => {:reason => "xxx"}}, {:user_id => @user.id} + ban = Ban.last + assert_equal("xxx", ban.reason) + assert_redirected_to(ban_path(ban)) + end + + should "destroy a ban" do + assert_difference("Ban.count", -1) do + post :destroy, {:id => @ban.id}, {:user_id => @user.id} + end + assert_redirected_to(bans_path) + end end end diff --git a/test/functional/comment_votes_controller_test.rb b/test/functional/comment_votes_controller_test.rb index d9feaa808..bb5b1922f 100644 --- a/test/functional/comment_votes_controller_test.rb +++ b/test/functional/comment_votes_controller_test.rb @@ -1,8 +1,24 @@ require 'test_helper' class CommentVotesControllerTest < ActionController::TestCase - # Replace this with your real tests. - test "the truth" do - assert true + context "A comment votes controller" do + setup do + CurrentUser.user = Factory.create(:user) + CurrentUser.ip_addr = "127.0.0.1" + @comment = Factory.create(:comment) + @user = Factory.create(:user) + end + + teardown do + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end + + should "create a vote" do + assert_difference("CommentVote.count", 1) do + post :create, {:format => "js", :comment_id => @comment.id}, {:user_id => @user.id} + assert_response :success + end + end end end diff --git a/test/functional/comments_controller_test.rb b/test/functional/comments_controller_test.rb index 8a86af59b..7246864ad 100644 --- a/test/functional/comments_controller_test.rb +++ b/test/functional/comments_controller_test.rb @@ -1,8 +1,36 @@ require 'test_helper' class CommentsControllerTest < ActionController::TestCase - # Replace this with your real tests. - test "the truth" do - assert true + context "A comments controller" do + setup do + CurrentUser.user = Factory.create(:user) + CurrentUser.ip_addr = "127.0.0.1" + @comment = Factory.create(:comment) + @user = Factory.create(:moderator_user) + end + + teardown do + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end + + should "render the index page" do + get :index + assert_response :success + end + + should "update a comment" do + post :update, {:id => @comment.id, :comment => {:body => "abc"}}, {:user_id => @comment.creator_id} + assert_redirected_to comment_path(@comment) + end + + should "create a comment" do + p = Factory.create(:post) + assert_difference("Comment.count", 1) do + post :create, {:comment => Factory.attributes_for(:comment, :post_id => p.id)}, {:user_id => @user.id} + end + comment = Comment.last + assert_redirected_to post_path(comment.post) + end end end