|
5 | 5 | it { should belong_to :topic } |
6 | 6 | it { should validate_presence_of :url } |
7 | 7 |
|
8 | | - it { should ensure_length_of(:referer).is_at_least(3).is_at_most(1000) } |
9 | | - it { should ensure_length_of(:domain).is_at_least(1).is_at_most(100) } |
10 | | - |
11 | 8 | let :post do |
12 | 9 | Fabricate(:post) |
13 | 10 | end |
|
46 | 43 | end |
47 | 44 |
|
48 | 45 | describe 'add' do |
| 46 | + class TestRequest<Rack::Request |
| 47 | + attr_accessor :remote_ip |
| 48 | + end |
| 49 | + def req(url, referer=nil) |
| 50 | + env = Rack::MockRequest.env_for(url) |
| 51 | + env['HTTP_REFERER'] = referer if referer |
| 52 | + TestRequest.new(env) |
| 53 | + end |
| 54 | + |
49 | 55 | it "does nothing if referer is empty" do |
50 | | - env = Rack::MockRequest.env_for("http://somesite.com") |
51 | | - request = Rack::Request.new(env) |
52 | 56 | IncomingLink.expects(:create).never |
53 | | - IncomingLink.add(request) |
| 57 | + IncomingLink.add(req('http://somesite.com')) |
54 | 58 | end |
55 | 59 |
|
56 | 60 | it "does nothing if referer is same as host" do |
57 | | - env = Rack::MockRequest.env_for("http://somesite.com") |
58 | | - env['HTTP_REFERER'] = 'http://somesite.com' |
59 | | - request = Rack::Request.new(env) |
60 | 61 | IncomingLink.expects(:create).never |
61 | | - IncomingLink.add(request) |
| 62 | + IncomingLink.add(req('http://somesite.com', 'http://somesite.com')) |
62 | 63 | end |
63 | 64 |
|
64 | 65 | it "expects to be called with referer and user id" do |
65 | | - env = Rack::MockRequest.env_for("http://somesite.com") |
66 | | - env['HTTP_REFERER'] = 'http://some.other.site.com' |
67 | | - request = Rack::Request.new(env) |
68 | 66 | IncomingLink.expects(:create).once.returns(true) |
69 | | - IncomingLink.add(request, 100) |
| 67 | + IncomingLink.add(req('http://somesite.com', 'http://some.other.site.com'), build(:user)) |
| 68 | + end |
| 69 | + |
| 70 | + it "is able to look up user_id and log it from the GET params" do |
| 71 | + user = Fabricate(:user, username: "Bob") |
| 72 | + IncomingLink.add(req('http://somesite.com?u=bob')) |
| 73 | + first = IncomingLink.first |
| 74 | + first.user_id.should == user.id |
70 | 75 | end |
71 | 76 | end |
72 | 77 |
|
|
0 commit comments