Skip to content

Commit

Permalink
Signed in users can delete posts
Browse files Browse the repository at this point in the history
Unable to get the right testing for the confirm dialog box-- may need to
add Selenium to test it?

Should make it so only author can delete her own posts...
  • Loading branch information
maxbeizer committed Nov 7, 2012
1 parent 2644a8c commit 1fa872a
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def update
end
end

def destroy
@post.destroy
redirect_to root_url, :notice => "Your post has been deleted"
end

protected

def lookup_post
Expand Down
3 changes: 3 additions & 0 deletions app/views/posts/_post.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<h1><%= link_to post.title, post %></h1>
<h4>By: <%= post.author.email %></h4>
<% if user_signed_in? %>
<%= link_to "Delete this post", post, method: :delete, data: { confirm: "Are you sure?" }, title: post.title %>
<% end %>
<p><%= post.body %></p>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

root to: "posts#index"

resources :posts, except: [:destroy] do
resources :posts do
resources :comments, only: [:create]
end

Expand Down
25 changes: 25 additions & 0 deletions features/authors_can_delete_posts.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Authors can delete posts

Scenario: I can delete a post I wrote
Given I am signed in as "[email protected]"
Given the following posts:
| title | body |
| Foo | Foo Bar |
When I go to the homepage
And I click "Delete this post"
Then I should see "Your post has been deleted"
And I should be on the homepage
And I should not see the title "Foo"
And I should not see "Foo Bar"

Scenario: I cannot delete posts if I am not signed in
Given I am not signed in
Given the following posts:
| title | body |
| Foo | Foo Bar |
When I go to the homepage
Then I should not see "Delete this post"
And I should be on the homepage
And I should see the title "Foo"
And I should see "Foo Bar"

4 changes: 4 additions & 0 deletions features/step_definitions/authentication_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
sign_in_as @user
end

Given /^I am not signed in$/ do
@user = nil

This comment has been minimized.

Copy link
@elizabrock

elizabrock Nov 12, 2012

Member

This doesn't ensure that you aren't signed in. It just sets the @user instance variable to nil. To ensure that the browser is actually signed out, you have to actually go to the logout route.

e.g.:

Given /^I am not logged in$/ do
  delete destroy_admin_user_session_path
end

This comment has been minimized.

Copy link
@maxbeizer

maxbeizer Nov 14, 2012

Author

fixed and pushed, although there is no admin user at this point, so it was just user_session_path

This comment has been minimized.

Copy link
@elizabrock

elizabrock Nov 14, 2012

Member

I pulled the example from a different project.

This comment has been minimized.

Copy link
@maxbeizer

maxbeizer Nov 14, 2012

Author

That's what I figured.

end

def sign_in_as user
steps %Q{
Given I am on the homepage
Expand Down
4 changes: 4 additions & 0 deletions features/step_definitions/interaction_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
page.should have_css("h1, h2", text: title_text)
end

Then /^I should not see the title "(.*?)"$/ do |title_text|
page.should_not have_css("h1, h2", text: title_text)
end

When /^I fill in "(.*?)" for "(.*?)"$/ do |text, field_label|
page.fill_in field_label, with: text
end
Expand Down

0 comments on commit 1fa872a

Please sign in to comment.