-
Notifications
You must be signed in to change notification settings - Fork 460
/
story_parser_spec.rb
139 lines (112 loc) · 5.61 KB
/
story_parser_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
require 'spec_helper'
describe StoryParser do
before(:each) do
@sp = StoryParser.new
end
describe "get_source_if_known:" do
describe "the SOURCE_FFNET pattern" do
it "should match http://fanfiction.net" do
url = "http://fanfiction.net"
@sp.get_source_if_known(StoryParser::CHAPTERED_STORY_LOCATIONS, url).should eq("ffnet")
end
it "should match fanfiction.net" do
url = "fanfiction.net"
@sp.get_source_if_known(StoryParser::CHAPTERED_STORY_LOCATIONS, url).should eq("ffnet")
end
it "should match http://www.fanfiction.net" do
url = "http://www.fanfiction.net"
@sp.get_source_if_known(StoryParser::CHAPTERED_STORY_LOCATIONS, url).should eq("ffnet")
end
it "should match www.fanfiction.net" do
url = "www.fanfiction.net"
@sp.get_source_if_known(StoryParser::CHAPTERED_STORY_LOCATIONS, url).should eq("ffnet")
end
it "should not match http://adultfanfiction.net" do
url = "http://adultfanfiction.net"
@sp.get_source_if_known(StoryParser::CHAPTERED_STORY_LOCATIONS, url).should be_nil
end
it "should not match adultfanfiction.net" do
url = "adultfanfiction.net"
@sp.get_source_if_known(StoryParser::CHAPTERED_STORY_LOCATIONS, url).should be_nil
end
it "should not match http://www.adultfanfiction.net" do
url = "http://www.adultfanfiction.net"
@sp.get_source_if_known(StoryParser::CHAPTERED_STORY_LOCATIONS, url).should be_nil
end
it "should not match www.adultfanfiction.net" do
url = "www.adultfanfiction.net"
@sp.get_source_if_known(StoryParser::CHAPTERED_STORY_LOCATIONS, url).should be_nil
end
end
describe "the SOURCE_LJ pattern" do
# SOURCE_LJ = '((live|dead|insane)?journal(fen)?\.com)|dreamwidth\.org'
it "should match a regular domain on livejournal" do
url = "http://mydomain.livejournal.com"
@sp.get_source_if_known(StoryParser::KNOWN_STORY_LOCATIONS, url).should eq("lj")
end
it "should match a domain with underscores within on livejournal" do
url = "http://my_domain.livejournal.com"
@sp.get_source_if_known(StoryParser::KNOWN_STORY_LOCATIONS, url).should eq("lj")
end
it "should match a folder style link to an individual user on livejournal" do
url = "http://www.livejournal.com/users/_underscore"
@sp.get_source_if_known(StoryParser::KNOWN_STORY_LOCATIONS, url).should eq("lj")
end
it "should match a folder style link to a community on livejournal" do
url = "http://www.livejournal.com/community/underscore_"
@sp.get_source_if_known(StoryParser::KNOWN_STORY_LOCATIONS, url).should eq("lj")
end
it "should match a domain on dreamwidth" do
url = "http://mydomain.dreamwidth.org"
@sp.get_source_if_known(StoryParser::KNOWN_STORY_LOCATIONS, url).should eq("lj")
end
it "should match a domain on deadjournal" do
url = "http://mydomain.deadjournal.com"
@sp.get_source_if_known(StoryParser::KNOWN_STORY_LOCATIONS, url).should eq("lj")
end
it "should match a domain on insanejournal" do
url = "http://mydomain.insanejournal.com"
@sp.get_source_if_known(StoryParser::KNOWN_STORY_LOCATIONS, url).should eq("lj")
end
# TODO: uncomment and remove this comment when (if) fixing the bug
it "should match a folder style link to an individual user on journalfen" # do
# url = "http://www.journalfen.net/users/username"
# @sp.get_source_if_known(StoryParser::KNOWN_STORY_LOCATIONS, url).should eq("lj")
# end
end
# TODO: KNOWN_STORY_PARSERS
end
describe "check_for_previous_import" do
let(:location_with_www) { "http://www.testme.org/welcome_to_test_vale.html" }
let(:location_no_www) { "http://testme.org/welcome_to_test_vale.html" }
it "should recognise previously imported www. works" do
@work = FactoryGirl.create(:work, imported_from_url: location_with_www)
expect { @sp.check_for_previous_import(location_no_www) }.to raise_exception
end
it "should recognise previously imported non-www. works" do
@work = FactoryGirl.create(:work, imported_from_url: location_no_www)
expect { @sp.check_for_previous_import(location_with_www) }.to raise_exception
end
end
describe "#parse_common" do
it "should convert relative to absolute links" do
# This one doesn't work because the sanitizer is converting the & to &
# ['http://foo.com/bar.html', 'search.php?here=is&a=query'] => 'http://foo.com/search.php?here=is&a=query',
{
['http://foo.com/bar.html', 'thisdir.html'] => 'http://foo.com/thisdir.html',
['http://foo.com/bar.html?hello=foo', 'thisdir.html'] => 'http://foo.com/thisdir.html',
['http://foo.com/bar.html', './thisdir.html'] => 'http://foo.com/thisdir.html',
['http://foo.com/bar.html', 'img.jpg'] => 'http://foo.com/img.jpg',
['http://foo.com/bat/bar.html', '../updir.html'] => 'http://foo.com/updir.html',
['http://foo.com/bar.html', 'http://bar.com/foo.html'] => 'http://bar.com/foo.html',
['http://foo.com/bar.html', 'search.php?hereis=aquery'] => 'http://foo.com/search.php?hereis=aquery',
}.each_pair do |input, output|
location, href = input
story_in = '<html><body><p>here is <a href="' + href + '">a link</a>.</p></body></html>'
story_out = 'here is <a href="' + output + '">a link</a>.'
results = @sp.parse_common(story_in, location)
expect(results[:chapter_attributes][:content]).to include(story_out)
end
end
end
end