<commit_before>Given /^I am signed in as a charity worker (un)?related to "(.*?)"$/ do |negate, organisation_name|
  organisation = Organisation.find_by_name(organisation_name)
  if negate
    users = User.where(superadmin: false)
    user = users.find { |user| user.organisation != organisation }
  else
    user = organisation.users.find { |user| !user.superadmin? }
  end
  page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user).unshift("User"))
end

Given /^I am signed in as a (non-)?siteadmin$/ do |negate|
  user = User.find_by(siteadmin: negate ? false : true)
  page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user).unshift("User"))
end
Given /^I am signed in as an? (non-)?superadmin$/ do |negate|
  user = User.find_by_superadmin(negate ? false : true)
  page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user).unshift("User"))
end

Given /^I am signed in as a pending admin of "(.*?)"$/ do |org_name|
  org = Organisation.find_by_name org_name
  user = User.find_by_pending_organisation_id(org.id)
  page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user).unshift("User"))
end

Given /^I sign up as "(.*?)" with password "(.*?)" and password confirmation "(.*?)"( on the legacy sign up page)?$/ do |email, password, password_confirmation, legacy|
  if legacy
    within('#new_user') do
      fill_in "user_email", :with => email
      fill_in "user_password", :with => password
      fill_in "user_password_confirmation", :with => password_confirmation
      click_button "Sign up"
    end
  else
    within('.dropdown-menu') do
      fill_in "signup_email", :with => email
      fill_in "signup_password", :with => password
      fill_in "signup_password_confirmation", :with => password_confirmation
      click_button "Sign up"
    end
  end
end

Given /^that I am logged in as any user$/ do
  steps %Q{
     Given the following users are registered:
   | email             | password | confirmed_at         |
   | <EMAIL> | pppppppp | 2007-01-01  10:00:00 |
  }
  steps %Q{
    Given I visit the home page
    And I sign in as "<EMAIL>" with password "<PASSWORD>"
  }
end<commit_msg>addresses javascript timing issue [Fixes #112902301]<commit_after>Given /^I am signed in as a charity worker (un)?related to "(.*?)"$/ do |negate, organisation_name|
  organisation = Organisation.find_by_name(organisation_name)
  if negate
    users = User.where(superadmin: false)
    user = users.find { |user| user.organisation != organisation }
  else
    user = organisation.users.find { |user| !user.superadmin? }
  end
  page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user).unshift("User"))
end

Given /^I am signed in as a (non-)?siteadmin$/ do |negate|
  user = User.find_by(siteadmin: negate ? false : true)
  page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user).unshift("User"))
end
Given /^I am signed in as an? (non-)?superadmin$/ do |negate|
  user = User.find_by_superadmin(negate ? false : true)
  page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user).unshift("User"))
end

Given /^I am signed in as a pending admin of "(.*?)"$/ do |org_name|
  org = Organisation.find_by_name org_name
  user = User.find_by_pending_organisation_id(org.id)
  page.set_rack_session("warden.user.user.key" => User.serialize_into_session(user).unshift("User"))
end

Given /^I sign up as "(.*?)" with password "(.*?)" and password confirmation "(.*?)"( on the legacy sign up page)?$/ do |email, password, password_confirmation, legacy|
  if legacy
    within('#new_user') do
      fill_in "user_email", :with => email
      fill_in "user_password", :with => password
      fill_in "user_password_confirmation", :with => password_confirmation
      click_button "Sign up"
    end
  else
    within('.dropdown-menu') do
      fill_in "signup_email", :with => email
      fill_in "signup_password", :with => password
      fill_in "signup_password_confirmation", :with => password_confirmation
      click_button "Sign up"
    end
    expect(page).not_to have_css('#signup')
  end
end

Given /^that I am logged in as any user$/ do
  steps %Q{
     Given the following users are registered:
   | email             | password | confirmed_at         |
   | <EMAIL> | pppppppp | 2007-01-01  10:00:00 |
  }
  steps %Q{
    Given I visit the home page
    And I sign in as "<EMAIL>" with password "<PASSWORD>"
  }
end
{'fraction_non_alphanumeric': 0.13192436040044495, 'fraction_numerical': 0.008231368186874305, 'mean_word_length': 3.8922742110990205, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '7384820', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1470, 'n_words': 396}