public
Clone URL: git://github.com/robbyrussell/rubyurl.git
Search Repo:
Robby Russell (author)
Sun Mar 02 17:08:48 -0800 2008
commit  e0346a323c6c102e38481411c412fde1998de331
tree    c3b21f236df0397612e6bc6be0792034e1ea29c3
parent  61057f107e17827d1653fc9d858eb0f68d5fd405
rubyurl / spec / models / link_spec.rb
100644 139 lines (111 sloc) 3.554 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require File.dirname(__FILE__) + '/../spec_helper'
 
describe Link, "with fixtures loaded" do
  fixtures :links
  
  it "should load a non-empty collection of links" do
    Link.find(:all).should_not be_empty
  end
  
  it "should have four records" do
    Link.should have(4).records
  end
end
 
describe "Planet Argon link " do
  fixtures :links, :visits
  
  before(:each) do
    @link = Link.find(links(:planetargon).id)
  end
  
  it "should have a matching website url" do
    @link.website_url.should eql(links(:planetargon).website_url)
  end
  
  it "should have two (2) visits" do
    @link.should have(2).visits
  end
  
  it "should not be flagged as spam" do
    @link.flagged_as_spam?.should be_false
  end
  
  it "should add a new visit with .add_visit" do
    request = mock('request')
    request.stub!(:remote_ip).and_return('127.0.0.1')
    lambda do
      @link.add_visit(request)
    end.should change(@link.visits, :count).by(1)
  end
end
 
describe "Spammer site" do
  fixtures :links, :visits
  
  before(:each) do
    @link = Link.find(links(:spammer_site).id)
  end
  
  it "should have one (1) visits" do
    @link.should have(1).visits
  end
  
  it "should have one flagged as spam visit" do
    @link.should have(1).spam_visits
  end
  
  it "should be flagged as spam" do
    @link.flagged_as_spam?.should be_true
  end
end
 
describe Link, "a new link" do
  include LinkSpecHelper
  
  before(:each) do
    @link = Link.new
  end
  
  it "should be invalid without a website url" do
    @link.attributes = valid_attributes.except(:website_url)
    @link.should have(1).error_on(:website_url)
  end
  
  it "should be invalid without an ip address" do
    @link.attributes = valid_attributes.except(:ip_address)
    @link.should have(1).error_on(:ip_address)
  end
  
  it "should be valid with valid attributes" do
    @link.attributes = valid_attributes
    @link.should be_valid
  end
  
  it "should generate a token when saved" do
    @link.attributes = valid_attributes
    @link.token.should be_nil
    @link.save.should be_true
    @link.token.should_not be_nil
  end
  
  it "should generate a permalink when created" do
    @link.attributes = valid_attributes
    @link.permalink.should be_nil
    @link.save.should be_true
    @link.permalink.should_not be_nil
    @link.permalink.should eql(DOMAIN_NAME + @link.token)
  end
end
 
describe "A new Link, which already exists" do
  include LinkSpecHelper
  
  before(:each) do
    @link = Link.new
    @link.attributes = valid_attributes
    @link.save
  end
  
  it "should return the original link rather than create a new one" do
    new_link = Link.find_or_create_by_website_url(valid_attributes[:website_url])
    new_link.should eql(@link)
  end
end
 
describe "A new link" do
  include LinkSpecHelper
  
  it "should not save when provided a URL without http://" do
    @link = Link.new
    @link.attributes = valid_attributes.except(:website_url)
    @link.website_url = 'google.com'
    @link.should have(1).error_on(:website_url)
  end
  
  it "should save a link without a .com/.net/.org/etc." do
    @link = Link.new
    @link.attributes = valid_attributes.except(:website_url)
    @link.website_url = 'http://hamster-style/'
    @link.should have(0).errors_on(:website_url)
  end
 
  it "should save a link with query string parameters" do
    @link = Link.new
    @link.attributes = valid_attributes.except(:website_url)
    @link.website_url = 'http://hamsterstyle.com/foo?x=1'
    @link.should have(0).errors_on(:website_url)
  end
end