From 2746b22c18875ee80ccff922607a6b9c92a866fc Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Tue, 21 Aug 2018 18:25:18 +0200 Subject: [PATCH] upload screenshot anonymously, login in, get screenshots assigned --- .../users/my_sessions_controller.rb | 6 +- test/controllers/packages_controller_test.rb | 67 +++++++++++-------- test/fixtures/users.yml | 2 +- test/functional/upload_test.rb | 27 ++++++++ 4 files changed, 70 insertions(+), 32 deletions(-) diff --git a/app/controllers/users/my_sessions_controller.rb b/app/controllers/users/my_sessions_controller.rb index a437838..f89f431 100644 --- a/app/controllers/users/my_sessions_controller.rb +++ b/app/controllers/users/my_sessions_controller.rb @@ -7,12 +7,14 @@ class Users::MySessionsController < Devise::SessionsController old_screenshots = session[:uploaded_screenshots] if old_screenshots.to_a.length > 0 old_screenshots.each do |id| + next unless id # sometimes we had 'nil' here ss = Screenshot.find(id) ss.user = @user ss.save! end - flash[:info] = "#{old_screenshots.to_a.length} uploads have been added to your account" - Log.log "Anonymously uploaded screenshots #{old_screenshots} added to account." + # The message is probably confusing. Just add the screenshots. + # flash[:info] = "#{old_screenshots.to_a.length} uploads have been added to your account" + Log.log "Anonymously uploaded screenshots #{old_screenshots} added to #{current_user}." session[:uploaded_screenshots] = nil end end diff --git a/test/controllers/packages_controller_test.rb b/test/controllers/packages_controller_test.rb index 4d79ffc..2cd206d 100644 --- a/test/controllers/packages_controller_test.rb +++ b/test/controllers/packages_controller_test.rb @@ -21,43 +21,52 @@ class PackagesControllerTest < ActionController::TestCase # assert_response :success # end - test "upload screenshot anonymously then login and get screenshots assigned" do - newest_screenshot1 = Package.find_by_name(:firefox).screenshots.order(id: :desc).first + # test "upload screenshot anonymously then login and get screenshots assigned" do + # newest_screenshot1 = Package.find_by_name(:firefox).screenshots.order(id: :desc).first - # Load the upload page - get :upload, params: { name: 'firefox' } - assert_response :success + # # Load the upload page + # get :upload, params: { name: 'firefox' } + # assert_response :success - # Upload a screenshot and except to be redirected afterwards - post :upload_receive, params: { name: 'firefox', file: [ fixture_file_upload('test/fixtures/files/large1.png','image/png') ] } - assert_redirected_to package_path(name: 'firefox') + # # Upload a screenshot and expect to be redirected afterwards + # post :upload_receive, params: { name: 'firefox', file: [ fixture_file_upload('test/fixtures/files/large1.png','image/png') ] } + # assert_redirected_to package_path(name: 'firefox') - # Check that the newest screenshot is not yet approved - newest_screenshot2 = Package.find_by_name(:firefox).screenshots.order(id: :desc).first - assert_not_equal newest_screenshot1, newest_screenshot2 - assert_equal newest_screenshot2.approved, false + # # Check that the newest screenshot is not yet approved + # newest_screenshot2 = Package.find_by_name(:firefox).screenshots.order(id: :desc).first + # assert_not_equal newest_screenshot1, newest_screenshot2 + # assert_equal newest_screenshot2.approved, false + + # # Login + # sign_in users(:normal) + + # # Get the information about the newest screenshot again + # newest_screenshot2 = Package.find_by_name(:firefox).screenshots.order(id: :desc).first + + # # Expect the uploaded screenshot to be assigned to this user + # byebug + # assert_equal newest_screenshot2.user, users(:normal) + # # (name: 'firefox') + # # img_count_before = page.find_all('img').count + # # attach_file 'file[]', Rails.root.join('test/fixtures/files/large1.png') + # # click_on 'Start upload' - # (name: 'firefox') - # img_count_before = page.find_all('img').count - # attach_file 'file[]', Rails.root.join('test/fixtures/files/large1.png') - # click_on 'Start upload' + # # # The cookie session must remember this anonymous upload + # # newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first + # # assert_equal session[:uploaded_screenshots].length, [newest_screenshot] - # # The cookie session must remember this anonymous upload - # newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first - # assert_equal session[:uploaded_screenshots].length, [newest_screenshot] + # # # + # # # sign_in users(:debian) - # # - # # sign_in users(:debian) + # # # # Screenshot is instantly public + # # # newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first + # # # assert_equal newest_screenshot.approved, true - # # # Screenshot is instantly public - # # newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first - # # assert_equal newest_screenshot.approved, true - - # # # Clean up - # newest_screenshot.destroy - # sign_out users(:normal) - end + # # # # Clean up + # # newest_screenshot.destroy + # # sign_out users(:normal) + # end end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index bcd0fb3..d7fab89 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -26,4 +26,4 @@ debian: sign_in_count: 0 provider: debian-sso admin: 0 - encrypted_password: <%= Devise::Encryptor.digest(User, 'normalsecret') %> + encrypted_password: nil diff --git a/test/functional/upload_test.rb b/test/functional/upload_test.rb index b7613c7..250f206 100644 --- a/test/functional/upload_test.rb +++ b/test/functional/upload_test.rb @@ -44,4 +44,31 @@ class BrowserBrowseTest < ActionDispatch::IntegrationTest sign_out users(:debian) end + test "upload screenshot anonymously, login in, get screenshots assigned" do + visit upload_path(name: 'firefox') + attach_file 'file[]', Rails.root.join('test/fixtures/files/large1.png') + click_on 'Start upload' + + # Screenshot has not yet a user assigned + newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first + assert_nil newest_screenshot.user + + # Login (sign_in is just a stub and would not call the callbacks!) + visit root_path + click_link 'Login' + within 'form' do + fill_in 'email address', with: users(:normal).email + fill_in 'password', with: 'normalsecret' + click_button 'Log in' + end + + # Check if screenshot was assigned to the user + newest_screenshot = Package.find_by_name(:firefox).screenshots.order(id: :desc).first + assert_equal newest_screenshot.user, users(:normal) + + # Clean up + newest_screenshot.destroy + sign_out users(:debian) + end + end