-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.rb
212 lines (150 loc) · 4.92 KB
/
tests.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
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
# Load the testing libraries
require 'test/unit'
require 'rack/test'
ENV['RACK_ENV'] = 'test'
require './app.rb'
class ApplicationTest < Test::Unit::TestCase
include Rack::Test::Methods
def app
Sinatra::Application
end
def setup
return unless File.exists?('test.sqlite3')
db = SQLite3::Database.new('test.sqlite3')
db.execute "DELETE FROM users WHERE 1;"
end
def login_user
name = "test_name_#{rand(256)}"
pass = "passpass#{rand(256)}"
post '/users/', { :name => name, :password => pass }
post '/login', { :name => name, :password => pass }
{ :name => name, :pass => pass }
end
def post_message(message=nil)
data = {
:message => message || "test_name_#{rand(256)}"
}
post '/', data
data
end
def test_homepage
get '/'
assert last_response.ok?,
"Homepage loaded without an error."
assert last_response.body.include?('Please leave me a message below!'),
"Expected text present."
end
def test_new_message_thanks
user = login_user
message = post_message
assert last_response.ok?,
"Form posts without an error."
assert last_response.body.include?(user[:name]),
"Page includes the name of the poster."
end
def test_messages_displayed
user = login_user
message = post_message
get '/'
assert last_response.ok?, "No errors returned."
assert last_response.body.include?(user[:name]),
"Posted name is displayed on the main page."
assert last_response.body.include?(message[:message]),
"Posted message is displayed on the main page."
end
def test_user_profile
user = login_user
name = user[:name]
post '/', { :message => 'First post' }
post '/', { :message => 'A second post' }
post '/', { :message => 'And a third' }
get '/users/'+name
assert last_response.ok?, "/users/#{name} resolves correctly"
assert last_response.body.include?(name),
"#{name} displayed on user page"
assert last_response.body.include?("First post"),
"First post of #{name} displayed on user page"
assert last_response.body.include?("A second post"),
"Second post of #{name} displayed on user page"
assert last_response.body.include?("And a third"),
"Third post of #{name} displayed on user page"
end
def test_user_editing_apologizes_when_no_user_found
name = "test_name_#{rand(256)}"
mess = "Sorry, the user \"#{name}\" doesn't exist."
get '/users/'+name+'/edit'
assert last_response.ok?, "/users/#{name} resolves correctly"
assert last_response.body.include?(mess),
"Displays '#{mess}' when the user can't be found."
end
def test_saving_new_name_for_user
d = login_user
name1 = d[:name]
name2 = "test_name_#{rand(256)}"
mess = "Sorry, the user \"#{name1}\" doesn't exist."
post '/', { :message => 'First post'}
post '/users/'+name1, {:name => name2}
get '/users/'+name2
assert last_response.ok?,
"The user's page has moved to the newly-named link"
assert last_response.body.include?('First post'),
"The user's post history is available."
get '/users/'+name1
assert last_response.body.include?(mess),
"The page under the old user name should display '#{mess}'."
end
def test_can_sign_up
name = "test_name_#{rand(256)}"
pass = "passpass#{rand(256)}"
post '/users/', { :name => name, :password => pass }
get '/users/'+name
assert last_response.ok?,
"User returned"
assert last_response.body.include? name
"Username found on user profile page."
end
def test_can_log_in
name = "test_name_#{rand(256)}"
pass = "passpass#{rand(256)}"
mess = "Thank you for logging in, #{name}!"
post '/users/', { :name => name, :password => pass }
post '/login', { :name => name, :password => pass }
assert last_response.ok?,
"Login path exists."
assert last_response.body.include?(mess),
"Login page says, '#{mess}'"
end
def test_reject_bad_password
name = "test_name_#{rand(256)}"
pass = "passpass#{rand(256)}"
mess = "Sorry, that password isn't correct."
post '/login', { :name => name, :password => pass }
assert last_response.ok?,
"Login path exists."
assert last_response.body.include?(mess),
"Login page says, '#{mess}'"
end
def test_rejects_posts_when_logged_out
post_message
mess = "Sorry, you must be logged in to post a message."
assert last_response.body.include?(mess),
"Page says, '#{mess}'"
end
def test_can_only_edit_own_username
user = login_user
name = user[:name]
name1 = "test_name_#{rand(256)}"
name2 = "test_name_#{rand(256)}"
mess = "Sorry, you are not user \"#{name1}\"."
post '/', { :message => 'First post' }
post '/users/'+name1, {:name => name2}
assert last_response.body.include?(mess),
"The user cannot edit another user's name"
post '/users/'+name, {:name => name2}
get '/users/'+name2
assert last_response.ok?,
"The user's page has moved to the newly-named link"
assert last_response.body.include?('First post'),
"The user's post history is available."
end
end