public
Description: The ultra-lightweight ultra-flexible blogging engine with a fetish for birds and misspellings.
Homepage: http://chyrp.net/
Clone URL: git://github.com/vito/chyrp.git
Click here to lend your support to: chyrp and make a donation at www.pledgie.com !
chyrp / tester.rb
100644 271 lines (204 sloc) 7.896 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require "test/unit"
require "net/http"
require "rubygems"
require "hpricot"
 
`rm -Rfv uploads/*`
`mysql -f --user=root -D chyrp -e 'TRUNCATE TABLE chyrp_posts; TRUNCATE TABLE chyrp_pages; TRUNCATE TABLE chyrp_comments; TRUNCATE TABLE chyrp_tags;' > /dev/null`
 
FUZZER = {
  :textarea => "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam urna. Vivamus nisl. Mauris iaculis rutrum elit. Cras ornare congue mi. Nullam mi quam, luctus dapibus, euismod ut, dapibus sed, dui. Praesent est lectus, rutrum ac, blandit vitae, hendrerit at, massa. Morbi mauris purus, lobortis vel, commodo vitae, aliquet vehicula, ante. Nunc commodo. Pellentesque vel lacus. Quisque eros. Maecenas et quam. Curabitur eget justo a ante dignissim dapibus. Sed et lacus. Suspendisse potenti. Vivamus ipsum mi, blandit vitae, scelerisque a, pellentesque vitae, nisl. Donec vitae est et est egestas laoreet. Vestibulum commodo elit ut nisl. Nullam volutpat nisi non elit. Morbi sapien eros, ornare et, dapibus id, mattis id, nibh. Suspendisse ut nisl id est scelerisque faucibus.\n\nPraesent viverra felis nec justo. Duis gravida tempor massa. Aliquam lobortis tortor eu purus. Phasellus volutpat, justo eget molestie rhoncus, nibh tortor suscipit justo, non vehicula tortor tortor id sapien. Vivamus quis nisl et neque ullamcorper viverra. Vestibulum accumsan, elit luctus auctor fermentum, lorem tellus dignissim odio, a lobortis magna nulla eget arcu. Phasellus vel erat at dolor sagittis luctus. Nulla facilisi. In eros eros, molestie sit amet, ornare a, fermentum et, dui. Vivamus vel turpis quis diam iaculis dapibus. Nunc lacinia. Integer commodo, urna interdum imperdiet pretium, libero nulla pellentesque turpis, in ultrices neque tortor at arcu. Sed mollis odio eget mauris ultricies bibendum. Vivamus malesuada metus vel arcu. Nam sit amet metus. Pellentesque quis felis non nibh adipiscing adipiscing.",
  :text => "Test Input"
}
 
SERVER = Net::HTTP.new "localhost"
 
CHYRP = "/chyrp"
 
HEADERS = {
  "Cookie" => "ChyrpSession=835e560250f81a6790747c40aa405a60", # NOTE: This has to be changed to keep in sync with your browser.
  "User-Agent" => "tester.rb"
}
 
POSTS = {
  :text => {
    "title" => "Test Text Post",
    "body" => FUZZER[:textarea]
  },
  :quote => {
    "quote" => FUZZER[:textarea].split(". ")[0] + ".",
    "source" => "Chyrp Tester"
  },
  :chat => {
    "title" => "Test Chat Post",
    "dialogue" => "Person: Hi!\nMe (me): Hello!\nPerson: How are you?\nMe: Great, thanks! And you?\nPerson: FUCKING AWESOME."
  },
  :link => {
    "name" => "Google Search",
    "source" => "http://google.com/",
    "description" => "I can't believe how long I've gone without finding this site."
  }
}
 
class Chyrp < Test::Unit::TestCase
  def test_add_post
    POSTS.each do |feather, attrs|
      resp, write = get "/admin/?action=write_post&feather="+ feather.to_s
 
      page = Hpricot(write)
 
      form_fuzz(page/"form").each do |key, val|
        attrs[key] = val unless attrs.has_key? key
      end
 
      attrs['feather'] = feather.to_s
 
      post (page/"form").attr("action"), attrs
    end
  end
 
  def test_add_draft
    POSTS.each do |feather, attrs|
      resp, write = get "/admin/?action=write_post&feather="+ feather.to_s
 
      page = Hpricot(write)
 
      form_fuzz(page/"form").each do |key, val|
        attrs[key] = val unless attrs.has_key? key
      end
 
      attrs['feather'] = feather.to_s
      attrs['draft'] = "true"
 
      post (page/"form").attr("action"), attrs
    end
  end
 
  def test_view_post
    resp, page = test_index
    page = Hpricot(page)
    return unless page =~ /class="post /
 
    post_url = (page/".post:first/h2/a").attr("href")
 
    get post_url
  end
 
  def test_add_page
    resp, write = get "/admin/?action=write_page"
 
    page = Hpricot(write)
 
    post (page/"form").attr("action"), form_fuzz(page/"form")
  end
 
  def test_view_page
    resp, page = test_index
    page = Hpricot(page)
 
    first_page = (page/"#sidebar/ul:nth(0)/li:nth(0)/a")
    return unless first_page
 
    page_url = first_page.attr("href")
 
    get page_url
  end
 
  def test_general_settings
    resp, page = get "/admin/?action=general_settings"
    page = Hpricot(page)
 
    settings = form_get(page/"form")
    settings['description'].reverse!
 
    post (page/"form").attr("action"), settings
  end
 
  def test_content_settings
    resp, page = get"/admin/?action=content_settings"
    page = Hpricot(page)
 
    post (page/"form").attr("action"), form_get(page/"form")
  end
 
  def test_user_settings
    resp, page = get "/admin/?action=user_settings"
    page = Hpricot(page)
 
    post (page/"form").attr("action"), form_get(page/"form")
  end
 
  def test_route_settings
    resp, page = get "/admin/?action=route_settings"
    page = Hpricot(page)
 
    post (page/"form").attr("action"), form_get(page/"form")
  end
 
  def test_archive
    get "/archive/"
  end
 
  def test_archive_year
    get "/archive/"+ Time.now.strftime("%y/")
  end
 
  def test_archive_month
    get "/archive/"+ Time.now.strftime("%y/%m/")
  end
 
  def test_archive_day
    get "/archive/"+ Time.now.strftime("%y/%m/%d/")
  end
 
  def test_index
    get "/"
  end
 
  def test_feed
    get "/feed/"
  end
 
  def test_search
    resp, page = get("/search/Lorem+ipsum/")
    assert_no_match /No Results/, page, "No search results listed."
  end
 
  def test_pagination
    resp, page = get("/page/2/")
    return unless page =~ /class="post /
    assert_match /Page 2 of /, page, "No pagination links displayed."
  end
 
  def test_drafts
    resp, page = get("/drafts/")
    assert_no_match /No Drafts/, page, "No draft posts listed."
  end
 
  def test_404
    assert (error?(get("/rghtueighntrwiu5ytn5ueygn/", false)) == "404 Not Found"), "Fuzzed dirty route was not a 404."
  end
 
  def test_dirty_404
    assert (error?(get("/?action=rghtueighntrwiu5ytn5ueygn", false)) == "404 Not Found"), "Fuzzed dirty route was not a 404."
  end
 
  private
    def get url, test = true
      url = CHYRP + url if url[0] == 47 # /
 
      receive = SERVER.get(url, HEADERS)
 
      if test
        error = error? receive
        assert (not error), error
      end
 
      receive
    end
 
    def post url, data, test = true
      send = []
      data.each do |key, val|
        send << key +"="+ val
      end
 
      url = CHYRP + url if url[0] == 47 # /
 
      send = SERVER.post(url, send.join("&"), HEADERS)
 
      if test
        error = error? send
        assert (not error), error
      end
 
      send
    end
 
    def form_fuzz form
      data = form_get form
 
      (form/"*[@name]").each do |field|
        next unless data[field['name']].nil? or data[field['name']].empty?
 
        if field.name == "textarea"
          data[field['name']] = FUZZER[:textarea]
        elsif field.name == "input"
          next if field['type'] != "text" or field['name'] == "trackbacks" or field['name'] == "slug"
          data[field['name']] = FUZZER[:text]
        end
      end
 
      data
    end
 
    def form_get form
      data = {}
 
      (form/"*[@name]").each do |field|
        if field.name == "select"
          selected = (field/"option[@selected]")
          option = selected.length > 0 ? selected : (field/"option:nth(0)")
          data[field['name']] = option.attr("value")
        elsif field.name != "button"
          if field['type'] == "checkbox"
            data[field['name']] = (field['checked'] || "no") == "checked" ? "1" : "0"
          else
            data[field['name']] = field['value'] || field.html
          end
        end
      end
 
      data
    end
 
    def error? response
      status, page = response
 
      page.gsub /^ERROR: (.+)/ do
        return $1
      end
 
      return "404 Not Found" if status.class == Net::HTTPNotFound
 
      false
    end
 
    def success? response
      status, page = response
      result =~ /^SUCCESS: (.+)/
    end
end