Raise error on unpermitted params.

Fail loudly if we forget to whitelist a param instead of silently
ignoring it.

misc models: convert to strong params.

artist commentaries: convert to strong params.

* Disallow changing or setting post_id to a nonexistent post.

artists: convert to strong params.

* Disallow setting `is_banned` in create/update actions. Changing it
  this way instead of with the ban/unban actions would leave the artist in
  a partially banned state.

bans: convert to strong params.

* Disallow changing the user_id after the ban has been created.

comments: convert to strong params.

favorite groups: convert to strong params.

news updates: convert to strong params.

post appeals: convert to strong params.

post flags: convert to strong params.

* Disallow users from setting the `is_deleted` / `is_resolved` flags.

ip bans: convert to strong params.

user feedbacks: convert to strong params.

* Disallow users from setting `disable_dmail_notification` when creating feedbacks.
* Disallow changing the user_id after the feedback has been created.

notes: convert to strong params.

wiki pages: convert to strong params.

* Also fix non-Builders being able to delete wiki pages.

saved searches: convert to strong params.

pools: convert to strong params.

* Disallow setting `post_count` or `is_deleted` in create/update actions.

janitor trials: convert to strong params.

post disapprovals: convert to strong params.

* Factor out quick-mod bar to shared partial.
* Fix quick-mod bar to use `Post#is_approvable?` to determine visibility
  of Approve button.

dmail filters: convert to strong params.

password resets: convert to strong params.

user name change requests: convert to strong params.

posts: convert to strong params.

users: convert to strong params.

* Disallow setting password_hash, last_logged_in_at, last_forum_read_at,
  has_mail, and dmail_filter_attributes[user_id].

* Remove initialize_default_image_size (dead code).

uploads: convert to strong params.

* Remove `initialize_status` because status already defaults to pending
  in the database.

tag aliases/implications: convert to strong params.

tags: convert to strong params.

forum posts: convert to strong params.

* Disallow changing the topic_id after creating the post.
* Disallow setting is_deleted (destroy/undelete actions should be used instead).
* Remove is_sticky / is_locked (nonexistent attributes).

forum topics: convert to strong params.

* merges https://github.com/evazion/danbooru/tree/wip-rails-5.1
* lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4)
* switch to factorybot and change all references

Co-authored-by: r888888888 <r888888888@gmail.com>
Co-authored-by: evazion <noizave@gmail.com>

add diffs
This commit is contained in:
r888888888
2018-04-02 10:51:26 -07:00
committed by Albert Yi
parent 01eda51020
commit abce4d2551
362 changed files with 4796 additions and 4799 deletions

View File

@@ -1,10 +1,14 @@
require 'test_helper'
class Admin::DashboardsControllerTest < ActionController::TestCase
class Admin::DashboardsControllerTest < ActionDispatch::IntegrationTest
context "The admin dashboard controller" do
setup do
@admin = create(:admin_user)
end
context "show action" do
should "render" do
get :show
get_auth admin_dashboard_path, @admin
assert_response :success
end
end

View File

@@ -1,18 +1,16 @@
require 'test_helper'
class Admin::UsersControllerTest < ActionController::TestCase
class Admin::UsersControllerTest < ActionDispatch::IntegrationTest
context "Admin::UsersController" do
setup do
@mod = FactoryGirl.create(:moderator_user)
CurrentUser.user = @mod
CurrentUser.ip_addr = "127.0.0.1"
@user = FactoryGirl.create(:user)
@admin = FactoryGirl.create(:admin_user)
@mod = create(:moderator_user)
@user = create(:user)
@admin = create(:admin_user)
end
context "#edit" do
should "render" do
get :edit, {:id => @user.id}, {:user_id => @mod.id}
get_auth edit_admin_user_path(@user), @mod
assert_response :success
end
end
@@ -20,7 +18,7 @@ class Admin::UsersControllerTest < ActionController::TestCase
context "#update" do
context "on a basic user" do
should "succeed" do
put :update, {:id => @user.id, :user => {:level => "30"}}, {:user_id => @mod.id}
put_auth admin_user_path(@user), @mod, params: {:user => {:level => "30"}}
assert_redirected_to(edit_admin_user_path(@user))
@user.reload
assert_equal(30, @user.level)
@@ -29,7 +27,7 @@ class Admin::UsersControllerTest < ActionController::TestCase
context "promoted to an admin" do
should "fail" do
put :update, {:id => @user.id, :user => {:level => "50"}}, {:user_id => @mod.id}
put_auth admin_user_path(@user), @mod, params: {:user => {:level => "50"}}
assert_response(403)
@user.reload
assert_equal(20, @user.level)
@@ -39,7 +37,7 @@ class Admin::UsersControllerTest < ActionController::TestCase
context "on an admin user" do
should "fail" do
put :update, {:id => @admin.id, :user => {:level => "30"}}, {:user_id => @mod.id}
put_auth admin_user_path(@admin), @mod, params: {:user => {:level => "30"}}
assert_response(403)
@admin.reload
assert_equal(50, @admin.level)

View File

@@ -1,23 +1,19 @@
require 'test_helper'
class ArtistCommentariesControllerTest < ActionController::TestCase
class ArtistCommentariesControllerTest < ActionDispatch::IntegrationTest
context "The artist commentaries controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:user)
@commentary1 = FactoryGirl.create(:artist_commentary)
@commentary2 = FactoryGirl.create(:artist_commentary)
end
teardown do
CurrentUser.user = nil
as_user do
@commentary1 = create(:artist_commentary)
@commentary2 = create(:artist_commentary)
end
end
context "index action" do
should "render" do
get :index
get artist_commentaries_path
assert_response :success
end
@@ -32,17 +28,17 @@ class ArtistCommentariesControllerTest < ActionController::TestCase
}
}
get :index, params
get artist_commentaries_path(params)
assert_response :success
end
end
context "show action" do
should "render" do
get :show, { id: @commentary1.id }
get artist_commentary_path(@commentary1.id)
assert_redirected_to(@commentary1.post)
get :show, { post_id: @commentary1.post_id }
get artist_commentary_path(post_id: @commentary1.post_id)
assert_redirected_to(@commentary1.post)
end
end
@@ -52,12 +48,15 @@ class ArtistCommentariesControllerTest < ActionController::TestCase
params = {
artist_commentary: {
original_title: "foo",
post_id: FactoryGirl.create(:post).id,
}
post_id: FactoryBot.create(:post).id,
},
format: "js"
}
post :create_or_update, params, { user_id: @user.id }
assert_redirected_to(ArtistCommentary.find_by_post_id(params[:artist_commentary][:post_id]))
assert_difference("ArtistCommentary.count", 1) do
put_auth create_or_update_artist_commentaries_path(params), @user, as: :js
end
assert_response :success
end
should "render for update" do
@@ -65,11 +64,13 @@ class ArtistCommentariesControllerTest < ActionController::TestCase
artist_commentary: {
post_id: @commentary1.post_id,
original_title: "foo",
}
},
format: "js"
}
post :create_or_update, params, { user_id: @user.id }
assert_redirected_to(@commentary1)
put_auth create_or_update_artist_commentaries_path(params), @user
@commentary1.reload
assert_response :success
assert_equal("foo", @commentary1.reload.original_title)
end
end
@@ -78,22 +79,20 @@ class ArtistCommentariesControllerTest < ActionController::TestCase
should "work" do
original_title = @commentary1.original_title
@commentary1.update(original_title: "foo")
post :revert, { :id => @commentary1.post_id, :version_id => @commentary1.versions(true).first.id }, {:user_id => @user.id}
assert_redirected_to(@commentary1)
@commentary1.reload
put_auth revert_artist_commentary_path(@commentary1.post_id, version_id: @commentary1.versions.first.id, format: "js"), @user
assert_response :success
assert_equal(original_title, @commentary1.reload.original_title)
end
should "return 404 when trying to revert a nonexistent commentary" do
post :revert, { :id => -1, :version_id => -1 }, {:user_id => @user.id}
put_auth revert_artist_commentary_path(-1, version_id: -1, format: "js"), @user
assert_response 404
end
should "not allow reverting to a previous version of another artist commentary" do
post :revert, { :id => @commentary1.post_id, :version_id => @commentary2.versions(true).first.id }, {:user_id => @user.id}
put_auth revert_artist_commentary_path(@commentary1.post_id, version_id: @commentary2.versions.first.id, format: "js"), @user
@commentary1.reload
assert_not_equal(@commentary1.original_title, @commentary2.original_title)
assert_response :missing
end

View File

@@ -1,10 +1,19 @@
require 'test_helper'
class ArtistCommentaryVersionsControllerTest < ActionController::TestCase
class ArtistCommentaryVersionsControllerTest < ActionDispatch::IntegrationTest
context "The artist commentary versions controller" do
setup do
@user = FactoryBot.create(:user)
as_user do
@commentary1 = FactoryBot.create(:artist_commentary)
@commentary2 = FactoryBot.create(:artist_commentary)
end
end
context "index action" do
should "render" do
get :index
get artist_commentary_versions_path
assert_response :success
end
end

View File

@@ -1,25 +1,21 @@
require 'test_helper'
class ArtistVersionsControllerTest < ActionController::TestCase
class ArtistVersionsControllerTest < ActionDispatch::IntegrationTest
context "An artist versions controller" do
setup do
CurrentUser.user = FactoryGirl.create(:gold_user)
CurrentUser.ip_addr = "127.0.0.1"
@artist = FactoryGirl.create(:artist)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = FactoryBot.create(:gold_user)
as_user do
@artist = create(:artist)
end
end
should "get the index page" do
get :index, {}, {:user_id => CurrentUser.id}
get_auth artist_versions_path, @user
assert_response :success
end
should "get the index page when searching for something" do
get :index, {:search => {:name => @artist.name}}, {:user_id => CurrentUser.id}
get_auth artist_versions_path(search: {name: @artist.name}), @user
assert_response :success
end
end

View File

@@ -1,164 +1,157 @@
require 'test_helper'
class ArtistsControllerTest < ActionController::TestCase
def assert_artist_found(expected_artist, source_url)
get :finder, { :format => :json, :url => source_url }, { :user_id => @user.id }
class ArtistsControllerTest < ActionDispatch::IntegrationTest
def assert_artist_found(expected_artist, source_url = nil)
if source_url
get_auth finder_artists_path(format: "json", url: source_url), @user
end
assert_response :success
assert_equal(1, assigns(:artists).size, "Testing URL: #{source_url}")
assert_equal(expected_artist, assigns(:artists).first.name)
json = JSON.parse(response.body)
assert_equal(1, json.size, "Testing URL: #{source_url}")
assert_equal(expected_artist, json[0]["name"])
end
def assert_artist_not_found(source_url)
get :finder, { :format => :json, :url => source_url }, { :user_id => @user.id }
get_auth finder_artists_path(format: "json", url: source_url), @user
assert_response :success
assert_equal(0, assigns(:artists).size, "Testing URL: #{source_url}")
json = JSON.parse(response.body)
assert_equal(0, json.size, "Testing URL: #{source_url}")
end
context "An artists controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@artist = FactoryGirl.create(:artist, :notes => "message")
@masao = FactoryGirl.create(:artist, :name => "masao", :url_string => "http://www.pixiv.net/member.php?id=32777")
@artgerm = FactoryGirl.create(:artist, :name => "artgerm", :url_string => "http://artgerm.deviantart.com/")
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@admin = create(:admin_user)
@user = create(:user)
as_user do
@artist = create(:artist, notes: "message")
@masao = create(:artist, name: "masao", url_string: "http://www.pixiv.net/member.php?id=32777")
@artgerm = create(:artist, name: "artgerm", url_string: "http://artgerm.deviantart.com/")
end
end
should "get the new page" do
get :new, {}, {:user_id => @user.id}
get_auth new_artist_path, @user
assert_response :success
end
should "get the show_or_new page" do
get :show_or_new, { name: "masao" }, { user_id: @user.id }
should "get the show_or_new page for an existing artist" do
get_auth show_or_new_artists_path(name: "masao"), @user
assert_redirected_to(@masao)
end
get :show_or_new, { name: "nobody" }, { user_id: @user.id }
should "get the show_or_new page for a nonexisting artist" do
get_auth show_or_new_artists_path(name: "nobody"), @user
assert_response :success
end
should "get the edit page" do
get :edit, {:id => @artist.id}, {:user_id => @user.id}
get_auth edit_artist_path(@artist.id), @user
assert_response :success
end
should "get the show page" do
get :show, {:id => @artist.id}
get artist_path(@artist.id)
assert_response :success
end
should "get the show page for a negated tag" do
@artist.update_attribute(:name, "-aaa")
get :show, {:id => @artist.id}
@artist.update(name: "-aaa")
get artist_path(@artist.id)
assert_response :success
end
should "get the banned page" do
get :banned
get banned_artists_path
assert_response :success
end
should "ban an artist" do
CurrentUser.scoped(FactoryGirl.create(:admin_user)) do
put :ban, { id: @artist.id }, { user_id: CurrentUser.id }
end
put_auth ban_artist_path(@artist.id), @admin
assert_redirected_to(@artist)
assert_equal(true, @artist.reload.is_banned)
@artist.reload
assert_equal(true, @artist.is_banned?)
assert_equal(true, TagImplication.exists?(antecedent_name: @artist.name, consequent_name: "banned_artist"))
end
should "unban an artist" do
CurrentUser.scoped(FactoryGirl.create(:admin_user)) do
as_admin do
@artist.ban!
put :unban, { id: @artist.id }, { user_id: CurrentUser.id }
end
put_auth unban_artist_path(@artist.id), @admin
assert_redirected_to(@artist)
assert_equal(false, @artist.reload.is_banned)
@artist.reload
assert_equal(false, @artist.is_banned?)
assert_equal(false, TagImplication.exists?(antecedent_name: @artist.name, consequent_name: "banned_artist"))
end
should "get the index page" do
get :index
get artists_path
assert_response :success
end
context "when searching the index page" do
should "find artists by name" do
get :index, { :name => "masao" }
assert_response :success
assert_equal(1, assigns(:artists).size)
assert_equal("masao", assigns(:artists).first.name)
get artists_path(name: "masao", format: "json")
assert_artist_found("masao")
end
should "find artists by image URL" do
get :index, { :name => "http://i2.pixiv.net/img04/img/syounen_no_uta/46170939_m.jpg" }
assert_response :success
assert_equal(1, assigns(:artists).size)
assert_equal("masao", assigns(:artists).first.name)
get artists_path(name: "http://i2.pixiv.net/img04/img/syounen_no_uta/46170939_m.jpg", format: "json")
assert_artist_found("masao")
end
should "find artists by page URL" do
url = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=46170939"
get :index, { :name => url }
assert_response :success
assert_equal(1, assigns(:artists).size)
assert_equal("masao", assigns(:artists).first.name)
get artists_path(name: url, format: "json")
assert_artist_found("masao")
end
end
should "create an artist" do
attributes = FactoryBot.attributes_for(:artist)
assert_difference("Artist.count", 1) do
attributes = FactoryGirl.attributes_for(:artist)
attributes.delete(:is_active)
post :create, {:artist => attributes}, {:user_id => @user.id}
post_auth artists_path, @user, params: {artist: attributes}
end
artist = Artist.last
assert_redirected_to(artist_path(artist))
artist = Artist.find_by_name(attributes[:name])
assert_not_nil(artist)
assert_redirected_to(artist_path(artist.id))
end
context "with an artist that has notes" do
setup do
@artist = FactoryGirl.create(:artist, :name => "aaa", :notes => "testing")
as(@admin) do
@artist = create(:artist, name: "aaa", notes: "testing")
end
@wiki_page = @artist.wiki_page
@another_user = FactoryGirl.create(:user)
@another_user = create(:user)
end
should "update an artist" do
old_timestamp = @wiki_page.updated_at
Timecop.travel(1.minute.from_now) do
post :update, {:id => @artist.id, :artist => {:notes => "rex"}}, {:user_id => @user.id}
travel_to(1.minute.from_now) do
put_auth artist_path(@artist.id), @user, params: {artist: {notes: "rex"}}
end
@artist.reload
@wiki_page.reload
@wiki_page = @artist.wiki_page
assert_equal("rex", @artist.notes)
assert_not_equal(old_timestamp, @wiki_page.updated_at)
assert_redirected_to(artist_path(@artist))
assert_redirected_to(artist_path(@artist.id))
end
should "not touch the updater_id and updated_at fields when nothing is changed" do
old_timestamp = @wiki_page.updated_at
old_updater_id = @wiki_page.updater_id
Timecop.travel(1.minutes.from_now) do
CurrentUser.scoped(@another_user) do
@artist.update_attributes(notes: "testing")
travel_to(1.minutes.from_now) do
as(@another_user) do
@artist.update(notes: "testing")
end
end
@wiki_page.reload
@artist.reload
@wiki_page = @artist.wiki_page
assert_equal(old_timestamp.to_i, @wiki_page.updated_at.to_i)
assert_equal(old_updater_id, @wiki_page.updater_id)
end
@@ -166,7 +159,7 @@ class ArtistsControllerTest < ActionController::TestCase
context "when renaming an artist" do
should "automatically rename the artist's wiki page" do
assert_difference("WikiPage.count", 0) do
post :update, {:id => @artist.id, :artist => {:name => "bbb", :notes => "more testing"}}, {:user_id => @user.id}
put_auth artist_path(@artist.id), @user, params: {artist: {name: "bbb", notes: "more testing"}}
end
@wiki_page.reload
assert_equal("bbb", @wiki_page.title)
@@ -174,49 +167,50 @@ class ArtistsControllerTest < ActionController::TestCase
end
should "merge the new notes with the existing wiki page's contents if a wiki page for the new name already exists" do
existing_wiki_page = FactoryGirl.create(:wiki_page, :title => "bbb", :body => "xxx")
post :update, {:id => @artist.id, :artist => {:name => "bbb", :notes => "yyy"}}, {:user_id => @user.id}
existing_wiki_page.reload
assert_equal("bbb", existing_wiki_page.title)
assert_equal("xxx\n\nyyy", existing_wiki_page.body)
as_user do
@existing_wiki_page = create(:wiki_page, title: "bbb", body: "xxx")
end
put_auth artist_path(@artist.id), @user, params: {artist: {name: "bbb", notes: "yyy"}}
@existing_wiki_page.reload
assert_equal("bbb", @existing_wiki_page.title)
assert_equal("xxx\n\nyyy", @existing_wiki_page.body)
end
end
end
should "delete an artist" do
CurrentUser.scoped(FactoryGirl.create(:builder_user)) do
delete :destroy, { id: @artist.id }, { user_id: CurrentUser.id }
end
assert_redirected_to(artist_path(@artist))
assert_equal(false, @artist.reload.is_active)
@builder = create(:builder_user)
delete_auth artist_path(@artist.id), @builder
assert_redirected_to(artist_path(@artist.id))
@artist.reload
assert_equal(false, @artist.is_active)
end
should "undelete an artist" do
CurrentUser.scoped(FactoryGirl.create(:builder_user)) do
put :undelete, { id: @artist.id }, { user_id: CurrentUser.id }
end
assert_redirected_to(artist_path(@artist))
@builder = create(:builder_user)
post_auth undelete_artist_path(@artist.id), @builder
assert_redirected_to(artist_path(@artist.id))
assert_equal(true, @artist.reload.is_active)
end
context "reverting an artist" do
should "work" do
@artist.update_attributes(:name => "xyz")
@artist.update_attributes(:name => "abc")
as_user do
@artist.update(name: "xyz")
@artist.update(name: "abc")
end
version = @artist.versions.first
post :revert, {:id => @artist.id, :version_id => version.id}
put_auth revert_artist_path(@artist.id), @user, params: {version_id: version.id}
end
should "not allow reverting to a previous version of another artist" do
@artist2 = FactoryGirl.create(:artist)
post :revert, { :id => @artist.id, :version_id => @artist2.versions(true).first.id }, {:user_id => @user.id}
as_user do
@artist2 = create(:artist)
end
put_auth artist_path(@artist.id), @user, params: {version_id: @artist2.versions.first.id}
@artist.reload
assert_not_equal(@artist.name, @artist2.name)
assert_response :missing
assert_redirected_to(artist_path(@artist.id))
end
end
@@ -233,14 +227,23 @@ class ArtistsControllerTest < ActionController::TestCase
assert_artist_found("artgerm", "http://fc06.deviantart.net/fs71/f/2014/150/d/c/peachy_princess_by_artgerm-d7k7tmu.jpg")
end
should "find pixiv artists" do
should "find pixiv artists for img##" do
assert_artist_found("masao", "http://i2.pixiv.net/img04/img/syounen_no_uta/46170939.jpg")
end
should "find pixiv artists for img-original" do
assert_artist_found("masao", "http://i2.pixiv.net/img-original/img/2014/09/25/00/57/24/46170939_p0.jpg")
end
should "find pixiv artists for member_illust.php" do
assert_artist_found("masao", "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=46170939")
end
should "not fail for malformed Pixiv URLs" do
should "fail for nonexisting illust ids" do
assert_artist_not_found("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=herpderp")
end
should "fail for malformed urls" do
assert_artist_not_found("http://www.pixiv.net/wharrgarbl")
end

View File

@@ -1,50 +1,50 @@
require 'test_helper'
class BansControllerTest < ActionController::TestCase
class BansControllerTest < ActionDispatch::IntegrationTest
context "A bans controller" do
setup do
@mod = FactoryGirl.create(:moderator_user)
CurrentUser.user = @mod
CurrentUser.ip_addr = "127.0.0.1"
@user = FactoryGirl.create(:user)
@ban = FactoryGirl.create(:ban, :user_id => @user.id)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@mod = create(:moderator_user)
@user = create(:user)
as(@mod) do
@ban = create(:ban, user: @user)
end
end
should "get the new page" do
get :new, {}, {:user_id => @mod.id}
get_auth new_ban_path, @mod
assert_response :success
end
should "get the edit page" do
get :edit, {:id => @ban.id}, {:user_id => @mod.id}
get_auth edit_ban_path(@ban.id), @mod
assert_response :success
end
should "get the show page" do
get :show, {:id => @ban.id}
get_auth ban_path(@ban.id), @mod
assert_response :success
end
should "get the index page" do
get :index
get_auth bans_path, @mod
assert_response :success
end
should "search" do
get_auth bans_path(search: {user_name: @user.name}), @mod
assert_response :success
end
should "create a ban" do
assert_difference("Ban.count", 1) do
post :create, {:ban => {:duration => 60, :reason => "xxx", :user_id => @user.id}}, {:user_id => @mod.id}
post_auth bans_path, @mod, params: {ban: {duration: 60, reason: "xxx", 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", :duration => 60}}, {:user_id => @mod.id}
put_auth ban_path(@ban.id), @mod, params: {ban: {reason: "xxx", duration: 60}}
@ban.reload
assert_equal("xxx", @ban.reason)
assert_redirected_to(ban_path(@ban))
@@ -52,7 +52,7 @@ class BansControllerTest < ActionController::TestCase
should "destroy a ban" do
assert_difference("Ban.count", -1) do
post :destroy, {:id => @ban.id}, {:user_id => @mod.id}
delete_auth ban_path(@ban.id), @mod
end
assert_redirected_to(bans_path)
end

View File

@@ -1,15 +1,15 @@
require 'test_helper'
class BulkUpdateRequestsControllerTest < ActionController::TestCase
class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
context "BulkUpdateRequestsController" do
setup do
@user = FactoryGirl.create(:user)
@admin = FactoryGirl.create(:admin_user)
@user = create(:user)
@admin = create(:admin_user)
end
context "#new" do
should "render" do
get :new, {}, {:user_id => @user.id}
get_auth bulk_update_requests_path, @user
assert_response :success
end
end
@@ -17,26 +17,26 @@ class BulkUpdateRequestsControllerTest < ActionController::TestCase
context "#create" do
should "succeed" do
assert_difference("BulkUpdateRequest.count", 1) do
post :create, {:bulk_update_request => {:skip_secondary_validations => "1", :script => "create alias aaa -> bbb", :title => "xxx"}}, {:user_id => @user.id}
post_auth bulk_update_requests_path, @user, params: {bulk_update_request: {skip_secondary_validations: "1", script: "create alias aaa -> bbb", title: "xxx"}}
end
end
end
context "#update" do
setup do
CurrentUser.scoped(@user) do
@bulk_update_request = FactoryGirl.create(:bulk_update_request)
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
should "still handle enabled secondary validations correctly" do
post :update, {:id => @bulk_update_request.id, :bulk_update_request => {:script => "create alias zzz -> 222", :skip_secondary_validations => "0"}}, {:user_id => @user.id}
put_auth bulk_update_request_path(@bulk_update_request.id), @user, params: {bulk_update_request: {script: "create alias zzz -> 222", skip_secondary_validations: "0"}}
@bulk_update_request.reload
assert_equal("create alias zzz -> 222", @bulk_update_request.script)
end
should "still handle disabled secondary validations correctly" do
post :update, {:id => @bulk_update_request.id, :bulk_update_request => {:script => "create alias zzz -> 222", :skip_secondary_validations => "1"}}, {:user_id => @user.id}
put_auth bulk_update_request_path(@bulk_update_request.id), @user, params: {bulk_update_request: {script: "create alias zzz -> 222", skip_secondary_validations: "1"}}
@bulk_update_request.reload
assert_equal("create alias zzz -> 222", @bulk_update_request.script)
end
@@ -44,27 +44,27 @@ class BulkUpdateRequestsControllerTest < ActionController::TestCase
context "#index" do
setup do
CurrentUser.scoped(@user) do
@bulk_update_request = FactoryGirl.create(:bulk_update_request)
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
should "render" do
get :index, {}, {:user_id => @user.id}
get bulk_update_requests_path
assert_response :success
end
end
context "#destroy" do
setup do
CurrentUser.scoped(@user) do
@bulk_update_request = FactoryGirl.create(:bulk_update_request)
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
context "for the creator" do
should "succeed" do
delete :destroy, {:id => @bulk_update_request.id}, {:user_id => @user.id}
delete_auth bulk_update_request_path(@bulk_update_request), @user
@bulk_update_request.reload
assert_equal("rejected", @bulk_update_request.status)
end
@@ -72,19 +72,19 @@ class BulkUpdateRequestsControllerTest < ActionController::TestCase
context "for another member" do
setup do
@another_user = FactoryGirl.create(:user)
@another_user = create(:user)
end
should "fail" do
assert_difference("BulkUpdateRequest.count", 0) do
delete :destroy, {:id => @bulk_update_request.id}, {:user_id => @another_user.id}
delete_auth bulk_update_request_path(@bulk_update_request), @another_user
end
end
end
context "for an admin" do
should "succeed" do
delete :destroy, {:id => @bulk_update_request.id}, {:user_id => @admin.id}
delete_auth bulk_update_request_path(@bulk_update_request), @admin
@bulk_update_request.reload
assert_equal("rejected", @bulk_update_request.status)
end
@@ -93,14 +93,14 @@ class BulkUpdateRequestsControllerTest < ActionController::TestCase
context "#approve" do
setup do
CurrentUser.scoped(@user) do
@bulk_update_request = FactoryGirl.create(:bulk_update_request)
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
context "for a member" do
should "fail" do
post :approve, {:id => @bulk_update_request.id}, {:user_id => @user.id}
post_auth approve_bulk_update_request_path(@bulk_update_request), @user
@bulk_update_request.reload
assert_equal("pending", @bulk_update_request.status)
end
@@ -108,7 +108,7 @@ class BulkUpdateRequestsControllerTest < ActionController::TestCase
context "for an admin" do
should "succeed" do
post :approve, {:id => @bulk_update_request.id}, {:user_id => @admin.id}
post_auth approve_bulk_update_request_path(@bulk_update_request), @admin
@bulk_update_request.reload
assert_equal("approved", @bulk_update_request.status)
end

View File

@@ -1,12 +1,12 @@
require 'test_helper'
class CommentVotesControllerTest < ActionController::TestCase
class CommentVotesControllerTest < ActionDispatch::IntegrationTest
context "A comment votes controller" do
setup do
CurrentUser.user = @user = FactoryGirl.create(:user)
CurrentUser.user = @user = create(:user)
CurrentUser.ip_addr = "127.0.0.1"
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
@comment = FactoryGirl.create(:comment)
@comment = create(:comment)
end
teardown do
@@ -17,16 +17,16 @@ class CommentVotesControllerTest < ActionController::TestCase
context "#create.json" do
should "create a vote" do
assert_difference("CommentVote.count", 1) do
post :create, {:format => "json", :comment_id => @comment.id, :score => 1}, {:user_id => @user.id}
post_auth comment_votes_path(comment_id: @comment.id, format: "json"), @user
assert_response :success
assert_equal("{\"success\": true}", @response.body.strip)
end
end
should "fail silently on errors" do
FactoryGirl.create(:comment_vote, :comment => @comment, :score => -1)
create(:comment_vote, comment: @comment, score: -1)
assert_difference("CommentVote.count", 0) do
post :create, {:format => "json", :comment_id => @comment.id, :score => -1}, {:user_id => @user.id}
post_auth comment_votes_path(comment_id: @comment.id, score: "-1", format: "json"), @user
assert_response 422
assert_equal("{\"success\": false, \"errors\": \"Validation failed: You have already voted for this comment\"}", @response.body.strip)
end
@@ -36,15 +36,15 @@ class CommentVotesControllerTest < ActionController::TestCase
context "#create.js" do
should "create a vote" do
assert_difference("CommentVote.count", 1) do
post :create, {:format => "js", :comment_id => @comment.id, :score => 1}, {:user_id => @user.id}
post_auth comment_votes_path(comment_id: @comment.id, format: "json", score: 1), @user
assert_response :success
end
end
should "fail on errors" do
FactoryGirl.create(:comment_vote, :comment => @comment, :score => -1)
create(:comment_vote, :comment => @comment, :score => -1)
assert_difference("CommentVote.count", 0) do
post :create, {:format => "js", :comment_id => @comment.id, :score => -1}, {:user_id => @user.id}
post_auth comment_votes_path(comment_id: @comment.id, :score => -1, format: "js"), @user
assert_response 422
end
end

View File

@@ -1,18 +1,18 @@
require 'test_helper'
class CommentsControllerTest < ActionController::TestCase
class CommentsControllerTest < ActionDispatch::IntegrationTest
context "A comments controller" do
setup do
@mod = FactoryGirl.create(:moderator_user)
@user = FactoryGirl.create(:member_user)
@mod = FactoryBot.create(:moderator_user)
@user = FactoryBot.create(:member_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
@post = FactoryGirl.create(:post)
@comment = FactoryGirl.create(:comment, :post => @post)
@post = FactoryBot.create(:post)
@comment = FactoryBot.create(:comment, :post => @post)
CurrentUser.scoped(@mod) do
@mod_comment = FactoryGirl.create(:comment, :post => @post)
@mod_comment = FactoryBot.create(:comment, :post => @post)
end
end
@@ -23,43 +23,43 @@ class CommentsControllerTest < ActionController::TestCase
context "index action" do
should "render for post" do
xhr :get, :index, { post_id: @post.id, group_by: "post", format: "js" }
get comments_path(post_id: @post.id, group_by: "post", format: "js")
assert_response :success
end
should "render by post" do
get :index, {:group_by => "post"}
get comments_path(group_by: "post")
assert_response :success
end
should "render by comment" do
get :index, {:group_by => "comment"}
get comments_path(group_by: "comment")
assert_response :success
end
should "render for atom feeds" do
get :index, {:format => "atom"}
get comments_path(format: "atom")
assert_response :success
end
end
context "search action" do
should "render" do
get :search
get search_comments_path
assert_response :success
end
end
context "show action" do
should "render" do
get :show, {:id => @comment.id}
get comment_path(@comment.id)
assert_response :success
end
end
context "edit action" do
should "render" do
get :edit, {:id => @comment.id}, {:user_id => @user.id}
get_auth edit_comment_path(@comment.id), @user
assert_response :success
end
end
@@ -67,13 +67,13 @@ class CommentsControllerTest < ActionController::TestCase
context "update action" do
context "when updating another user's comment" do
should "succeed if updater is a moderator" do
post :update, {:id => @comment.id, :comment => {:body => "abc"}}, {:user_id => @mod.id}
put_auth comment_path(@comment.id), @user, params: {comment: {body: "abc"}}
assert_equal("abc", @comment.reload.body)
assert_redirected_to post_path(@comment.post)
end
should "fail if updater is not a moderator" do
post :update, {:id => @mod_comment.id, :comment => {:body => "abc"}}, {:user_id => @user.id}
put_auth comment_path(@mod_comment.id), @user, params: {comment: {body: "abc"}}
assert_not_equal("abc", @mod_comment.reload.body)
assert_response 403
end
@@ -81,51 +81,43 @@ class CommentsControllerTest < ActionController::TestCase
context "when stickying a comment" do
should "succeed if updater is a moderator" do
CurrentUser.user = @mod
post :update, {:id => @comment.id, :comment => {:is_sticky => true}}, {:user_id => @mod.id}
put_auth comment_path(@comment.id), @mod, params: {comment: {is_sticky: true}}
assert_equal(true, @comment.reload.is_sticky)
assert_redirected_to @comment.post
end
should "fail if updater is not a moderator" do
post :update, {:id => @comment.id, :comment => {:is_sticky => true}}, {:user_id => @user.id}
put_auth comment_path(@comment.id), @user, params: {comment: {is_sticky: true}}
assert_equal(false, @comment.reload.is_sticky)
assert_redirected_to @comment.post
end
end
should "update the body" do
post :update, {:id => @comment.id, :comment => {:body => "abc"}}, {:user_id => @comment.creator_id}
put_auth comment_path(@comment.id), @comment.creator, params: {comment: {body: "abc"}}
assert_equal("abc", @comment.reload.body)
assert_redirected_to post_path(@comment.post)
end
should "allow changing the body and is_deleted" do
params = {
id: @comment.id,
comment: {
body: "herp derp",
do_not_bump_post: true,
is_deleted: true,
post_id: FactoryGirl.create(:post).id,
}
}
post :update, params, { :user_id => @comment.creator_id }
@comment.reload
assert_equal("herp derp", @comment.body)
assert_equal(false, @comment.do_not_bump_post)
put_auth comment_path(@comment.id), @comment.creator, params: {comment: {body: "herp derp", is_deleted: true}}
assert_equal("herp derp", @comment.reload.body)
assert_equal(true, @comment.is_deleted)
assert_equal(@post.id, @comment.post_id)
assert_redirected_to post_path(@post)
end
should "not allow changing do_not_bump_post or post_id" do
as_user do
@another_post = create(:post)
end
put_auth comment_path(@comment.id), @comment.creator, params: {do_not_bump_post: true, post_id: @another_post.id}
assert_equal(false, @comment.reload.do_not_bump_post)
assert_equal(@post.id, @comment.post_id)
end
end
context "new action" do
should "redirect" do
get :new, {}, {:user_id => @user.id}
get_auth new_comment_path, @user
assert_redirected_to comments_path
end
end
@@ -133,31 +125,35 @@ class CommentsControllerTest < ActionController::TestCase
context "create action"do
should "create a comment" do
assert_difference("Comment.count", 1) do
post :create, {:comment => FactoryGirl.attributes_for(:comment, :post_id => @post.id)}, {:user_id => @user.id}
post_auth comments_path, @user, params: {comment: FactoryBot.attributes_for(:comment, post_id: @post.id)}
end
comment = Comment.last
assert_redirected_to post_path(comment.post)
end
should "not allow commenting on nonexistent posts" do
post :create, {:comment => FactoryGirl.attributes_for(:comment, :post_id => -1)}, {:user_id => @user.id}
assert_response :error
assert_difference("Comment.count", 0) do
post_auth comments_path, @user, params: {comment: FactoryBot.attributes_for(:comment, post_id: -1)}
end
assert_redirected_to comments_path
end
end
context "destroy action" do
should "mark comment as deleted" do
delete :destroy, {:id => @comment.id}, {:user_id => @user.id}
delete_auth comment_path(@comment.id), @user
assert_equal(true, @comment.reload.is_deleted)
assert_redirected_to @comment
end
end
context "undelete action" do
setup do
@comment.delete!
end
should "mark comment as undeleted" do
@comment.delete!
put :undelete, { id: @comment.id }, { user_id: @user.id }
post_auth undelete_comment_path(@comment.id), @user
assert_equal(false, @comment.reload.is_deleted)
assert_redirected_to(@comment)
end

View File

@@ -1,10 +1,10 @@
require 'test_helper'
class CountsControllerTest < ActionController::TestCase
class CountsControllerTest < ActionDispatch::IntegrationTest
context "The counts commentary controller" do
context "posts action" do
should "render" do
get :posts
get posts_counts_path
assert_response :success
end
end

View File

@@ -1,10 +1,10 @@
require 'test_helper'
class DelayedJobsControllerTest < ActionController::TestCase
class DelayedJobsControllerTest < ActionDispatch::IntegrationTest
context "The delayed jobs controller" do
context "index action" do
should "render" do
get :index
get delayed_jobs_path
assert_response :success
end
end

View File

@@ -1,13 +1,13 @@
require 'test_helper'
class DmailsControllerTest < ActionController::TestCase
class DmailsControllerTest < ActionDispatch::IntegrationTest
context "The dmails controller" do
setup do
@user = FactoryGirl.create(:user)
@unrelated_user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@dmail = FactoryGirl.create(:dmail, :owner => @user)
@user = create(:user)
@unrelated_user = create(:user)
as_user do
@dmail = create(:dmail, :owner => @user)
end
end
teardown do
@@ -17,55 +17,52 @@ class DmailsControllerTest < ActionController::TestCase
context "new action" do
should "get the page" do
get :new, {}, {:user_id => @user.id}
get_auth new_dmail_path, @user
assert_response :success
end
context "with a respond_to_id" do
should "check privileges" do
@user2 = FactoryGirl.create(:user)
get :new, {:respond_to_id => @dmail}, {:user_id => @user2.id}
@user2 = create(:user)
get_auth new_dmail_path, @user2, params: {:respond_to_id => @dmail.id}
assert_response 403
end
should "prefill the fields" do
get :new, {:respond_to_id => @dmail}, {:user_id => @user.id}
get_auth new_dmail_path, @user, params: {:respond_to_id => @dmail.id}
assert_response :success
assert_not_nil assigns(:dmail)
assert_equal(@dmail.from_id, assigns(:dmail).to_id)
end
context "and a forward flag" do
should "not populate the to field" do
get :new, {:respond_to_id => @dmail, :forward => true}, {:user_id => @user.id}
get_auth new_dmail_path, @user, params: {:respond_to_id => @dmail.id, :forward => true}
assert_response :success
assert_not_nil assigns(:dmail)
assert_nil(assigns(:dmail).to_id)
end
end
end
end
context "index action" do
should "show dmails owned by the current user" do
get :index, {:search => {:owner_id => @dmail.owner_id, :folder => "sent"}}, {:user_id => @dmail.owner_id}
should "show dmails owned by the current user by sent" do
get_auth dmails_path, @user, params: {:search => {:owner_id => @dmail.owner_id, :folder => "sent"}}
assert_response :success
assert_equal(1, assigns[:dmails].size)
end
get :index, {:search => {:owner_id => @dmail.owner_id, :folder => "received"}}, {:user_id => @dmail.owner_id}
should "show dmails owned by the current user by received" do
get_auth dmails_path, @user, params: {:search => {:owner_id => @dmail.owner_id, :folder => "received"}}
assert_response :success
assert_equal(1, assigns[:dmails].size)
end
should "not show dmails not owned by the current user" do
get :index, {:search => {:owner_id => @dmail.owner_id}}, {:user_id => @unrelated_user.id}
get_auth dmails_path, @user, params: {:search => {:owner_id => @dmail.owner_id}}
assert_response :success
assert_equal(0, assigns[:dmails].size)
end
should "work for banned users" do
ban = FactoryGirl.create(:ban, :user => @user, :banner => FactoryGirl.create(:admin_user))
get :index, {:search => {:owner_id => @dmail.owner_id, :folder => "sent"}}, {:user_id => @dmail.owner_id}
as(create(:admin_user)) do
create(:ban, :user => @user)
end
get_auth dmails_path, @dmail.owner, params: {:search => {:owner_id => @dmail.owner_id, :folder => "sent"}}
assert_response :success
end
@@ -73,25 +70,25 @@ class DmailsControllerTest < ActionController::TestCase
context "show action" do
should "show dmails owned by the current user" do
get :show, {:id => @dmail.id}, {:user_id => @dmail.owner_id}
get_auth dmail_path(@dmail), @dmail.owner
assert_response :success
end
should "not show dmails not owned by the current user" do
get :show, {:id => @dmail.id}, {:user_id => @unrelated_user.id}
get_auth dmail_path(@dmail), @unrelated_user
assert_response(403)
end
end
context "create action" do
setup do
@user_2 = FactoryGirl.create(:user)
@user_2 = create(:user)
end
should "create two messages, one for the sender and one for the recipient" do
assert_difference("Dmail.count", 2) do
dmail_attribs = {:to_id => @user_2.id, :title => "abc", :body => "abc"}
post :create, {:dmail => dmail_attribs}, {:user_id => @user.id}
post_auth dmails_path, @user, params: {:dmail => dmail_attribs}
assert_redirected_to dmail_path(Dmail.last)
end
end
@@ -100,14 +97,14 @@ class DmailsControllerTest < ActionController::TestCase
context "destroy action" do
should "allow deletion if the dmail is owned by the current user" do
assert_difference("Dmail.count", -1) do
post :destroy, {:id => @dmail.id}, {:user_id => @dmail.owner_id}
delete_auth dmail_path(@dmail), @user
assert_redirected_to dmails_path
end
end
should "not allow deletion if the dmail is not owned by the current user" do
assert_difference("Dmail.count", 0) do
post :destroy, {:id => @dmail.id}, {:user_id => @unrelated_user.id}
delete_auth dmail_path(@dmail), @unrelated_user
end
end
end

View File

@@ -1,10 +1,10 @@
require 'test_helper'
class DtextPreviewsControllerTest < ActionController::TestCase
class DtextPreviewsControllerTest < ActionDispatch::IntegrationTest
context "The dtext previews controller" do
context "create action" do
should "render" do
post :create, { body: "h1. Touhou\n\n* [[touhou]]" }
post dtext_preview_path, params: { body: "h1. Touhou\n\n* [[touhou]]" }
assert_response :success
end
end

View File

@@ -1,38 +1,39 @@
require "test_helper"
module Explore
class PostsControllerTest < ActionController::TestCase
class PostsControllerTest < ActionDispatch::IntegrationTest
context "in all cases" do
setup do
CurrentUser.user = FactoryGirl.create(:user)
CurrentUser.ip_addr = "127.0.0.1"
FactoryGirl.create(:post)
@user = create(:user)
as_user do
create(:post)
end
end
context "#popular" do
should "render" do
get :popular
get popular_explore_posts_path
assert_response :success
end
end
context "#searches" do
should "render" do
get :searches
get searches_explore_posts_path
assert_response :success
end
end
context "#missed_searches" do
should "render" do
get :missed_searches
get missed_searches_explore_posts_path
assert_response :success
end
end
context "#intro" do
should "render" do
get :intro
get intro_explore_posts_path
assert_response :success
end
end

View File

@@ -1,80 +1,75 @@
require 'test_helper'
class FavoriteGroupsControllerTest < ActionController::TestCase
class FavoriteGroupsControllerTest < ActionDispatch::IntegrationTest
context "The favorite groups controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:user)
as_user do
@favgroup = create(:favorite_group)
end
end
context "index action" do
should "render" do
get :index
get favorite_groups_path
assert_response :success
end
end
context "show action" do
should "render" do
favgroup = FactoryGirl.create(:favorite_group)
get :show, { id: favgroup.id }
get favorite_group_path(@favgroup)
assert_response :success
end
end
context "new action" do
should "render" do
get :new, {}, { user_id: @user.id }
get_auth new_favorite_group_path, @user
assert_response :success
end
end
context "create action" do
should "render" do
post :create, { favorite_group: FactoryGirl.attributes_for(:favorite_group) }, { user_id: @user.id }
post_auth favorite_groups_path, @user, params: { favorite_group: FactoryBot.attributes_for(:favorite_group) }
assert_redirected_to favorite_groups_path
end
end
context "edit action" do
should "render" do
favgroup = FactoryGirl.create(:favorite_group, creator: @user)
get :edit, { id: favgroup.id }, { user_id: @user.id }
get_auth edit_favorite_group_path(@favgroup), @user
assert_response :success
end
end
context "update action" do
should "render" do
favgroup = FactoryGirl.create(:favorite_group, creator: @user)
params = { id: favgroup.id, favorite_group: { name: "foo" } }
put :update, params, { user_id: @user.id }
assert_redirected_to favgroup
assert_equal("foo", favgroup.reload.name)
params = { favorite_group: { name: "foo" } }
put_auth favorite_group_path(@favgroup), @user, params: params
assert_redirected_to @favgroup
assert_equal("foo", @favgroup.reload.name)
end
end
context "destroy action" do
should "render" do
favgroup = FactoryGirl.create(:favorite_group, creator: @user)
delete :destroy, { id: favgroup.id }, { user_id: @user.id }
delete_auth favorite_group_path(@favgroup), @user
assert_redirected_to favorite_groups_path
end
end
context "add_post action" do
should "render" do
favgroup = FactoryGirl.create(:favorite_group, creator: @user)
post = FactoryGirl.create(:post)
as_user do
@post = FactoryBot.create(:post)
end
put :add_post, { id: favgroup.id, post_id: post.id, format: "js" }, { user_id: @user.id }
put_auth add_post_favorite_group_path(@favgroup), @user, params: {post_id: @post.id, format: "js"}
assert_response :success
assert_equal([post.id], favgroup.reload.post_id_array)
@favgroup.reload
assert_equal([@post.id], @favgroup.post_id_array)
end
end
end

View File

@@ -1,59 +1,51 @@
require 'test_helper'
class FavoritesControllerTest < ActionController::TestCase
class FavoritesControllerTest < ActionDispatch::IntegrationTest
context "The favorites controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:user)
end
context "index action" do
setup do
@post = FactoryGirl.create(:post)
@post = create(:post)
@post.add_favorite!(@user)
end
context "with a specified tags parameter" do
should "redirect to the posts controller" do
get :index, {:tags => "fav:#{@user.name} abc"}, {:user_id => @user}
get_auth favorites_path, @user, params: {:tags => "fav:#{@user.name} abc"}
assert_redirected_to(posts_path(:tags => "fav:#{@user.name} abc"))
end
end
should "display the current user's favorites" do
get :index, {}, {:user_id => @user.id}
get_auth favorites_path, @user
assert_response :success
assert_not_nil(assigns(:favorite_set))
end
end
context "create action" do
setup do
@post = FactoryGirl.create(:post)
@post = create(:post)
end
should "create a favorite for the current user" do
assert_difference("Favorite.count", 1) do
post :create, {:format => "js", :post_id => @post.id}, {:user_id => @user.id}
post_auth favorites_path, @user, params: {:format => "js", :post_id => @post.id}
end
end
end
context "destroy action" do
setup do
@post = FactoryGirl.create(:post)
@post = create(:post)
@post.add_favorite!(@user)
end
should "remove the favorite from the current user" do
assert_difference("Favorite.count", -1) do
post :destroy, {:format => "js", :id => @post.id}, {:user_id => @user.id}
delete_auth favorite_path(@post.id), @user, params: {:format => "js"}
end
end
end

View File

@@ -1,96 +1,91 @@
require 'test_helper'
class ForumPostsControllerTest < ActionController::TestCase
class ForumPostsControllerTest < ActionDispatch::IntegrationTest
context "The forum posts controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@other_user = FactoryGirl.create(:user)
@mod = FactoryGirl.create(:moderator_user)
@forum_topic = FactoryGirl.create(:forum_topic, :title => "my forum topic", :creator => @user)
@forum_post = FactoryGirl.create(:forum_post, :topic_id => @forum_topic.id, :body => "xxx")
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:user)
@other_user = create(:user)
@mod = create(:moderator_user)
as_user do
@forum_topic = create(:forum_topic, :title => "my forum topic")
@forum_post = create(:forum_post, :topic_id => @forum_topic.id, :body => "xxx")
end
end
context "index action" do
should "list all forum posts" do
get :index
get forum_posts_path
assert_response :success
end
context "with search conditions" do
should "list all matching forum posts" do
get :index, {:search => {:body_matches => "xxx"}}
get forum_posts_path, params: {:search => {:body_matches => "xxx"}}
assert_response :success
assert_equal(1, assigns(:forum_posts).size)
assert_select "#forum-post-#{@forum_post.id}"
end
should "list nothing for when the search matches nothing" do
get :index, {:search => {:body_matches => "bababa"}}
get forum_posts_path, params: {:search => {:body_matches => "bababa"}}
assert_response :success
assert_equal(0, assigns(:forum_posts).size)
assert_select "#forum-post-#{@forum_post.id}", false
end
should "list by creator id" do
get :index, {:search => {:creator_id => @user.id}}
get forum_posts_path, params: {:search => {:creator_id => @user.id}}
assert_response :success
assert_equal(1, assigns(:forum_posts).size)
assert_select "#forum-post-#{@forum_post.id}"
end
end
context "with private topics" do
setup do
CurrentUser.user = @mod
@mod_topic = FactoryGirl.create(:mod_up_forum_topic)
@mod_posts = 2.times.map do
FactoryGirl.create(:forum_post, :topic_id => @mod_topic.id)
as(@mod) do
@mod_topic = create(:mod_up_forum_topic)
@mod_posts = 2.times.map do
create(:forum_post, :topic_id => @mod_topic.id)
end
end
@mod_post_ids = ([@forum_post] + @mod_posts).map(&:id).reverse
end
should "list only permitted posts for members" do
CurrentUser.user = @user
get :index, {}, { :user_id => @user.id }
get forum_posts_path
assert_response :success
assert_equal([@forum_post.id], assigns(:forum_posts).map(&:id))
assert_select "#forum-post-#{@forum_post.id}"
assert_select "#forum-post-#{@mod_posts[0].id}", false
end
should "list only permitted posts for mods" do
CurrentUser.user = @mod
get :index, {}, { :user_id => @mod.id }
get_auth forum_posts_path, @mod
assert_response :success
assert_equal(@mod_post_ids, assigns(:forum_posts).map(&:id))
assert_select "#forum-post-#{@mod_posts[0].id}"
end
end
end
context "edit action" do
should "render if the editor is the creator of the topic" do
get :edit, {:id => @forum_post.id}, {:user_id => @user.id}
get_auth edit_forum_post_path(@forum_post), @user
assert_response :success
end
should "render if the editor is a moderator" do
get :edit, {:id => @forum_post.id}, {:user_id => @mod.id}
get_auth edit_forum_post_path(@forum_post), @mod
assert_response :success
end
should "fail if the editor is not the creator of the topic and is not a moderator" do
get :edit, {:id => @forum_post.id}, {:user_id => @other_user.id}
get_auth edit_forum_post_path(@forum_post), @other_user
assert_response(403)
end
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id, :topic_id => @forum_topic.id}, {:user_id => @user.id}
get_auth new_forum_post_path, @user, params: {:topic_id => @forum_topic.id}
assert_response :success
end
end
@@ -98,7 +93,7 @@ class ForumPostsControllerTest < ActionController::TestCase
context "create action" do
should "create a new forum post" do
assert_difference("ForumPost.count", 1) do
post :create, {:forum_post => {:body => "xaxaxa", :topic_id => @forum_topic.id}}, {:user_id => @user.id}
post_auth forum_posts_path, @user, params: {:forum_post => {:body => "xaxaxa", :topic_id => @forum_topic.id}}
end
forum_post = ForumPost.last
@@ -108,8 +103,7 @@ class ForumPostsControllerTest < ActionController::TestCase
context "destroy action" do
should "destroy the posts" do
CurrentUser.user = @mod
post :destroy, {:id => @forum_post.id}, {:user_id => @mod.id}
delete_auth forum_post_path(@forum_post), @mod
assert_redirected_to(forum_post_path(@forum_post))
@forum_post.reload
assert_equal(true, @forum_post.is_deleted?)
@@ -118,12 +112,13 @@ class ForumPostsControllerTest < ActionController::TestCase
context "undelete action" do
setup do
@forum_post.update_attribute(:is_deleted, true)
as(@mod) do
@forum_post.update(is_deleted: true)
end
end
should "restore the post" do
CurrentUser.user = @mod
post :undelete, {:id => @forum_post.id}, {:user_id => @mod.id}
post_auth undelete_forum_post_path(@forum_post), @mod
assert_redirected_to(forum_post_path(@forum_post))
@forum_post.reload
assert_equal(false, @forum_post.is_deleted?)

View File

@@ -1,136 +1,149 @@
require 'test_helper'
class ForumTopicsControllerTest < ActionController::TestCase
class ForumTopicsControllerTest < ActionDispatch::IntegrationTest
context "The forum topics controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@other_user = FactoryGirl.create(:user)
@mod = FactoryGirl.create(:moderator_user)
@forum_topic = FactoryGirl.create(:forum_topic, :title => "my forum topic", :creator => @user, :original_post_attributes => {:body => "xxx"})
end
@user = create(:user)
@other_user = create(:user)
@mod = create(:moderator_user)
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
as_user do
@forum_topic = create(:forum_topic, :title => "my forum topic", :original_post_attributes => {:body => "xxx"})
end
end
context "for a level restricted topic" do
setup do
CurrentUser.user = @mod
@forum_topic.update_attribute(:min_level, User::Levels::MODERATOR)
CurrentUser.user = @user
as(@mod) do
@forum_topic.update(min_level: User::Levels::MODERATOR)
end
end
should "not allow users to see the topic" do
get :show, {:id => @forum_topic.id}
get_auth forum_topic_path(@forum_topic), @user
assert_redirected_to forum_topics_path
end
should "not bump the forum for users without access" do
@gold_user = FactoryGirl.create(:gold_user)
CurrentUser.user = @gold_user
@gold_user = create(:gold_user)
# An open topic should bump...
@open_topic = FactoryGirl.create(:forum_topic)
assert_equal(true, @gold_user.has_forum_been_updated?)
as(@gold_user) do
@open_topic = create(:forum_topic)
end
@gold_user.reload
as(@gold_user) do
assert(@gold_user.has_forum_been_updated?)
end
# Marking it as read should clear it...
CurrentUser.scoped(@gold_user) do
post :mark_all_as_read, {}, {:user_id => @gold_user.id}
as(@gold_user) do
post_auth mark_all_as_read_forum_topics_path, @gold_user
end
@gold_user.reload
assert_redirected_to(forum_topics_path)
assert_equal(false, @gold_user.reload.has_forum_been_updated?)
as(@gold_user) do
assert(!@gold_user.has_forum_been_updated?)
end
# Then adding an unread private topic should not bump.
CurrentUser.scoped(@mod) do
FactoryGirl.create(:forum_post, :topic_id => @forum_topic.id)
as(@mod) do
create(:forum_post, :topic_id => @forum_topic.id)
end
@gold_user.reload
as(@gold_user) do
assert_equal(false, @gold_user.has_forum_been_updated?)
end
assert_equal(false, @gold_user.reload.has_forum_been_updated?)
end
end
context "show action" do
should "render" do
get :show, {:id => @forum_topic.id}
get forum_topic_path(@forum_topic)
assert_response :success
end
should "record a topic visit for html requests" do
get :show, {id: @forum_topic.id}, {user_id: @user.id}
assert_not_nil(@user.reload.last_forum_read_at)
get_auth forum_topic_path(@forum_topic), @user
@user.reload
assert_not_nil(@user.last_forum_read_at)
end
should "not record a topic visit for non-html requests" do
get :show, {id: @forum_topic.id, format: :json}, {user_id: @user.id}
assert_nil(@user.reload.last_forum_read_at)
get_auth forum_topic_path(@forum_topic), @user, params: {format: :json}
@user.reload
assert_nil(@user.last_forum_read_at)
end
should "render for atom feed" do
get :show, {:id => @forum_topic.id, :format => :atom}
get forum_topic_path(@forum_topic), params: {:format => :atom}
assert_response :success
end
end
context "index action" do
setup do
@topic1 = FactoryGirl.create(:forum_topic, :is_sticky => true, :creator => @user, :original_post_attributes => {:body => "xxx"})
@topic2 = FactoryGirl.create(:forum_topic, :creator => @user, :original_post_attributes => {:body => "xxx"})
as_user do
@topic1 = create(:forum_topic, :is_sticky => true, :original_post_attributes => {:body => "xxx"})
@topic2 = create(:forum_topic, :original_post_attributes => {:body => "xxx"})
end
end
should "list all forum topics" do
get :index
get forum_topics_path
assert_response :success
end
should "not list stickied topics first for JSON responses" do
get :index, {format: :json}
get forum_topics_path, params: {format: :json}
forum_topics = JSON.parse(response.body)
assert_equal([@topic2.id, @topic1.id, @forum_topic.id], forum_topics.map {|t| t["id"]})
end
should "render for atom feed" do
get :index, {:format => :atom}
get forum_topics_path, params: {:format => :atom}
assert_response :success
end
context "with search conditions" do
should "list all matching forum topics" do
get :index, {:search => {:title_matches => "forum"}}
get forum_topics_path, params: {:search => {:title_matches => "forum"}}
assert_response :success
assert_equal(1, assigns(:forum_topics).size)
assert_select "a.forum-post-link", @forum_topic.title
assert_select "a.forum-post-link", {count: 0, text: @topic1.title}
assert_select "a.forum-post-link", {count: 0, text: @topic2.title}
end
should "list nothing for when the search matches nothing" do
get :index, {:search => {:title_matches => "bababa"}}
get forum_topics_path, params: {:search => {:title_matches => "bababa"}}
assert_response :success
assert_equal(0, assigns(:forum_topics).size)
assert_select "a.forum-post-link", {count: 0, text: @forum_topic.title}
assert_select "a.forum-post-link", {count: 0, text: @topic1.title}
assert_select "a.forum-post-link", {count: 0, text: @topic2.title}
end
end
end
context "edit action" do
should "render if the editor is the creator of the topic" do
get :edit, {:id => @forum_topic.id}, {:user_id => @user.id}
get_auth edit_forum_topic_path(@forum_topic), @user
assert_response :success
end
should "render if the editor is a moderator" do
get :edit, {:id => @forum_topic.id}, {:user_id => @mod.id}
get_auth edit_forum_topic_path(@forum_topic), @mod
assert_response :success
end
should "fail if the editor is not the creator of the topic and is not a moderator" do
get :edit, {:id => @forum_topic.id}, {:user_id => @other_user.id}
get_auth edit_forum_topic_path(@forum_topic), @other_user
assert_response(403)
end
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id}
get_auth new_forum_topic_path, @user
assert_response :success
end
end
@@ -138,7 +151,7 @@ class ForumTopicsControllerTest < ActionController::TestCase
context "create action" do
should "create a new forum topic and post" do
assert_difference(["ForumPost.count", "ForumTopic.count"], 1) do
post :create, {:forum_topic => {:title => "bababa", :original_post_attributes => {:body => "xaxaxa"}}}, {:user_id => @user.id}
post_auth forum_topics_path, @user, params: {:forum_topic => {:title => "bababa", :original_post_attributes => {:body => "xaxaxa"}}}
end
forum_topic = ForumTopic.last
@@ -148,29 +161,31 @@ class ForumTopicsControllerTest < ActionController::TestCase
context "destroy action" do
setup do
@post = FactoryGirl.create(:forum_post, :topic_id => @forum_topic.id)
as_user do
@post = create(:forum_post, :topic_id => @forum_topic.id)
end
end
should "destroy the topic and any associated posts" do
CurrentUser.user = @mod
post :destroy, {:id => @forum_topic.id}, {:user_id => @mod.id}
delete_auth forum_topic_path(@forum_topic), @mod
assert_redirected_to(forum_topic_path(@forum_topic))
@forum_topic.reload
assert_equal(true, @forum_topic.is_deleted?)
assert(@forum_topic.is_deleted?)
end
end
context "undelete action" do
setup do
@forum_topic.update_attribute(:is_deleted, true)
as(@mod) do
@forum_topic.update(is_deleted: true)
end
end
should "restore the topic" do
CurrentUser.user = @mod
post :undelete, {:id => @forum_topic.id}, {:user_id => @mod.id}
post_auth undelete_forum_topic_path(@forum_topic), @mod
assert_redirected_to(forum_topic_path(@forum_topic))
@forum_topic.reload
assert_equal(false, @forum_topic.is_deleted?)
assert(!@forum_topic.is_deleted?)
end
end
end

View File

@@ -1,16 +1,14 @@
require 'test_helper'
class IpBansControllerTest < ActionController::TestCase
class IpBansControllerTest < ActionDispatch::IntegrationTest
context "The ip bans controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@admin = create(:admin_user)
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @admin.id}
get_auth new_ip_ban_path, @admin
assert_response :success
end
end
@@ -18,24 +16,26 @@ class IpBansControllerTest < ActionController::TestCase
context "create action" do
should "create a new ip ban" do
assert_difference("IpBan.count", 1) do
post :create, {:ip_ban => {:ip_addr => "1.2.3.4", :reason => "xyz"}}, {:user_id => @admin.id}
post_auth ip_bans_path, @admin, params: {:ip_ban => {:ip_addr => "1.2.3.4", :reason => "xyz"}}
end
end
end
context "index action" do
setup do
FactoryGirl.create(:ip_ban)
as(@admin) do
create(:ip_ban)
end
end
should "render" do
get :index, {}, {:user_id => @admin.id}
get_auth ip_bans_path, @admin
assert_response :success
end
context "with search parameters" do
should "render" do
get :index, {:search => {:ip_addr => "1.2.3.4"}}, {:user_id => @admin.id}
get_auth ip_bans_path, @admin, params: {:search => {:ip_addr => "1.2.3.4"}}
assert_response :success
end
end
@@ -43,12 +43,14 @@ class IpBansControllerTest < ActionController::TestCase
context "destroy action" do
setup do
@ip_ban = FactoryGirl.create(:ip_ban)
as(@admin) do
@ip_ban = create(:ip_ban)
end
end
should "destroy an ip ban" do
assert_difference("IpBan.count", -1) do
post :destroy, {:id => @ip_ban.id, :format => "js"}, {:user_id => @admin.id}
delete_auth ip_ban_path(@ip_ban), @admin, params: {:format => "js"}
end
end
end

View File

@@ -1,34 +1,33 @@
require 'test_helper'
class IqdbQueriesControllerTest < ActionController::TestCase
class IqdbQueriesControllerTest < ActionDispatch::IntegrationTest
context "The iqdb controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@posts = FactoryGirl.create_list(:post, 2)
@user = create(:user)
as_user do
@posts = FactoryBot.create_list(:post, 2)
end
mock_iqdb_service!
end
context "create action" do
should "render with a post_id" do
mock_iqdb_matches!(@posts[0], @posts)
post :create, { post_id: @posts[0].id, format: "js" }, { user_id: @user.id }
post_auth iqdb_queries_path, @user, params: { post_id: @posts[0].id, format: "js" }
assert_response :success
end
should "render with an url" do
mock_iqdb_matches!(@posts[0].source, @posts)
post :create, { url: @posts[0].source }, { user_id: @user.id }
post_auth iqdb_queries_path, @user, params: { url: @posts[0].source }
assert_response :success
end
should "render for a json response" do
mock_iqdb_matches!(@posts[0].source, @posts)
get :show, { url: @posts[0].source, format: "json" }, { user_id: @user.id }
get_auth iqdb_queries_path, @user, params: { url: @posts[0].source, format: "json" }
assert_response :success
end

View File

@@ -1,21 +1,15 @@
require 'test_helper'
class JanitorTrialsControllerTest < ActionController::TestCase
class JanitorTrialsControllerTest < ActionDispatch::IntegrationTest
context "The janitor trials controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
@user = FactoryGirl.create(:user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
@admin = create(:admin_user)
@user = create(:user)
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @admin.id}
get_auth new_janitor_trial_path, @admin
assert_response :success
end
end
@@ -23,18 +17,20 @@ class JanitorTrialsControllerTest < ActionController::TestCase
context "create action" do
should "create a new janitor trial" do
assert_difference("JanitorTrial.count", 1) do
post :create, {:janitor_trial => {:user_id => @user.id}}, {:user_id => @admin.id}
post_auth janitor_trials_path, @admin, params: {:janitor_trial => {:user_id => @user.id}}
end
end
end
context "promote action" do
setup do
@janitor_trial = FactoryGirl.create(:janitor_trial, :user_id => @user.id)
as(@admin) do
@janitor_trial = create(:janitor_trial, :user_id => @user.id)
end
end
should "promote the janitor trial" do
post :promote, {:id => @janitor_trial.id}, {:user_id => @admin.id}
put_auth promote_janitor_trial_path(@janitor_trial), @admin
@user.reload
assert(@user.can_approve_posts?)
@janitor_trial.reload
@@ -44,11 +40,13 @@ class JanitorTrialsControllerTest < ActionController::TestCase
context "demote action" do
setup do
@janitor_trial = FactoryGirl.create(:janitor_trial, :user_id => @user.id)
as(@admin) do
@janitor_trial = create(:janitor_trial, :user_id => @user.id)
end
end
should "demote the janitor trial" do
post :demote, {:id => @janitor_trial.id}, {:user_id => @admin.id}
put_auth demote_janitor_trial_path(@janitor_trial), @admin
@user.reload
assert(!@user.can_approve_posts?)
@janitor_trial.reload
@@ -58,17 +56,19 @@ class JanitorTrialsControllerTest < ActionController::TestCase
context "index action" do
setup do
FactoryGirl.create(:janitor_trial)
as(@admin) do
create(:janitor_trial)
end
end
should "render" do
get :index, {}, {:user_id => @admin.id}
get_auth janitor_trials_path, @admin
assert_response :success
end
context "with search parameters" do
should "render" do
get :index, {:search => {:user_name => @user.name}}, {:user_id => @admin.id}
get_auth janitor_trials_path, @admin, params: {:search => {:user_name => @user.name}}
assert_response :success
end
end

View File

@@ -2,57 +2,49 @@ require 'test_helper'
module Maintenance
module User
class ApiKeysControllerTest < ActionController::TestCase
def params(password = "password")
{ :user_id => @user.id, :user => { :password => password } }
end
class ApiKeysControllerTest < ActionDispatch::IntegrationTest
context "An api keys controller" do
setup do
@user = FactoryGirl.create(:gold_user, :password => "password")
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:gold_user, :password => "password")
ApiKey.generate!(@user)
end
teardown do
@user.api_key.destroy if @user.api_key
end
context "#show" do
should "render" do
get :show, {:user_id => @user.id}, {:user_id => @user.id}
get_auth maintenance_user_api_key_path, @user, params: {user_id: @user.id}
assert_response :success
end
end
context "#view" do
context "with an incorrect password" do
should "redirect" do
post :view, params("hunter2"), { :user_id => @user.id }
assert_redirected_to(user_api_key_path(@user))
end
end
context "with a correct password" do
should "succeed" do
post :view, params, { :user_id => @user.id }
post_auth view_maintenance_user_api_key_path(user_id: @user.id), @user, params: {user: {password: "password"}}
assert_response :success
end
should "generate an API key if the user didn't already have one" do
@user.api_key.destroy
# hard to test this in integrationtest
# context "if the user doesn't already have an api key" do
# setup do
# ::User.any_instance.stubs(:api_key).returns(nil)
# cookies[:user_name] = @user.name
# cookies[:password_hash] = @user.bcrypt_cookie_password_hash
# end
assert_difference("ApiKey.count", 1) do
post :view, params, { :user_id => @user.id }
end
# should "generate one" do
# ApiKey.expects(:generate!)
assert_not_nil(@user.reload.api_key)
end
# assert_difference("ApiKey.count", 1) do
# post view_maintenance_user_api_key_path(user_id: @user.id), params: {user: {password: "password"}}
# end
# assert_not_nil(@user.reload.api_key)
# end
# end
should "not generate another API key if the user already has one" do
assert_difference("ApiKey.count", 0) do
post :view, params, { :user_id => @user.id }
post_auth view_maintenance_user_api_key_path(user_id: @user.id), @user, params: {user: {password: "password"}}
end
end
end
@@ -61,14 +53,14 @@ module Maintenance
context "#update" do
should "regenerate the API key" do
old_key = @user.api_key
post :update, params, { :user_id => @user.id }
put_auth maintenance_user_api_key_path, @user, params: {user_id: @user.id, user: {password: "password"}}
assert_not_equal(old_key.key, @user.reload.api_key.key)
end
end
context "#destroy" do
should "delete the API key" do
post :destroy, params, { :user_id => @user.id }
delete_auth maintenance_user_api_key_path, @user, params: {user_id: @user.id, user: {password: "password"}}
assert_nil(@user.reload.api_key)
end
end

View File

@@ -2,24 +2,22 @@ require "test_helper"
module Maintenance
module User
class DeletionsControllerTest < ActionController::TestCase
class DeletionsControllerTest < ActionDispatch::IntegrationTest
context "in all cases" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:user)
end
context "#show" do
should "render" do
get :show, {}, {:user_id => @user.id}
get_auth maintenance_user_deletion_path, @user
assert_response :success
end
end
context "#destroy" do
should "render" do
post :destroy, {:password => "password"}, {:user_id => @user.id}
delete_auth maintenance_user_deletion_path, @user, params: {:password => "password"}
assert_redirected_to(posts_path)
end
end

View File

@@ -2,24 +2,21 @@ require "test_helper"
module Maintenance
module User
class DmailFiltersControllerTest < ActionController::TestCase
class DmailFiltersControllerTest < ActionDispatch::IntegrationTest
context "The dmail filters controller" do
setup do
@user1 = FactoryGirl.create(:user)
@user2 = FactoryGirl.create(:user)
CurrentUser.user = @user1
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user1 = create(:user)
@user2 = create(:user)
end
context "update action" do
should "not allow a user to create a filter belonging to another user" do
@dmail = FactoryGirl.create(:dmail, :owner => @user1)
setup do
as(@user1) do
@dmail = create(:dmail, owner: @user1)
end
end
should "not allow a user to create a filter belonging to another user" do
params = {
:dmail_id => @dmail.id,
:dmail_filter => {
@@ -28,10 +25,8 @@ module Maintenance
}
}
post :update, params, { :user_id => @user1.id }
put_auth maintenance_user_dmail_filter_path, @user1, params: params
assert_not_equal("owned", @user2.reload.dmail_filter.try(&:words))
assert_redirected_to(@dmail)
end
end
end

View File

@@ -2,17 +2,15 @@ require "test_helper"
module Maintenance
module User
class EmailChangesControllerTest < ActionController::TestCase
class EmailChangesControllerTest < ActionDispatch::IntegrationTest
context "in all cases" do
setup do
@user = FactoryGirl.create(:user, :email => "bob@ogres.net")
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:user, :email => "bob@ogres.net")
end
context "#new" do
should "render" do
get :new, {}, {:user_id => @user.id}
get_auth new_maintenance_user_email_change_path, @user
assert_response :success
end
end
@@ -20,7 +18,7 @@ module Maintenance
context "#create" do
context "with the correct password" do
should "work" do
post :create, {:email_change => {:password => "password", :email => "abc@ogres.net"}}, {:user_id => @user.id}
post_auth maintenance_user_email_change_path, @user, params: {:email_change => {:password => "password", :email => "abc@ogres.net"}}
assert_redirected_to(edit_user_path(@user))
@user.reload
assert_equal("abc@ogres.net", @user.email)
@@ -29,7 +27,7 @@ module Maintenance
context "with the incorrect password" do
should "not work" do
post :create, {:email_change => {:password => "passwordx", :email => "abc@ogres.net"}}, {:user_id => @user.id}
post_auth maintenance_user_email_change_path, @user, params: {:email_change => {:password => "passwordx", :email => "abc@ogres.net"}}
@user.reload
assert_equal("bob@ogres.net", @user.email)
end

View File

@@ -2,37 +2,28 @@ require "test_helper"
module Maintenance
module User
class LoginRemindersControllerTest < ActionController::TestCase
class LoginRemindersControllerTest < ActionDispatch::IntegrationTest
context "A login reminder controller" do
setup do
@user = FactoryGirl.create(:user)
@blank_email_user = FactoryGirl.create(:user, :email => "")
CurrentUser.user = nil
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:user)
@blank_email_user = create(:user, :email => "")
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.deliveries.clear
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "render the new page" do
get :new
get new_maintenance_user_login_reminder_path
assert_response :success
end
should "deliver an email with the login to the user" do
post :create, {:user => {:email => @user.email}}
assert_equal(flash[:notice], "Email sent")
post maintenance_user_login_reminder_path, params: {:user => {:email => @user.email}}
assert_equal(1, ActionMailer::Base.deliveries.size)
end
context "for a user with a blank email" do
should "fail" do
post :create, {:user => {:email => ""}}
assert_equal("Email address not found", flash[:notice])
post maintenance_user_login_reminder_path, params: {:user => {:email => ""}}
@blank_email_user.reload
assert_equal(@blank_email_user.created_at.to_i, @blank_email_user.updated_at.to_i)
assert_equal(0, ActionMailer::Base.deliveries.size)

View File

@@ -2,30 +2,23 @@ require "test_helper"
module Maintenance
module User
class PasswordResetsControllerTest < ActionController::TestCase
class PasswordResetsControllerTest < ActionDispatch::IntegrationTest
context "A password resets controller" do
setup do
@user = FactoryGirl.create(:user, :email => "abc@com.net")
CurrentUser.user = nil
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:user, :email => "abc@com.net")
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.deliveries.clear
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "render the new page" do
get :new
get new_maintenance_user_password_reset_path
assert_response :success
end
context "create action" do
context "given invalid parameters" do
setup do
post :create, {:nonce => {:email => ""}}
post maintenance_user_password_reset_path, params: {:nonce => {:email => ""}}
end
should "not create a new nonce" do
@@ -43,7 +36,7 @@ module Maintenance
context "given valid parameters" do
setup do
post :create, {:nonce => {:email => @user.email}}
post maintenance_user_password_reset_path, params: {:nonce => {:email => @user.email}}
end
should "create a new nonce" do
@@ -63,7 +56,7 @@ module Maintenance
context "edit action" do
context "with invalid parameters" do
setup do
get :edit, :email => "a@b.c"
get edit_maintenance_user_password_reset_path, params: {:email => "a@b.c"}
end
should "succeed silently" do
@@ -73,10 +66,10 @@ module Maintenance
context "with valid parameters" do
setup do
@user = FactoryGirl.create(:user)
@nonce = FactoryGirl.create(:user_password_reset_nonce, :email => @user.email)
@user = create(:user)
@nonce = create(:user_password_reset_nonce, :email => @user.email)
ActionMailer::Base.deliveries.clear
get :edit, :email => @nonce.email, :key => @nonce.key
get edit_maintenance_user_password_reset_path, params: {:email => @nonce.email, :key => @nonce.key}
end
should "succeed" do
@@ -86,23 +79,13 @@ module Maintenance
end
context "update action" do
context "with invalid parameters" do
setup do
get :update
end
should "fail" do
assert_redirected_to new_maintenance_user_password_reset_path
end
end
context "with valid parameters" do
setup do
@user = FactoryGirl.create(:user)
@nonce = FactoryGirl.create(:user_password_reset_nonce, :email => @user.email)
@user = create(:user)
@nonce = create(:user_password_reset_nonce, :email => @user.email)
ActionMailer::Base.deliveries.clear
@old_password = @user.bcrypt_password_hash
post :update, :email => @nonce.email, :key => @nonce.key
put maintenance_user_password_reset_path, params: {:email => @nonce.email, :key => @nonce.key}
end
should "succeed" do

View File

@@ -1,10 +1,10 @@
require 'test_helper'
class MetaSearchesControllerTest < ActionController::TestCase
class MetaSearchesControllerTest < ActionDispatch::IntegrationTest
context "The meta searches controller" do
context "tags action" do
should "work" do
get :tags, { name: "long_hair" }
get meta_searches_tags_path, params: {name: "long_hair"}
assert_response :success
end
end

View File

@@ -1,10 +1,10 @@
require 'test_helper'
class ModActionsControllerTest < ActionController::TestCase
class ModActionsControllerTest < ActionDispatch::IntegrationTest
context "The mod actions controller" do
context "index action" do
should "work" do
get :index
get mod_actions_path
assert_response :success
end
end

View File

@@ -1,130 +1,141 @@
require 'test_helper'
module Moderator
class DashboardsControllerTest < ActionController::TestCase
class DashboardsControllerTest < ActionDispatch::IntegrationTest
context "The moderator dashboards controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
travel_to(1.month.ago) do
@user = create(:gold_user)
end
@admin = create(:admin_user)
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
end
context "show action" do
context "for mod actions" do
setup do
@mod_action = FactoryGirl.create(:mod_action)
as(@admin) do
@mod_action = create(:mod_action)
end
end
should "render" do
assert_equal(1, ModAction.count)
get :show, {}, {:user_id => @admin.id}
get_auth moderator_dashboard_path, @admin
assert_response :success
end
end
context "for user feedbacks" do
setup do
@feedback = FactoryGirl.create(:user_feedback)
as(@user) do
@feedback = create(:user_feedback)
end
end
should "render" do
assert_equal(1, UserFeedback.count)
get :show, {}, {:user_id => @admin.id}
get_auth moderator_dashboard_path, @admin
assert_response :success
end
end
context "for wiki pages" do
setup do
@wiki_page = FactoryGirl.create(:wiki_page)
as(@user) do
@wiki_page = create(:wiki_page)
end
end
should "render" do
assert_equal(1, WikiPageVersion.count)
get :show, {}, {:user_id => @admin.id}
get_auth moderator_dashboard_path, @admin
assert_response :success
end
end
context "for tags and uploads" do
setup do
@post = FactoryGirl.create(:post)
as(@user) do
@post = create(:post)
end
end
should "render" do
assert_equal(1, PostArchive.count)
get :show, {}, {:user_id => @admin.id}
get_auth moderator_dashboard_path, @admin
assert_response :success
end
end
context "for notes"do
setup do
@post = FactoryGirl.create(:post)
@note = FactoryGirl.create(:note, :post_id => @post.id)
as(@user) do
@post = create(:post)
@note = create(:note, :post_id => @post.id)
end
end
should "render" do
assert_equal(1, NoteVersion.count)
get :show, {}, {:user_id => @admin.id}
get_auth moderator_dashboard_path, @admin
assert_response :success
end
end
context "for comments" do
setup do
@users = (0..5).map {FactoryGirl.create(:user)}
@users = (0..5).map {create(:user)}
CurrentUser.scoped(@users[0], "1.2.3.4") do
@comment = FactoryGirl.create(:comment)
CurrentUser.as(@users[0]) do
@comment = create(:comment)
end
@users.each do |user|
CurrentUser.scoped(user, "1.2.3.4") do
CurrentUser.as(user) do
@comment.vote!(-1)
end
end
end
should "render" do
get :show, {}, {:user_id => @admin.id}
get_auth moderator_dashboard_path, @admin
assert_response :success
end
end
context "for artists" do
setup do
@artist = FactoryGirl.create(:artist)
as(@user) do
@artist = create(:artist)
end
end
should "render" do
get :show, {}, {:user_id => @admin.id}
assert_equal(1, ArtistVersion.count)
get_auth moderator_dashboard_path, @admin
assert_response :success
end
end
context "for flags" do
setup do
@post = FactoryGirl.create(:post)
@post.flag!("blah")
as(@user) do
@post = create(:post)
@post.flag!("blah")
end
end
should "render" do
get :show, {}, {:user_id => @admin.id}
get_auth moderator_dashboard_path, @admin
assert_response :success
end
end
context "for appeals" do
setup do
@post = FactoryGirl.create(:post, :is_deleted => true)
@post.appeal!("blah")
as(@user) do
@post = create(:post, :is_deleted => true)
@post.appeal!("blah")
end
end
should "render" do
get :show, {}, {:user_id => @admin.id}
get_auth moderator_dashboard_path, @admin
assert_response :success
end
end

View File

@@ -1,34 +0,0 @@
require 'test_helper'
module Moderator
class InvitationsControllerTest < ActionController::TestCase
context "The invitations controller" do
setup do
@mod = FactoryGirl.create(:moderator_user)
CurrentUser.user = @mod
CurrentUser.ip_addr = "127.0.0.1"
@user_1 = FactoryGirl.create(:user)
@user_2 = FactoryGirl.create(:user, :inviter_id => @mod.id)
end
should "render the new page" do
get :new, {:invitation => {:name => @user_1.name}}, {:user_id => @mod.id}
assert_response :success
end
should "create a new invite" do
post :create, {:invitation => {:user_id => @user_1.id, :level => User::Levels::BUILDER, :can_upload_free => "1"}}, {:user_id => @mod.id}
assert_redirected_to(moderator_invitations_path)
@user_1.reload
assert_equal(User::Levels::BUILDER, @user_1.level)
assert_equal(true, @user_1.can_upload_free?)
end
should "list invites" do
get :index, {}, {:user_id => @mod.id}
assert_response :success
end
end
end
end

View File

@@ -1,40 +1,38 @@
require 'test_helper'
module Moderator
class IpAddrsControllerTest < ActionController::TestCase
class IpAddrsControllerTest < ActionDispatch::IntegrationTest
context "The ip addrs controller" do
setup do
PoolArchive.delete_all
PostArchive.delete_all
@user = FactoryGirl.create(:moderator_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
FactoryGirl.create(:comment)
end
travel_to(1.month.ago) do
@user = create(:moderator_user)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
as_user do
create(:comment)
end
end
should "find by ip addr" do
get :index, {:search => {:ip_addr => "127.0.0.1"}}, {:user_id => @user.id}
get_auth moderator_ip_addrs_path, @user, params: {:search => {:ip_addr => "127.0.0.1"}}
assert_response :success
end
should "find by user id" do
get :index, {:search => {:user_id => @user.id.to_s}}, {:user_id => @user.id}
get_auth moderator_ip_addrs_path, @user, params: {:search => {:user_id => @user.id.to_s}}
assert_response :success
end
should "find by user name" do
get :index, {:search => {:user_name => @user.name}}, {:user_id => @user.id}
get_auth moderator_ip_addrs_path, @user, params: {:search => {:user_name => @user.name}}
assert_response :success
end
should "render the search page" do
get :search, {}, {:user_id => @user.id}
get_auth search_moderator_ip_addrs_path, @user
assert_response :success
end
end

View File

@@ -2,19 +2,18 @@ require 'test_helper'
module Moderator
module Post
class ApprovalsControllerTest < ActionController::TestCase
class ApprovalsControllerTest < ActionDispatch::IntegrationTest
context "The moderator post approvals controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post, :is_pending => true)
@admin = create(:admin_user)
as_admin do
@post = create(:post, :is_pending => true)
end
end
context "create action" do
should "render" do
post :create, {:post_id => @post.id, :format => "js"}, {:user_id => @admin.id}
post_auth moderator_post_approval_path, @admin, params: {:post_id => @post.id, :format => "js"}
assert_response :success
@post.reload
assert(!@post.is_pending?)

View File

@@ -2,20 +2,19 @@ require 'test_helper'
module Moderator
module Post
class DisapprovalsControllerTest < ActionController::TestCase
class DisapprovalsControllerTest < ActionDispatch::IntegrationTest
context "The moderator post disapprovals controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post, :is_pending => true)
@admin = create(:admin_user)
as_user do
@post = create(:post, :is_pending => true)
end
end
context "create action" do
should "render" do
assert_difference("PostDisapproval.count", 1) do
post :create, {:post_id => @post.id, :format => "js"}, {:user_id => @admin.id}
post_auth moderator_post_disapproval_path, @admin, params: { post_disapproval: { post_id: @post.id, reason: "breaks_rules" }, format: "js" }
end
assert_response :success
end

View File

@@ -2,44 +2,47 @@ require 'test_helper'
module Moderator
module Post
class PostsControllerTest < ActionController::TestCase
class PostsControllerTest < ActionDispatch::IntegrationTest
context "The moderator posts controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@admin = create(:admin_user)
travel_to(1.month.ago) do
@user = create(:gold_user)
end
as_user do
@post = create(:post)
end
end
context "confirm_delete action" do
should "render" do
get :confirm_delete, { id: @post.id }, { user_id: @admin.id }
get_auth confirm_delete_moderator_post_post_path(@post), @admin
assert_response :success
end
end
context "delete action" do
should "render" do
post :delete, {:id => @post.id, :reason => "xxx", :format => "js", :commit => "Delete"}, {:user_id => @admin.id}
post_auth delete_moderator_post_post_path(@post), @admin, params: {:reason => "xxx", :format => "js", :commit => "Delete"}
assert(@post.reload.is_deleted?)
end
should "work even if the deleter has flagged the post previously" do
PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
post :delete, {:id => @post.id, :reason => "xxx", :format => "js", :commit => "Delete"}, {:user_id => @admin.id}
as_user do
PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
end
post_auth delete_moderator_post_post_path(@post), @admin, params: {:reason => "xxx", :format => "js", :commit => "Delete"}
assert(@post.reload.is_deleted?)
end
end
context "undelete action" do
should "render" do
@post.update(is_deleted: true)
post :undelete, {:id => @post.id, :format => "js"}, {:user_id => @admin.id}
as_user do
@post.update(is_deleted: true)
end
post_auth undelete_moderator_post_post_path(@post), @admin, params: {:format => "js"}
assert_response :success
assert(!@post.reload.is_deleted?)
@@ -48,41 +51,38 @@ module Moderator
context "confirm_move_favorites action" do
should "render" do
get :confirm_move_favorites, { id: @post.id }, { user_id: @admin.id }
get_auth confirm_ban_moderator_post_post_path(@post), @admin
assert_response :success
end
end
context "move_favorites action" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@admin = create(:admin_user)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "render" do
as_user do
@parent = create(:post)
@child = create(:post, parent: @parent)
end
users = FactoryBot.create_list(:user, 2)
users.each do |u|
@child.add_favorite!(u)
end
should "1234 render" do
parent = FactoryGirl.create(:post)
child = FactoryGirl.create(:post, parent: parent)
users = FactoryGirl.create_list(:user, 2)
users.each { |u| child.add_favorite!(u) }
put :move_favorites, { id: child.id, commit: "Submit" }, { user_id: @admin.id }
CurrentUser.user = @admin
assert_redirected_to(child)
assert_equal(users, parent.reload.favorited_users)
assert_equal([], child.reload.favorited_users)
post_auth move_favorites_moderator_post_post_path(@child.id), @admin, params: { commit: "Submit" }
assert_redirected_to(@child)
as(@admin) do
assert_equal(users, @parent.reload.favorited_users)
assert_equal([], @child.reload.favorited_users)
end
end
end
context "expunge action" do
should "render" do
put :expunge, { id: @post.id, format: "js" }, { user_id: @admin.id }
post_auth expunge_moderator_post_post_path(@post), @admin, params: { format: "js" }
assert_response :success
assert_equal(false, ::Post.exists?(@post.id))
@@ -91,14 +91,14 @@ module Moderator
context "confirm_ban action" do
should "render" do
get :confirm_ban, { id: @post.id }, { user_id: @admin.id }
get_auth confirm_ban_moderator_post_post_path(@post), @admin
assert_response :success
end
end
context "ban action" do
should "render" do
put :ban, { id: @post.id, commit: "Ban", format: "js" }, { user_id: @admin.id }
post_auth ban_moderator_post_post_path(@post), @admin, params: { commit: "Ban", format: "js" }
assert_response :success
assert_equal(true, @post.reload.is_banned?)
@@ -108,7 +108,7 @@ module Moderator
context "unban action" do
should "render" do
@post.ban!
put :unban, { id: @post.id, format: "js" }, { user_id: @admin.id }
post_auth unban_moderator_post_post_path(@post), @admin, params: { format: "js" }
assert_redirected_to(@post)
assert_equal(false, @post.reload.is_banned?)

View File

@@ -2,26 +2,26 @@ require 'test_helper'
module Moderator
module Post
class QueuesControllerTest < ActionController::TestCase
class QueuesControllerTest < ActionDispatch::IntegrationTest
context "The moderator post queues controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post, :is_pending => true)
@admin = create(:admin_user)
@user = create(:user)
as_user do
@post = create(:post, :is_pending => true)
end
end
context "show action" do
should "render" do
get :show, {}, {:user_id => @admin.id}
get_auth moderator_post_queue_path, @admin
assert_response :success
end
end
context "random action" do
should "render" do
get :random, {}, {:user_id => @admin.id}
get_auth moderator_post_queue_path, @admin
assert_response :success
end
end

View File

@@ -1,27 +1,27 @@
require 'test_helper'
module Moderator
class TagsControllerTest < ActionController::TestCase
class TagsControllerTest < ActionDispatch::IntegrationTest
context "The tags controller" do
setup do
@user = FactoryGirl.create(:moderator_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post)
@user = create(:moderator_user)
as_user do
@post = create(:post)
end
end
should "render the edit action" do
get :edit, {}, {:user_id => @user.id}
get_auth edit_moderator_tag_path, @user
assert_response :success
end
should "execute the update action" do
post :update, {:tag => {:predicate => "aaa", :consequent => "bbb"}}, {:user_id => @user.id}
put_auth moderator_tag_path, @user, params: {:tag => {:predicate => "aaa", :consequent => "bbb"}}
assert_redirected_to edit_moderator_tag_path
end
should "fail gracefully if the update action fails" do
post :update, {:tag => {:predicate => "", :consequent => "bbb"}}, {:user_id => @user.id}
put_auth moderator_tag_path, @user, params: {:tag => {:predicate => "", :consequent => "bbb"}}
assert_redirected_to edit_moderator_tag_path
end
end

View File

@@ -1,58 +1,57 @@
require 'test_helper'
class NewsUpdatesControllerTest < ActionController::TestCase
class NewsUpdatesControllerTest < ActionDispatch::IntegrationTest
context "the news updates controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@news_update = FactoryGirl.create(:news_update)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@admin = create(:admin_user)
as(@admin) do
@news_update = create(:news_update)
end
end
context "index action" do
should "render" do
get :index, {}, :user_id => @admin.id
get_auth news_updates_path, @admin
assert_response :success
end
end
context "new action" do
should "render" do
get :new, {}, :user_id => @admin.id
get_auth new_news_update_path, @admin
assert_response :success
end
end
context "edit action" do
should "render" do
get :edit, {:id => @news_update.id}, {:user_id => @admin.id}
get_auth edit_news_update_path(@news_update), @admin
assert_response :success
end
end
context "update action" do
should "work" do
post :update, {:id => @news_update.id, :news_update => {:message => "zzz"}}, {:user_id => @admin.id}
put_auth news_update_path(@news_update), @admin, params: {:news_update => {:message => "zzz"}}
assert_redirected_to(news_updates_path)
end
end
context "create action" do
should "work" do
post :create, {:news_update => {:message => "zzz"}}, {:user_id => @admin.id}
assert_difference("NewsUpdate.count") do
post_auth news_updates_path, @admin, params: {:news_update => {:message => "zzz"}}
end
assert_redirected_to(news_updates_path)
end
end
context "destroy action" do
should "work" do
post :destroy, {:id => @news_update.id, :format => "js"}, {:user_id => @admin.id}
assert_response :success
assert_difference("NewsUpdate.count", -1) do
delete_auth news_update_path(@news_update), @admin
end
assert_redirected_to(news_updates_path)
end
end
end

View File

@@ -1,10 +1,10 @@
require 'test_helper'
class NotePreviewsControllerTest < ActionController::TestCase
class NotePreviewsControllerTest < ActionDispatch::IntegrationTest
context "The note previews controller" do
context "show action" do
should "work" do
get :show, { body: "<b>test</b>", format: "json" }
get note_previews_path, params: { body: "<b>test</b>", format: "json" }
assert_response :success
end
end

View File

@@ -1,22 +1,17 @@
require 'test_helper'
class NoteVersionsControllerTest < ActionController::TestCase
class NoteVersionsControllerTest < ActionDispatch::IntegrationTest
context "The note versions controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:user)
end
context "index action" do
setup do
@note = FactoryGirl.create(:note)
@user_2 = FactoryGirl.create(:user)
as_user do
@note = create(:note)
end
@user_2 = create(:user)
CurrentUser.scoped(@user_2, "1.2.3.4") do
@note.update_attributes(:body => "1 2")
@@ -28,17 +23,13 @@ class NoteVersionsControllerTest < ActionController::TestCase
end
should "list all versions" do
get :index
get note_versions_path
assert_response :success
assert_not_nil(assigns(:note_versions))
assert_equal(3, assigns(:note_versions).size)
end
should "list all versions that match the search criteria" do
get :index, {:search => {:updater_id => @user_2.id}}
get note_versions_path, params: {:search => {:updater_id => @user_2.id}}
assert_response :success
assert_not_nil(assigns(:note_versions))
assert_equal(1, assigns(:note_versions).size)
end
end
end

View File

@@ -1,21 +1,17 @@
require 'test_helper'
class NotesControllerTest < ActionController::TestCase
class NotesControllerTest < ActionDispatch::IntegrationTest
context "The notes controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@note = FactoryGirl.create(:note, body: "000")
end
teardown do
CurrentUser.user = nil
@user = create(:user)
as_user do
@note = create(:note, body: "000")
end
end
context "index action" do
should "list all notes" do
get :index
get notes_path
assert_response :success
end
@@ -32,14 +28,14 @@ class NotesControllerTest < ActionController::TestCase
}
}
get :index, params
get notes_path, params: params
assert_response :success
end
end
context "show action" do
should "render" do
get :show, { id: @note.id, format: "json" }
get note_path(@note), params: { format: "json" }
assert_response :success
end
end
@@ -47,53 +43,58 @@ class NotesControllerTest < ActionController::TestCase
context "create action" do
should "create a note" do
assert_difference("Note.count", 1) do
@post = FactoryGirl.create(:post)
post :create, {:note => {:x => 0, :y => 0, :width => 10, :height => 10, :body => "abc", :post_id => @post.id}, :format => :json}, {:user_id => @user.id}
as_user do
@post = create(:post)
end
post_auth notes_path, @user, params: {:note => {:x => 0, :y => 0, :width => 10, :height => 10, :body => "abc", :post_id => @post.id}, :format => :json}
end
end
end
context "update action" do
should "update a note" do
post :update, {:id => @note.id, :note => {:body => "xyz"}}, {:user_id => @user.id}
put_auth note_path(@note), @user, params: {:note => {:body => "xyz"}}
assert_equal("xyz", @note.reload.body)
end
should "not allow changing the post id to another post" do
@other = FactoryGirl.create(:post)
post :update, {:format => "json", :id => @note.id, :note => {:post_id => @other.id}}, {:user_id => @user.id}
as(@admin) do
@other = create(:post)
end
put_auth note_path(@note), @user, params: {:format => "json", :id => @note.id, :note => {:post_id => @other.id}}
assert_not_equal(@other.id, @note.reload.post_id)
end
end
context "destroy action" do
should "destroy a note" do
post :destroy, {:id => @note.id}, {:user_id => @user.id}
delete_auth note_path(@note), @user
assert_equal(false, @note.reload.is_active?)
end
end
context "revert action" do
setup do
Timecop.travel(1.day.from_now) do
@note.update_attributes(:body => "111")
end
Timecop.travel(2.days.from_now) do
@note.update_attributes(:body => "222")
as_user do
travel_to(1.day.from_now) do
@note.update(:body => "111")
end
travel_to(2.days.from_now) do
@note.update(:body => "222")
end
end
end
should "revert to a previous version" do
post :revert, {:id => @note.id, :version_id => @note.versions(true).first.id}, {:user_id => @user.id}
put_auth revert_note_path(@note), @user, params: {:version_id => @note.versions.first.id}
assert_equal("000", @note.reload.body)
end
should "not allow reverting to a previous version of another note" do
@note2 = FactoryGirl.create(:note, :body => "note 2")
post :revert, { :id => @note.id, :version_id => @note2.versions(true).first.id }, {:user_id => @user.id}
as_user do
@note2 = create(:note, :body => "note 2")
end
put_auth revert_note_path(@note), @user, params: { :version_id => @note2.versions.first.id }
assert_not_equal(@note.reload.body, @note2.body)
assert_response :missing
end

View File

@@ -1,33 +1,32 @@
require 'test_helper'
class PoolElementsControllerTest < ActionController::TestCase
class PoolElementsControllerTest < ActionDispatch::IntegrationTest
context "The pools posts controller" do
setup do
mock_pool_archive_service!
start_pool_archive_transaction
@user = Timecop.travel(1.month.ago) {FactoryGirl.create(:user)}
@mod = FactoryGirl.create(:moderator_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post)
@pool = FactoryGirl.create(:pool, :name => "abc")
@user = travel_to(1.month.ago) {create(:user)}
@mod = create(:moderator_user)
as_user do
@post = create(:post)
@pool = create(:pool, :name => "abc")
end
end
teardown do
rollback_pool_archive_transaction
CurrentUser.user = nil
end
context "create action" do
should "add a post to a pool" do
post :create, {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}, {:user_id => @user.id}
post_auth pool_element_path, @user, params: {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}
@pool.reload
assert_equal([@post.id], @pool.post_id_array)
end
should "add a post to a pool once and only once" do
@pool.add!(@post)
post :create, {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}, {:user_id => @user.id}
post_auth pool_element_path, @user, params: {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}
@pool.reload
assert_equal([@post.id], @pool.post_id_array)
end
@@ -39,14 +38,16 @@ class PoolElementsControllerTest < ActionController::TestCase
end
should "remove a post from a pool" do
post :destroy, {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}, {:user_id => @user.id}
delete_auth pool_element_path, @user, params: {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}
@pool.reload
assert_equal([], @pool.post_id_array)
end
should "do nothing if the post is not a member of the pool" do
@pool.remove!(@post)
post :destroy, {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}, {:user_id => @user.id}
as_user do
@pool.remove!(@post)
end
delete_auth pool_element_path, @user, params: {:pool_id => @pool.id, :post_id => @post.id, :format => "json"}
@pool.reload
assert_equal([], @pool.post_id_array)
end

View File

@@ -1,48 +1,50 @@
require 'test_helper'
class PoolVersionsControllerTest < ActionController::TestCase
class PoolVersionsControllerTest < ActionDispatch::IntegrationTest
context "The pool versions controller" do
setup do
mock_pool_archive_service!
start_pool_archive_transaction
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:user)
end
teardown do
rollback_pool_archive_transaction
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "index action" do
setup do
@pool = FactoryGirl.create(:pool)
@user_2 = FactoryGirl.create(:user)
@user_3 = FactoryGirl.create(:user)
as_user do
@pool = create(:pool)
end
@user_2 = create(:user)
@user_3 = create(:user)
CurrentUser.scoped(@user_2, "1.2.3.4") do
@pool.update_attributes(:post_ids => "1 2")
@pool.update(:post_ids => "1 2")
end
CurrentUser.scoped(@user_3, "5.6.7.8") do
@pool.update_attributes(:post_ids => "1 2 3 4")
@pool.update(:post_ids => "1 2 3 4")
end
@versions = @pool.versions
end
should "list all versions" do
get :index
get pool_versions_path
assert_response :success
assert_not_nil(assigns(:pool_versions))
assert_equal(3, assigns(:pool_versions).size)
assert_select "#pool-version-#{@versions[0].id}"
assert_select "#pool-version-#{@versions[1].id}"
assert_select "#pool-version-#{@versions[2].id}"
end
should "list all versions that match the search criteria" do
get :index, {:search => {:updater_id => @user_2.id}}
get pool_versions_path, params: {:search => {:updater_id => @user_2.id}}
assert_response :success
assert_not_nil(assigns(:pool_versions))
assert_equal(1, assigns(:pool_versions).size)
assert_select "#pool-version-#{@versions[0].id}", false
assert_select "#pool-version-#{@versions[1].id}"
assert_select "#pool-version-#{@versions[2].id}", false
end
end
end

View File

@@ -1,63 +1,55 @@
require 'test_helper'
class PoolsControllerTest < ActionController::TestCase
class PoolsControllerTest < ActionDispatch::IntegrationTest
context "The pools controller" do
setup do
Timecop.travel(1.month.ago) do
@user = FactoryGirl.create(:user)
@mod = FactoryGirl.create(:moderator_user)
end
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post)
mock_pool_archive_service!
PoolArchive.sqs_service.stubs(:merge?).returns(false)
start_pool_archive_transaction
travel_to(1.month.ago) do
@user = create(:user)
@mod = create(:moderator_user)
end
as_user do
@post = create(:post)
@pool = create(:pool)
end
end
teardown do
rollback_pool_archive_transaction
CurrentUser.user = nil
end
context "index action" do
setup do
FactoryGirl.create(:pool, :name => "abc")
end
should "list all pools" do
get :index
get pools_path
assert_response :success
end
should "list all pools (with search)" do
get :index, {:search => {:name_matches => "abc"}}
get pools_path, params: {:search => {:name_matches => @pool.name}}
assert_response :success
end
end
context "show action" do
setup do
@pool = FactoryGirl.create(:pool)
end
should "render" do
get :show, {:id => @pool.id}
get pool_path(@pool)
assert_response :success
end
end
context "gallery action" do
should "render" do
pool = FactoryGirl.create(:pool)
get :gallery, {:id => pool.id}
get gallery_pools_path
assert_response :success
end
end
context "new action" do
should "render" do
get :new, {}, { user_id: @user.id }
get_auth new_pool_path, @user
assert_response :success
end
end
@@ -65,39 +57,35 @@ class PoolsControllerTest < ActionController::TestCase
context "create action" do
should "create a pool" do
assert_difference("Pool.count", 1) do
post :create, {:pool => {:name => "xxx", :description => "abc"}}, {:user_id => @user.id}
post_auth pools_path, @user, params: {:pool => {:name => "xxx", :description => "abc"}}
end
end
end
context "edit action" do
should "render" do
pool = FactoryGirl.create(:pool)
get :edit, { id: pool.id }, { user_id: @user.id }
get_auth edit_pool_path(@pool), @user
assert_response :success
end
end
context "update action" do
setup do
@pool = FactoryGirl.create(:pool)
should "update a pool" do
put_auth pool_path(@pool), @user, params: { pool: { name: "xyz", post_ids: @post.id.to_s }}
assert_equal("xyz", @pool.reload.name)
assert_equal(@post.id.to_s, @pool.post_ids)
end
should "update a pool" do
post :update, {:id => @pool.id, :pool => {:name => "xyz"}}, {:user_id => @user.id}
@pool.reload
assert_equal("xyz", @pool.name)
should "not allow updating unpermitted attributes" do
put_auth pool_path(@pool), @user, params: { pool: { is_deleted: true, post_count: -42 }}
assert_equal(false, @pool.reload.is_deleted?)
assert_equal(0, @pool.post_count)
end
end
context "destroy action" do
setup do
@pool = FactoryGirl.create(:pool)
end
should "destroy a pool" do
post :destroy, {:id => @pool.id}, {:user_id => @mod.id}
delete_auth pool_path(@pool), @mod
@pool.reload
assert_equal(true, @pool.is_deleted?)
end
@@ -105,13 +93,14 @@ class PoolsControllerTest < ActionController::TestCase
context "undelete action" do
setup do
@pool = FactoryGirl.create(:pool)
@pool.is_deleted = true
@pool.save
as(@mod) do
@pool.is_deleted = true
@pool.save
end
end
should "restore a pool" do
post :undelete, {:id => @pool.id}, {:user_id => @mod.id}
post_auth undelete_pool_path(@pool), @mod
@pool.reload
assert_equal(false, @pool.is_deleted?)
end
@@ -119,28 +108,30 @@ class PoolsControllerTest < ActionController::TestCase
context "revert action" do
setup do
@post_2 = FactoryGirl.create(:post)
@pool = FactoryGirl.create(:pool, :post_ids => "#{@post.id}")
CurrentUser.ip_addr = "1.2.3.4" # this is to get around the version collation
@pool.update_attributes(:post_ids => "#{@post.id} #{@post_2.id}")
CurrentUser.ip_addr = "127.0.0.1"
as_user do
@post_2 = create(:post)
@pool = create(:pool, :post_ids => "#{@post.id}")
end
CurrentUser.scoped(@user, "1.2.3.4") do
@pool.update(:post_ids => "#{@post.id} #{@post_2.id}")
end
end
should "revert to a previous version" do
@pool.reload
version = @pool.versions.first
assert_equal([@post.id], version.post_ids)
post :revert, {:id => @pool.id, :version_id => version.id}, {:user_id => @mod.id}
put_auth revert_pool_path(@pool), @mod, params: {:version_id => version.id}
@pool.reload
assert_equal([@post.id], @pool.post_id_array)
end
should "not allow reverting to a previous version of another pool" do
@pool2 = FactoryGirl.create(:pool)
post :revert, { :id => @pool.id, :version_id => @pool2.versions.first.id }, {:user_id => @user.id}
as_user do
@pool2 = create(:pool)
end
put_auth revert_pool_path(@pool), @user, params: {:version_id => @pool2.versions.first.id }
@pool.reload
assert_not_equal(@pool.name, @pool2.name)
assert_response :missing
end

View File

@@ -1,39 +1,34 @@
require 'test_helper'
class PostAppealsControllerTest < ActionController::TestCase
class PostAppealsControllerTest < ActionDispatch::IntegrationTest
context "The post appeals controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:user)
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id}
get_auth new_post_appeal_path, @user
assert_response :success
end
end
context "index action" do
setup do
@post = FactoryGirl.create(:post, :is_deleted => true)
@post_appeal = FactoryGirl.create(:post_appeal, :post => @post)
as_user do
@post = create(:post, :is_deleted => true)
@post_appeal = create(:post_appeal, :post => @post)
end
end
should "render" do
get :index, {}, {:user_id => @user.id}
get_auth post_appeals_path, @user
assert_response :success
end
context "with search parameters" do
should "render" do
get :index, {:search => {:post_id => @post_appeal.post_id}}, {:user_id => @user.id}
get_auth post_appeals_path, @user, params: {:search => {:post_id => @post_appeal.post_id}}
assert_response :success
end
end
@@ -41,14 +36,15 @@ class PostAppealsControllerTest < ActionController::TestCase
context "create action" do
setup do
@post = FactoryGirl.create(:post, :is_deleted => true)
as_user do
@post = create(:post, :is_deleted => true)
end
end
should "create a new appeal" do
assert_difference("PostAppeal.count", 1) do
post :create, {:format => "js", :post_appeal => {:post_id => @post.id, :reason => "xxx"}}, {:user_id => @user.id}
assert_not_nil(assigns(:post_appeal))
assert_equal([], assigns(:post_appeal).errors.full_messages)
post_auth post_appeals_path, @user, params: {:format => "js", :post_appeal => {:post_id => @post.id, :reason => "xxx"}}
assert_response :success
end
end
end

View File

@@ -1,41 +1,34 @@
require 'test_helper'
class PostEventsControllerTest < ActionController::TestCase
def setup
super
Timecop.travel(2.weeks.ago) do
CurrentUser.user = FactoryGirl.create(:user)
CurrentUser.ip_addr = "127.0.0.1"
class PostEventsControllerTest < ActionDispatch::IntegrationTest
setup do
travel_to(2.weeks.ago) do
@user = create(:user)
@mod = create(:mod_user)
end
@post = FactoryGirl.create(:post)
@post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
@post_appeal = PostAppeal.create(:post => @post, :reason => "aaa")
as_user do
@post = create(:post)
@post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
@post_appeal = PostAppeal.create(:post => @post, :reason => "aaa")
end
end
def teardown
super
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "GET /posts/:post_id/events" do
context "get /posts/:post_id/events" do
should "render" do
get :index, {:post_id => @post.id}, {:user_id => CurrentUser.user.id}
get_auth post_events_path(post_id: @post.id), @user
assert_response :ok
end
should "render for mods" do
get :index, {:post_id => @post.id}, {:user_id => FactoryGirl.create(:moderator_user).id }
get_auth post_events_path(post_id: @post.id), @mod
assert_response :success
end
end
context "GET /posts/:post_id/events.xml" do
context "get /posts/:post_id/events.xml" do
setup do
get :index, {:post_id => @post.id, :format => :xml}, {:user_id => CurrentUser.user.id}
get_auth post_events_path(post_id: @post.id), @user, params: {:format => "xml"}
@xml = Hash.from_xml(response.body)
@appeal = @xml["post_events"].find { |e| e["type"] == "a" }
end

View File

@@ -1,41 +1,36 @@
require 'test_helper'
class PostFlagsControllerTest < ActionController::TestCase
class PostFlagsControllerTest < ActionDispatch::IntegrationTest
context "The post flags controller" do
setup do
Timecop.travel(2.weeks.ago) do
@user = FactoryGirl.create(:user)
travel_to(2.weeks.ago) do
@user = create(:user)
end
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id}
get_auth new_post_flag_path, @user
assert_response :success
end
end
context "index action" do
setup do
@post = FactoryGirl.create(:post)
@post_flag = FactoryGirl.create(:post_flag, :post => @post)
@user.as_current do
@post = create(:post)
@post_flag = create(:post_flag, :post => @post)
end
end
should "render" do
get :index, {}, {:user_id => @user.id}
get_auth post_flags_path, @user
assert_response :success
end
context "with search parameters" do
should "render" do
get :index, {:search => {:post_id => @post_flag.post_id}}, {:user_id => @user.id}
get_auth post_flags_path, @user, params: {:search => {:post_id => @post_flag.post_id}}
assert_response :success
end
end
@@ -43,14 +38,16 @@ class PostFlagsControllerTest < ActionController::TestCase
context "create action" do
setup do
@post = FactoryGirl.create(:post)
@user.as_current do
@post = create(:post)
end
end
should "create a new flag" do
assert_difference("PostFlag.count", 1) do
post :create, {:format => "js", :post_flag => {:post_id => @post.id, :reason => "xxx"}}, {:user_id => @user.id}
assert_not_nil(assigns(:post_flag))
assert_equal([], assigns(:post_flag).errors.full_messages)
assert_difference("PostFlag.count") do
post_auth post_flags_path, @user, params: {:format => "js", :post_flag => {:post_id => @post.id, :reason => "xxx"}}
end
end
end
end

View File

@@ -1,16 +1,15 @@
require 'test_helper'
class PostReplacementsControllerTest < ActionController::TestCase
class PostReplacementsControllerTest < ActionDispatch::IntegrationTest
context "The post replacements controller" do
setup do
Delayed::Worker.delay_jobs = true # don't delete the old images right away
@user = FactoryGirl.create(:moderator_user, can_approve_posts: true, created_at: 1.month.ago)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post)
@post_replacement = FactoryGirl.create(:post_replacement, post_id: @post.id)
@user = create(:moderator_user, can_approve_posts: true, created_at: 1.month.ago)
@user.as_current do
@post = create(:post)
@post_replacement = create(:post_replacement, post: @post)
end
end
teardown do
@@ -27,12 +26,12 @@ class PostReplacementsControllerTest < ActionController::TestCase
}
}
assert_difference("@post.replacements.size") do
post :create, params, { user_id: @user.id }
assert_difference(lambda { @post.replacements.size }) do
post_auth post_replacements_path, @user, params: params
@post.reload
end
Timecop.travel(Time.now + PostReplacement::DELETION_GRACE_PERIOD + 1.day) do
travel_to(Time.now + PostReplacement::DELETION_GRACE_PERIOD + 1.day) do
Delayed::Worker.new.work_off
end
@@ -54,9 +53,8 @@ class PostReplacementsControllerTest < ActionController::TestCase
}
}
put :update, params, { user_id: @user.id }
put_auth post_replacement_path(@post_replacement), @user, params: params
@post_replacement.reload
assert_equal(23, @post_replacement.file_size_was)
assert_equal(42, @post_replacement.file_size)
end
@@ -64,7 +62,7 @@ class PostReplacementsControllerTest < ActionController::TestCase
context "index action" do
should "render" do
get :index, {format: :json}
get post_replacements_path, params: {format: "json"}
assert_response :success
end
end

View File

@@ -1,39 +1,38 @@
require 'test_helper'
class PostVersionsControllerTest < ActionController::TestCase
def setup
super
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
def teardown
super
CurrentUser.user = nil
CurrentUser.ip_addr = nil
class PostVersionsControllerTest < ActionDispatch::IntegrationTest
setup do
@user = create(:user)
end
context "The post versions controller" do
context "index action" do
setup do
@post = FactoryGirl.create(:post)
@post.update_attributes(:tag_string => "1 2", :source => "xxx")
@post.update_attributes(:tag_string => "2 3", :rating => "e")
setup do
@user.as_current do
@post = create(:post)
travel_to(2.hours.from_now) do
@post.update(:tag_string => "1 2", :source => "xxx")
end
travel_to(4.hours.from_now) do
@post.update(:tag_string => "2 3", :rating => "e")
end
@versions = @post.versions
@post2 = create(:post)
end
end
should "list all versions" do
get :index, {}, {:user_id => @user.id}
get_auth post_versions_path, @user
assert_response :success
assert_not_nil(assigns(:post_versions))
assert_select "#post-version-#{@versions[0].id}"
assert_select "#post-version-#{@versions[1].id}"
assert_select "#post-version-#{@versions[2].id}"
end
should "list all versions that match the search criteria" do
get :index, {:search => {:post_id => @post.id}}, {:user_id => @user.id}
get_auth post_versions_path, @user, params: {:search => {:post_id => @post.id}}
assert_response :success
assert_not_nil(assigns(:post_versions))
assert_select "#post-version-#{@post2.versions[0].id}", false
end
end
end

View File

@@ -1,71 +1,52 @@
require 'test_helper'
class PostVotesControllerTest < ActionController::TestCase
class PostVotesControllerTest < ActionDispatch::IntegrationTest
context "The post vote controller" do
setup do
@user = FactoryGirl.create(:gold_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:gold_user)
@user.as_current do
@post = create(:post)
end
end
context "create action" do
should "not allow anonymous users to vote" do
p1 = FactoryGirl.create(:post)
post :create, {:post_id => p1.id, :score => "up", :format => "js"}
post post_votes_path(post_id: @post.id), params: {:score => "up", :format => "js"}
assert_response 403
assert_equal(0, p1.reload.score)
assert_equal(0, @post.reload.score)
end
should "not allow banned users to vote" do
CurrentUser.scoped(FactoryGirl.create(:banned_user)) do
p1 = FactoryGirl.create(:post)
post :create, {:post_id => p1.id, :score => "up", :format => "js"}, {:user_id => CurrentUser.id}
assert_response 403
assert_equal(0, p1.reload.score)
end
@banned = create(:banned_user)
post_auth post_votes_path(post_id: @post.id), @banned, params: {:score => "up", :format => "js"}
assert_response 403
assert_equal(0, @post.reload.score)
end
should "not allow members to vote" do
CurrentUser.scoped(FactoryGirl.create(:member_user)) do
p1 = FactoryGirl.create(:post)
post :create, {:post_id => p1.id, :score => "up", :format => "js"}, {:user_id => CurrentUser.id}
assert_response 403
assert_equal(0, p1.reload.score)
end
@member = create(:member_user)
post_auth post_votes_path(post_id: @post.id), @member, params: {:score => "up", :format => "js"}
assert_response 403
assert_equal(0, @post.reload.score)
end
should "increment a post's score if the score is positive" do
post :create, {:post_id => @post.id, :score => "up", :format => "js"}, {:user_id => @user.id}
post_auth post_votes_path(post_id: @post.id), @user, params: {:score => "up", :format => "js"}
assert_response :success
@post.reload
assert_equal(1, @post.score)
end
context "that fails" do
should "return a 500" do
post :create, {:post_id => @post.id, :score => "up", :format => "json"}, {:user_id => @user.id}
post :create, {:post_id => @post.id, :score => "up", :format => "json"}, {:user_id => @user.id}
assert_equal("{\"success\": false, \"reason\": \"You have already voted for this post\"}", response.body.strip)
end
end
context "for a post that has already been voted on" do
setup do
@post.vote!("up")
@user.as_current do
@post.vote!("up")
end
end
should "fail silently on an error" do
assert_nothing_raised do
post :create, {:post_id => @post.id, :score => "up", :format => "js"}, {:user_id => @user.id}
post_auth post_votes_path(post_id: @post.id), @user, params: {:score => "up", :format => "js"}
end
end
end

View File

@@ -1,36 +1,30 @@
require "test_helper"
class PostsControllerTest < ActionController::TestCase
class PostsControllerTest < ActionDispatch::IntegrationTest
context "The posts controller" do
setup do
@user = Timecop.travel(1.month.ago) {FactoryGirl.create(:user)}
@api_key = ApiKey.generate!(@user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@post = FactoryGirl.create(:post, :uploader_id => @user.id, :tag_string => "aaaa")
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = travel_to(1.month.ago) {create(:user)}
as_user do
@post = create(:post, :tag_string => "aaaa")
end
end
context "for api calls" do
setup do
@api_key = ApiKey.generate!(@user)
end
context "passing the api limit" do
setup do
@post = FactoryGirl.create(:post)
@bucket = TokenBucket.create(user_id: @user.id, token_count: 5, last_touched_at: Time.now)
User.any_instance.stubs(:api_burst_limit).returns(5)
User.any_instance.stubs(:api_regen_multiplier).returns(0)
as_user do
@post = create(:post)
end
TokenBucket.any_instance.stubs(:throttled?).returns(true)
@bucket = TokenBucket.create(user_id: @user.id, token_count: 0, last_touched_at: Time.now)
end
should "work" do
@user.api_burst_limit.times do
post :update, {:format => "json", :id => @post.id, :post => {:rating => "q"}, :login => @user.name, :api_key => @user.api_key.key}
assert_response :success
end
post :update, {:format => "json", :id => @post.id, :post => {:rating => "q"}, :login => @user.name, :api_key => @user.api_key.key}
put post_path(@post), params: {:format => "json", :post => {:rating => "q"}, :login => @user.name, :api_key => @user.api_key.key}
assert_response 429
end
end
@@ -38,39 +32,37 @@ class PostsControllerTest < ActionController::TestCase
context "using http basic auth" do
should "succeed for password matches" do
@basic_auth_string = "Basic #{::Base64.encode64("#{@user.name}:#{@api_key.key}")}"
@request.env['HTTP_AUTHORIZATION'] = @basic_auth_string
get :index, {:format => "json"}
get posts_path, params: {:format => "json"}, headers: {'HTTP_AUTHORIZATION' => @basic_auth_string}
assert_response :success
end
should "fail for password mismatches" do
@basic_auth_string = "Basic #{::Base64.encode64("#{@user.name}:badpassword")}"
@request.env['HTTP_AUTHORIZATION'] = @basic_auth_string
get :index, {:format => "json"}
get posts_path, params: {:format => "json"}, headers: {'HTTP_AUTHORIZATION' => @basic_auth_string}
assert_response 401
end
end
context "using the api_key parameter" do
should "succeed for password matches" do
get :index, {:format => "json", :login => @user.name, :api_key => @api_key.key}
get posts_path, params: {:format => "json", :login => @user.name, :api_key => @api_key.key}
assert_response :success
end
should "fail for password mismatches" do
get :index, {:format => "json", :login => @user.name, :api_key => "bad"}
get posts_path, params: {:format => "json", :login => @user.name, :api_key => "bad"}
assert_response 401
end
end
context "using the password_hash parameter" do
should "succeed for password matches" do
get :index, {:format => "json", :login => @user.name, :password_hash => User.sha1("password")}
get posts_path, params: {:format => "json", :login => @user.name, :password_hash => User.sha1("password")}
assert_response :success
end
# should "fail for password mismatches" do
# get :index, {:format => "json", :login => @user.name, :password_hash => "bad"}
# get posts_path, {:format => "json", :login => @user.name, :password_hash => "bad"}
# assert_response 403
# end
end
@@ -78,20 +70,20 @@ class PostsControllerTest < ActionController::TestCase
context "index action" do
should "render" do
get :index
get posts_path
assert_response :success
end
context "with a search" do
should "render" do
get :index, {:tags => "aaaa"}
get posts_path, params: {:tags => "aaaa"}
assert_response :success
end
end
context "with an md5 param" do
should "render" do
get :index, { md5: @post.md5 }
get posts_path, params: { md5: @post.md5 }
assert_redirected_to(@post)
end
end
@@ -99,33 +91,33 @@ class PostsControllerTest < ActionController::TestCase
context "show_seq action" do
should "render" do
posts = FactoryGirl.create_list(:post, 3)
posts = FactoryBot.create_list(:post, 3)
get :show_seq, { seq: "prev", id: posts[1].id }
get show_seq_post_path(posts[1].id), params: { seq: "prev" }
assert_redirected_to(posts[2])
get :show_seq, { seq: "next", id: posts[1].id }
get show_seq_post_path(posts[1].id), params: { seq: "next" }
assert_redirected_to(posts[0])
end
end
context "random action" do
should "render" do
get :random, { tags: "aaaa" }
get random_posts_path, params: { tags: "aaaa" }
assert_redirected_to(post_path(@post, tags: "aaaa"))
end
end
context "show action" do
should "render" do
get :show, {:id => @post.id}
get post_path(@post), params: {:id => @post.id}
assert_response :success
end
end
context "update action" do
should "work" do
post :update, {:id => @post.id, :post => {:tag_string => "bbb"}}, {:user_id => @user.id}
put_auth post_path(@post), @user, params: {:post => {:tag_string => "bbb"}}
assert_redirected_to post_path(@post)
@post.reload
@@ -133,35 +125,35 @@ class PostsControllerTest < ActionController::TestCase
end
should "ignore restricted params" do
post :update, {:id => @post.id, :post => {:last_noted_at => 1.minute.ago}}, {:user_id => @user.id}
assert_redirected_to post_path(@post)
@post.reload
assert_nil(@post.last_noted_at)
put_auth post_path(@post), @user, params: {:post => {:last_noted_at => 1.minute.ago}}
assert_nil(@post.reload.last_noted_at)
end
end
context "revert action" do
setup do
PostArchive.sqs_service.stubs(:merge?).returns(false)
@post.update_attributes(:tag_string => "zzz")
as_user do
@post.update(tag_string: "zzz")
end
end
should "work" do
@version = @post.versions.first
assert_equal("aaaa", @version.tags)
post :revert, {:id => @post.id, :version_id => @version.id}, {:user_id => @user.id}
put_auth revert_post_path(@post), @user, params: {:version_id => @version.id}
assert_redirected_to post_path(@post)
@post.reload
assert_equal("aaaa", @post.tag_string)
end
should "not allow reverting to a previous version of another post" do
@post2 = FactoryGirl.create(:post, :uploader_id => @user.id, :tag_string => "herp")
as_user do
@post2 = create(:post, :uploader_id => @user.id, :tag_string => "herp")
end
post :revert, { :id => @post.id, :version_id => @post2.versions.first.id }, {:user_id => @user.id}
put_auth revert_post_path(@post), @user, params: { :version_id => @post2.versions.first.id }
@post.reload
assert_not_equal(@post.tag_string, @post2.tag_string)
assert_response :missing
end

View File

@@ -1,10 +1,10 @@
require 'test_helper'
class RelatedTagsControllerTest < ActionController::TestCase
class RelatedTagsControllerTest < ActionDispatch::IntegrationTest
context "The related tags controller" do
context "show action" do
should "work" do
get :show, { query: "touhou" }
get related_tag_path, params: { query: "touhou" }
assert_response :success
end
end

View File

@@ -1,43 +1,27 @@
require 'test_helper'
class ReportsControllerTest < ActionController::TestCase
def setup
super
CurrentUser.user = FactoryGirl.create(:mod_user)
CurrentUser.ip_addr = "127.0.0.1"
session[:user_id] = CurrentUser.user.id
@users = FactoryGirl.create_list(:contributor_user, 2)
@posts = @users.map { |u| FactoryGirl.create(:post, uploader: u) }
end
def teardown
super
CurrentUser.user = nil
CurrentUser.ip_addr = nil
session[:user_id] = nil
end
class ReportsControllerTest < ActionDispatch::IntegrationTest
context "The reports controller" do
context "uploads action" do
should "render" do
get :uploads
assert_response :success
setup do
@mod = create(:mod_user)
@users = FactoryBot.create_list(:contributor_user, 2)
@posts = @users.map do |u|
as(u) do
create(:post)
end
end
end
context "similar_users action" do
context "uploads action" do
should "render" do
#get :similar_users
#assert_response :success
get_auth reports_uploads_path, @mod
assert_response :success
end
end
context "post_versions action" do
should "render" do
get :post_versions
get_auth reports_post_versions_path, @mod
assert_response :success
end
end

View File

@@ -1,55 +1,65 @@
require 'test_helper'
class SavedSearchesControllerTest < ActionController::TestCase
class SavedSearchesControllerTest < ActionDispatch::IntegrationTest
context "The saved searches controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:user)
as_user do
@saved_search = create(:saved_search, user: @user)
end
mock_saved_search_service!
end
context "index action" do
should "render" do
get :index, {}, { user_id: @user.id }
get_auth saved_searches_path, @user
assert_response :success
assert_select "#saved-search-#{@saved_search.id}"
end
end
context "create action" do
should "render" do
params = { saved_search_tags: "bkub", saved_search_labels: "artist" }
post :create, params, { user_id: @user.id }
post_auth saved_searches_path, @user, params: { saved_search: { query: "bkub", label_string: "artist" }}
assert_response :redirect
end
should "disable labels when the disable_labels param is given" do
post_auth saved_searches_path, @user, params: { saved_search: { query: "bkub", disable_labels: "1" }}
assert_equal(true, @user.reload.disable_categorized_saved_searches)
end
end
context "edit action" do
should "render" do
saved_search = FactoryGirl.create(:saved_search, user: @user)
as_user do
@saved_search = create(:saved_search, user: @user)
end
get :edit, { id: saved_search.id }, { user_id: @user.id }
get_auth edit_saved_search_path(@saved_search), @user, params: { id: @saved_search.id }
assert_response :success
end
end
context "update action" do
should "render" do
saved_search = FactoryGirl.create(:saved_search, user: @user)
params = { id: saved_search.id, saved_search: { label_string: "foo" } }
put :update, params, { user_id: @user.id }
as_user do
@saved_search = create(:saved_search, user: @user)
end
params = { id: @saved_search.id, saved_search: { label_string: "foo" } }
put_auth saved_search_path(@saved_search), @user, params: params
assert_redirected_to saved_searches_path
assert_equal(["foo"], saved_search.reload.labels)
assert_equal(["foo"], @saved_search.reload.labels)
end
end
context "destroy action" do
should "render" do
saved_search = FactoryGirl.create(:saved_search, user: @user)
as_user do
@saved_search = create(:saved_search, user: @user)
end
delete :destroy, { id: saved_search.id }, { user_id: @user.id }
delete_auth saved_search_path(@saved_search), @user
assert_redirected_to saved_searches_path
end
end

View File

@@ -1,56 +1,31 @@
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
class SessionsControllerTest < ActionDispatch::IntegrationTest
context "the sessions controller" do
setup do
@user = FactoryGirl.create(:user)
@user = create(:user)
end
context "new action" do
should "render" do
get :new
get new_session_path
assert_response :success
end
end
context "create action" do
should "create a new session" do
post :create, {:name => @user.name, :password => "password"}
post session_path, params: {:name => @user.name, :password => "password"}
assert_redirected_to posts_path
@user.reload
assert_equal(@user.id, session[:user_id])
assert_not_nil(@user.last_ip_addr)
end
should "unban user if user has expired ban" do
CurrentUser.scoped(@user, "127.0.0.1") do
@banned = FactoryGirl.create(:banned_user, ban_duration: 3)
end
travel_to(4.days.from_now) do
post :create, {name: @banned.name, password: "password"}
SessionLoader.new(session, {}, request, {}).load
assert_equal(@banned.id, session[:user_id])
assert_equal(true, @banned.ban_expired?)
assert_equal(false, @banned.reload.is_banned)
end
end
end
context "destroy action" do
setup do
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "clear the session" do
post :destroy, {}, {:user_id => @user.id}
delete_auth session_path, @user
assert_redirected_to posts_path
assert_nil(session[:user_id])
end

View File

@@ -1,15 +1,16 @@
require 'test_helper'
class SourcesControllerTest < ActionController::TestCase
class SourcesControllerTest < ActionDispatch::IntegrationTest
context "The sources controller" do
context "show action" do
should "work for a pixiv URL" do
get :show, { url: "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=14901720", format: "json" }
get source_path, params: { url: "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=14901720", format: "json" }
assert_response :success
end
should "work for a direct twitter URL with referer" do
get :show, {
skip "Twitter keys are not set" if !Danbooru.config.twitter_api_key
get source_path, params: {
url: "https://pbs.twimg.com/media/B4HSEP5CUAA4xyu.png:large",
ref: "https://twitter.com/nounproject/status/540944400767922176",
format: "json"

View File

@@ -1,29 +1,24 @@
require 'test_helper'
class TagAliasCorrectionsControllerTest < ActionController::TestCase
class TagAliasCorrectionsControllerTest < ActionDispatch::IntegrationTest
context "The tag alias correction controller" do
setup do
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@tag_alias = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@admin = create(:admin_user)
as(@admin) do
@tag_alias = create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
context "show action" do
should "render" do
get :show, {:tag_alias_id => @tag_alias.id}, {:user_id => @admin.id}
get_auth tag_alias_correction_path(tag_alias_id: @tag_alias.id), @admin
assert_response :success
end
end
context "create action" do
should "render" do
post :create, {:tag_alias_id => @tag_alias.id, :commit => "Fix"}, {:user_id => @admin.id}
post_auth tag_alias_correction_path(tag_alias_id: @tag_alias.id), @admin, params: {:commit => "Fix"}
assert_redirected_to(tag_alias_correction_path(:tag_alias_id => @tag_alias.id))
end
end

View File

@@ -1,21 +1,14 @@
require 'test_helper'
class TagAliasRequestsControllerTest < ActionController::TestCase
class TagAliasRequestsControllerTest < ActionDispatch::IntegrationTest
context "The tag alias request controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:user)
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id}
get_auth new_tag_alias_request_path, @user
assert_response :success
end
end
@@ -23,7 +16,7 @@ class TagAliasRequestsControllerTest < ActionController::TestCase
context "create action" do
should "render" do
assert_difference("ForumTopic.count", 1) do
post :create, {:tag_alias_request => {:antecedent_name => "aaa", :consequent_name => "bbb", :reason => "ccc", :skip_secondary_validations => true}}, {:user_id => @user.id}
post_auth tag_alias_request_path, @user, params: {:tag_alias_request => {:antecedent_name => "aaa", :consequent_name => "bbb", :reason => "ccc", :skip_secondary_validations => true}}
end
assert_redirected_to(forum_topic_path(ForumTopic.last))
end

View File

@@ -1,47 +1,46 @@
require 'test_helper'
class TagAliasesControllerTest < ActionController::TestCase
class TagAliasesControllerTest < ActionDispatch::IntegrationTest
context "The tag aliases controller" do
setup do
@user = FactoryGirl.create(:admin_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:admin_user)
end
context "edit action" do
setup do
@tag_alias = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
as_admin do
@tag_alias = create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
should "render" do
get :edit, {:id => @tag_alias.id}
get_auth edit_tag_alias_path(@tag_alias), @user
assert_response :success
end
end
context "update action" do
setup do
@tag_alias = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
as_admin do
@tag_alias = create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
context "for a pending alias" do
setup do
@tag_alias.update_attribute(:status, "pending")
as_admin do
@tag_alias.update(status: "pending")
end
end
should "succeed" do
post :update, {:id => @tag_alias.id, :tag_alias => {:antecedent_name => "xxx"}}, {:user_id => @user.id}
put_auth tag_alias_path(@tag_alias), @user, params: {:tag_alias => {:antecedent_name => "xxx"}}
@tag_alias.reload
assert_equal("xxx", @tag_alias.antecedent_name)
end
should "not allow changing the status" do
post :update, {:id => @tag_alias.id, :tag_alias => {:status => "active"}}, {:user_id => @user.id}
put_auth tag_alias_path(@tag_alias), @user, params: {:tag_alias => {:status => "active"}}
@tag_alias.reload
assert_equal("pending", @tag_alias.status)
end
@@ -56,7 +55,7 @@ class TagAliasesControllerTest < ActionController::TestCase
end
should "fail" do
post :update, {:id => @tag_alias.id, :tag_alias => {:antecedent_name => "xxx"}}, {:user_id => @user.id}
put_auth tag_alias_path(@tag_alias), @user, params: {:tag_alias => {:antecedent_name => "xxx"}}
@tag_alias.reload
assert_equal("aaa", @tag_alias.antecedent_name)
end
@@ -65,28 +64,32 @@ class TagAliasesControllerTest < ActionController::TestCase
context "index action" do
setup do
@tag_alias = FactoryGirl.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
as_admin do
@tag_alias = create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
should "list all tag alias" do
get :index, {}, {:user_id => @user.id}
get_auth tag_aliases_path, @user
assert_response :success
end
should "list all tag_alias (with search)" do
get :index, {:search => {:antecedent_name => "aaa"}}, {:user_id => @user.id}
get_auth tag_aliases_path, @user, params: {:search => {:antecedent_name => "aaa"}}
assert_response :success
end
end
context "destroy action" do
setup do
@tag_alias = FactoryGirl.create(:tag_alias)
as_admin do
@tag_alias = create(:tag_alias)
end
end
should "destroy a tag_alias" do
assert_difference("TagAlias.count", -1) do
post :destroy, {:id => @tag_alias.id}, {:user_id => @user.id}
delete_auth tag_alias_path(@tag_alias), @user
end
end
end

View File

@@ -1,23 +1,16 @@
require 'test_helper'
class TagImplicationRequestsControllerTest < ActionController::TestCase
class TagImplicationRequestsControllerTest < ActionDispatch::IntegrationTest
context "The tag implication request controller" do
setup do
Timecop.travel(1.month.ago) do
@user = FactoryGirl.create(:user)
travel_to(1.month.ago) do
@user = create(:user)
end
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id}
get_auth new_tag_implication_request_path, @user
assert_response :success
end
end
@@ -25,7 +18,7 @@ class TagImplicationRequestsControllerTest < ActionController::TestCase
context "create action" do
should "create forum post" do
assert_difference("ForumTopic.count", 1) do
post :create, {:tag_implication_request => {:antecedent_name => "aaa", :consequent_name => "bbb", :reason => "ccc", :skip_secondary_validations => true}}, {:user_id => @user.id}
post_auth tag_implication_request_path, @user, params: {:tag_implication_request => {:antecedent_name => "aaa", :consequent_name => "bbb", :reason => "ccc", :skip_secondary_validations => true}}
end
assert_redirected_to(forum_topic_path(ForumTopic.last))
end
@@ -40,14 +33,10 @@ class TagImplicationRequestsControllerTest < ActionController::TestCase
}
}
post :create, params, {:user_id => @user.id}
tir = assigns(:tag_implication_request)
assert_redirected_to(forum_topic_path(tir.forum_topic))
assert("foo", tir.tag_implication.antecedent_name)
assert("bar", tir.tag_implication.consequent_name)
assert("pending", tir.tag_implication.status)
assert_difference("ForumTopic.count") do
post_auth tag_implication_request_path, @user, params: params
end
assert_redirected_to(forum_topic_path(ForumTopic.last))
end
end
end

View File

@@ -1,54 +1,49 @@
require 'test_helper'
class TagImplicationsControllerTest < ActionController::TestCase
class TagImplicationsControllerTest < ActionDispatch::IntegrationTest
context "The tag implications controller" do
setup do
@user = FactoryGirl.create(:admin_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@user = create(:admin_user)
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "edit action" do
setup do
@tag_implication = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
as_admin do
@tag_implication = create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
should "render" do
get :edit, {:id => @tag_implication.id}
get_auth tag_implication_path(@tag_implication), @user
assert_response :success
end
end
context "update action" do
setup do
@tag_implication = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
as_admin do
@tag_implication = create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
context "for a pending implication" do
setup do
@tag_implication.update_attribute(:status, "pending")
as_admin do
@tag_implication.update(status: "pending")
end
end
should "succeed" do
post :update, {:id => @tag_implication.id, :tag_implication => {:antecedent_name => "xxx"}}, {:user_id => @user.id}
put_auth tag_implication_path(@tag_implication), @user, params: {:tag_implication => {:antecedent_name => "xxx"}}
@tag_implication.reload
assert_equal("xxx", @tag_implication.antecedent_name)
end
should "not allow changing the status" do
post :update, {:id => @tag_implication.id, :tag_implication => {:status => "active"}}, {:user_id => @user.id}
put_auth tag_implication_path(@tag_implication), @user, params: {:tag_implication => {:status => "active"}}
@tag_implication.reload
assert_equal("pending", @tag_implication.status)
end
# TODO: Broken in shoulda-matchers 2.8.0. Need to upgrade to 3.1.1.
should_eventually permit(:antecedent_name, :consequent_name, :forum_topic_id).for(:update)
end
context "for an approved implication" do
@@ -57,7 +52,7 @@ class TagImplicationsControllerTest < ActionController::TestCase
end
should "fail" do
post :update, {:id => @tag_implication.id, :tag_implication => {:antecedent_name => "xxx"}}, {:user_id => @user.id}
put_auth tag_implication_path(@tag_implication), @user, params: {:tag_implication => {:antecedent_name => "xxx"}}
@tag_implication.reload
assert_equal("aaa", @tag_implication.antecedent_name)
end
@@ -66,32 +61,32 @@ class TagImplicationsControllerTest < ActionController::TestCase
context "index action" do
setup do
CurrentUser.scoped(@user, "127.0.0.1") do
@tag_implication = FactoryGirl.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
as_user do
@tag_implication = create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
should "list all tag implications" do
get :index
get tag_implications_path
assert_response :success
end
should "list all tag_implications (with search)" do
get :index, {:search => {:antecedent_name => "aaa"}}
get tag_implications_path, params: {:search => {:antecedent_name => "aaa"}}
assert_response :success
end
end
context "destroy action" do
setup do
CurrentUser.scoped(@user, "127.0.0.1") do
@tag_implication = FactoryGirl.create(:tag_implication)
as_user do
@tag_implication = create(:tag_implication)
end
end
should "destroy a tag_implication" do
assert_difference("TagImplication.count", -1) do
post :destroy, {:id => @tag_implication.id}, {:user_id => @user.id}
delete_auth tag_implication_path(@tag_implication), @user
end
end
end

View File

@@ -1,50 +1,37 @@
require 'test_helper'
class TagsControllerTest < ActionController::TestCase
class TagsControllerTest < ActionDispatch::IntegrationTest
context "The tags controller" do
setup do
@user = FactoryGirl.create(:builder_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:builder_user)
as_user do
@tag = create(:tag, name: "touhou", category: Tag.categories.copyright, post_count: 1)
end
end
context "edit action" do
setup do
@tag = FactoryGirl.create(:tag, :name => "aaa")
end
should "render" do
get :edit, {:id => @tag.id}, {:user_id => @user.id}
get_auth tag_path(@tag), @user, params: {:id => @tag.id}
assert_response :success
end
end
context "index action" do
setup do
@tag = FactoryGirl.create(:tag, name: "aaa", post_count: 1)
end
should "render" do
get :index
get tags_path
assert_response :success
end
context "with search parameters" do
should "render" do
get :index, {:search => {:name_matches => "aaa"}}
get tags_path, params: {:search => {:name_matches => "touhou"}}
assert_response :success
end
end
context "with blank search parameters" do
should "strip the blank parameters with a redirect" do
get :index, { search: { name: "touhou", category: "" } }
get tags_path, params: { search: { name: "touhou", category: "" } }
assert_redirected_to tags_path(search: { name: "touhou" })
end
end
@@ -52,42 +39,72 @@ class TagsControllerTest < ActionController::TestCase
context "autocomplete action" do
should "render" do
FactoryGirl.create(:tag, name: "touhou", post_count: 1)
get :autocomplete, { search: { name_matches: "t" }, format: :json }
get autocomplete_tags_path, params: { search: { name_matches: "t" }, format: :json }
assert_response :success
end
end
context "show action" do
setup do
@tag = FactoryGirl.create(:tag)
end
should "render" do
get :show, {:id => @tag.id}
get tag_path(@tag)
assert_response :success
end
end
context "update action" do
setup do
@tag = FactoryGirl.create(:tag)
@mod = create(:moderator_user)
end
should "update the tag" do
post :update, {:id => @tag.id, :tag => {:category => "1"}}, {:user_id => @user.id}
put_auth tag_path(@tag), @user, params: {:tag => {:category => Tag.categories.general}}
assert_redirected_to tag_path(@tag)
@tag.reload
assert_equal(1, @tag.category)
assert_equal(Tag.categories.general, @tag.reload.category)
end
should "lock the tag for a moderator" do
put_auth tag_path(@tag), @mod, params: { tag: { is_locked: true } }
assert_redirected_to @tag
assert_equal(true, @tag.reload.is_locked)
end
should "not lock the tag for a user" do
put_auth tag_path(@tag), @user, params: {tag: { is_locked: true }}
assert_equal(false, @tag.reload.is_locked)
end
context "for a tag with >50 posts" do
setup do
as_user do
@tag.update(post_count: 100)
end
end
should "not update the category for a member" do
@member = create(:member_user)
put_auth tag_path(@tag), @member, params: {tag: { category: Tag.categories.general }}
assert_not_equal(Tag.categories.general, @tag.reload.category)
end
should "update the category for a builder" do
put_auth tag_path(@tag), @user, params: {tag: { category: Tag.categories.general }}
assert_redirected_to @tag
assert_equal(Tag.categories.general, @tag.reload.category)
end
end
should "not change category when the tag is too large to be changed by a builder" do
@tag.update_columns(post_count: 1001)
post :update, {:id => @tag.id, :tag => {:category => "1"}}, {:user_id => @user.id}
as_user do
@tag.update(category: Tag.categories.general, post_count: 1001)
end
put_auth tag_path(@tag), @user, params: {:tag => {:category => Tag.categories.artist}}
assert_response :forbidden
assert_equal(0, @tag.reload.category)
assert_equal(Tag.categories.general, @tag.reload.category)
end
end
end

View File

@@ -1,34 +1,24 @@
require 'test_helper'
class UploadsControllerTest < ActionController::TestCase
def setup
super
mock_iqdb_service!
end
class UploadsControllerTest < ActionDispatch::IntegrationTest
context "The uploads controller" do
setup do
@user = FactoryGirl.create(:contributor_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:contributor_user)
mock_iqdb_service!
end
context "batch action" do
context "for twitter galleries" do
should "render" do
get :batch, {:url => "https://twitter.com/lvlln/status/567054278486151168"}, {:user_id => @user.id}
skip "Twitter keys are not set" unless Danbooru.config.twitter_api_key
get_auth batch_uploads_path, @user, params: {:url => "https://twitter.com/lvlln/status/567054278486151168"}
assert_response :success
end
end
context "for pixiv ugoira galleries" do
should "render" do
get :batch, {:url => "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=59523577"}, {:user_id => @user.id}
get_auth batch_uploads_path, @user, params: {:url => "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=59523577"}
assert_response :success
assert_no_match(/59523577_ugoira0\.jpg/, response.body)
end
@@ -37,46 +27,47 @@ class UploadsControllerTest < ActionController::TestCase
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id}
get_auth new_upload_path, @user
assert_response :success
end
context "for a twitter post" do
setup do
get :new, {:url => "https://twitter.com/frappuccino/status/566030116182949888"}, {:user_id => @user.id}
end
should "render" do
skip "Twitter keys are not set" unless Danbooru.config.twitter_api_key
get_auth new_upload_path, @user, params: {:url => "https://twitter.com/frappuccino/status/566030116182949888"}
assert_response :success
end
end
context "for a post that has already been uploaded" do
setup do
@post = FactoryGirl.create(:post, :source => "aaa")
as_user do
@post = create(:post, :source => "aaa")
end
end
should "initialize the post" do
get :new, {:url => "aaa"}, {:user_id => @user.id}
get_auth new_upload_path, @user, params: {:url => "http://google.com/aaa"}
assert_response :success
assert_not_nil(assigns(:post))
end
end
end
context "index action" do
setup do
@upload = FactoryGirl.create(:source_upload)
as_user do
@upload = create(:source_upload)
end
end
should "render" do
get :index, {}, {:user_id => @user.id}
get_auth uploads_path, @user
assert_response :success
end
context "with search parameters" do
should "render" do
get :index, {:search => {:source => @upload.source}}, {:user_id => @user.id}
get_auth uploads_path, @user, params: {:search => {:source => @upload.source}}
assert_response :success
end
end
@@ -84,11 +75,13 @@ class UploadsControllerTest < ActionController::TestCase
context "show action" do
setup do
@upload = FactoryGirl.create(:jpg_upload)
as_user do
@upload = create(:jpg_upload)
end
end
should "render" do
get :show, {:id => @upload.id}, {:user_id => @user.id}
get_auth upload_path(@upload), @user
assert_response :success
end
end
@@ -97,19 +90,20 @@ class UploadsControllerTest < ActionController::TestCase
should "create a new upload" do
assert_difference("Upload.count", 1) do
file = Rack::Test::UploadedFile.new("#{Rails.root}/test/files/test.jpg", "image/jpeg")
file.stubs(:tempfile).returns(file)
post :create, {:upload => {:file => file, :tag_string => "aaa", :rating => "q", :source => "aaa"}}, {:user_id => @user.id}
post_auth uploads_path, @user, params: {:upload => {:file => file, :tag_string => "aaa", :rating => "q", :source => "aaa"}}
end
end
end
context "update action" do
setup do
@upload = FactoryGirl.create(:jpg_upload)
as_user do
@upload = create(:source_upload)
end
end
should "process an unapproval" do
post :update, {:id => @upload.id}, {:user_id => @user.id}
put_auth upload_path(@upload), @user
@upload.reload
assert_equal("completed", @upload.status)
end

View File

@@ -1,51 +1,48 @@
require 'test_helper'
class UserFeedbacksControllerTest < ActionController::TestCase
class UserFeedbacksControllerTest < ActionDispatch::IntegrationTest
context "The user feedbacks controller" do
setup do
@user = FactoryGirl.create(:user)
@critic = FactoryGirl.create(:gold_user)
@mod = FactoryGirl.create(:moderator_user)
CurrentUser.user = @critic
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:user)
@critic = create(:gold_user)
@mod = create(:moderator_user)
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @critic.id}
get_auth new_user_feedback_path, @critic, params: { user_feedback: { user_id: @user.id } }
assert_response :success
end
end
context "edit action" do
setup do
@user_feedback = FactoryGirl.create(:user_feedback)
as(@critic) do
@user_feedback = create(:user_feedback, user: @user)
end
end
should "render" do
get :edit, {:id => @user_feedback.id}, {:user_id => @critic.id}
get_auth edit_user_feedback_path(@user_feedback), @critic
assert_response :success
end
end
context "index action" do
setup do
@user_feedback = FactoryGirl.create(:user_feedback)
as(@critic) do
@user_feedback = create(:user_feedback, user: @user)
end
end
should "render" do
get :index, {}, {:user_id => @user.id}
get_auth user_feedbacks_path, @user
assert_response :success
end
context "with search parameters" do
should "render" do
get :index, {:search => {:user_id => @user.id}}, {:user_id => @critic.id}
get_auth user_feedbacks_path, @critic, params: {:search => {:user_id => @user.id}}
assert_response :success
end
end
@@ -54,35 +51,50 @@ class UserFeedbacksControllerTest < ActionController::TestCase
context "create action" do
should "create a new feedback" do
assert_difference("UserFeedback.count", 1) do
post :create, {:user_feedback => {:category => "positive", :user_name => @user.name, :body => "xxx"}}, {:user_id => @critic.id}
assert_not_nil(assigns(:user_feedback))
assert_equal([], assigns(:user_feedback).errors.full_messages)
post_auth user_feedbacks_path, @critic, params: {:user_feedback => {:category => "positive", :user_name => @user.name, :body => "xxx"}}
end
end
end
context "update action" do
should "update the feedback" do
as(@critic) do
@feedback = create(:user_feedback, user: @user, category: "negative")
end
put_auth user_feedback_path(@feedback), @critic, params: { id: @feedback.id, user_feedback: { category: "positive" }}
assert_redirected_to(@feedback)
assert("positive", @feedback.reload.category)
end
end
context "destroy action" do
setup do
@user_feedback = FactoryGirl.create(:user_feedback, user: @user)
as(@critic) do
@user_feedback = create(:user_feedback, user: @user)
end
end
should "delete a feedback" do
assert_difference "UserFeedback.count", -1 do
post :destroy, {:id => @user_feedback.id}, {:user_id => @critic.id}
delete_auth user_feedback_path(@user_feedback), @critic
end
end
context "by a moderator" do
should "allow deleting feedbacks given to other users" do
assert_difference "UserFeedback.count", -1 do
post :destroy, {:id => @user_feedback.id}, {:user_id => @mod.id}
delete_auth user_feedback_path(@user_feedback), @mod
end
end
should "not allow deleting feedbacks given to themselves" do
@user_feedback = FactoryGirl.create(:user_feedback, user: @mod)
as(@critic) do
@user_feedback = create(:user_feedback, user: @mod)
end
assert_difference "UserFeedback.count", 0 do
post :destroy, {:id => @user_feedback.id}, {:user_id => @mod.id}
delete_auth user_feedback_path(@user_feedback), @mod
end
end
end

View File

@@ -1,41 +1,45 @@
require 'test_helper'
class UserNameChangeRequestsControllerTest < ActionController::TestCase
class UserNameChangeRequestsControllerTest < ActionDispatch::IntegrationTest
context "The user name change requests controller" do
setup do
@user = FactoryGirl.create(:gold_user)
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@change_request = UserNameChangeRequest.create!(
:user_id => @user.id,
:original_name => @user.name,
:desired_name => "abc",
:change_reason => "hello"
)
@user = create(:gold_user)
@admin = create(:admin_user)
as(@user) do
@change_request = UserNameChangeRequest.create!(
:user_id => @user.id,
:original_name => @user.name,
:desired_name => "abc",
:change_reason => "hello"
)
end
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id}
get_auth new_user_name_change_request_path, @user
assert_response :success
end
end
context "create action" do
should "work" do
post_auth user_name_change_requests_path, @user, params: { user_name_change_request: { desired_name: "zun" }}
assert_response :success
end
end
context "show action" do
should "render" do
get :show, {:id => @change_request.id}, {:user_id => @user.id}
get_auth user_name_change_request_path(@change_request), @user
assert_response :success
end
context "when the current user is not an admin and does not own the request" do
setup do
CurrentUser.user = FactoryGirl.create(:user)
end
should "fail" do
get :show, {:id => @change_request.id}
assert_redirected_to(new_session_path(:url => user_name_change_request_path(@change_request)))
@another_user = create(:user)
get_auth user_name_change_request_path(@change_request), @another_user
assert_response :forbidden
end
end
end
@@ -43,21 +47,21 @@ class UserNameChangeRequestsControllerTest < ActionController::TestCase
context "for actions restricted to admins" do
context "index action" do
should "render" do
get :index, {}, {:user_id => @admin.id}
get_auth user_name_change_requests_path, @admin
assert_response :success
end
end
context "approve action" do
should "succeed" do
post :approve, {:id => @change_request.id}, {:user_id => @admin.id}
post_auth approve_user_name_change_request_path(@change_request), @admin
assert_redirected_to(user_name_change_request_path(@change_request))
end
end
context "reject action" do
should "succeed" do
post :reject, {:id => @change_request.id}, {:user_id => @admin.id}
post_auth reject_user_name_change_request_path(@change_request), @admin
assert_redirected_to(user_name_change_request_path(@change_request))
end
end

View File

@@ -1,35 +1,29 @@
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
class UsersControllerTest < ActionDispatch::IntegrationTest
context "The users controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
@user = create(:user)
end
context "index action" do
should "list all users" do
get :index
get users_path
assert_response :success
end
should "list all users for /users?name=<name>" do
get :index, { name: @user.name }
get users_path, params: { name: @user.name }
assert_redirected_to(@user)
end
should "raise error for /users?name=<nonexistent>" do
get :index, { name: "nobody" }
get users_path, params: { name: "nobody" }
assert_response :error
end
should "list all users (with search)" do
get :index, {:search => {:name_matches => @user.name}}
get users_path, params: {:search => {:name_matches => @user.name}}
assert_response :success
end
end
@@ -37,18 +31,20 @@ class UsersControllerTest < ActionController::TestCase
context "show action" do
setup do
# flesh out profile to get more test coverage of user presenter.
@user = FactoryGirl.create(:banned_user, can_approve_posts: true, is_super_voter: true)
FactoryGirl.create(:saved_search, user: @user)
FactoryGirl.create(:post, uploader: @user, tag_string: "fav:#{@user.name}")
@user = create(:banned_user, can_approve_posts: true, is_super_voter: true)
as_user do
create(:saved_search, user: @user)
create(:post, uploader: @user, tag_string: "fav:#{@user.name}")
end
end
should "render" do
get :show, {:id => @user.id}
get user_path(@user)
assert_response :success
end
should "show hidden attributes to the owner" do
get :show, {id: @user.id, format: :json}, {user_id: @user.id}
get_auth user_path(@user), @user, params: {format: :json}
json = JSON.parse(response.body)
assert_response :success
@@ -56,9 +52,9 @@ class UsersControllerTest < ActionController::TestCase
end
should "not show hidden attributes to others" do
another = FactoryGirl.create(:user)
@another = create(:user)
get :show, {id: another.id, format: :json}, {user_id: @user.id}
get_auth user_path(@another), @user, params: {format: :json}
json = JSON.parse(response.body)
assert_response :success
@@ -66,7 +62,7 @@ class UsersControllerTest < ActionController::TestCase
end
should "strip '?' from attributes" do
get :show, {id: @user.id, format: :xml}, {user_id: @user.id}
get_auth user_path(@user), @user, params: {format: :xml}
xml = Hash.from_xml(response.body)
assert_response :success
@@ -80,7 +76,7 @@ class UsersControllerTest < ActionController::TestCase
end
should "render" do
get :new
get new_user_path
assert_response :success
end
end
@@ -88,58 +84,53 @@ class UsersControllerTest < ActionController::TestCase
context "create action" do
should "create a user" do
assert_difference("User.count", 1) do
post :create, {:user => {:name => "xxx", :password => "xxxxx1", :password_confirmation => "xxxxx1"}}, {:user_id => @user.id}
assert_not_nil(assigns(:user))
assert_equal([], assigns(:user).errors.full_messages)
post users_path, params: {:user => {:name => "xxx", :password => "xxxxx1", :password_confirmation => "xxxxx1"}}
end
end
should "not allow registering multiple accounts with the same IP" do
Danbooru.config.unstub(:enable_sock_puppet_validation?)
request.env["REMOTE_ADDR"] = "1.2.3.4"
CurrentUser.user = nil
context "with sockpuppet validation enabled" do
setup do
Danbooru.config.unstub(:enable_sock_puppet_validation?)
@user.update(last_ip_addr: "127.0.0.1")
end
post :create, {:user => {:name => "user", :password => "xxxxx1", :password_confirmation => "xxxxx1"}}, {}
session.clear
post :create, {:user => {:name => "dupe", :password => "xxxxx1", :password_confirmation => "xxxxx1"}}, {}
assert_equal(true, User.where(name: "user").exists?)
assert_equal(false, User.where(name: "dupe").exists?)
assert_equal(IPAddr.new("1.2.3.4"), User.find_by_name("user").last_ip_addr)
assert_match(/Sign up failed: Last ip addr was used recently/, flash[:notice])
should "not allow registering multiple accounts with the same IP" do
assert_difference("User.count", 0) do
post users_path, params: {:user => {:name => "dupe", :password => "xxxxx1", :password_confirmation => "xxxxx1"}}
end
end
end
end
context "edit action" do
setup do
@user = FactoryGirl.create(:user)
@user = create(:user)
end
should "render" do
get :edit, {:id => @user.id}, {:user_id => @user.id}
get_auth edit_user_path(@user), @user
assert_response :success
end
end
context "update action" do
setup do
@user = FactoryGirl.create(:user)
@user = create(:user)
end
should "update a user" do
post :update, {:id => @user.id, :user => {:favorite_tags => "xyz"}}, {:user_id => @user.id}
put_auth user_path(@user), @user, params: {:user => {:favorite_tags => "xyz"}}
@user.reload
assert_equal("xyz", @user.favorite_tags)
end
context "changing the level" do
setup do
@cuser = FactoryGirl.create(:user)
@cuser = create(:user)
end
should "not work" do
post :update, {:id => @user.id, :user => {:level => 40}}, {:user_id => @cuser.id}
put_auth user_path(@user), @cuser, params: {:user => {:level => 40}}
@user.reload
assert_equal(20, @user.level)
end
@@ -147,8 +138,8 @@ class UsersControllerTest < ActionController::TestCase
context "for a banned user" do
should "allow the user to edit their settings" do
@user = FactoryGirl.create(:banned_user)
post :update, {:id => @user.id, :user => {:favorite_tags => "xyz"}}, {:user_id => @user.id}
@user = create(:banned_user)
put_auth user_path(@user), @user, params: {:user => {:favorite_tags => "xyz"}}
assert_equal("xyz", @user.reload.favorite_tags)
end

View File

@@ -1,46 +1,38 @@
require 'test_helper'
class WikiPageVersionsControllerTest < ActionController::TestCase
class WikiPageVersionsControllerTest < ActionDispatch::IntegrationTest
context "The wiki page versions controller" do
setup do
@user = FactoryGirl.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@wiki_page = FactoryGirl.create(:wiki_page)
@wiki_page.update_attributes(:body => "1 2")
@wiki_page.update_attributes(:body => "2 3")
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
@user = create(:user)
as_user do
@wiki_page = create(:wiki_page)
@wiki_page.update(:body => "1 2")
@wiki_page.update(:body => "2 3")
end
end
context "index action" do
should "list all versions" do
get :index
get wiki_page_versions_path
assert_response :success
assert_not_nil(assigns(:wiki_page_versions))
end
should "list all versions that match the search criteria" do
get :index, {:search => {:wiki_page_id => @wiki_page.id}}
get wiki_page_versions_path, params: {:search => {:wiki_page_id => @wiki_page.id}}
assert_response :success
assert_not_nil(assigns(:wiki_page_versions))
end
end
context "show action" do
should "render" do
get :show, { id: @wiki_page.versions.first.id }
get wiki_page_version_path(@wiki_page.versions.first)
assert_response :success
end
end
context "diff action" do
should "render" do
get :diff, { thispage: @wiki_page.versions.first.id, otherpage: @wiki_page.versions.last.id }
get diff_wiki_page_versions_path, params: { thispage: @wiki_page.versions.first.id, otherpage: @wiki_page.versions.last.id }
assert_response :success
end
end

View File

@@ -1,95 +1,99 @@
require 'test_helper'
class WikiPagesControllerTest < ActionController::TestCase
class WikiPagesControllerTest < ActionDispatch::IntegrationTest
context "The wiki pages controller" do
setup do
@user = FactoryGirl.create(:user)
@mod = FactoryGirl.create(:moderator_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
@user = create(:user)
@mod = create(:moderator_user)
end
context "index action" do
setup do
@wiki_page_abc = FactoryGirl.create(:wiki_page, :title => "abc")
@wiki_page_def = FactoryGirl.create(:wiki_page, :title => "def")
as_user do
@wiki_page_abc = create(:wiki_page, :title => "abc")
@wiki_page_def = create(:wiki_page, :title => "def")
end
end
should "list all wiki_pages" do
get :index
get wiki_pages_path
assert_response :success
end
should "list all wiki_pages (with search)" do
get :index, {:search => {:title => "abc"}}
get wiki_pages_path, params: {:search => {:title => "abc"}}
assert_redirected_to(wiki_page_path(@wiki_page_abc))
end
should "list wiki_pages without tags with order=post_count" do
get :index, {:search => {:title => "abc", :order => "post_count"}}
get wiki_pages_path, params: {:search => {:title => "abc", :order => "post_count"}}
assert_redirected_to(wiki_page_path(@wiki_page_abc))
end
end
context "show action" do
setup do
@wiki_page = FactoryGirl.create(:wiki_page)
as_user do
@wiki_page = create(:wiki_page)
end
end
should "render" do
get :show, {:id => @wiki_page.id}
get wiki_page_path(@wiki_page)
assert_response :success
end
should "render for a title" do
get :show, {:id => @wiki_page.title}
get wiki_page_path(:id => @wiki_page.title)
assert_response :success
end
should "redirect for a nonexistent title" do
get :show, {:id => "what"}
get wiki_page_path(:id => "what")
assert_redirected_to(show_or_new_wiki_pages_path(title: "what"))
end
should "render for a negated tag" do
@wiki_page.update_attribute(:title, "-aaa")
get :show, {:id => @wiki_page.id}
as_user do
@wiki_page.update(title: "-aaa")
end
get wiki_page_path(:id => @wiki_page.id)
assert_response :success
end
end
context "show_or_new action" do
setup do
@wiki_page = FactoryGirl.create(:wiki_page)
as_user do
@wiki_page = create(:wiki_page)
end
end
should "redirect when given a title" do
get :show_or_new, { title: @wiki_page.title }
get show_or_new_wiki_pages_path, params: { title: @wiki_page.title }
assert_redirected_to(@wiki_page)
end
should "render when given a nonexistent title" do
get :show_or_new, { title: "what" }
get show_or_new_wiki_pages_path, params: { title: "what" }
assert_response :success
end
end
context "new action" do
should "render" do
get :new, {}, { user_id: @mod.id }
get_auth new_wiki_page_path, @mod, params: { wiki_page: { title: "test" }}
assert_response :success
end
end
context "edit action" do
should "render" do
wiki_page = FactoryGirl.create(:wiki_page)
as_user do
@wiki_page = create(:wiki_page)
end
get :edit, { id: wiki_page.id }, { user_id: @mod.id }
get_auth wiki_page_path(@wiki_page), @mod
assert_response :success
end
end
@@ -97,54 +101,57 @@ class WikiPagesControllerTest < ActionController::TestCase
context "create action" do
should "create a wiki_page" do
assert_difference("WikiPage.count", 1) do
post :create, {:wiki_page => {:title => "abc", :body => "abc"}}, {:user_id => @user.id}
post_auth wiki_pages_path, @user, params: {:wiki_page => {:title => "abc", :body => "abc"}}
end
end
end
context "update action" do
setup do
@tag = FactoryGirl.create(:tag, name: "foo", post_count: 42)
@wiki_page = FactoryGirl.create(:wiki_page, title: "foo")
as_user do
@tag = create(:tag, name: "foo", post_count: 42)
@wiki_page = create(:wiki_page, title: "foo")
end
end
should "update a wiki_page" do
post :update, {:id => @wiki_page.id, :wiki_page => {:body => "xyz"}}, {:user_id => @user.id}
put_auth wiki_page_path(@wiki_page), @user, params: {:wiki_page => {:body => "xyz"}}
@wiki_page.reload
assert_equal("xyz", @wiki_page.body)
end
should "not rename a wiki page with a non-empty tag" do
post :update, {:id => @wiki_page.id, :wiki_page => {:title => "bar"}}, {:user_id => @user.id}
put_auth wiki_page_path(@wiki_page), @user, params: {:wiki_page => {:title => "bar"}}
assert_equal("foo", @wiki_page.reload.title)
end
should "rename a wiki page with a non-empty tag if secondary validations are skipped" do
post :update, {:id => @wiki_page.id, :wiki_page => {:title => "bar", :skip_secondary_validations => "1"}}, {:user_id => @user.id}
put_auth wiki_page_path(@wiki_page), @user, params: {:wiki_page => {:title => "bar", :skip_secondary_validations => "1"}}
assert_equal("bar", @wiki_page.reload.title)
end
should "not allow non-Builders to delete wiki pages" do
put_auth wiki_page_path(@wiki_page), @user, params: {wiki_page: { is_deleted: true }}
assert_equal(false, @wiki_page.reload.is_deleted?)
end
end
context "destroy action" do
setup do
@wiki_page = FactoryGirl.create(:wiki_page)
@mod = FactoryGirl.create(:mod_user)
as_user do
@wiki_page = create(:wiki_page)
end
@mod = create(:mod_user)
end
should "destroy a wiki_page" do
CurrentUser.scoped(@mod) do
post :destroy, {:id => @wiki_page.id}, {:user_id => @mod.id}
end
delete_auth wiki_page_path(@wiki_page), @mod
@wiki_page.reload
assert_equal(true, @wiki_page.is_deleted?)
end
should "record the deleter" do
CurrentUser.scoped(@mod) do
post :destroy, {:id => @wiki_page.id}, {:user_id => @mod.id}
end
delete_auth wiki_page_path(@wiki_page), @mod
@wiki_page.reload
assert_equal(@mod.id, @wiki_page.updater_id)
end
@@ -152,27 +159,31 @@ class WikiPagesControllerTest < ActionController::TestCase
context "revert action" do
setup do
@wiki_page = FactoryGirl.create(:wiki_page, :body => "1")
Timecop.travel(1.day.from_now) do
@wiki_page.update_attributes(:body => "1 2")
as_user do
@wiki_page = create(:wiki_page, :body => "1")
end
Timecop.travel(2.days.from_now) do
@wiki_page.update_attributes(:body => "1 2 3")
travel_to(1.day.from_now) do
@wiki_page.update(:body => "1 2")
end
travel_to(2.days.from_now) do
@wiki_page.update(:body => "1 2 3")
end
end
should "revert to a previous version" do
version = @wiki_page.versions(true).first
version = @wiki_page.versions.first
assert_equal("1", version.body)
post :revert, {:id => @wiki_page.id, :version_id => version.id}, {:user_id => @user.id}
put_auth revert_wiki_page_path(@wiki_page), @user, params: {:version_id => version.id}
@wiki_page.reload
assert_equal("1", @wiki_page.body)
end
should "not allow reverting to a previous version of another wiki page" do
@wiki_page_2 = FactoryGirl.create(:wiki_page)
as_user do
@wiki_page_2 = create(:wiki_page)
end
post :revert, { :id => @wiki_page.id, :version_id => @wiki_page_2.versions(true).first.id }, {:user_id => @user.id}
put_auth revert_wiki_page_path(@wiki_page), @user, params: { :version_id => @wiki_page_2.versions.first.id }
@wiki_page.reload
assert_not_equal(@wiki_page.body, @wiki_page_2.body)