Permalink
Browse files

cannot vote more than once per suggestion (with dirty cooking)

  • Loading branch information...
1 parent a1ec08b commit 73a4074782d8d3ce491a297fb4b6c8cf958278ca @dnagir committed Dec 15, 2010
Showing with 111 additions and 3 deletions.
  1. +14 −3 app/controllers/suggestions_controller.rb
  2. +97 −0 spec/controllers/suggestions_controller_spec.rb
@@ -31,11 +31,22 @@ def update
def vote
@suggestion = Suggestion.find(params[:suggestion_id])
- @suggestion.vote(params[:value].to_i)
+ can_vote = cookies[:voted].to_s.split(';').find {|s| s.to_i == @suggestion.id }.blank?
+ notice = if can_vote
+ cookies[:voted] = {
+ :expires => 1.year.from_now,
+ :value => (cookies[:voted] || '') << ";#{@suggestion.id}"
+ }
+ @suggestion.vote(params[:value].to_i)
+ 'Thanks for your vote!'
+ else
+ 'You have already voted for this suggestion before'
+ end
+
if inline?
- redirect_to inline_project_url(@suggestion.project), :notice => 'Thanks for your vote!'
+ redirect_to inline_project_url(@suggestion.project), :notice => notice
else
- redirect_to project_url(@suggestion.project), :notice => 'Thanks for your vote!'
+ redirect_to project_url(@suggestion.project), :notice => notice
end
end
@@ -73,6 +73,103 @@ def vote(value, more = {})
}.to change { subject.reload.votes }.by(1)
end
+ it 'should mark suggestion as voted by me' do
+ pending "haven't figured out how to get/set cookie"
+ vote(1)
+ cookies[:voted].should include suggestion.id.to_s
+ end
+
+ it 'should not vote twice' do
+ pending "haven't figured out how to get/set cookie"
+ cookies[:voted] = suggestion.id.to_s
+ expect { vote(1) }.to_not change { suggestion.votes }
+ end
+
+ it 'should explain why I cannot vote doing it twice' do
+ pending "haven't figured out how to get/set cookie"
+ cookies[:voted] = suggestion.id.to_s
+ vote(1)
+ flash[:notice].should match /already voted/
+ end
+ end
+
+
+ context 'with user' do
+ subject { Factory(:suggestion, :project => project) }
+ before do
+ sign_in me
+ end
+
+ describe 'editing' do
+ def edit
+ get(:edit, :project_id=>project.id, :id=>subject.id)
+ end
+
+ it 'should show form' do
+ edit.should have_selector('form') do |f|
+ f.should have_selector("input[name='suggestion[content]']")
+ end
+ end
+
+ end
+
+
+
+ describe 'updating' do
+ def update(attrs={})
+ post(:update, :project_id=>project.id, :id=>subject.id, :suggestion=>Factory.attributes_for(:suggestion, attrs))
+ assigns(:suggestion)
+ end
+
+ it 'should ask to log-in' do
+ sign_out :user
+ update
+ response.should require_authentication
+ end
+
+ context 'by another user' do
+ before do
+ sign_in Factory(:user)
+ end
+ it 'should not allow' do
+ update
+ response.should deny_access
+ end
+ end
+
+ it 'should redirect to project' do
+ update
+ response.should redirect_to project_url(project)
+ end
+
+ it 'should update attributes' do
+ update(:status=>:done).status.should == :done
+ end
+ end
+
+ end
+
+ describe 'PivotalTracker' do
+ def valid_story
+ {'password'=>'123456', 'name'=>suggestion.content_brief, 'description'=>suggestion.content, 'email'=>me.email,'project_id'=>999}
+ end
+ def do_get
+ get(:pivotal_story, :project_id=>project.id, :suggestion_id=>suggestion.id)
+ end
+ def do_create
+ post(:pivotal_story, :project_id=>project.id, :suggestion_id=>suggestion.id, :integrations_pivotal_tracker_story => valid_story)
+ end
+ before do
+ story = Integrations::PivotalTracker::Story.new(valid_story)
+ story.stub(:valid?).and_return(true)
+ story.stub(:do_create!).and_return(true)
+ Integrations::PivotalTracker::Story.stub(:new).and_return(story)
+ end
+
+ it 'should require user' do
+
+ end
+
end

0 comments on commit 73a4074

Please sign in to comment.