forked from publify/publify
/
note_spec.rb
167 lines (134 loc) · 6.37 KB
/
note_spec.rb
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# coding: utf-8
require 'spec_helper'
describe Note do
context "with a simple blog" do
let!(:blog) { create(:blog) }
describe "validations" do
it { expect(build(:note)).to be_valid }
it { expect(build(:note, body:nil)).to be_invalid }
it "with a nil body, return default error message" do
note = build(:note, body:nil)
note.save
expect(note.errors[:body]).to eq(["can't be blank"])
end
context "with an existing note" do
let(:existing_note) { create(:note) }
it { expect(build(:note, guid: existing_note.guid)).to be_invalid }
end
end
describe :permalink do
let(:note) { create(:note, body:"àé") }
it { expect(note.permalink).to eq("#{note.id}-ae") }
it { expect(note.permalink_url).to eq("#{blog.base_url}/note/#{note.id}-ae") }
context "with a particular blog" do
before(:each) do
Blog.any_instance.stub(:custom_url_shortener).and_return(url_shortener)
Blog.any_instance.stub(:base_url).and_return("http://mybaseurl.net")
end
context "with a blog that have a custome url shortener" do
let(:url_shortener) { "shor.tl" }
it { expect(note.short_link).to eq("#{url_shortener} #{note.redirects.first.from_path}") }
end
context "with a blog that have a custome url shortener" do
let(:url_shortener) { nil }
it { expect(note.short_link).to eq("mybaseurl.net #{note.redirects.first.from_path}") }
end
end
end
describe :redirects do
let(:note) { create(:note) }
it { expect(note.redirects.map(&:to_path)).to eq([note.permalink_url]) }
end
describe :scopes do
describe :published do
let(:now) { DateTime.new(2012,5,20,14,23) }
let(:note) { create(:note, published_at: now - 1.minute) }
before(:each) { Time.stub(:now).and_return(now) }
context "with a unpubilshed note" do
let(:unpublished_note) { create(:unpublished_note) }
it { expect(Note.published).to eq([note]) }
end
context "with a note to publish later" do
let(:later_note) { create(:note, published_at: now + 3.days) }
it { expect(Note.published).to eq([note]) }
end
end
end
describe :send_to_twitter do
let(:note) { build(:note) }
context "with twitter configured for blog and user" do
before(:each) do
Blog.any_instance.should_receive(:has_twitter_configured?).and_return(true)
User.any_instance.should_receive(:has_twitter_configured?).and_return(true)
end
it { expect(note.send_to_twitter).to be_false }
end
context "with twitter not configured for blog" do
before(:each) do
Blog.any_instance.should_receive(:has_twitter_configured?).and_return(false)
end
it { expect(note.send_to_twitter).to be_false }
end
context "with a twitter configured for blog but not user" do
before(:each) do
Blog.any_instance.should_receive(:has_twitter_configured?).and_return(true)
User.any_instance.should_receive(:has_twitter_configured?).and_return(false)
end
it { expect(note.send_to_twitter).to be_false }
end
end
describe :default_text_filter do
let(:note) { build(:note) }
it { expect(note.default_text_filter.name).to eq(Blog.default.text_filter) }
end
describe :twitter_message do
let(:note) { create(:note, body: tweet) }
context "with a short simple message" do
let(:tweet) { "A message without URL" }
it { expect(note.twitter_message).to start_with(tweet) }
it { expect(note.twitter_message).to end_with(" (#{note.short_link})") }
end
context "with a short message with short HTTP url" do
let(:tweet) { "A message with a short URL http://foo.com" }
it { expect(note.twitter_message).to start_with(tweet) }
end
context "with a short message much more than 114 char" do
let(:tweet) { "A very big(10) message with lot of text (40)inside just to try the shortener and (80)the new link that publify must construct(124)" }
it { expect(note.twitter_message).to start_with(tweet[0..note.twitter_message_max_length]) }
it { expect(note.twitter_message).to_not include(tweet[113..-1]) }
it { expect(note.twitter_message).to end_with(" (#{note.redirects.first.to_url})") }
end
context "with a bug message" do
let(:tweet) { "\"JSFuck is an esoteric and educational programming style based on the atomic parts of JavaScript. It uses only six different characters to write and execute code.\" http://www.jsfuck.com/ " }
it { expect(note.twitter_message).to start_with(tweet[0..note.twitter_message_max_length]) }
it { expect(note.twitter_message.length).to be <= 140 }
it { expect(note.twitter_message).to end_with(" (#{note.redirects.first.to_url})") }
end
end
end
context "with a dofollowify blog" do
let!(:blog) { create(:blog, dofollowify: true) }
describe "Testing hashtag and @mention replacement in html postprocessing" do
it "should replace a hashtag with a proper URL to Twitter search" do
note = build(:note, body: "A test tweet with a #hashtag")
expected = "A test tweet with a <a href='https://twitter.com/search?q=%23hashtag&src=tren&mode=realtime'>#hashtag</a>"
expect(note.html_preprocess(nil, note.body)).to eq(expected)
end
it "should replace a @mention by a proper URL to the twitter account" do
note = create(:note, body: "A test tweet with a @mention")
expected = "A test tweet with a <a href='https://twitter.com/mention'>@mention</a>"
expect(note.html_preprocess(nil, note.body)).to eq(expected)
end
it "should replace a http URL by a proper link" do
note = create(:note, body: "A test tweet with a http://link.com")
expected = "A test tweet with a <a href='http://link.com'>http://link.com</a>"
expect(note.html_preprocess(nil, note.body)).to eq(expected)
end
it "should replace a https URL with a proper link" do
note = create(:note, body: "A test tweet with a https://link.com")
expected = "A test tweet with a <a href='https://link.com'>https://link.com</a>"
expect(note.html_preprocess(nil, note.body)).to eq(expected)
end
end
end
end