2017 refactor

This commit is contained in:
Godwin
2017-04-09 11:37:16 -07:00
parent 3d00b70087
commit d1db46ffaf
302 changed files with 7100 additions and 8082 deletions
+102
View File
@@ -0,0 +1,102 @@
Given /^(?:(?:that )?there is )?an? (upcoming|past)( regional)? conference(?: in '(.+)')?$/i do |when_, is_regional, location|
location ||= 'Brooklyn NY' # this will set up our mocks to use a valid poster
TestState.last_conference = FactoryGirl.build("#{when_}#{is_regional ? '_regional' : ''}_conference".to_sym)
TestState.last_conference.city = City.search(location)
TestState.last_organization = create_org(nil, location)
TestState.last_conference.organizations << TestState.last_organization
# generate the slug
TestState.last_conference.save!
# set the poster
poster = File.join(Rails.root, 'features', 'support', 'assets', 'images',
'posters', "#{TestState.last_conference.slug}.png")
TestState.last_conference.poster = Rack::Test::UploadedFile.new(poster) if File.exist?(poster)
TestState.last_conference.save!
end
Given /^(?:the conference |it )has no (poster|date)$/i do |field|
if field == 'date'
TestState.last_conference.start_date = nil
TestState.last_conference.end_date = nil
else
TestState.last_conference.send("#{field}=".to_sym, nil)
end
TestState.last_conference.save!
end
Given /^(?:the conference |it )?is not (featured|public)$/i do |field|
TestState.last_conference.send("is_#{field}=".to_sym, false)
TestState.last_conference.save!
end
Given /^the conference accepts housing providers that live within (\d+)(mi|km)$/i do |number, unit|
TestState.last_conference.provider_conditions = {'distance' => {'number' => number.to_i, 'unit' => unit}}
TestState.last_conference.save!
end
Given /^the conference accepts paypal$/i do
TestState.last_conference.paypal_email_address = Forgery(:internet).email_address
TestState.last_conference.paypal_username = Forgery(:internet).user_name
TestState.last_conference.paypal_password = Forgery(:basic).password
TestState.last_conference.paypal_signature = Forgery(:basic).password
TestState.last_conference.save!
end
Then /^I am( not)? a member of (.+)$/i do |state, org_name|
user = nil
should_be = !(state =~ / not/)
org = Organization.find_by(:name => org_name)
if should_be
org.should be
org.users.should be
elsif org.nil? || org.users.nil?
return
end
org.users.each { |u|
if u.email == TestState.last_email_entered
user = u
end
}
user.send(should_be ? 'should' : 'should_not', be)
end
Given /^the event locations are:$/i do |table|
table.hashes.each do |location|
create_location(headers_to_attributes(location))
end
end
Given /^the workshop times are:$/i do |table|
blocks = []
wday = TestState.last_conference.start_date.wday
table.hashes.each do |block|
blocks << {
'time' => string_to_time(block['Time']).to_s,
'length' => string_to_time_length(block['Length']).to_s,
'days' => block['Days'].split(/\s*,\s*/).map { |day| str_to_wday(day).to_s }
}
end
TestState.last_conference.workshop_blocks = blocks
TestState.last_conference.save!
end
Given /^the schedule on (.+) is:$/i do |day, table|
@locations = {}
conference_day = str_to_wday(day)
table.hashes.each_with_index do |slot, block|
slot.each do |location_title, workshop_title|
location = (@locations[location_title] ||= EventLocation.find_by_title(location_title))
workshop = create_workshop(workshop_title)
workshop.block = {
'day' => conference_day,
'block' => block
}
workshop.event_location_id = location.id
workshop.save!
end
end
end
+54
View File
@@ -0,0 +1,54 @@
Then /^(?:I )?should not get an email$/i do
ActionMailer::Base.deliveries.size.should eq 0
end
Then /^(.*) should get (.+) '(.+)' emails?$/i do |to, amount, subject|
address = email_address(to)
emails = emails_to(address, subject)
unless emails.length == (str_to_num(amount))
email_log = []
ActionMailer::Base.deliveries.each do |mail|
email_log << "\t#{mail.to.join(', ')}: #{mail.subject}"
end
total_emails = ActionMailer::Base.deliveries.length
fail "Failed to find #{amount} email#{amount == 1 ? '' : 's'} to #{address} with #{subject} in the subject amoung #{total_emails} total email#{total_emails == 1 ? '' : 's'}:\n#{email_log.join("\n")}"
end
TestState.last_email = emails.first
end
Then /^th(?:e|at) email should contain (.+)$/i do |value|
TestState.last_email = ActionMailer::Base.deliveries.last
if TestState.last_email.parts && TestState.last_email.parts.first
TestState.last_email.parts.first.body.raw_source.should include(value)
TestState.last_email.parts.last.body.raw_source.should include(value)
else
TestState.last_email.body.raw_source.should include(value)
end
end
Then /^in th(?:e|at) email I should see (.+)$/i do |value|
TestState.last_email = ActionMailer::Base.deliveries.last
if /(an?|the|my) (.+) link/ =~ value
value = path_to Regexp.last_match(2)
end
if TestState.last_email.parts
TestState.last_email.parts.first.body.raw_source.should include(value)
TestState.last_email.parts.last.body.raw_source.should include(value)
else
TestState.last_email.body.raw_source.should include(value)
end
end
Then /^(?:I )?click (?:on )?(?:the )?(?:a )?'(.+?)' link in the email?$/i do | text |
href = find_in_last_email("a:contains('#{text}')", :href)
attempt_to do
visit href
sleep 1
end
end
+124 -454
View File
@@ -1,487 +1,157 @@
require 'forgery'
Given(/^(I )?(am on |visit )the (.+) page$/) do |a, b, page_name|
visit path_to(page_name)
Then /^(?:I )?(?:should )?(not )?see (?:the |an? )?'(.+)' link$/i do |no, item|
attempt_to do
if no.present?
expect(page).not_to have_link item
else
TestState.it = first('a', text: item)
end
end
end
Given(/^I am on a (.+) error page$/) do |page_name|
case page_name
when '404'
path = '/error_404'
else
raise "Unknown error page #{page_name}"
end
visit path
Then /^(?:I )?(?:should )?(?:still )?(not )?see '(.+)'$/i do |negate, item|
attempt_to do
expect(page).send(negate ? :not_to : :to, have_text(item))
end
end
Given(/^I am on the (.+) site$/) do |language|
ApplicationController::set_host (get_language_code(language) + '.bikebike.org')
Then /^(?:I )?(?:should )?(?:still )?(not )?see (?:my |the )([^']+)$/i do |negate, item|
attempt_to do
expect(page).send(negate ? :not_to : :to, have_text(TestState::Values[get_field(item)]))
end
end
Given(/^I am in (.+)$/) do |location|
ApplicationController::set_location (location)
Then /^(?:I )?click (?:on )?(?:the )?(a )?'(.+?)'( button| link)?(?: beside '(.+?)')?(?: again)?$/i do |first, item, type, beside|
attempt_to do
begin
root_item = beside.present? ? element_with_text(/#{Regexp.escape(beside)}.*?#{Regexp.escape(item)}/) : page
if type.present?
type.strip!
selector = {button: 'button, a.button', link: 'a'}[type.to_sym]
element = root_item.first(selector, text: item) || root_item.first(selector, text: item, visible: false) || root_item.find(selector, text: item)
else
element = element_with_text(item, root_item)
end
element.click
rescue Exception => e
puts text
raise e
end
sleep(1) # let any aimations or page loads to complete
end
end
When(/^I go to the (.+) page$/) do |page_name|
visit path_to(page_name)
Then /^(?:I )?press (.+)$/i do |item|
attempt_to do
text = (item =~ /'(.*)'/ ? $1 : page.find('button[value$="' + item.gsub(/\s+/, '_') + '"]').text)
click_link_or_button(text)
sleep(1) # let any aimations or page loads to complete
end
end
When(/^(I )?(finish|cancel) ((with )?(paypal|the payment))$/) do |a, action, b, c, d|
if action != 'cancel'
@my_registration = ConferenceRegistration.find(@my_registration.id)
@my_registration.payment_confirmation_token = 'token'
@my_registration.save!
url = register_paypal_confirm_path(@last_conference.slug, :paypal_confirm, 'token')
visit url
end
Then /^(?:I )?(un)?check '(.+)'$/i do |uncheck, text|
begin
find('.check-box-field label', text: text).click
rescue Exception => e
# if we didn't find a label with the text, look for an 'Other' option
begin
find(".check-box-field input[placeholder*='#{text}']").click
rescue
# if we failed to find that too, raise the original exception
raise e
end
end
end
Then(/^(I )?pay \$?([\d\.]+)$/) do | a, amount |
button = nil
paypal_info = YAML.load(File.read(Rails.root.join("config/paypal.yml")))['test'].symbolize_keys
@last_conference.paypal_username = paypal_info[:username]
@last_conference.paypal_password = paypal_info[:password]
@last_conference.paypal_signature = paypal_info[:signature]
@last_conference.save!
@my_registration.payment_info = {:payer_id => '1234', :token => '5678', :amount => amount.to_f, :status => 'Completed'}.to_yaml
@my_registration.save!
control = page.all("[value^=\"#{amount}\"]")
@last_payment_amount = amount
if control.length > 0
control.first.trigger('click')
else
fill_in(locate('amount'), :with => amount)
click_link_or_button(locate('payment'))
end
Then /^(?:my )?'(.+)' should (not )?be checked$/i do |text, negate|
label = find('.check-box-field label', text: text)
find("##{label[:for]}", visible: false).send(negate ? :should_not : :should, be_checked)
end
Then(/^(I )?(don't )?have enough funds$/) do | a, status |
if status.blank?
info = YAML.load(@my_registration.payment_info)
info[:status] = 'Completed'
@my_registration.payment_info = info.to_yaml
@my_registration.save!
end
Then /^(?:I )?(?:select|choose|want) (?:an? |the )?'(.+?)'$/i do |value|
option = first('option', text: value)
option.first(:xpath, './/..').set(option.value)
end
Given(/^There is an upcoming conference( in .+)?$/) do |location|
@last_conference = FactoryGirl.create(:upcoming_conference)
if location
@org = create_org(nil, location)
@last_conference.organizations << @org
@last_conference.save!
end
Then /^(?:I )?fill in (.+?) with '(.+)'$/i do |field, value|
field = field.gsub(/^\s*(my|the)\s*(.+)$/, '\2')
find(selector_for(field)).set value
if /email/ =~ field && !(/organization/ =~ field)
TestState.last_email_entered = value
end
end
Given(/^an organization( named .+)? exists in (.+)?$/) do |name, location|
if location =~ /every country/i
Carmen::World.instance.subregions.each { |country|
if country.subregions?
country.subregions.each { |region|
org = Organization.new(name: rand(36**16).to_s(36), slug: rand(36**16).to_s(36))
org.locations << Location.new(city: 'City', country: country.code, territory: region.code)
org.save!
}
else
org = Organization.new(name: rand(36**16).to_s(36), slug: rand(36**16).to_s(36))
org.locations << Location.new(city: 'City', country: country.code)
org.save!
end
}
else
create_org(name ? name.gsub(/^\s*named\s+(.*?)\s*$/, '\1') : nil, location)
end
Then /^(?:I )?enter (?:my |an? |some |the )?(.+?)(?: as '(.+)')?$/i do |field, value|
field = get_field(field)
sel = selector_for(field)
element = first(sel, visible: true) || first(sel, visible: false)
unless value.present?
value = case field
when /email(_address)?/
TestState.last_email_entered || Forgery(:internet).email_address
when 'name'
Forgery(field).full_name
when 'city', 'country', 'phone', 'province', 'state' ,'street_address', 'address'
aliases = {'address' => 'street_address'}
Forgery('address').send((aliases[field] || field).to_sym)
when 'subject', 'title'
Forgery::LoremIpsum.sentence(random: true).gsub(/\.$/, '').titlecase
when /(comment|reply)/
Forgery::LoremIpsum.paragraphs(2, sentences: 6, random: true)
when 'message'
Forgery::LoremIpsum.paragraphs(2, sentences: 6, random: true)
when 'info'
Forgery::LoremIpsum.paragraphs(rand(1..4), sentences: rand(3..8), random: true)
else
fail "Unknown selector '#{field}'"
end
end
(TestState::Values[field] = value)
element.set value
if /email/ =~ field && !(/organization/ =~ field)
TestState.last_email_entered = value
end
end
Given(/^a workshop( titled .+)? exists?$/) do |title|
workshop = Workshop.new
workshop.conference_id = @last_conference.id
workshop.title = title ? title.gsub(/^\s*titled\s*(.*?)\s*$/, '\1') : Forgery::LoremIpsum.sentence({:random => true}).gsub(/\.$/, '').titlecase
workshop.info = Forgery::LoremIpsum.paragraph({:random => true})
workshop.locale = :en
workshop.save
username = Forgery::LoremIpsum.word({:random => true})
user = User.create(firstname: username, email: "#{username}@bikebike.org")
WorkshopFacilitator.create(user_id: user.id, workshop_id: workshop.id, role: :creator)
@last_workshop = workshop
Then /^(?:my )?(.+)? should (not )?be set to (.+)$/i do |field, should, value|
field = field.gsub(/^\s*(my|the)\s*(.+)$/, '\2')
page.find('[id$="' + field.gsub(/\s+/, '_') + '"]').value.send(should.nil? ? 'should' : 'should_not', eq(value))
end
Given(/^the workshop is scheduled for day (\d+) at (\d\d?):(\d\d) at (.+)$/) do |day, hour, minute, location|
@last_workshop.start_time = @last_conference.start_date.change({hour: hour.to_i, min: minute.to_i}) + (day.to_i - 1).days
@last_workshop.end_time = @last_workshop.start_time + 1.5.hours
#puts " === [#{location}] === "
#puts EventLocation.all.to_yaml.to_s
@last_workshop.event_location_id = EventLocation.find_by_title(location).id
@last_workshop.save
Then /^(?:I )?set (.+?) to (.+)$/i do |field, value|
field = field.gsub(/^\s*(my|the)\s*(.+)$/, '\2')
page.find('[id$="' + field.gsub(/\s+/, '_') + '"]', :visible => false).set value
end
Given(/^the workshop schedule is (not )?published$/) do |is_not_published|
@last_conference.workshop_schedule_published = is_not_published ? false : true
@last_conference.save
Then /^(?:I )?wait for (.+?) to appear$/i do |field|
count = 0
element = nil
while element.nil? && count < 120
begin element = page.find('[id$="' + field.gsub(/\s+/, '_') + '"]'); rescue; end
begin element ||= page.find('[id$="' + field.gsub(/\s+/, '_') + '"]', :visible => false); rescue; end
sleep(1)
count += 1
end
end
Given(/^a location( named .+)? exists?$/) do |title|
location = EventLocation.new
location.conference_id = @last_conference.id
location.title = title ? title.gsub(/^\s*named\s*(.*?)\s*$/, '\1') : Forgery::LoremIpsum.sentence({:random => true}).gsub(/\.$/, '').titlecase
#location.info = Forgery::LoremIpsum.paragraph({:random => true})
#location.locale = :en
location.save
@last_location = location
Then /^(?:I )?select (.+?) from (.+)$/i do |value, field|
select(value, :from => locate(field))
end
Given(/^(I )have created a workshop titled (.+)( with (\d+) facilitators)?$/) do |a, title, b, facilitator_count|
workshop = Workshop.new
workshop.conference_id = @last_conference.id
workshop.title = title ? title.gsub(/^\s*titled\s*(.*?)\s*$/, '\1') : Forgery::LoremIpsum.sentence({:random => true}).gsub(/\.$/, '').titlecase
workshop.info = Forgery::LoremIpsum.paragraph({:random => true})
workshop.locale = :en
workshop.save
WorkshopFacilitator.create(user_id: @my_account.id, workshop_id: workshop.id, role: :creator)
(1..(facilitator_count || '0').to_i).each do |i|
username = Forgery::LoremIpsum.word({:random => true})
user = User.create(firstname: username, email: "#{username}@bikebike.org")
WorkshopFacilitator.create(user_id: user.id, workshop_id: workshop.id, role: :collaborator)
end
@last_workshop = workshop
Then /^in a new session$/i do
Capybara.reset_sessions!
end
Given(/^(\d+) (person is|people are) interested in the workshop$/) do |interested,a|
(1..interested.to_i).each do |i|
WorkshopInterest.create(workshop_id: @last_workshop.id, user_id: (@my_account ? @my_account.id + 1 : 1000) + i)
end
end
Given(/^(\d+) (person has|people have) requested to facilitate (the|my) workshop$/) do |request_count,a,b|
(1..(request_count || '0').to_i).each do |i|
username = Forgery::LoremIpsum.word({:random => true})
user = User.create(firstname: username, email: "#{username}@bikebike.org")
WorkshopFacilitator.create(user_id: user.id, workshop_id: @last_workshop.id, role: :requested)
end
end
Given(/^(.+) has requested to facilitate (the|my) workshop$/) do |username,a|
user = User.create(firstname: username, email: "#{username.gsub(/\s+/, '.').downcase}@bikebike.org")
WorkshopFacilitator.create(user_id: user.id, workshop_id: @last_workshop.id, role: :requested)
end
Given(/^(.+) is (also )?facilitating (the|my) workshop$/) do |username,a,b|
user = User.create(firstname: username, email: "#{username.gsub(/\s+/, '.')}@bikebike.org")
WorkshopFacilitator.create(user_id: user.id, workshop_id: @last_workshop.id, role: :collaborator)
end
Given(/^a user named (.+?)( with the email (.+))? exists$/) do |username,a,email|
user = User.create(firstname: username, email: email || "#{username.gsub(/\s+/, '.')}@bikebike.org")
end
Then(/^(I )(approve|deny) the facilitator request from (.+)$/) do |a,action,username|
user = User.find_by_firstname(username)
visit "/conferences/#{@last_conference.slug}/workshops/#{@last_workshop.id}/facilitate_request/#{user.id}/#{action}/"
end
Given(/^Registration is (open|closed)$/) do |status|
@last_conference.registration_open = (status == 'open')
@last_conference.save!
end
Then(/^(I )?(should )?(not )?see (.+)$/) do | a, b, no, item |
if /(the )?Bike!Bike! logo$/ =~ item
if no.present?
expect(page).not_to have_selector('.bb-icon-logo')
else
expect(page).to have_selector('.bb-icon-logo')
end
elsif /(the|a)?\s?(.+) menu item$/ =~ item
within('#main-nav') {
if no.present?
expect(page).not_to have_link Regexp.last_match(2)
else
expect(page).to have_link Regexp.last_match(2)
end
}
elsif /(the|a)?\s?(.+) image$/ =~ item
if no.present?
expect(page).not_to have_selector("##{Regexp.last_match(2)} img")
else
expect(page).to have_selector("##{Regexp.last_match(2)} img")
end
elsif /(the|a)?\s?(.+) link$/ =~ item
if no.present?
expect(page).not_to have_link Regexp.last_match(2)
else
expect(page).to have_link Regexp.last_match(2)
end
else
if no.present?
expect(page).not_to have_text item
else
expect(page).to have_text item
end
end
end
Then(/^(I )?wait (\d+) seconds$/) do | a, time |
sleep time.to_i
end
## ======= Forms ======= ##
Then(/^(I )?click (on )?(the first )?(.+?)( button| link| which is hidden)?$/) do | a, b, first, item, type |
item = item.gsub(/^\s*(a|the)\s*(.*)$/, '\2')
if type && type.strip == 'button'
click_button(item)
elsif type && type.strip == 'link'
#print page.html
click_link(item)
elsif type && type =~ /hidden/
find('[id$="' + item.gsub(/\s+/, '_') + '"]', :visible => false).click
else
if first.present?
page.first(:link, item).trigger('click')
else
page.find_link(item).trigger('click')
end
end
end
Then(/^(I )?press (.+)$/) do | a, item |
click_link_or_button(page.find('button[value$="' + item.gsub(/\s+/, '_') + '"]').text)#locate(item))
end
Then(/^(I )?(un)?check (.+)$/) do | a, state, item |
if state == 'un'
uncheck(locate(item))
else
check(locate(item))
end
end
# (a ) (b ) (c ) (value)(d ) (group)
Then(/^(I )?(select|choose|want) (an? |the )?(.+?)( as my| as the| as an?| for)? (.+)$/) do | a, b, c, value, d, group |
if (control = page.all("[name=\"#{group.pluralize}[#{value}]\"]".gsub(/^\s+$/, '_'))).length > 0
method = check(control.first[:id])
elsif (control = page.all("[name=\"#{group}\"][value=\"#{value}\"]".gsub(/^\s+$/, '_'))).length > 0
control.first.trigger('click')
else
raise "Could not find control to select"
end
end
Then(/^(I )?fill in (.+?) with (.+)$/) do | a, field, value |
field = field.gsub(/^\s*(my|the)\s*(.+)$/, '\2')
fill_in(locate(field), :with => value)
if /email/ =~ field && !(/organization/ =~ field)
@last_email_entered = value
end
end
Then(/^(I )?enter (.+?) (as my |as the |in the|as an? )(.+)$/) do | a, value, b, field |
field = field.gsub(/^\s*(my|the)\s*(.+)$/, '\2')
fill_in(locate(field), :with => value)
if /email/ =~ field && !(/organization/ =~ field)
@last_email_entered = value
end
end
Then(/^(my )?(.+)? should (not )?be set to (.+)$/) do | a, field, should, value |
field = field.gsub(/^\s*(my|the)\s*(.+)$/, '\2')
page.find('[id$="' + field.gsub(/\s+/, '_') + '"]').value.send(should.nil? ? 'should' : 'should_not', eq(value))
end
Then(/^(my )?(.+)? should (not )?be checked$/) do | a, field, should |
field = field.gsub(/^\s*(my|the)\s*(.+)$/, '\2')
page.find('[id$="' + field.gsub(/\s+/, '_') + '"]').send(should.nil? ? 'should' : 'should_not', be_checked)
end
Then(/^I set (.+?) to (.+)$/) do | field, value |
field = field.gsub(/^\s*(my|the)\s*(.+)$/, '\2')
page.find('[id$="' + field.gsub(/\s+/, '_') + '"]', :visible => false).set value
end
Then(/^(I )wait for (.+?) to appear$/) do | a, field |
count = 0
element = nil
while element.nil? && count < 120
begin element = page.find('[id$="' + field.gsub(/\s+/, '_') + '"]'); rescue; end
begin element ||= page.find('[id$="' + field.gsub(/\s+/, '_') + '"]', :visible => false); rescue; end
sleep(1)
count += 1
end
end
Then(/^(I )?show the (page|url)$/) do | a, item |
if item == 'url'
print current_url
else
print page.html
end
end
Then(/^I select (.+?) from (.+)$/) do | value, field |
select(value, :from => locate(field))
end
## ======= Emails ======= ##
Then(/^I should not get an email$/) do
ActionMailer::Base.deliveries.size.should eq 0
end
Then (/^I should get an? (.+) email$/) do |subject|
@last_email = ActionMailer::Base.deliveries.last
if @last_email_entered
@last_email.to.should include @last_email_entered
end
@last_email.subject.should include(subject)
end
Then (/^th(e|at) email should contain (.+)$/) do |a, value|
@last_email = ActionMailer::Base.deliveries.last
if @last_email.parts && @last_email.parts.first
@last_email.parts.first.body.raw_source.should include(value)
@last_email.parts.last.body.raw_source.should include(value)
else
@last_email.body.raw_source.should include(value)
end
end
Then (/^in th(e|at) email I should see (.+)$/) do |a, value|
@last_email = ActionMailer::Base.deliveries.last
if /(an?|the|my) (.+) link/ =~ value
value = path_to Regexp.last_match(2)
end
if @last_email.parts
@last_email.parts.first.body.raw_source.should include(value)
@last_email.parts.last.body.raw_source.should include(value)
else
@last_email.body.raw_source.should include(value)
end
end
Then(/^(I )?confirm my account$/) do | a |
@my_account = User.find_user(@last_email_entered)
@confirmation = EmailConfirmation.where(["user_id = ?", @my_account.id]).order("created_at DESC").first
visit "/confirm/#{@confirmation.token}"
end
Then (/^I should (not )?be registered for the conference$/) do |state|
@my_account = User.find_by(:email => @last_email_entered)
@my_registration = ConferenceRegistration.find_by(:user_id => @my_account.id, :conference_id => @last_conference.id)
if state && state.strip == 'not'
@my_registration.should be_nil
else
@my_registration.should_not be_nil
end
end
Given (/^(I )?am logged in as (.+)$/) do |a, email|
#include Sorcery::TestHelpers::Rails
@my_account = User.create(:email => email)
EmailConfirmation.create(:token => 'test', :user_id => @my_account.id)
visit "/confirm/test"
fill_in(locate('email'), :with => email)
click_link_or_button(page.find('button[type="submit"]').text)
end
Given (/^My name is (.+)$/) do |name|
@my_account.firstname = name
@my_account.save
end
Given (/^(I am|(.+) is) registered for the conference$/) do |a,username|
user = username ? User.find_by_firstname(username) : @my_account
registration = ConferenceRegistration.create(
:user_id => user.id,
:conference_id => @last_conference.id,
:is_attending => true,
:is_confirmed => true,
:is_participant => true,
:city => 'Somewhere',
:arrival => '2015-09-28 00:00:00',
:departure => '2015-09-28 00:00:00',
:housing => 'house',
:bike => 'medium',
:other => '',
:allergies => '',
:languages => '["en"]',
:food => 'meat'
)
if username
@last_registration = registration
else
@my_registration = registration
end
end
Given (/^(I )?have paid( \$?\d+|\$?\d+\.\d+)? for registration$/) do |a, amount|
@my_registration.registration_fees_paid = amount ? amount.to_f : 50.0
@my_registration.save
end
Given (/^(I )?am a conference host$/) do |a|
org = @last_conference.organizations.first
org.users ||= Array.new
org.users << @my_account
org.save
end
Then (/^(I )?save (my |the )workshop$/) do |a, b|
title = find('[name=title]').value
click_button('save')
@last_workshop = Workshop.order('created_at DESC').first
end
Then (/^(I )?(view|edit|delete) (my |the )workshop$/) do |a, action, b|
if action == 'view'
visit "/conferences/#{@last_conference.slug}/workshops/#{@last_workshop.id}"
else
visit "/conferences/#{@last_conference.slug}/workshops/#{@last_workshop.id}/#{action}"
end
end
Then (/^my registration (should( not)? be|is( not)?) (confirmed|completed?|paid)$/) do |state, x, y, field|
ConferenceRegistration.find_by!(:email => @last_email_entered).
send(field == 'confirmed' ? 'is_confirmed' : (field == 'paid' ? 'registration_fees_paid' : field)).
send(state =~ / not/ ? 'should_not' : 'should', be)
end
Then (/^I am( not)? a user$/) do |state|
User.find_user(@last_email_entered).
send(state =~ / not/ ? 'should_not' : 'should', be)
end
Then (/^I am( not)? a member of (.+)$/) do |state, org_name|
user = nil
should_be = !(state =~ / not/)
org = Organization.find_by(:name => org_name)
if should_be
org.should be
org.users.should be
elsif org.nil? || org.users.nil?
return
end
org.users.each { |u|
if u.email == @last_email_entered
user = u
end
}
user.send(should_be ? 'should' : 'should_not', be)
end
Then (/^My (.+) should(not )? be (.+)$/) do |field, state, value|
User.find_user(@last_email_entered).
send(field.gsub(/\s/, '_')).
send(state =~ / not/ ? 'should_not' : 'should', eq(value))
Then /^(?:my |the )([A-Z][a-z]+ )?(workshop|conference|user|conference registration) (.+?) should (not )?be '(.+?)'$/i do |locale, type, attribute, negate, value|
object = TestState.send("last_#{type.gsub(' ', '_')}")
attribute = attribute.gsub(' ', '_').downcase
actual_value = locale ? object.get_column_for_locale!(attribute, get_locale(locale)) : object.send(attribute)
expect(actual_value).send(negate ? :not_to : :to, (be == value))
end
+70
View File
@@ -0,0 +1,70 @@
Given /^(?:I )?(?:(?:am )?on |visit )(?:the |my )(.+) page$/i do |page_name|
attempt_to do
visit path_to(page_name)
end
end
Given /^(?:(?:I )?am )?on an? (.+) error page$/i do |page_name|
case page_name
when '404', '500'
path = "/error_#{page_name}"
when 'locale not available'
path = 'locale_not_available_error/tlh'
else
raise "Unknown error page #{page_name}"
end
attempt_to do
visit path
end
end
Then /^(?:I )?should be on (?:the |an? | my)?(.+) page$/i do |page_name|
attempt_to do
path = path_to(page_name)
path = /(https?\/\/:[^\/]+)?#{Regexp.escape(path)}\/?(\?|#|$)/ unless path.is_a?(Regexp)
current_url.should match path
end
end
Given /^I am on the (.+) site$/i do |language|
ApplicationController::set_host (get_language_code(language) + '.bikebike.org')
end
Given /^I am in (.+)$/i do |location|
ApplicationController::set_location (location)
end
When /^I go to the (.+) page$/i do |page_name|
visit path_to(page_name)
end
Given /^a location( named .+)? exists?$/i do |title|
location = EventLocation.new
location.conference_id = TestState.last_conference.id
location.title = title ? title.gsub(/^\s*named\s*(.*?)\s*$/, '\1') : Forgery::LoremIpsum.sentence({:random => true}).gsub(/\.$/, '').titlecase
location.save
TestState.last_location = location
end
Then /^(?:I )?show the (page|url)$/i do |item|
if item == 'url'
print current_url
else
print page.html
end
end
Then /^(?:I )?wait (\d+(?:\.\d+)?) seconds?$/i do |time|
sleep time.to_i
end
Then /^take a screenshot?$/i do
page.save_screenshot(File.expand_path('./test.png'), full: true)
end
When /^(?:I )?re(?:fresh|load) the page$/i do
attempt_to do
visit current_url
end
end
+93
View File
@@ -0,0 +1,93 @@
Given /^(?:I am |'(.+)' is )?registered(?: for the conference)?$/i do |username|
create_registration(username.present? ? get_user(username) : TestState.my_account)
end
Given /^(?:I )?have paid( \$?\d+|\$?\d+\.\d+)? for registration$/i do |amount|
TestState.my_registration.registration_fees_paid = amount ? amount.to_f : 50.0
TestState.my_registration.save!
end
Then /^my registration should( not)? be (confirmed|completed?|paid)$/i do |state, field|
ConferenceRegistration.find_by!(email: TestState.last_email_entered).
send(field == 'confirmed' ? 'is_confirmed' : (field == 'paid' ? 'registration_fees_paid' : field)).
send(state =~ / not/ ? 'should_not' : 'should', be)
end
Then /^I should (not )?be registered for the conference$/i do |state|
if state && state.strip == 'not'
TestState.my_registration.should be_nil
else
TestState.my_registration.should_not be_nil
end
end
When /^(?:I )?(finish|cancel|don't finish) (?:(?:with )?(?:paypal|the payment))$/i do |action|
TestState.my_registration ||= ConferenceRegistration.find_by(user_id: TestState.my_account.id, conference_id: TestState.last_conference.id)
TestState.my_registration.payment_confirmation_token = 'token'
unless action == 'cancel'
info = YAML.load(TestState.my_registration.payment_info) || {}
info[:status] = action == 'finish' ? 'Completed' : 'Incomplete'
TestState.my_registration.payment_info = info.to_yaml
end
TestState.my_registration.save!
visit send("register_paypal_#{action == 'cancel' ? 'cancel' : 'confirm'}_path".to_sym, TestState.last_conference.slug, :paypal_confirm, 'token', amount: YAML.load(TestState.my_registration.payment_info)[:amount])
end
# Then /^(?:I )?pay \$?([\d\.]+)$/i do |amount|
# button = nil
# paypal_info = YAML.load(File.read(Rails.root.join("config/paypal.yml")))['test'].symbolize_keys
# TestState.last_conference.paypal_username = paypal_info[:username]
# TestState.last_conference.paypal_password = paypal_info[:password]
# TestState.last_conference.paypal_signature = paypal_info[:signature]
# TestState.last_conference.save!
# TestState.my_registration.payment_info = {
# payer_id: '1234',
# token: '5678',
# amount: amount.to_f,
# status: 'Completed'
# }.to_yaml
# TestState.my_registration.save!
# control = page.all("[value^=\"#{amount}\"]")
# TestState.last_payment_amount = amount
# if control.length > 0
# control.first.trigger('click')
# else
# fill_in(locate('amount'), with: amount)
# click_link_or_button(locate('payment'))
# end
# end
# Then /^(?:I )?(don't )?have enough funds$/i do |status|
# if status.blank?
# info = YAML.load(TestState.my_registration.payment_info)
# info[:status] = 'Completed'
# TestState.my_registration.payment_info = info.to_yaml
# TestState.my_registration.save!
# end
# end
Given /^a workshop( titled .+)? exists?$/i do |title|
workshop = Workshop.new
workshop.conference_id = TestState.last_conference.id
workshop.title = title ? title.gsub(/^\s*titled\s*(.*?)\s*$/, '\1') : Forgery::LoremIpsum.sentence({random: true}).gsub(/\.$/, '').titlecase
workshop.info = Forgery::LoremIpsum.paragraph({random: true})
workshop.locale = :en
workshop.save
username = Forgery::LoremIpsum.word({random: true})
user = User.create(firstname: username, email: "#{username}@bikebike.org")
WorkshopFacilitator.create(user_id: user.id, workshop_id: workshop.id, role: :creator)
TestState.last_workshop = workshop
end
Given /^registration is (open|closed)$/i do |status|
TestState.last_conference.registration_status = status
TestState.last_conference.save!
end
+93
View File
@@ -0,0 +1,93 @@
Then /^I am( not)? a user$/i do |state|
TestState.my_account.send(state =~ / not/ ? 'should_not' : 'should', be)
end
Given /^(?:I )?(?:am logged|log) in(?: as '(.+)')?$/i do |email|
TestState.my_account = get_user(email)
TestState.last_token = 'test'
attempt_to do
EmailConfirmation.create(token: TestState.last_token, user_id: TestState.my_account.id)
attempt_to do
visit path_to(:confirm)
end
first(selector_for('email')).set(TestState.my_account.email)
first('#token + button[type="submit"]').click
begin
expect(page).to have_link TestState.my_account.name
rescue
fail "Error logging in"
end
end
end
Given /^(?:I )?(only )?speak (.+)$/i do |only, language|
TestState.my_account.languages ||= ['en']
if only
TestState.my_account.languages = [get_locale(language)]
else
TestState.my_account.languages << get_locale(language)
end
TestState.my_account.save!
end
Then /^(?:I )?should (not )?be (?:logged|signed) in$/ do |negate|
expect(logged_in?).to_be !negate
end
When /^(?:I )?log in with facebook$/i do
visit oauth_callback_path(
name: TestState.my_account.firstname,
email: TestState.my_account.email,
fb_id: TestState.my_account.fb_id
)
end
Given /^(?:I )?have a facebook account$/i do
TestState.my_account = FactoryGirl.build(:user)
TestState.my_account.fb_id = '1'
end
Given /^(?:I )?am a registered user$/i do
TestState.my_account.save!
end
Given /^my facebook account has no email address$/ do
TestState.my_account.email = nil
TestState.my_account.save! if TestState.my_account.id.present?
end
Given /^my name is '(.+)'$/i do |name|
TestState.my_account.firstname = name
TestState.my_account.save! if TestState.my_account.id.present?
end
Given /^(?:I )?am an? (.+)$/i do |role|
if role == 'conference host'
org = TestState.last_conference.organizations.first
org.users ||= Array.new
org.users << TestState.my_account
org.save
else
case role
when /(site )?admin(istrator)?/
role = 'administrator'
end
TestState.my_account.role = role
TestState.my_account.save!
end
end
Then /^(?:I )?confirm my account$/i do
TestState.my_account = User.find_user(TestState.last_email_entered)
TestState.confirmation = EmailConfirmation.where(["user_id = ?", TestState.my_account.id]).order("created_at DESC").first
visit "/confirm/#{TestState.confirmation.token}"
end
Given /^a user named (.+?)(?: with the email (.+))? exists$/i do |username, email|
user = User.create(firstname: username, email: email || "#{username.gsub(/\s+/, '.')}@bikebike.org")
end
+107
View File
@@ -0,0 +1,107 @@
Given /^((?:I )?have|there is) a workshop (?:named |titled )?'(.+?)'$/ do |owner, title|
if owner =~ /^there is$/
owner = create_user
create_registration(owner)
else
owner = TestState.my_account
end
create_workshop(title, owner)
end
Given /^(?:it|'(.+?)') is (not )?looking for facilitators$/ do |title, negate|
workshop = title.present? ? TestState.last_workshop : Workshop.find_by_title(title)
workshop.needs_facilitators = !negate
workshop.save!
end
Given /^(?:I am|'(.+)' is) facilitating(?: '(.+?)')?$/ do |user, title|
workshop = title.present? ? Workshop.find_by_title(title) : TestState.last_workshop
user = user.present? ? get_user(user) : TestState.my_account
WorkshopFacilitator.create(
user_id: user.id,
workshop_id: workshop.id,
role: :collaborator
)
end
Then /^(?:I )?save (?:my |the )workshop$/i do
title = find('[name=title]').value
click_button('save')
TestState.last_workshop = Workshop.order('created_at DESC').first
end
Then /^(?:I )?(view|edit|delete) (?:my |the )workshop$/i do |action|
if action == 'view'
visit "/conferences/#{TestState.last_conference.slug}/workshops/#{TestState.last_workshop.id}"
else
visit "/conferences/#{TestState.last_conference.slug}/workshops/#{TestState.last_workshop.id}/#{action}"
end
end
Given /^the workshop is scheduled for day (\d+) at (\d\d?):(\d\d) at (.+)$/i do |day, hour, minute, location|
TestState.last_workshop.start_time = TestState.last_conference.start_date.change({hour: hour.to_i, min: minute.to_i}) + (day.to_i - 1).days
TestState.last_workshop.end_time = TestState.last_workshop.start_time + 1.5.hours
TestState.last_workshop.event_location_id = EventLocation.find_by_title(location).id
TestState.last_workshop.save!
end
Given /^the workshop schedule is (not )?published$/i do |is_not_published|
TestState.last_conference.workshop_schedule_published = is_not_published ? false : true
TestState.last_conference.save!
end
Given /^(?:I )?have created a workshop titled (.+)(?: with (\d+) facilitators)?$/i do |title, facilitator_count|
workshop = Workshop.new
workshop.conference_id = TestState.last_conference.id
workshop.title = title ? title.gsub(/^\s*titled\s*(.*?)\s*$/, '\1') : Forgery::LoremIpsum.sentence({:random => true}).gsub(/\.$/, '').titlecase
workshop.info = Forgery::LoremIpsum.paragraph({:random => true})
workshop.locale = :en
workshop.save
WorkshopFacilitator.create(user_id: TestState.my_account.id, workshop_id: workshop.id, role: :creator)
(1..(facilitator_count || '0').to_i).each do |i|
username = Forgery::LoremIpsum.word({:random => true})
user = User.create(firstname: username, email: "#{username}@bikebike.org")
WorkshopFacilitator.create(user_id: user.id, workshop_id: workshop.id, role: :collaborator)
end
TestState.last_workshop = workshop
end
Given /^(.+?) (?:person is |people are )?interested(?: in '(.+?)')?$/i do |interested, title|
workshop = title.present? ? Workshop.find_by_title(title) : TestState.last_workshop
str_to_num(interested).times do
user = create_user
WorkshopInterest.create(workshop_id: workshop.id, user_id: user.id)
end
end
Given /^(\d+) (?:person has |people have )?requested to facilitate (?:the |my )?workshop$/i do |request_count|
(1..(request_count || '0').to_i).each do |i|
username = Forgery::LoremIpsum.word({:random => true})
user = User.create(firstname: username, email: "#{username}@bikebike.org")
WorkshopFacilitator.create(user_id: user.id, workshop_id: TestState.last_workshop.id, role: :requested)
end
end
Given /^'(.+)' has requested to facilitate (?:the |my )?workshop$/i do |username|
user = get_user(username)
WorkshopFacilitator.create(user_id: user.id, workshop_id: TestState.last_workshop.id, role: :requested)
end
Given /^(.+) is (?:also )?facilitating (?:the |my )?workshop$/i do |username|
user = User.create(firstname: username, email: "#{username.gsub(/\s+/, '.')}@bikebike.org")
WorkshopFacilitator.create(user_id: user.id, workshop_id: TestState.last_workshop.id, role: :collaborator)
end
Then /^(?:I )?(approve|deny) the facilitator request from (.+)$/i do |action, username|
user = User.find_by_firstname(username)
visit "/conferences/#{TestState.last_conference.slug}/workshops/#{TestState.last_workshop.id}/facilitate_request/#{user.id}/#{action}/"
end
Then /^(?:I )?(?:should )?(not )?see (.+) workshops?(?: in total)?(?: under '(.+?)')?$/i do |negate, number, heading|
if heading
parent = parent_element(element_with_text(heading))
else
parent = page
end
compare(number, parent.all('.workshop-list > li, #schedule-preview .workshop').size, negate)
end