<commit_before># frozen_string_literal: true
require 'test_helper'

#
# == Admin namespace
#
module Admin
  #
  # == UsersController test
  #
  class UsersControllerTest < ActionController::TestCase
    include Devise::TestHelpers

    setup :initialize_test

    #
    # == Routes / Templates
    #
    test 'should redirect to users/sign_in if not logged in' do
      sign_out @administrator
      assert_crud_actions(@administrator, new_user_session_path, model_name)
    end

    test 'should show index page if logged in' do
      get :index
      assert_response :success
    end

    test 'should show show page if logged in' do
      get :show, id: @administrator
      assert_response :success
    end

    test 'should destroy user' do
      assert_difference 'User.count', -1 do
        delete :destroy, id: @administrator
      end
      assert_redirected_to admin_users_path
    end

    #
    # == Batch actions
    #
    test 'should return correct value for toggle_active batch action' do
      post :batch_action, batch_action: 'toggle_active', collection_selection: [@administrator_2.id, @bart.id]
      [@administrator_2, @bart].each(&:reload)
      assert_not @administrator_2.account_active?
      assert @bart.account_active?
    end

    test 'should redirect to back and have correct flash notice for toggle_active batch action' do
      post :batch_action, batch_action: 'toggle_active', collection_selection: [@administrator_2.id]
      assert_redirected_to admin_users_path
      assert_equal I18n.t('active_admin.batch_actions.toggle_active'), flash[:notice]
    end

    #####################
    ## Superadministator
    ####################
    test 'should be able to create user' do
      sign_in @super_administrator
      assert_difference 'User.count' do
        post :create, user: { username: 'Marco', email: '<EMAIL>', password: 'password', confirm_password: 'password' }
      end
    end

    test 'should not be able to create if username already taken' do
      sign_in @super_administrator<commit_msg>Add test to ensure user cannot connect if account is not validated<commit_after># frozen_string_literal: true
require 'test_helper'

#
# == Admin namespace
#
module Admin
  #
  # == UsersController test
  #
  class UsersControllerTest < ActionController::TestCase
    include Devise::TestHelpers

    setup :initialize_test

    #
    # == Routes / Templates
    #
    test 'should redirect to users/sign_in if not logged in' do
      sign_out @administrator
      assert_crud_actions(@administrator, new_user_session_path, model_name)
    end

    test 'should show index page if logged in' do
      get :index
      assert_response :success
    end

    test 'should show show page if logged in' do
      get :show, id: @administrator
      assert_response :success
    end

    test 'should destroy user' do
      assert_difference 'User.count', -1 do
        delete :destroy, id: @administrator
      end
      assert_redirected_to admin_users_path
    end

    #
    # == Batch actions
    #
    test 'should return correct value for toggle_active batch action' do
      post :batch_action, batch_action: 'toggle_active', collection_selection: [@administrator_2.id, @bart.id]
      [@administrator_2, @bart].each(&:reload)
      assert_not @administrator_2.account_active?
      assert @bart.account_active?
    end

    test 'should redirect to back and have correct flash notice for toggle_active batch action' do
      post :batch_action, batch_action: 'toggle_active', collection_selection: [@administrator_2.id]
      assert_redirected_to admin_users_path
      assert_equal I18n.t('active_admin.batch_actions.toggle_active'), flash[:notice]
    end

    #
    # == Account validation
    #
    test 'should not be able to sign in if account is not active' do
      sign_in @bart
      get :show, id: @bart
      assert_redirected_to new_user_session_path
      assert_equal I18n.t('devise.failure.inactive'), flash[:alert]
    end

    test 'should be able to sign in if account is active' do
      @bart.update_attribute(:account_active, true)
      sign_in @bart
      get :show, id: @bart
      assert_response :success
    end

    #####################
    ## Superadministator
    ####################
    test 'should be able to create user' do
      sign_in @super_administrator
      assert_difference 'User.count' do
        post :create, user: { username: 'Marco', email: '<EMAIL>', password: 'password', confirm_password: 'password' }
      end
    end

    test 'should not be able to create if username already taken' do
      sign_in @super_administrator
{'fraction_non_alphanumeric': 0.09232432432432433, 'fraction_numerical': 0.0034594594594594594, 'mean_word_length': 3.240146654445463, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2266676', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1301, 'n_words': 416}