bomberstudios / bliki

A small blog + wiki engine built on Sinatra + Stone

bliki / test / test_bliki.rb
100644 199 lines (188 sloc) 7.154 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
require 'test/helper'
 
class BlikiTest < Test::Unit::TestCase
  def setup
    # reset cache
    Dir["public/**/*"].each do |file|
      FileUtils.rm file unless File.directory? file
    end
    FileUtils.mkdir_p "test/public"
    Dir["test/public/**/*"].each do |file|
      FileUtils.rm file unless File.directory? file
    end
    # clear mock content
    Dir["db/test/datastore/**/*"].each do |file|
      FileUtils.rm file unless File.directory? file
    end
    Stone.start(Dir.pwd + "/db/#{Sinatra.env.to_s}", Dir.glob(File.join(Dir.pwd,"models/*")))
    # create one post
    p = Post.new(:title => "First post", :body => "This is a sample post", :tags => "test")
  end
  def teardown
    # clear mock content
    Dir["db/test/datastore/**/*"].each do |file|
      FileUtils.rm file unless File.directory? file
    end
  end
 
  # Test application runs at all
  test "Sinatra is loaded" do
    assert_instance_of Module, Sinatra
  end
  test "Views folder is correctly setup" do
    assert_equal "themes/#{Sinatra.options.theme}", Sinatra.options.views
  end
  test "Application is running" do
    get_it "/"
    assert_equal 200, status
  end
 
  # Content
  test "Title is Ok" do
    get_it "/"
    assert body.scan(/#{Sinatra.options.title}/).size > 0
  end
 
  # Mock content
  # Make sure authorization is disabled
  test "Auth is disabled in testing environment" do
    assert_equal false, Sinatra.application.options.use_auth
  end
  # Mock content: Posts
  test "Post creation works under the hood" do
    first_post = Post.new(:title => "First post", :body => "Wadus wadus", :tags => "foo, bar")
    get_it "/post/first-post"
    assert_equal 200, status
    get_it "/tag/foo"
    assert_equal 200, status
    get_it "/tag/bar"
    assert_equal 200, status
  end
  test "Post creation works over the hood" do
    post_it "/new", :title => "Second post", :body => "Wadus wadus", :tags => "wadus, badus"
    get_it "/post/second-post"
    assert_equal 200, status
    get_it "/tag/wadus"
    assert_equal 200, status
    get_it "/tag/badus"
    assert_equal 200, status
  end
  # Mock content: Pages
  test "Page creation works under the hood" do
    first_page = Page.new(:title => "First page", :body => "Wadus wadus", :tags => "foo, bar")
    get_it "/first-page"
    assert_equal 200, status
    get_it "/tag/foo"
    assert_equal 200, status
    get_it "/tag/bar"
    assert_equal 200, status
  end
  test "Page creation works over the hood" do
    post_it "/2/new", :title => "Second page", :body => "Wadus wadus", :tags => "wadus, badus"
    get_it "/second-page"
    assert_equal 200, status
    get_it "/tag/wadus"
    assert_equal 200, status
    get_it "/tag/badus"
    assert_equal 200, status
  end
 
  # Stone
  test "Stone works as expected" do
    all_posts_start = Post.all.size
    first_post = Post[1]
    assert_equal 1, first_post.id
    new_post = Post.new(:title => "Third post", :body => "Third post", :tags => "third")
    all_posts_end = Post.all.size
    assert_equal all_posts_end, all_posts_start + 1
  end
  test "Stone works with more than 99 existing posts" do
    post_count = Post.all.size
    (1..200-post_count).each do |i|
      tmp_post = Post.new(:title => "Post #{i}", :body => "Body #{i}", :tags => "tag#{i}" )
    end
    all_posts = Post.all
    assert_equal(200, all_posts.size)
    assert_equal(200, all_posts.last.id)
    assert_equal(Post[200], all_posts.last)
    (1..100).each do |i|
      tmp_post = Post.new(:title => "Post #{i}", :body => "Body #{i}", :tags => "tag#{i}" )
    end
    all_posts = Post.all
    assert_equal(300, all_posts.size)
    assert_equal(Post[300], all_posts.last)
  end
  test "Posts have a creation date" do
    first_post = Post[1]
    assert_not_nil first_post.created_at
  end
  test "Posts have an update date" do
    first_post = Post[1]
    assert_not_nil first_post.updated_at
    assert_kind_of DateTime, first_post.updated_at
  end
  test "Posts updated_at field is updated on save" do
    first_post = Post[1]
    original_updated_at = first_post.updated_at
    first_post.tags = "foo, bar, baz"
    first_post.save
    assert_not_equal original_updated_at, first_post.updated_at
    assert_kind_of DateTime, first_post.updated_at
  end
  test "Posts updated_at field is updated on put" do
    first_post = Post[1]
    original_updated_at = first_post.updated_at
    first_post.update_attributes(
      :tags => "foo, bar, baz"
    )
    assert_not_equal original_updated_at, first_post.updated_at
    assert_kind_of DateTime, first_post.updated_at
  end
 
  # Tags
  test "Tag page works" do
    get_it "/tag/tag1"
    assert_equal 200, status
  end
 
  # Content
  test "wikilinks are converted to links" do
    new_page = Page.new(:title => "test_page", :body => "[[wikilink1]] [[wikilink2]]", :tags => "wiki")
    get_it "/test_page"
    assert body.scan("<a href=\"#{Sinatra.options.base_url}/wikilink1\">wikilink1</a>").size > 0
    assert body.scan("<a href=\"#{Sinatra.options.base_url}/wikilink2\">wikilink2</a>").size > 0
  end
  test "WikiWords are converted to links" do
    new_page = Page.new(:title => "test_wikiwords", :body => "WikiWord WikiWikiWord", :tags => "wiki")
    get_it "/test_wikiwords"
    assert body.scan("<a href=\"#{Sinatra.options.base_url}/wikiword\">WikiWord</a>").size > 0
    assert body.scan("<a href=\"#{Sinatra.options.base_url}/wikiwikiword\">WikiWikiWord</a>").size > 0
  end
 
  # CSS: Base CSS
  test "CSS works" do
    get_it "/base.css"
    assert_equal 200, status
  end
 
  # Attachments
  test "attachment relationships work at model level" do
    post_with_attach = Post.new(:title => "Post with attach", :body => "this post has an attach", :tags => "attach")
    a = Attachment.new(:name => "foo", :path => Sinatra.options.public, :content => File.open("README.markdown").read, :post_id => post_with_attach.id)
    b = Attachment.new(:name => "bar", :path => Sinatra.options.public, :content => File.open("README.markdown").read, :post_id => post_with_attach.id)
    assert_equal 2, post_with_attach.attachments.size
  end
  test "Attachments are created with unique names" do
    a = Attachment.new(:name => "test_one", :path => Sinatra.options.public, :content => File.open("README.markdown").read)
    assert a.save == true
    b = Attachment.new(:name => "test_one", :path => Sinatra.options.public, :content => File.open("README.markdown").read)
    assert b.save == false
  end
  test "Files are created when saving attachments" do
    a = Attachment.new(:name => "attach", :path => Sinatra.options.public, :content => File.open("README.markdown").read)
    assert a.save == true, "File already exists"
    assert File.exist?(Sinatra.options.public / a.name ), "File not created"
  end
  test "Content for attachments is saved correctly" do
    a = Attachment.new(:name => "attach_content", :path => Sinatra.options.public, :content => File.open("README.markdown").read)
    assert File.open(a.path / a.name,"r").read.scan("bliki").size > 1
  end
 
  # Feed
  test "Feed is valid" do
    get_it "/feed/"
    assert_equal 200, status
    assert_valid_feed body
  end
 
  # TODO: Test passenger, somehow
end