This repository has been archived by the owner on May 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
= simple_form_for @post do |f| | ||
= f.input :title | ||
= f.input :content | ||
- if @current_user.admin? | ||
= f.input_field :featured, as: :boolean | ||
= f.submit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddFeaturedToPosts < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :posts, :featured, :boolean, default: false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,76 @@ | ||
require 'test_helper' | ||
require 'mocha/test_unit' | ||
|
||
class PostsControllerTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
test 'post_params has all valid params if user is admin' do | ||
current_user = create_user(admin: true) | ||
params = ActionController::Parameters.new( | ||
post: { | ||
title: 'sample title', | ||
content: 'sample content', | ||
featured: true, | ||
invalid_param: true, | ||
} | ||
) | ||
|
||
posts_controller = PostsController.new | ||
posts_controller.stubs(:params).returns(params) | ||
posts_controller.stubs(:current_user).returns(current_user) | ||
post_params = posts_controller.send(:post_params) | ||
assert_includes post_params.keys, 'title' | ||
assert_includes post_params.keys, 'content' | ||
assert_includes post_params.keys, 'featured' | ||
end | ||
|
||
test 'post_params drops invalid params if user is admin' do | ||
current_user = create_user(admin: true) | ||
params = ActionController::Parameters.new( | ||
post: { | ||
title: 'sample title', | ||
content: 'sample content', | ||
featured: true, | ||
invalid_param: true, | ||
} | ||
) | ||
|
||
posts_controller = PostsController.new | ||
posts_controller.stubs(:params).returns(params) | ||
posts_controller.stubs(:current_user).returns(current_user) | ||
post_params = posts_controller.send(:post_params) | ||
refute_includes post_params.keys, 'invalid_param' | ||
end | ||
|
||
test 'post_params has all valid params if user is not admin' do | ||
current_user = create_user | ||
params = ActionController::Parameters.new( | ||
post: { | ||
title: 'sample title', | ||
content: 'sample content', | ||
} | ||
) | ||
|
||
posts_controller = PostsController.new | ||
posts_controller.stubs(:params).returns(params) | ||
posts_controller.stubs(:current_user).returns(current_user) | ||
post_params = posts_controller.send(:post_params) | ||
assert_includes post_params.keys, 'title' | ||
assert_includes post_params.keys, 'content' | ||
end | ||
|
||
test 'post_params drops invalid params if user is not admin' do | ||
current_user = create_user | ||
params = ActionController::Parameters.new( | ||
post: { | ||
title: 'sample title', | ||
content: 'sample content', | ||
featured: true, | ||
} | ||
) | ||
|
||
posts_controller = PostsController.new | ||
posts_controller.stubs(:params).returns(params) | ||
posts_controller.stubs(:current_user).returns(current_user) | ||
post_params = posts_controller.send(:post_params) | ||
refute_includes post_params.keys, 'featured' | ||
end | ||
end |