<commit_before>require File.dirname(__FILE__) + '/../test_helper'
require 'action_controller'
require 'action_controller/test_process'

require File.dirname(__FILE__) + '/../fixtures/users_controller.rb'

class UsersControllerTest < ActionController::TestCase

  include SimplyAuthenticate::Helpers

  def setup
    @controller = UsersController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new

    @request.host = 'localhost'

    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []

    ActionController::Routing::Routes.draw do |map|
      map.root :controller => 'users', :action => 'root'
    end
  end

  def test_logged_in
    # not yet logged in
    get :login
    assert !logged_in?

    # setup session & check again
    session[:user_id] = @bob.id
    assert logged_in?
  end

  def test_load_invalid_user_from_session
    get :login
    session[:user_id] = 54151515616
    assert_raise(ActiveRecord::RecordNotFound) {load_user}
    assert_equal @current_user, nil
  end

  def test_load_user_from_session
    get :login
    session[:user_id] = @bob.id
    load_user
    assert_equal @current_user, @bob
  end

  def test_roles
    get :login

    # user
    session[:user_id] = @bob.id
    load_user
    assert logged_in?
    assert user?
    assert !editor?
    assert !administrator?
    assert !moderator?

    # administrator
    session[:user_id] = @administrator.id
    load_user
    assert logged_in?
    assert user?
    assert !editor?
    assert administrator?
    assert !moderator?

    # moderator
    session[:user_id] = @moderator.id
    load_user
    assert logged_in?
    assert user?
    assert !editor?
    assert !administrator?
    assert moderator?
  end

  def test_login_required
    # can't access 'profile' if not logged in
    get :profile
    assert_response :redirect
    assert_redirected_to login_path
    assert flash.has_key?(:error)

    login_as(@bob)

    # can access it now
    get :profile
    assert_response :success
  end

  def test_invalid_registration
    # set legal notice to empty (not required)
    SimplyAuthenticate::Settings.legal_notice = ''
    SimplyAuthenticate::Settings.legal_requirements_message = ''

    # fetch registration page
    get :register
    assert_response :success
    assert_select 'h1', :text => /REJESTRACJA/
    assert !flash[:success]
    assert !flash[:error]

    # empty email
    post :register, {:user => {:email => ''}}
    assert_response :success
    assert flash[:error]
    assert @response.template_objects['user'].errors.invalid?(:email)

    # too short email
    post :register, {:user => {:email => 'eee'}}
    assert_response :success
    assert flash[:error]
    assert @response.template_objects['user'].errors.invalid?(:email)

    # wrong email
    post :register, {:user => {:email => 'wrong@email'}}
    assert_response :success
    assert flash[:error]
    assert @response.template_objects['user'].errors.invalid?(:email)

    # wrong email (collision)
    post :register, {:user => {:email => @bob.email}}
    assert_response :success
    assert flash[:error]
    assert @response.template_objects['user'].errors.invalid?(:email)

    # require confirmation of legal notice
    SimplyAuthenticate::Settings.legal_notice = 'Required now'
    SimplyAuthenticate::Settings.legal_requirements_message = 'Really required'

    # legal notice not accepted
    post :register, {:user => {:email => '<EMAIL>'}}
    assert_response :success
    assert flash[:error]
  end

  def test_registration
    # require confirmation of legal notice
    SimplyAuthenticate::Settings.legal_notice = 'Required now'
    SimplyAuthenticate::Settings.legal_requirements_message = 'Really required'

    email = '<EMAIL>'

    # register
    post :register, {:user => {:email => email}, :legal_requirements => {:accepted => '1'}}
    assert flash.has_key?(:success)
    assert_response :redirect
    assert flash[:success]
    assert_redirected_to SimplyAuthenticate::Settings.default_redirect_to

    # check proper role assignment
    @albert = User.find_by_email(email)
    assert @albert.roles.include?(Role.find_by_slug('user'))

    # extract password and activation code
    email_sent = ActionMailer::Base.deliveries.first
    assert_not_nil email_sent
    password = email_sent.body[/Hasło: (\w+)$/, 1]
    assert_not_nil password
    activation_code = email_sent.body[/aktywacja\/(\w+)$/, 1]
    assert_not_nil activation_code

    # check that we cannot yet login
    post :login, {:user => {:email => email, :password => password}, :remember => {:me => '0'}}
    assert_response :success
    assert_nil session[:user_id]
    assert_select 'div', :text => /Twoje konto nie zostało jeszcze aktywowane/

    # assume the activation key was lost and request it again
    get :send_activation_code
    assert_response :success
    post :send_activation_code, {:user => {:email => email}}<commit_msg>FIX helpers test; switch to using flash.has_key? everywhere (when testing flashes)<commit_after>require File.dirname(__FILE__) + '/../test_helper'
require 'action_controller'
require 'action_controller/test_process'

require File.dirname(__FILE__) + '/../fixtures/users_controller.rb'

class UsersControllerTest < ActionController::TestCase

  include SimplyAuthenticate::Helpers

  def setup
    @controller = UsersController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new

    @request.host = 'localhost'

    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []

    ActionController::Routing::Routes.draw do |map|
      map.root :controller => 'users', :action => 'root'
    end
  end

  def test_logged_in
    # not yet logged in
    get :login
    assert !logged_in?

    # setup session & check again
    session[:user_id] = @bob.id
    assert logged_in?
  end

  def test_load_invalid_user_from_session
    get :login
    session[:user_id] = 54151515616
    assert_raise(ActiveRecord::RecordNotFound) {load_user}
    assert_equal @current_user, nil
  end

  def test_load_user_from_session
    get :login
    session[:user_id] = @bob.id
    load_user
    assert_equal @current_user, @bob
  end

  def test_roles
    get :login

    # user
    session[:user_id] = @bob.id
    load_user
    assert logged_in?
    assert user?
    assert !editor?
    assert !administrator?
    assert !moderator?

    # administrator
    session[:user_id] = @administrator.id
    load_user
    assert logged_in?
    assert user?
    assert !editor?
    assert administrator?
    assert !moderator?

    # moderator
    session[:user_id] = @moderator.id
    load_user
    assert logged_in?
    assert user?
    assert !editor?
    assert !administrator?
    assert moderator?
  end

  def test_login_required
    # can't access 'profile' if not logged in
    get :profile
    assert_response :redirect
    assert_redirected_to login_path
    assert flash.has_key?(:error)

    login_as(@bob)

    # can access it now
    get :profile
    assert_response :success
  end

  def test_invalid_registration
    # set legal notice to empty (not required)
    SimplyAuthenticate::Settings.legal_notice = ''
    SimplyAuthenticate::Settings.legal_requirements_message = ''

    # fetch registration page
    get :register
    assert_response :success
    assert_select 'h1', :text => /REJESTRACJA/
    assert !flash.has_key?(:success)
    assert !flash.has_key?(:error)

    # empty email
    post :register, {:user => {:email => ''}}
    assert_response :success
    assert flash.has_key?(:error)
    assert @response.template_objects['user'].errors.invalid?(:email)

    # too short email
    post :register, {:user => {:email => 'eee'}}
    assert_response :success
    assert flash.has_key?(:error)
    assert @response.template_objects['user'].errors.invalid?(:email)

    # wrong email
    post :register, {:user => {:email => 'wrong@email'}}
    assert_response :success
    assert flash.has_key?(:error)
    assert @response.template_objects['user'].errors.invalid?(:email)

    # wrong email (collision)
    post :register, {:user => {:email => @bob.email}}
    assert_response :success
    assert flash.has_key?(:error)
    assert @response.template_objects['user'].errors.invalid?(:email)

    # require confirmation of legal notice
    SimplyAuthenticate::Settings.legal_notice = 'Required now'
    SimplyAuthenticate::Settings.legal_requirements_message = 'Really required'

    # legal notice not accepted
    post :register, {:user => {:email => '<EMAIL>'}}
    assert_response :success
    assert flash.has_key?(:error)
  end

  def test_registration
    # require confirmation of legal notice
    SimplyAuthenticate::Settings.legal_notice = 'Required now'
    SimplyAuthenticate::Settings.legal_requirements_message = 'Really required'

    email = '<EMAIL>'

    # register
    post :register, {:user => {:email => email}, :legal_requirements => {:accepted => '1'}}
    assert flash.has_key?(:success)
    assert_response :redirect
    assert flash.has_key?(:success)
    assert_redirected_to SimplyAuthenticate::Settings.default_redirect_to

    # check proper role assignment
    @albert = User.find_by_email(email)
    assert @albert.roles.include?(Role.find_by_slug('user'))

    # extract password and activation code
    email_sent = ActionMailer::Base.deliveries.first
    assert_not_nil email_sent
    password = email_sent.body[/Hasło: (\w+)$/, 1]
    assert_not_nil password
    activation_code = email_sent.body[/aktywacja\/(\w+)$/, 1]
    assert_not_nil activation_code

    # check that we cannot yet login
    post :login, {:user => {:email => email, :password => password}, :remember => {:me => '0'}}
    assert_response :success
    assert_nil session[:user_id]
    assert_select 'div', :text => /Twoje konto nie zostało jeszcze aktywowane/

    # assume the activation key was lost and request it again
    get :send_activation_code
    assert_response :success
    post :send_activation_code, {:user => {:email => email}}
{'fraction_non_alphanumeric': 0.10751739684406547, 'fraction_numerical': 0.0031363324512398314, 'mean_word_length': 3.7109879963065557, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '340760', 'n_tokens_mistral': 3167, 'n_tokens_neox': 3026, 'n_words': 747}