username lookup tool

This commit is contained in:
Kyle Drake 2016-04-11 17:57:51 -07:00
parent fd3a7ccabc
commit 05e5b3998b
4 changed files with 112 additions and 3 deletions

View file

@ -82,3 +82,42 @@ end
get '/thankyou' do get '/thankyou' do
erb :'thankyou' erb :'thankyou'
end end
get '/forgot_username' do
erb :'forgot_username'
end
post '/forgot_username' do
if params[:email].blank?
flash[:error] = 'Cannot use an empty email address!'
redirect '/forgot_username'
end
sites = Site.where(email: params[:email]).all
sites.each do |site|
body = <<-EOT
Hello! This is the Neocities cat, and I have received a username lookup request using this email address.
Your username is #{site.username}
If you didn't request this, you can ignore it. Or hide under a bed. Or take a nap. Your call.
Meow,
the Neocities Cat
EOT
body.strip!
EmailWorker.perform_async({
from: 'web@neocities.org',
to: params[:email],
subject: '[Neocities] Username lookup',
body: body
})
end
flash[:success] = 'If your email was valid, the Neocities Cat will send an e-mail with your username in it.'
redirect '/'
end

View file

@ -63,4 +63,41 @@ describe '/' do
end end
end end
end end
describe 'username lookup' do
before do
@site = Fabricate :site
Capybara.reset_sessions!
EmailWorker.jobs.clear
visit '/signin'
click_link 'I forgot my username.'
end
it 'works for valid email' do
page.current_url.must_match /\/forgot_username$/
fill_in :email, with: @site.email
click_button 'Find username'
URI.parse(page.current_url).path.must_equal '/'
page.must_have_content 'If your email was valid, the Neocities Cat will send an e-mail with your username in it'
email_args = EmailWorker.jobs.first['args'].first
email_args['to'].must_equal @site.email
email_args['subject'].must_match /username lookup/i
email_args['body'].must_match /your username is #{@site.username}/i
end
it 'fails silently for unknown email' do
fill_in :email, with: 'N-O-P-E@example.com'
click_button 'Find username'
URI.parse(page.current_url).path.must_equal '/'
page.must_have_content 'If your email was valid, the Neocities Cat will send an e-mail with your username in it'
EmailWorker.jobs.length.must_equal 0
end
it 'fails for no input' do
click_button 'Find username'
URI.parse(page.current_url).path.must_equal '/forgot_username'
page.must_have_content 'Cannot use an empty email address'
end
end
end end

31
views/forgot_username.erb Normal file
View file

@ -0,0 +1,31 @@
<div class="header-Outro">
<div class="row content single-Col txt-Center">
<h1>Forgot Username</h1>
<h3 class="subtitle"></h3>
</div>
</div>
<div class="content txt-Center single-Col misc-page">
<% if flash.keys.length > 0 %>
<div class="alert alert-block">
<p>
<% flash.keys.select {|s| s == :error}.each do |key| %>
<%= flash[key] %>
<% end %>
</p>
</div>
<% end %>
<p>If you forgot your sitename (AKA username), you can have it emailed to you here.</p>
<p>Keep in mind that we cannot help you locate your sitename without a valid email address, and we don't disclose whether an email address exists in our system. This is for security and privacy reasons.</p>
<form method="POST" action="/forgot_username" class="content">
<input name="csrf_token" type="hidden" value="<%= csrf_token %>">
<fieldset>
<label for="email">Enter your email</label>
<input name="email" type="text" placeholder="email@address.com" class="input-Area" autocapitalize="off" autocorrect="off" value="<%= flash[:email] %>" style="width: 290px">
</fieldset>
<input class="btn-Action" type="submit" value="Find username">
</form>
</div>

View file

@ -15,7 +15,7 @@
</p> </p>
</div> </div>
<% end %> <% end %>
<form method="POST" action="/signin" class="content"> <form method="POST" action="/signin" class="content">
<input name="csrf_token" type="hidden" value="<%= csrf_token %>"> <input name="csrf_token" type="hidden" value="<%= csrf_token %>">
<fieldset> <fieldset>
@ -32,6 +32,8 @@
<a href="/#new" title="Sign me up!">I don't have an account yet.</a> <a href="/#new" title="Sign me up!">I don't have an account yet.</a>
</p> </p>
<p> <p>
<a href="/password_reset" title="What did I huh?">I forgot my password.</a> <a href="/password_reset" title="You should use a password manager like LastPass, it will change your life.">I forgot my password.</a>
</p> </p>
</div> <p>
<a href="/forgot_username" title="Seriously?">I forgot my username.</a>
</div>