diff --git a/app/index.rb b/app/index.rb index e905414c..a047a56f 100644 --- a/app/index.rb +++ b/app/index.rb @@ -82,3 +82,42 @@ end get '/thankyou' do erb :'thankyou' 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 diff --git a/tests/acceptance/index_tests.rb b/tests/acceptance/index_tests.rb index ff8aee1b..5435858e 100644 --- a/tests/acceptance/index_tests.rb +++ b/tests/acceptance/index_tests.rb @@ -63,4 +63,41 @@ describe '/' do 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 diff --git a/views/forgot_username.erb b/views/forgot_username.erb new file mode 100644 index 00000000..b0318fb5 --- /dev/null +++ b/views/forgot_username.erb @@ -0,0 +1,31 @@ +
+ <% flash.keys.select {|s| s == :error}.each do |key| %> + <%= flash[key] %> + <% end %> +
+If you forgot your sitename (AKA username), you can have it emailed to you here.
+ +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.
+ + +