Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

improve rspec coverage, and fixed several minor bugs

  • Loading branch information...
commit 46d4cabb2f4308f3a0a596a974ea379de13d10c1 1 parent 780fd0b
Ping Yu authored committed
View
2  .gitignore
@@ -11,3 +11,5 @@ db/*.sqlite3
index/*
public/javascripts/signal.js
public/stylesheets/signal.css
+.project
+.loadpath
View
2  app/controllers/flags_controller.rb
@@ -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
View
3  app/controllers/pages_controller.rb
@@ -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
View
3  app/helpers/pages_helper.rb
@@ -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
7 app/models/page.rb
@@ -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)
View
2  app/views/pages/diff.html.erb
@@ -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">
View
2  app/views/pages/revisions.html.erb
@@ -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 %>
View
72 spec/controllers/flags_controller_spec.rb
@@ -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
View
238 spec/controllers/pages_controller_spec.rb
@@ -3,7 +3,9 @@
def create_page
Page.create(:title => "hee haw", :body => "moop", :site_id => 1)
end
-
+def create_delete_page
+ Page.create(:title => "to be delete", :body => "moop", :site_id => 1)
+end
describe PagesController, " with site that requires login, a user not logged in" do
fixtures :sites, :pages, :page_versions, :users
integrate_views
@@ -12,16 +14,68 @@ def create_page
controller.stub!(:current_user).and_return :false
end
+ it "show all pages" do
+ get :index
+ response.should be_success
+ response.should render_template("index")
+ end
+
+ it "shows page if it exists" do
+ page = create_page
+ page.body = "MEEP"
+ page.save!
+
+ get :show, :id => page.permalink
+ response.should be_success
+ response.should render_template("show")
+ end
+
+ it "redirect to session/new if page is a private page" do
+ page = create_page
+ page.private_page = true
+ page.save!
+
+ get :show, :id => page.permalink
+ response.should_not be_success
+ response.should redirect_to("session/new")
+
+ end
+ it "redirect to page/new (and it will in turn redirect to session/new) if showing a page that does not exist" do
+ get :show, :id => "unavailable"
+ response.should redirect_to("pages/new")
+ end
+
it "does not render 'new'" do
get :new
response.should redirect_to('session/new')
end
- it "renders 'revision'" do
+ it "does not render 'diff'" do
page = create_page
page.body = "MEEP"
page.save!
+ get :diff, :id => page.permalink, :v1 => page.version, :v2 => page.version - 1
+ response.should redirect_to('session/new')
+ end
+
+ it "does not render 'revisions'" do
+ page = create_page
+ get :revisions, :id => page.permalink
+ response.should redirect_to('session/new')
+ end
+
+ it "does not render 'edit'" do
+ page = create_page
+ get :edit, :id => page.permalink
+ response.should redirect_to('session/new')
+ end
+
+ it "renders 'revision'" do
+ page = create_page
+ page.body = "moep MEEPp"
+ page.save!
+
get :revision, :id => page.permalink, :version => page.version - 1
response.should be_success
response.should render_template("show")
@@ -65,6 +119,15 @@ def create_page
response.should redirect_to('session/new')
end.should_not change(Page, :count)
end
+ it "can not delete a page" do
+ page = create_delete_page
+
+ lambda do
+ delete :destroy, :id => page.permalink
+ response.should_not be_success
+ response.should redirect_to('session/new')
+ end.should_not change(Page, :count)
+ end
end
describe PagesController, " with site that does not require login, a user not logged in" do
@@ -79,11 +142,60 @@ def create_page
controller.stub!(:current_user).and_return :false
end
+ it "show all pages" do
+ get :index
+ response.should be_success
+ response.should render_template("index")
+ end
+
+ it "shows page if it exists" do
+ page = create_page
+ page.body = "MEEP"
+ page.save!
+
+ get :show, :id => page.permalink
+ response.should be_success
+ response.should render_template("show")
+ end
+
+ it "shows new page if it does not exist" do
+ get :show, :id => "unavailable"
+ response.should redirect_to("pages/new")
+ end
+
it "renders 'new'" do
get :new
response.should be_success
end
+ it "shows 'diff'" do
+ page = create_page
+ page.body = "MEEP"
+ page.save!
+
+ get :diff, :id => page.permalink, :v1 => page.version, :v2 => page.version - 1
+ assigns(:v1).should == page.versions.find_by_version(params[:v1])
+ assigns(:v2).should == page.versions.find_by_version(params[:v2])
+ response.should be_success
+ response.should render_template("diff")
+ end
+
+ it "shows 'revisions'" do
+ page = create_page
+ get :revisions, :id => page.permalink
+ assigns(:revisions).should == page.versions
+ response.should be_success
+ response.should render_template("revisions")
+ end
+
+ it "shows 'edit'" do
+ page = create_page
+ get :edit, :id => page.permalink
+ assigns(:page).should == page
+ response.should be_success
+ response.should render_template("edit")
+ end
+
it "can rollback a page" do
page = create_page
page.body = "MEEP"
@@ -113,6 +225,16 @@ def create_page
response.should redirect_to('o-hai')
end.should change(Page, :count)
end
+
+ it "cannot delete a page" do
+ page = create_delete_page
+ lambda do
+ delete :destroy, :id => page.permalink
+ response.should_not be_success
+ response.should redirect_to('session/new')
+ end.should_not change(Page, :count)
+ end
+
end
describe PagesController, "a user logged in as normal user" do
@@ -125,12 +247,40 @@ def create_page
controller.stub!(:current_user).and_return users(:jeremy)
end
+ it "show all pages" do
+ get :index
+ response.should be_success
+ response.should render_template("index")
+ end
+
+ it "shows page if it exists" do
+ page = create_page
+ page.body = "MEEP"
+ page.save!
+
+ get :show, :id => page.permalink
+ response.should be_success
+ response.should render_template("show")
+ end
+ it "shows new page if it does not exist" do
+ get :show, :id => "unavailable"
+ response.should redirect_to("pages/new")
+ end
+
it "renders 'new'" do
get :new
response.should be_success
response.should render_template("new")
end
+
+ it "shows 'revisions'" do
+ page = create_page
+ get :revisions, :id => page.permalink
+ assigns(:revisions).should == page.versions
+ response.should be_success
+ response.should render_template("revisions")
+ end
it "renders 'revision'" do
page = create_page
@@ -141,6 +291,25 @@ def create_page
response.should be_success
response.should render_template("show")
end
+ it "shows 'edit'" do
+ page = create_page
+ get :edit, :id => page.permalink
+ assigns(:page).should == page
+ response.should be_success
+ response.should render_template("edit")
+ end
+
+ it "shows 'diff'" do
+ page = create_page
+ page.body = "MEEP"
+ page.save!
+
+ get :diff, :id => page.permalink, :v1 => page.version, :v2 => page.version - 1
+ assigns(:v1).should == page.versions.find_by_version(params[:v1])
+ assigns(:v2).should == page.versions.find_by_version(params[:v2])
+ response.should be_success
+ response.should render_template("diff")
+ end
it "searches for pages" do
get :search, :query => "home"
@@ -181,6 +350,15 @@ def create_page
get :lock, :id => 'hai'
response.should redirect_to('pages/home')
end
+ it "can delete a page" do
+ page = create_delete_page
+ page.title = "to be delete"
+ page.save
+ lambda do
+ delete :destroy, :id => page.permalink
+ response.should redirect_to('pages')
+ end.should change(Page, :count)
+ end
end
describe PagesController, "a user logged in as admin" do
@@ -191,12 +369,48 @@ def create_page
controller.stub!(:logged_in?).and_return true
controller.stub!(:current_user).and_return users(:admin)
end
+
+ it "show all pages" do
+ get :index
+ response.should be_success
+ response.should render_template("index")
+ end
+
+ it "shows page if it exists" do
+ page = create_page
+ page.body = "MEEP"
+ page.save!
+
+ get :show, :id => page.permalink
+ response.should be_success
+ response.should render_template("show")
+ end
+ it "shows new page if it does not exist" do
+ get :show, :id => "unavailable"
+ response.should redirect_to("pages/new")
+ end
+
it "renders 'new'" do
get :new
response.should be_success
response.should render_template("new")
end
+
+ it "shows 'revisions'" do
+ page = create_page
+ get :revisions, :id => page.permalink
+ assigns(:revisions).should == page.versions
+ response.should be_success
+ response.should render_template("revisions")
+ end
+ it "shows 'edit'" do
+ page = create_page
+ get :edit, :id => page.permalink
+ assigns(:page).should == page
+ response.should be_success
+ response.should render_template("edit")
+ end
it "renders 'revision'" do
page = create_page
@@ -208,6 +422,17 @@ def create_page
response.should render_template("show")
end
+ it "shows 'diff'" do
+ page = create_page
+ page.body = "MEEP"
+ page.save!
+
+ get :diff, :id => page.permalink, :v1 => page.version, :v2 => page.version - 1
+ assigns(:v1).should == page.versions.find_by_version(params[:v1])
+ assigns(:v2).should == page.versions.find_by_version(params[:v2])
+ response.should be_success
+ response.should render_template("diff")
+ end
it "searches for pages" do
get :search, :query => "home"
response.should be_success
@@ -256,5 +481,14 @@ def create_page
page.reload
page.should_not be_locked
end
+ it "can delete a page" do
+ page = create_delete_page
+ page.title = "to be delete"
+ page.save
+ lambda do
+ delete :destroy, :id => page.permalink
+ response.should redirect_to('pages')
+ end.should change(Page, :count)
+ end
end
View
21 spec/helpers/application_helper_spec.rb
@@ -0,0 +1,21 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe ApplicationHelper do
+
+ it "returns gravatar_icons for existing email with default size 70" do
+ Digest::MD5.stub!(:hexdigest).and_return "digest"
+ gravatar_url("test@email.com").should == "http://www.gravatar.com/avatar.php?size=70&gravatar_id=digest"
+ end
+
+ it "returns gravatar_icons for existing email with requested size" do
+ Digest::MD5.stub!(:hexdigest).and_return "digest"
+ gravatar_url("test@email.com", 80).should == "http://www.gravatar.com/avatar.php?size=80&gravatar_id=digest"
+ end
+
+ it "returns gravatar_icons for non-existing email" do
+ Digest::MD5.should_receive(:hexdigest).with("nil@nil.com").and_return "digest"
+ gravatar_url(nil).should == "http://www.gravatar.com/avatar.php?size=70&gravatar_id=digest"
+ end
+
+
+end
View
70 spec/helpers/pages_helper_spec.rb
@@ -14,4 +14,72 @@
wikified_body("hello[[there|hai]]whats[[up]]").should == "<p>helloMONKEYSwhatsMONKEYS</p>"
end
-end
+end
+
+describe PagesHelper do
+ before do
+ @wiki_word = "o hai"
+ @link_text = "test"
+ @perma_link = {:permalink => "o-hai"}
+ @class_str = "class=\"new_wiki_link\""
+ end
+ it "links to wiki permalink if page exists" do
+
+ Page.should_receive(:exists?).with(@perma_link).and_return true
+ link = wiki_link(@wiki_word, @link_text)
+ link.should include(@link_text)
+ link.should_not include(@wiki_word)
+ link.should_not include(@class_str)
+
+ end
+ it "links to wiki permalink with proper style if page doesnot exist" do
+
+ Page.should_receive(:exists?).with(@perma_link).and_return false
+ link = wiki_link(@wiki_word, @link_text)
+ link.should include(@link_text)
+ link.should_not include(@wiki_word)
+ link.should include(@class_str)
+ end
+ end
+
+describe PagesHelper do
+ before do
+ @site = mock_model(Site)
+ @form = mock_model(Object)
+ @attr = :body
+ self.should_receive(:site).and_return @site
+ end
+ it "should render textarea if site disable teh" do
+ @site.should_receive(:disable_teh).and_return true
+ @form.should_receive(:text_area)
+ text_input(@form, @attr)
+ end
+ it "should render tech if site enable teh" do
+ @site.should_receive(:disable_teh).and_return false
+ @form.should_not_receive(:text_area)
+ self.should_receive(:textile_editor)
+ text_input(@form, @attr)
+ end
+ end
+describe PagesHelper do
+ before do
+ @id = 1
+ @page = mock_model(Page)
+ @version = mock_model(Page)
+ @version2 = mock_model(Page)
+ end
+ it "checks existing page with same version" do
+ Page.should_receive(:find).with(@id).and_return @page
+ @page.should_receive(:version).and_return @version
+ current_revision(@id, @version).should == true
+ end
+ it "checks existing page with different version" do
+ Page.should_receive(:find).with(@id).and_return @page
+ @page.should_receive(:version).and_return @version2
+ current_revision(@id, @version).should == false
+ end
+ it "checks non-existing page version" do
+ Page.should_receive(:find).with(@id).and_return nil
+ current_revision(@id, @version).should == false
+ end
+ end
View
14 spec/models/page_spec.rb
@@ -5,6 +5,7 @@
before(:each) do
@page = Page.new :site_id => 1
+ @site = mock_model(Site)
end
it "should not be valid" do
@@ -17,6 +18,19 @@
@page.should be_valid
end
+ it "should not be valid if site is spam" do
+ @page.stub!(:is_spam?).and_return true
+ @page.stub!(:site).and_return @site
+ @site.should_receive(:akismet_key?).and_return true
+ @page.should_not be_valid
+ end
+
+ it "should connect to viking for spam info" do
+ @v = mock_model(Object)
+ Viking.should_receive(:connect).and_return @v
+ @v.should_receive(:check_comment).and_return :spam => true
+ @page.is_spam?(@page.site).should == true
+ end
end
describe Page, "creating links" do
View
22 spec/models/page_sweeper_spec.rb
@@ -0,0 +1,22 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe PageSweeper do
+ fixtures :sites, :pages
+ before(:each) do
+ @page = Page.new :title => "hee haw", :body => "moop", :permalink => "hee-haw", :site_id => 1
+ @page1 = mock_model(Page)
+ @page2 = mock_model(Page)
+ @pages = [@page1, @page2]
+ @permalink = "hee-haw"
+ @page_sweeper = PageSweeper.send(:new)
+ end
+
+ it "should expire all pages with same permalink" do
+ Page.should_receive(:find_all_by_wiki_word).at_least(1).with("hee haw").and_return @pages
+ @pages.each do |page|
+ page.should_receive(:permalink).at_least(1)
+ end
+ @page_sweeper.should_receive(:expire_page).exactly(3)
+ @page.save
+ end
+end
View
76 spec/models/user_spec.rb
@@ -0,0 +1,76 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+def valid_user_attributes
+ {:login => "test",
+ :email => "user@test.com",
+ :password => "password",
+ :password_confirmation => "password"}
+end
+describe User, "validity" do
+ before(:each) do
+ @user = User.new
+ end
+
+ it "should not be valid" do
+ @user.should_not be_valid
+ end
+
+ it "is valid if open_id" do
+ @user.identity_url = "open_id"
+ @user.should be_valid
+ puts @user.errors.inspect
+ end
+
+ it "is valid with login, email, password, and passwrod_confirmation if not open_id" do
+ @user.update_attributes(valid_user_attributes)
+ @user.should be_valid
+ puts @user.errors.inspect
+ end
+end
+
+describe User, "autheticate" do
+ before(:each) do
+ @user = User.new(valid_user_attributes)
+ @user.save
+ @user.reload
+ end
+
+ it "should have encrypted password" do
+ @user.crypted_password.should_not be_blank
+ @user.crypted_password.should_not == @user.password
+ end
+
+ it "should autheticate the right password" do
+ User.authenticate(@user.login, @user.password).should == @user
+ end
+
+ it "should autheticate the wrong password" do
+ User.authenticate(@user.login, "#{@user.password}11").should == nil
+ end
+end
+
+describe User, "remember cookies" do
+ before(:each) do
+ @user = User.new(valid_user_attributes)
+ @user.save
+ @user.reload
+ end
+
+ it "should set remember_token" do
+ @user.remember_token_expires_at.should be_blank
+ @user.remember_token.should be_blank
+ @user.remember_me
+ @user.remember_token_expires_at.should_not be_blank
+ @user.remember_token.should_not be_blank
+
+ @user.forget_me
+ @user.remember_token_expires_at.should be_blank
+ @user.remember_token.should be_blank
+ end
+
+ it "should expires" do
+ @user.remember_me_for(1.second)
+ sleep(2)
+ @user.remember_token?.should == false
+ end
+end
View
2  spec/rcov.opts
@@ -0,0 +1,2 @@
+--exclude "spec/*,gems/*,lib/authenticated_system.rb"
+--rails
Please sign in to comment.
Something went wrong with that request. Please try again.