Bookmarks now clean link in instantiate, posting them should work right again
This commit is contained in:
parent
683bb0dbef
commit
144e9ed439
3 changed files with 44 additions and 39 deletions
|
|
@ -4,11 +4,6 @@ class BookmarksController < ApplicationController
|
|||
def index
|
||||
@bookmark = Bookmark.new
|
||||
@bookmarks = Bookmark.paginate :page => params[:page], :order => 'created_at DESC'
|
||||
|
||||
|
||||
respond_to do |format|
|
||||
format.html
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
|
|
|
|||
|
|
@ -12,8 +12,6 @@ class Bookmark < Post
|
|||
validates_format_of :link, :with =>
|
||||
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
|
||||
|
||||
before_validation :clean_link
|
||||
|
||||
def to_activity
|
||||
<<-XML
|
||||
<entry>
|
||||
|
|
@ -27,12 +25,18 @@ class Bookmark < Post
|
|||
</entry>
|
||||
XML
|
||||
end
|
||||
|
||||
def self.instantiate params
|
||||
params[:link] = clean_link(params[:link])
|
||||
create params
|
||||
end
|
||||
|
||||
protected
|
||||
def clean_link
|
||||
if self.link
|
||||
self.link = 'http://' + self.link unless self.link.match('https?://')
|
||||
self.link = self.link + '/' if self.link[-1,1] != '/'
|
||||
def self.clean_link link
|
||||
if link
|
||||
link = 'http://' + link unless link.match('https?://')
|
||||
link = link + '/' if link[-1,1] != '/'
|
||||
link
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -10,34 +10,6 @@ describe Bookmark do
|
|||
|
||||
it 'should validate its link' do
|
||||
bookmark = Factory.build(:bookmark)
|
||||
|
||||
#links changed valid
|
||||
bookmark.link = "google.com"
|
||||
bookmark.valid?.should == true
|
||||
bookmark.link.should == "http://google.com/"
|
||||
|
||||
bookmark.link = "www.google.com"
|
||||
bookmark.valid?.should == true
|
||||
bookmark.link.should == "http://www.google.com/"
|
||||
|
||||
bookmark.link = "google.com/"
|
||||
bookmark.valid?.should == true
|
||||
bookmark.link.should == "http://google.com/"
|
||||
|
||||
bookmark.link = "www.google.com/"
|
||||
bookmark.valid?.should == true
|
||||
bookmark.link.should == "http://www.google.com/"
|
||||
|
||||
bookmark.link = "http://google.com"
|
||||
bookmark.valid?.should == true
|
||||
bookmark.link.should == "http://google.com/"
|
||||
|
||||
bookmark.link = "http://www.google.com"
|
||||
bookmark.valid?.should == true
|
||||
|
||||
#bookmark.link = "http://babycakes.sofaer.net:3000"
|
||||
#bookmark.valid?.should == true
|
||||
|
||||
#invalid links
|
||||
bookmark.link = "zsdvzxdg"
|
||||
bookmark.valid?.should == false
|
||||
|
|
@ -54,6 +26,24 @@ describe Bookmark do
|
|||
bookmark.link = "http:///www.asodij.com/"
|
||||
bookmark.valid?.should == false
|
||||
end
|
||||
|
||||
it 'should clean links' do
|
||||
bad_links = [
|
||||
"google.com",
|
||||
"www.google.com",
|
||||
"google.com/",
|
||||
"www.google.com/",
|
||||
"http://google.com",
|
||||
"http://www.google.com"
|
||||
]
|
||||
|
||||
bad_links.each{ |link|
|
||||
Bookmark.clean_link(link).should satisfy{ |link|
|
||||
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix.match(link)
|
||||
}
|
||||
}
|
||||
|
||||
end
|
||||
|
||||
describe "XML" do
|
||||
it 'should serialize to XML' do
|
||||
|
|
@ -71,4 +61,20 @@ describe Bookmark do
|
|||
parsed.valid?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with encryption' do
|
||||
before do
|
||||
unstub_mocha_stubs
|
||||
@user = Factory.create(:user)
|
||||
end
|
||||
|
||||
after do
|
||||
stub_signature_verification
|
||||
end
|
||||
|
||||
it 'should save a signed bookmark' do
|
||||
bookmark = @user.post(:bookmark, :title => "I love cryptography", :link => "http://pgp.mit.edu/")
|
||||
bookmark.created_at.should_not be nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue