Permalink
Browse files

improve rspec coverage, and fixed several minor bugs

  • Loading branch information...
1 parent 780fd0b commit 46d4cabb2f4308f3a0a596a974ea379de13d10c1 Ping Yu committed with Apr 19, 2008
View
@@ -11,3 +11,5 @@ db/*.sqlite3
index/*
public/javascripts/signal.js
public/stylesheets/signal.css
+.project
+.loadpath
@@ -13,7 +13,7 @@ def admin_required
# collection methods
def create
- flag = current_user.flags.create!(params[:flag])
+ flag = current_user.flags.create(params[:flag])
flash[:notice] = if flag.new_record?
"You already flagged this content!"
else # success
@@ -1,5 +1,6 @@
class PagesController < ApplicationController
before_filter :require_login, :except => [:index, :show, :revision, :search]
+ before_filter :login_required, :only => [:destroy]
before_filter :require_admin, :only => [:lock]
before_filter :check_private, :only => [:show, :revision]
caches_page :show
@@ -91,7 +92,7 @@ def create
format.html { redirect_to(wiki_page_url(@page)) }
format.xml { render :xml => @page, :status => :created, :location => @page }
else
- format.html { render :action => "new" }
+ format.html { puts @page.errors.inspect; render :action => "new" }
format.xml { render :xml => @page.errors, :status => :unprocessable_entity }
end
end
@@ -17,7 +17,8 @@ def wiki_link(wiki_words, link_text = nil)
end
def current_revision(id, version)
- version == Page.find(id).version
+ page = Page.find(id)
+ page ? (version == page.version) : false
end
def body_input(f)
View
@@ -43,8 +43,8 @@ def validate
def is_spam?(site)
v = Viking.connect("akismet", {:api_key => site.akismet_key, :blog => site.akismet_url})
- response = v.check_comment(:comment_content => body.to_s, :comment_author => user.login.to_s, :user_ip => ip.to_s, :user_agent => agent.to_s, :referrer => referrer.to_s)
- logger.info "Calling Akismet for page #{permalink} by #{user.login.to_s} using ip #{ip}: #{response[:spam]}"
+ response = v.check_comment(:comment_content => body.to_s, :comment_author => username.to_s, :user_ip => ip.to_s, :user_agent => agent.to_s, :referrer => referrer.to_s)
+ logger.info "Calling Akismet for page #{permalink} by #{username.to_s} using ip #{ip}: #{response[:spam]}"
return response[:spam]
end
@@ -104,6 +104,9 @@ def unlock
RAILS_DEFAULT_LOGGER.info "UNLOCKED #{self.permalink}"
end
+ def username
+ user.blank? ? "anonymous" : user.login
+ end
def self.find_all_by_wiki_word(wiki_word, site = nil)
site ||= Site.find(:first)
pages = site.pages.find(:all)
@@ -1,5 +1,5 @@
Comparing
-<span style="background-color: #cfc; padding: 1px 4px;">version <%= @v2.version %></span>
+<span style="background-color: #cfc; padding: 1px 4px;">version <%= @v1.version %></span>
and
<span style="background-color: #ddd; padding: 1px 4px;">version <%= @v2.version %></span>
<pre class="diff">
@@ -8,7 +8,7 @@
updated <%= time_ago_in_words(rev.updated_at) %> ago
by <%= author(rev.user_id) %>
<% if rev.respond_to?(:previous) && !rev.previous.nil? %>
- <%= link_to "(Diff)", diff_page_url(@page.permalink, :v1 => rev.version, :v2 => rev.previous) %>
+ <%= link_to "(Diff)", diff_page_url(@page.permalink, :v1 => rev.version, :v2 => rev.previous.version) %>
<% end %>
</li>
<% end %>
@@ -9,13 +9,21 @@
controller.stub!(:current_user).and_return :false
end
- it "does not render 'index'" do
- get :index
+ %w[index new].each do |action|
+ it "#{action} should redirect to login" do
+ get action
+ response.should redirect_to('session/new')
+ end
+ end
+
+ it 'can not flag something' do
+ post :create, :flag => { :flaggable_type => 'Page', :flaggable_id => 1, :reason => 'outdated' }
response.should redirect_to('session/new')
end
- it 'can not flag something' do
- post :create, :flag => { :flaggable_type => 'Page', :flaggable_id => 1, :reason => 'outdated' }
+ it 'can not delete' do
+ delete :destroy, :id => "1"
+ response.should_not be_success
response.should redirect_to('session/new')
end
end
@@ -38,12 +46,40 @@
response.should redirect_to('session/new')
end
- it 'can flag something' do
+ it 'canflag something' do
lambda {
post :create, :flag => { :flaggable_type => 'Page', :flaggable_id => 1, :reason => 'outdated', :user_id => @user.id }
response.should redirect_to('pages/hai')
}.should change(Flag, :count).by(1)
end
+ it 'can not flag same page twice' do
+ post :create, :flag => { :flaggable_type => 'Page', :flaggable_id => 1, :reason => 'outdated', :user_id => @user.id }
+ lambda {
+ post :create, :flag => { :flaggable_type => 'Page', :flaggable_id => 1, :reason => 'outdated', :user_id => @user.id }
+ #flash[:notice].should == "You already flagged this content!"
+ }.should_not change(Flag, :count)
+ end
+
+ it "render 'new'" do
+ get :new, :flaggable_type => 'Page', :flaggable_id => 1
+ response.should be_success
+ response.should render_template('new')
+ end
+
+ it "render / with error if flaggable_type is not found" do
+ get :new, :flaggable_type => 'NotFound', :flaggable_id => 1
+ response.should_not be_success
+ response.should redirect_to('/')
+ #flash[:error].should_not be_empty
+ end
+
+ it "can delete a page" do
+ flag = @user.flags.create({ :flaggable_type => 'Page', :flaggable_id => 1, :reason => 'outdated', :user_id => @user.id })
+ lambda do
+ delete :destroy, :id => flag
+ response.should redirect_to('flags')
+ end.should change(Flag, :count)
+ end
end
@@ -71,5 +107,31 @@
}.should change(Flag, :count).by(1)
end
+ it "render 'new'" do
+ get :new, :flaggable_type => 'Page', :flaggable_id => 1
+ response.should be_success
+ response.should render_template("new")
+ end
+ it 'can not flag same page twice' do
+ post :create, :flag => { :flaggable_type => 'Page', :flaggable_id => 1, :reason => 'outdated', :user_id => @user.id }
+ lambda {
+ post :create, :flag => { :flaggable_type => 'Page', :flaggable_id => 1, :reason => 'outdated', :user_id => @user.id }
+ #flash[:notice].should == "You already flagged this content!"
+ }.should_not change(Flag, :count)
+ end
+ it "render / with error if flaggable_type is not found" do
+ get :new, :flaggable_type => 'NotFound', :flaggable_id => 1
+ response.should_not be_success
+ response.should redirect_to('/')
+ #flash[:error].should_not be_empty
+ end
+ it "can delete a page" do
+ flag = @user.flags.create({ :flaggable_type => 'Page', :flaggable_id => 1, :reason => 'outdated', :user_id => @user.id })
+ lambda do
+ delete :destroy, :id => flag
+ response.should redirect_to('flags')
+ end.should change(Flag, :count)
+ end
+
end
Oops, something went wrong.

0 comments on commit 46d4cab

Please sign in to comment.