Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require(File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths")))
- require((RAILS_ROOT + "/lib/test/imap_mock"))
- def edit_url_for_merchant(merchant_name)
- merchant_id = look_up_merchant(merchant_name).id
- "/admin/mass_catalog/edit?merchant_id=#{merchant_id}"
- end
- def look_up_merchant(merchant_name)
- Merchant.find_by_name(merchant_name)
- end
- def login_as(login, password = "password")
- email = login.is_a?(String) ? (login) : (login.email)
- visit("/login")
- fill_in("login_email", :with => (email))
- fill_in("login_password", :with => (password))
- click_button("btn-login-submit")
- end
- def make_account_with_role(role, password)
- role = Account.const_get("ROLE_#{role}".upcase) rescue role_mapping[role.downcase]
- login = Factory.create_login(:email => ("#{/\w+/.gen}@#{/\w+/.gen}.com"), :password => (password))
- login.account.role = role
- login.save!
- login
- end
- def role_mapping
- { "user" => (Account::ROLE_USER), "merchant" => (Account::ROLE_MERCHANT), "admin" => (Account::ROLE_ADMIN), "csr" => (Account::ROLE_SERVICE) }
- end
- def invite_merchant(merchant_name)
- merchant = Merchant.find_by_name(merchant_name)
- confirm_url = "/admin/merchants/#{merchant.id}/confirm_invite"
- visit(confirm_url)
- url = "/admin/merchants/#{merchant.id}/invite"
- visit(url, :put)
- end
- def generate_mail(choice)
- mail = TMail::Mail.new
- mail.from = /\w+@\w+\.com/.gen
- mail.to = choice.email_alias
- mail.subject = "Email to choice #{choice.id}"
- mail.body = mail.subject
- mail
- end
- def download_sent_file
- sio = StringIO.new
- response.body[response, sio]
- sio.string
- end
- Given(/A merchant has sent a email that they have confirmed my request/) do
- @login = Factory.create_login
- Factory.create_mailbox(:account_id => (@login.account_id))
- @choice = Factory.create_catalog_choice(:account_id => (@login.account_id))
- @imap = ImapMock.new
- @imap.mock_add_email("#{MerchantEmailProcessing::Category::BOX_PREFIX}Confirmation", generate_mail(@choice))
- end
- Given(/A merchant has sent an invalid email/) do
- @login = Factory.create_login
- Factory.create_mailbox(:account_id => (@login.account_id))
- @choice = Factory.create_catalog_choice(:account_id => (@login.account_id))
- @imap = ImapMock.new
- mail = stub_everything(:to_s => "INVALID")
- @imap.mock_add_email("#{MerchantEmailProcessing::Category::BOX_PREFIX}Acknowledge", mail)
- end
- Given(/I am on the new users_can_sign_up page/) do
- visit("/users_can_sign_ups/new")
- end
- Given(/I create a catalog choice for "(.*)"/) do |catalog_title|
- visit("/")
- catalog = Catalog.find_by_title(catalog_title)
- click_link("Find Catalogs")
- click_link("Browse")
- click_link(catalog_title.first.upcase)
- click_link("set_mail_preference_#{catalog.id}")
- click_button("btn-submit-mail-pref")
- response.body.should(match(/successfully recorded/))
- end
- Given(/I have not signed up/) { }
- Given(/I invite the merchant named "(.*)"/) do |merchant_name|
- Given("I log in as a CSR")
- @merchant = Merchant.find_by_name(merchant_name)
- put(invite_admin_merchant_path(@merchant.id))
- end
- Given(/^"(.*)" belongs to a (not)? participating merchant/) do |catalog_title, participation|
- merchant_relationship = if participation.nil? then
- Merchant::RELATIONSHIP_HEALTHY
- else
- Merchant::RELATIONSHIP_REFUSED
- end
- @catalog = Catalog.find_by_title(catalog_title)
- @catalog.merchant.merchant_relationship = merchant_relationship
- @catalog.save
- end
- Given(/^"(.*)" is (not )?a participating merchant/) do |merchant_name, participation_string|
- @merchant = Merchant.find_by_name(merchant_name)
- participating_flag = (not (participation_string == "not "))
- if participating_flag then
- @merchant.relationship_status = Merchant::RELATIONSHIP_HEALTHY
- else
- @merchant.relationship_status = Merchant::RELATIONSHIP_REFUSED
- end
- @merchant.save!
- end
- Given(/^"(.*)" opted out of "(.*)"$/) do |fixture_name, catalog_name|
- account = Factory.create_account
- individual = Factory.create_individual(:display_name => (fixture_name))
- catalog = (Catalog.find_by_title(catalog_name) or Factory.create_catalog(:title => (catalog_name)))
- choice = Factory.create_catalog_choice(:account_id => (account.id), :catalog_id => (catalog.id))
- (individual.catalog_choices << choice)
- end
- Given(/^A merchant has sent a email that they have received my choice/) do
- @login = Factory.create_login
- Factory.create_mailbox(:account_id => (@login.account_id))
- @choice = Factory.create_catalog_choice(:account_id => (@login.account_id))
- @imap = ImapMock.new
- @imap.mock_add_email("#{MerchantEmailProcessing::Category::BOX_PREFIX}Acknowledge", generate_mail(@choice))
- end
- Given(/^I am logged in as a (CSR|merchant|user)/) { |role| }
- Given(/^I am on the home page$/) { visit("/") }
- Given(/^I am registered as (.*) with password (.*)$/) do |email, password|
- @login = Factory.create_login(:email => (email), :password => (password))
- end
- Given(/^I follow "(.*)" with id "(.*)"/) do |link_text, id|
- click_link(link_text, :id => (id))
- end
- Given(/^I have logged in as a user$/) do
- password = "password"
- @login = make_account_with_role("user", password)
- name = @login.name
- mailbox = Factory.create_mailbox
- (@login.account.mailboxes << mailbox)
- @login.account.save!
- @login.save!
- visit("/login")
- fill_in("login_email", :with => (@login.email))
- fill_in("login_password", :with => (password))
- click_button("btn-login-submit")
- response.should(have_tag("#logged-in-status", name.to_regexp))
- end
- Given(/^I have set a mail preference for "(.*)"$/) do |catalog_title|
- catalog = (Catalog.find_by_title(catalog) or Factory.create_catalog(:title => (catalog_title)))
- visit("/catalogs/browse")
- click_link("#{catalog.title[0, 1].upcase}")
- visit("/catalogs/#{catalog.id}/set_preference")
- click_button("btn-submit-mail-pref")
- click_link("Welcome")
- end
- Given(/^I log in as a CSR$/) do
- visit("/logout")
- password = "password"
- @login = make_account_with_role("csr", password)
- name = @login.name
- mailbox = Factory.create_mailbox
- (@login.account.mailboxes << mailbox)
- @login.account.save!
- @login.save!
- visit("/login")
- fill_in("login[email]", :with => (@login.email))
- fill_in("login[password]", :with => (password))
- click_button("btn-login-submit")
- response.should(have_tag("#logged-in-status", name.to_regexp))
- end
- Given(/^I log in as a merchant/) do
- password = "password"
- @login = make_account_with_role("merchant", password)
- @merchant = Factory.create_merchant
- @merchant.account = @login.account
- @merchant.account.save!
- @merchant.save!
- @login.save!
- visit("/login")
- fill_in("login_email", :with => (@login.email))
- fill_in("login_password", :with => (password))
- click_button("btn-login-submit")
- end
- Given(/^I log in as an admin/) do
- password = "password"
- @login = make_account_with_role("admin", password)
- name = @login.name
- mailbox = Factory.create_mailbox
- (@login.account.mailboxes << mailbox)
- @login.account.save!
- @login.save!
- visit("/login")
- fill_in("login_email", :with => (@login.email))
- fill_in("login_password", :with => (password))
- click_button("btn-login-submit")
- response.should(have_tag("#logged-in-status", name.to_regexp))
- end
- Given(/^I log in as the merchant "(.*)"/) do |merchant_name|
- visit("/logout")
- merchant = Merchant.find_by_name(merchant_name)
- visit("/login")
- fill_in("login[email]", :with => (merchant.login.email))
- fill_in("login[password]", :with => "password")
- click_button("btn-login-submit")
- response.should(have_tag("title", "Merchants".to_regexp))
- end
- Given(/^I log in as the user "(.*)"/) do |email_address|
- Given("there exists a verified user with the email \"#{email_address}\"")
- visit("/logout")
- visit("/login")
- fill_in("login[email]", :with => (email_address))
- fill_in("login[password]", :with => "password")
- click_button("btn-login-submit")
- end
- Given(/^I should see an invitation to "(.*)"$/) do |merchant_name|
- response.should(have_tag("pre", "Dear #{merchant_name}".to_regexp))
- end
- Given(/^I view the invitation for "(.*)"$/) do |merchant_name|
- merchant_id = look_up_merchant(merchant_name).id
- visit("/admin/merchants/#{merchant_id}/preview_invite")
- end
- Given(/^Ultrasphinx has indexed all objects in the system$/) do
- `rake ultrasphinx:index RAILS_ENV=test`
- end
- Given(/^a CSR has created a merchant account for "(.*)"$/) do |merchant_name|
- click_link("Merchants")
- click_link("New Merchant")
- fill_in("Name", :with => (merchant_name))
- fill_in("Address", :with => (/\d{4} \w+/.gen))
- fill_in("Email address", :with => (/\w{4,7}@testmail.com/.gen))
- fill_in("City", :with => "Madison")
- select("Michigan", :from => "State")
- fill_in("Zip code", :with => "53703")
- click_button("Create Merchant")
- response.body.should =~ /Successfully created merchant/m
- end
- Given(/^change the contact email for "(.*)" to "(.*)"$/) do |merchant_name, email|
- visit(edit_url_for_merchant(merchant_name))
- fill_in("catalog[email_customer_service]", :with => (email))
- click_button("Save Changes")
- merchant = look_up_merchant(merchant_name)
- (merchant.email_customer_service.should == email)
- end
- Given(/^send an invitation to "(.*)"$/) do |merchant_name|
- merchant_id = look_up_merchant(merchant_name).id
- put("/admin/merchants/#{merchant_id}/invite")
- end
- Given(/^the CSR has logged out$/) { visit("/logout") }
- Given(/^the catalog "(.*)" has the mail preference "(.*)"$/) do |catalog, mail_preference|
- catalog = (Catalog.find_by_title(catalog) or Factory.create_catalog(:title => (catalog)))
- mail_preference = Factory.create_mail_preference(:title => (mail_preference), :catalog_id => (catalog.id))
- (catalog.mail_preferences << mail_preference)
- catalog.save
- end
- Given(/^the merchant "(.*)" is (not )?participating/) do |merchant_name, participation_string|
- @merchant = Merchant.find_by_name(merchant_name)
- participating_flag = (not (participation_string == "not "))
- if participating_flag then
- @merchant.relationship_status = Merchant::RELATIONSHIP_HEALTHY
- else
- @merchant.relationship_status = Merchant::RELATIONSHIP_REFUSED
- end
- @merchant.save!
- end
- Given(/^the merchant for "(.*)" is "(.*)"/) do |catalog_title, merchant_relationship_string|
- @catalog = Factory.create_catalog(:title => (catalog_title))
- email_notification_method = Factory.create_email_notification_method(:optout_email => "professor_gryff@seventhgeneration.com")
- @catalog.notification_method = email_notification_method
- @catalog.save
- @merchant = @catalog.merchant
- case merchant_relationship_string
- when "participating" then
- merchant_relationship = Merchant::RELATIONSHIP_HEALTHY
- when "not participationg" then
- merchant_relationship = Merchant::RELATIONSHIP_REFUSED
- else
- # do nothing
- end
- @merchant.relationship_status = merchant_relationship
- @merchant.save
- end
- Given(/^the merchant has sent an email "(.*)" my choice/) do |email_action|
- if (email_action == "confirming") then
- email_folder = "Confirmation"
- else
- email_folder = "Acceptance" if (email_action == "accepting")
- end
- @imap = ImapMock.new
- @imap.mock_add_email("Unprocessed/#{email_folder}", generate_mail(@choice))
- end
- Given(/^there exists a catalog for "(.*)" called "(.*)"/) do |merchant_name, catalog_name|
- Given("I log in as a CSR")
- Given("there exists a merchant named \"#{merchant_name}\"")
- visit("/logout")
- Given("I log in as a CSR")
- Given("I go to create a new catalog for \"#{merchant_name}\"")
- fill_in("catalog[title]", :with => (catalog_name))
- click_button("Create")
- Given("I should see \"created the catalog\"")
- Given("I follow \"Log Out\"")
- end
- Given(/^there exists a merchant named "(.*)"/) do |merchant_name|
- email = /\w{3,7}.\w{5,7}@cchoice.org/.gen
- Given("I follow \"Merchants & Catalogs\"")
- Given("I follow \"New Merchant\"")
- fill_in("Name", :with => (merchant_name))
- fill_in("Address", :with => "1234 Wealthy St.")
- fill_in("City", :with => "Grand Rapids")
- select("Michigan", :from => "State")
- fill_in("Zip code", :with => "49506")
- fill_in("merchant[email_customer_service]", :with => (email))
- fill_in("merchant[contact_name]", :with => "The Contact")
- fill_in("merchant[contact_title]", :with => "SVP")
- fill_in("merchant[contact_email]", :with => "merchantemail@cchoice.org")
- click_button("Create Merchant")
- Given("I should see \"created merchant\"")
- Given("I invite the merchant named \"#{merchant_name}\"")
- Given("the merchant named \"#{merchant_name}\" accepts the invitation")
- end
- Given(/^there exists a verified user with the email "(.*)"$/) do |email_address|
- return if Login.find_by_email(email_address)
- visit("/logout")
- click_link("Get Started!")
- fill_in("Name", :with => (/\w{5,10}/.gen))
- fill_in("Email address", :with => (email_address))
- fill_in("login[email_confirmation]", :with => (email_address))
- password = "password"
- fill_in("New password", :with => (password))
- fill_in("login[password_confirmation]", :with => (password))
- fill_in("Address", :with => (/\d{3} \w{5} St./.gen))
- fill_in("City", :with => "Grand Rapids")
- fill_in("State", :with => "MI")
- fill_in("Zip code", :with => (/\d{5}/.gen))
- check("account[tos]")
- check("account[eighteen_or_older]")
- click_button("btn-signup-submit")
- verification_mail = VerificationMail.find_by_email_sent_to(email_address)
- visit("activate/#{verification_mail.encoded_key}")
- click_link("Log Out")
- end
- Given(/^there is a catalog in the system called "(.*)"$/) do |catalog_title|
- account = Factory.create_account
- merchant = (@merchant or Factory.create_merchant(:name => ("The Catalogs Company #{/\w{2,5}/.gen}"), :account_id => (account.id)))
- catalog = Factory.create_catalog(:title => (catalog_title), :merchant_id => (merchant.id))
- catalog.save!
- @merchant.reload if @merchant
- end
- Given(/the merchant "(.*)" has elected to receive instant notifications for opt-outs/) do |merchant_name|
- Given("I log in as the merchant \"#{merchant_name}\"")
- click_link("Manage Catalogs")
- click_link("Edit catalog")
- select("Email", :from => "notification_method")
- fill_in("email_notification_method[optout_email]", :with => ("#{merchant_name.gsub(/\W+/, "_").downcase}+optout@gmail.com"))
- click_button("btn-submit-edit-catalog")
- response.should(have_tag("#flash_content", "Changes to the catalog were successfully saved".to_regexp))
- end
- Given(/the merchant "(.*)" sends the user "(.*)" a (Confirmation|Acknowledge) email/) do |merchant_name, user_email, email_type|
- @merchant = Merchant.find_by_name(merchant_name)
- @login = Login.find_by_email(user_email)
- @choice = @login.account.catalog_choices.first
- @imap = ImapMock.new
- @imap.mock_add_email("Unprocessed/#{email_type}", generate_mail(@choice))
- end
- Given(/the merchant named "(.*)" accepts the invitation/) do |merchant_name|
- @merchant = Merchant.find_by_name(merchant_name)
- @verification_mail = VerificationMail.find_by_email_sent_to(@merchant.login.email)
- visit("/logout")
- visit("/merchants/#{@verification_mail.encoded_key}")
- click_link("Confirm Your Account")
- fill_in("Password", :with => "password")
- fill_in("Type it again", :with => "password")
- check("merchant[merchant_license]")
- click_button("btn-confirmaccount")
- Given("I should see \"Welcome, #{@merchant.name}!\"")
- end
- Then(/I should be logged in/) { }
- Then(/^I debug$/) { debugger }
- Then(/^I fill in the merchant code$/) do
- visit("/merchants/account/confirm?id=#{@verification_mail.encoded_key}")
- end
- Then(/^I puts$/) { puts(response.body) }
- Then(/^I should not see "(.*)"$/) do |text|
- response.body.should_not(contain(text))
- end
- Then(/^I should not see \/(.*)\/$/) do |text|
- response.body.should_not(contain(Regexp.new(text)))
- end
- Then(/^I should see "(.*)" opted out of "(.*)" in the catalog choice updates file$/) do |user_name, catalog|
- require("fastercsv")
- header, user = FasterCSV.parse(download_sent_file)
- (header[(0..2)].should == ["Catalog", "Customer Number", "Name"])
- (user[0].should == catalog)
- (user[2].should == user_name)
- end
- Then(/^I should see "(.*)"$/) { |text| response.body.should(contain(text)) }
- Then(/^I should see \/(.*)\/$/) do |text|
- response.body.should(contain(Regexp.new(text)))
- end
- Then(/^I should see a search result for "(.*)"$/) do |catalog_title|
- response.should(have_tag("li.catalog h3", catalog_title.to_regexp))
- end
- Then(/^I should see that the status of my choice is "(.*)"$/) do |status_string|
- visit("/mail_preferences/#{@choice.id}")
- response.body.should(have_tag("a[href=/mail_preferences/#{@choice.id}]", :text => (status_string.titleize)))
- end
- Then(/^the "(.*)" checkbox should be checked$/) do |label|
- field_labeled(label).should(be_checked)
- end
- Then(/the imap server should have the invalid email in error category/) do
- box = @imap.fetch_all_for_box("Error/Acknowledge")
- (box.size.should == 1)
- (box[0].to_s.should == "INVALID")
- end
- When(/^I attach the file at "(.*)" to "(.*)" $/) do |path, field|
- attach_file(field, path)
- end
- When(/^I check "(.*)"$/) { |field| check(field) }
- When(/^I choose "(.*)"$/) { |field| choose(field) }
- When(/^I fill in "(.*)" with "(.*)"$/) do |field, value|
- fill_in(field, :with => (value))
- end
- When(/^I follow "(.*)"$/) { |link| click_link(link) }
- When(/^I follow Frequency Options for "(.*)"$/) do |catalog_title|
- catalog = Catalog.find_by_title(catalog_title)
- click_link("Frequency Options", :id => ("frequency_options_#{catalog.id}"))
- end
- When(/^I follow the edit catalog link for "(.*)"$/) do |title|
- catalog = Catalog.find_by_title(title)
- click_link("Edit catalog", :id => ("edit_catalog_#{catalog.id}"))
- end
- When(/^I follow the merchant invitation for "(.*)"$/) do |merchant_name|
- merchant = Merchant.find_by_name(merchant_name)
- @verification_mail = VerificationMail.find_by_login_id(merchant.login.id)
- @verification_mail.should_not(be_nil)
- visit("/merchants")
- end
- When(/^I press "(.*)"$/) { |button| click_button(button) }
- When(/^I select "(.*)" as the "(.*)" date and time$/) do |datetime, datetime_label|
- select_datetime(datetime, :from => (datetime_label))
- end
- When(/^I select "(.*)" as the "(.*)" date$/) do |date, date_label|
- select_date(date, :from => (date_label))
- end
- When(/^I select "(.*)" as the "(.*)" time$/) do |time, time_label|
- select_time(time, :from => (time_label))
- end
- When(/^I select "(.*)" as the date and time$/) do |time|
- select_datetime(time)
- end
- When(/^I select "(.*)" as the date$/) { |date| select_date(date) }
- When(/^I select "(.*)" as the time$/) { |time| select_time(time) }
- When(/^I select "(.*)" from "(.*)"$/) do |value, field|
- select(value, :from => (field))
- end
- When(/^I uncheck "(.*)"$/) { |field| uncheck(field) }
- When(/^I view my dashboard/) do
- login_as(@login)
- visit("/")
- end
- When(/^I visit 'My Choices'/) do
- login_as(@login)
- visit("/mail_preferences")
- end
- When(/^the CSR sends an invitation for "(.*)"$/) do |merchant_name|
- invite_merchant(merchant_name)
- end
- When(/^the mail processing daemon runs/) do
- MerchantEmailProcessing::MailProcessor.construct(:imap => (@imap), :logger => (stub_everything)).process
- end
Add Comment
Please, Sign In to add comment