Commit 83fa8728 authored by Cool Fire's avatar Cool Fire

Finished handling most of the detail form, added warning page, using session...

Finished handling most of the detail form, added warning page, using session directly instread of error_ data.
parent 1990e8f6
Pipeline #347 passed with stages
in 1 minute and 33 seconds
---
error_message_username: Username appears to be invald.
error_message_email: Email address appears to be invalid.
error_message_phone: Phone number appears to be invald.
error_message_reason: It appears you have not specified a reason. This is technically allowed but makes it extremely unlikely your application will be accepted.
......@@ -12,16 +12,23 @@ class SignupSite < Sinatra::Base
end
post '/check-code' do
if params[:input_invite] !~ /^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/
session[:invite] = Rack::Utils.escape_html(params[:input_invite])
if check_code session[:invite]
cc = YAML.load_file('content/check-code.yaml')
session[:error_message] = cc['error_message']
session[:error_location] = '/enter-code'
session[:error_progress] = 25
session[:error_data] = Rack::Utils.escape_html(params[:input_invite])
redirect '/error', 302
else
session[:invite] = Rack::Utils.escape_html(params[:input_invite])
redirect '/enter-details', 302
end
end
def check_code(code)
return true if code.nil?
return false if code.match?(/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/)
# TODO: Check code against database
true
end
end
......@@ -13,6 +13,46 @@ class SignupSite < Sinatra::Base
end
post '/check-details' do
'Not yet implemented.'
cd = YAML.load_file('content/check-details.yaml')
session[:error_location] = '/enter-details'
session[:error_proceed] = '/not-yet-implemented'
session[:error_progress] = 50
session[:username] = Rack::Utils.escape_html(params[:input_username])
session[:email] = Rack::Utils.escape_html(params[:input_email])
session[:phone] = Rack::Utils.escape_html(params[:input_phone])
session[:reason] = Rack::Utils.escape_html(params[:input_reason])
if session[:username] !~ /^\w+$/
session[:error_message] = cd['error_message_username']
redirect '/error', 302
elsif session[:email] !~ /^\w+@\w+\.\w+$/
session[:error_message] = cd['error_message_email']
redirect '/error', 302
elsif check_phone session[:phone]
session[:error_message] = cd['error_message_phone']
redirect '/error', 302
elsif session[:reason].empty?
session[:error_message] = cd['error_message_reason']
redirect '/warning', 302
else
redirect '/not-yet-implemented', 302
end
end
def check_phone(number)
# Not providing a phone number is allowed
return false if number.empty?
# Normalize phone number formatting
number.delete!('-')
number.gsub!(/^(00|\+)/, '')
# Check for stray characters
return false if number.match?(/^\d+$/)
# Check with messagebird HLR if the phone number is valid
# TODO: Actually implement HLR lookup.
true
end
end
......@@ -10,4 +10,13 @@ class SignupSite < Sinatra::Base
session[:error_reload_form] = true
erb :index
end
get '/warning' do
@header = 'Look out!'
@body = session[:error_message]
@form = erb :warning
@progress = session[:error_progress]
session[:error_reload_form] = true
erb :index
end
end
......@@ -5,7 +5,7 @@ require 'yaml'
# Main class for signup site
class SignupSite < Sinatra::Base
use Rack::Session::Pool, :expire_after => 60 * 60 * 4
use Rack::Session::Pool, expire_after: 60 * 60 * 4
set :title, 'Shell signup - Insomnia 24/7'
set :gitid, IO.read('.current')
......
......@@ -40,14 +40,14 @@ describe 'Enter code page' do
get '/enter-code'
last_response.body.must_include 'Invite code'
end
end
describe 'Enter code page' do
it 'should return invite code form' do
get '/enter-code'
last_response.body.must_include 'Invite code'
end
end
describe 'Check code page' do
it 'should reject empty invite codes' do
post '/check-code'
follow_redirect!
......@@ -55,18 +55,21 @@ describe 'Enter code page' do
end
it 'should reject improperly formatted invite codes' do
post '/check-code', input_invite: 'not an invite code'
post '/check-code',
input_invite: 'not an invite code'
follow_redirect!
last_request.url.must_include 'error'
end
it 'should accept properly formatted invite codes' do
post '/check-code', input_invite: 'c65d8c31-482b-4329-b288-8b0959a43ea2'
post '/check-code',
input_invite: 'c65d8c31-482b-4329-b288-8b0959a43ea2'
assert_equal last_response.status, 302
end
it 'should redirect to details form for properly formatted invite codes' do
post '/check-code', input_invite: 'c65d8c31-482b-4329-b288-8b0959a43ea2'
post '/check-code',
input_invite: 'c65d8c31-482b-4329-b288-8b0959a43ea2'
follow_redirect!
last_request.url.must_include 'enter-details'
end
......@@ -83,3 +86,63 @@ describe 'Enter details page' do
last_response.body.must_include 'User name'
end
end
describe 'Check username' do
it 'should reject empty usernames' do
post '/check-details',
input_email: 'username@example.com'
follow_redirect!
last_request.url.must_include 'error'
end
it 'should reject usernames with invalid characters' do
post '/check-details',
input_username: 'user%name',
input_email: 'username@example.com'
follow_redirect!
last_request.url.must_include 'error'
end
it 'should accept valid usernames, email and reason' do
post '/check-details',
input_username: 'username',
input_email: 'username@example.com',
input_reason: 'My reasons are my own.'
follow_redirect!
last_request.url.must_include 'not-yet-implemented'
end
end
describe 'Check reason' do
it 'should warn for empty reasons' do
post '/check-details',
input_username: 'username',
input_email: 'username@example.com'
follow_redirect!
last_request.url.must_include 'warning'
end
end
describe 'Check phonenumber' do
it 'should reject invalid phone numbers' do
post '/check-details',
input_username: 'username',
input_email: 'username@example.com',
input_reason: 'My reasons are my own.',
input_phone: '+31g28282828'
follow_redirect!
last_request.url.must_include 'error'
end
it 'should accept valid phone numbers' do
post '/check-details',
input_username: 'username',
input_email: 'username@example.com',
input_reason: 'My reasons are my own.',
input_phone: '+31628282828'
follow_redirect!
last_request.url.must_include 'not-yet-implemented'
end
# TODO: checks for error and warning pages
end
......@@ -4,7 +4,7 @@
<label for="input_invite" class="col-lg-2 control-label">Invite code</label>
<div class="col-lg-10">
<% if session[:error_reload_form] %>
<input type="text" class="form-control" id="input_invite" name="input_invite" value="<%= session[:error_data] %>">
<input type="text" class="form-control" id="input_invite" name="input_invite" value="<%= session[:invite] %>">
<% session[:error_reload_form] = false %>
<% else %>
<input type="text" class="form-control" id="input_invite" name="input_invite" placeholder="1234abcd-1234-5678-9abc-432abc987def">
......
......@@ -3,7 +3,11 @@
<div class="form-group">
<label for="input_username" class="col-lg-2 control-label">User name</label>
<div class="col-lg-10">
<% if session[:error_reload_form] %>
<input type="text" class="form-control" id="input_username" name="input_username" value="<%= session[:username] %>">
<% else %>
<input type="text" class="form-control" id="input_username" name="input_username" placeholder="User name">
<% end %>
<span class="help-block">We strongly recommend a user name with only lowercase letters and numbers for compatibility reasons.</span>
</div>
</div>
......@@ -11,14 +15,22 @@
<div class="form-group">
<label for="input_email" class="col-lg-2 control-label">E-mail address</label>
<div class="col-lg-10">
<% if session[:error_reload_form] %>
<input type="text" class="form-control" id="input_email" name="input_email" value="<%= session[:email] %>">
<% else %>
<input type="text" class="form-control" id="input_email" name="input_email" placeholder="E-mail address">
<% end %>
</div>
</div>
<div class="form-group">
<label for="input_phone" class="col-lg-2 control-label">Phone number (Optional)</label>
<div class="col-lg-10">
<% if session[:error_reload_form] %>
<input type="text" class="form-control" id="input_phone" name="input_phone" value="<%= session[:phone] %>">
<% else %>
<input type="text" class="form-control" id="input_phone" name="input_phone" placeholder="+15415553421">
<% end %>
<span class="help-block">If you provide your phone number here we will use it to send you an SMS message with your password. Please enter your full phone number including the country code.</span>
</div>
</div>
......@@ -26,7 +38,12 @@
<div class="form-group">
<label for="input_reason" class="col-lg-2 control-label">Reasons</label>
<div class="col-lg-10">
<% if session[:error_reload_form] %>
<textarea class="form-control" rows="10" id="input_reason" name="input_reason"><%= session[:reason] %></textarea>
<% session[:error_reload_form] = false %>
<% else %>
<textarea class="form-control" rows="10" id="input_reason" name="input_reason"><% if session.has_key? :invite %>I have an invite!<% end %></textarea>
<% end %>
<span class="help-block">Please read "Specifying your reasons" above.</span>
</div>
</div>
......
<form class="form-horizontal" method="GET" action="<%= session[:error_location] %>">
<fieldset>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-info">Go back</button>
<a href="<%= session[:error_proceed] %>" class="btn btn-info">Proceed anyway</a>
</div>
</div>
</fieldset>
</form>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment