Guest User

Untitled

a guest
Jun 2nd, 2018
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.20 KB | None | 0 0
  1. require(File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths")))
  2. require((RAILS_ROOT + "/lib/test/imap_mock"))
  3.  
  4.  
  5. def edit_url_for_merchant(merchant_name)
  6. merchant_id = look_up_merchant(merchant_name).id
  7. "/admin/mass_catalog/edit?merchant_id=#{merchant_id}"
  8. end
  9.  
  10. def look_up_merchant(merchant_name)
  11. Merchant.find_by_name(merchant_name)
  12. end
  13.  
  14. def login_as(login, password = "password")
  15. email = login.is_a?(String) ? (login) : (login.email)
  16. visit("/login")
  17. fill_in("login_email", :with => (email))
  18. fill_in("login_password", :with => (password))
  19. click_button("btn-login-submit")
  20. end
  21.  
  22. def make_account_with_role(role, password)
  23. role = Account.const_get("ROLE_#{role}".upcase) rescue role_mapping[role.downcase]
  24. login = Factory.create_login(:email => ("#{/\w+/.gen}@#{/\w+/.gen}.com"), :password => (password))
  25. login.account.role = role
  26. login.save!
  27. login
  28. end
  29.  
  30. def role_mapping
  31. { "user" => (Account::ROLE_USER), "merchant" => (Account::ROLE_MERCHANT), "admin" => (Account::ROLE_ADMIN), "csr" => (Account::ROLE_SERVICE) }
  32. end
  33.  
  34. def invite_merchant(merchant_name)
  35. merchant = Merchant.find_by_name(merchant_name)
  36. confirm_url = "/admin/merchants/#{merchant.id}/confirm_invite"
  37. visit(confirm_url)
  38. url = "/admin/merchants/#{merchant.id}/invite"
  39. visit(url, :put)
  40. end
  41.  
  42. def generate_mail(choice)
  43. mail = TMail::Mail.new
  44. mail.from = /\w+@\w+\.com/.gen
  45. mail.to = choice.email_alias
  46. mail.subject = "Email to choice #{choice.id}"
  47. mail.body = mail.subject
  48. mail
  49. end
  50.  
  51. def download_sent_file
  52. sio = StringIO.new
  53. response.body[response, sio]
  54. sio.string
  55. end
  56.  
  57.  
  58. Given(/A merchant has sent a email that they have confirmed my request/) do
  59. @login = Factory.create_login
  60. Factory.create_mailbox(:account_id => (@login.account_id))
  61. @choice = Factory.create_catalog_choice(:account_id => (@login.account_id))
  62. @imap = ImapMock.new
  63. @imap.mock_add_email("#{MerchantEmailProcessing::Category::BOX_PREFIX}Confirmation", generate_mail(@choice))
  64. end
  65.  
  66. Given(/A merchant has sent an invalid email/) do
  67. @login = Factory.create_login
  68. Factory.create_mailbox(:account_id => (@login.account_id))
  69. @choice = Factory.create_catalog_choice(:account_id => (@login.account_id))
  70. @imap = ImapMock.new
  71. mail = stub_everything(:to_s => "INVALID")
  72. @imap.mock_add_email("#{MerchantEmailProcessing::Category::BOX_PREFIX}Acknowledge", mail)
  73. end
  74.  
  75. Given(/I am on the new users_can_sign_up page/) do
  76. visit("/users_can_sign_ups/new")
  77. end
  78.  
  79. Given(/I create a catalog choice for "(.*)"/) do |catalog_title|
  80. visit("/")
  81. catalog = Catalog.find_by_title(catalog_title)
  82. click_link("Find Catalogs")
  83. click_link("Browse")
  84. click_link(catalog_title.first.upcase)
  85. click_link("set_mail_preference_#{catalog.id}")
  86. click_button("btn-submit-mail-pref")
  87. response.body.should(match(/successfully recorded/))
  88. end
  89.  
  90. Given(/I have not signed up/) { }
  91.  
  92. Given(/I invite the merchant named "(.*)"/) do |merchant_name|
  93. Given("I log in as a CSR")
  94. @merchant = Merchant.find_by_name(merchant_name)
  95. put(invite_admin_merchant_path(@merchant.id))
  96. end
  97.  
  98. Given(/^"(.*)" belongs to a (not)? participating merchant/) do |catalog_title, participation|
  99. merchant_relationship = if participation.nil? then
  100. Merchant::RELATIONSHIP_HEALTHY
  101. else
  102. Merchant::RELATIONSHIP_REFUSED
  103. end
  104. @catalog = Catalog.find_by_title(catalog_title)
  105. @catalog.merchant.merchant_relationship = merchant_relationship
  106. @catalog.save
  107. end
  108.  
  109. Given(/^"(.*)" is (not )?a participating merchant/) do |merchant_name, participation_string|
  110. @merchant = Merchant.find_by_name(merchant_name)
  111. participating_flag = (not (participation_string == "not "))
  112. if participating_flag then
  113. @merchant.relationship_status = Merchant::RELATIONSHIP_HEALTHY
  114. else
  115. @merchant.relationship_status = Merchant::RELATIONSHIP_REFUSED
  116. end
  117. @merchant.save!
  118. end
  119.  
  120. Given(/^"(.*)" opted out of "(.*)"$/) do |fixture_name, catalog_name|
  121. account = Factory.create_account
  122. individual = Factory.create_individual(:display_name => (fixture_name))
  123. catalog = (Catalog.find_by_title(catalog_name) or Factory.create_catalog(:title => (catalog_name)))
  124. choice = Factory.create_catalog_choice(:account_id => (account.id), :catalog_id => (catalog.id))
  125. (individual.catalog_choices << choice)
  126. end
  127.  
  128. Given(/^A merchant has sent a email that they have received my choice/) do
  129. @login = Factory.create_login
  130. Factory.create_mailbox(:account_id => (@login.account_id))
  131. @choice = Factory.create_catalog_choice(:account_id => (@login.account_id))
  132. @imap = ImapMock.new
  133. @imap.mock_add_email("#{MerchantEmailProcessing::Category::BOX_PREFIX}Acknowledge", generate_mail(@choice))
  134. end
  135.  
  136. Given(/^I am logged in as a (CSR|merchant|user)/) { |role| }
  137.  
  138. Given(/^I am on the home page$/) { visit("/") }
  139.  
  140. Given(/^I am registered as (.*) with password (.*)$/) do |email, password|
  141. @login = Factory.create_login(:email => (email), :password => (password))
  142. end
  143.  
  144. Given(/^I follow "(.*)" with id "(.*)"/) do |link_text, id|
  145. click_link(link_text, :id => (id))
  146. end
  147.  
  148. Given(/^I have logged in as a user$/) do
  149. password = "password"
  150. @login = make_account_with_role("user", password)
  151. name = @login.name
  152. mailbox = Factory.create_mailbox
  153. (@login.account.mailboxes << mailbox)
  154. @login.account.save!
  155. @login.save!
  156. visit("/login")
  157. fill_in("login_email", :with => (@login.email))
  158. fill_in("login_password", :with => (password))
  159. click_button("btn-login-submit")
  160. response.should(have_tag("#logged-in-status", name.to_regexp))
  161. end
  162.  
  163. Given(/^I have set a mail preference for "(.*)"$/) do |catalog_title|
  164. catalog = (Catalog.find_by_title(catalog) or Factory.create_catalog(:title => (catalog_title)))
  165. visit("/catalogs/browse")
  166. click_link("#{catalog.title[0, 1].upcase}")
  167. visit("/catalogs/#{catalog.id}/set_preference")
  168. click_button("btn-submit-mail-pref")
  169. click_link("Welcome")
  170. end
  171.  
  172. Given(/^I log in as a CSR$/) do
  173. visit("/logout")
  174. password = "password"
  175. @login = make_account_with_role("csr", password)
  176. name = @login.name
  177. mailbox = Factory.create_mailbox
  178. (@login.account.mailboxes << mailbox)
  179. @login.account.save!
  180. @login.save!
  181. visit("/login")
  182. fill_in("login[email]", :with => (@login.email))
  183. fill_in("login[password]", :with => (password))
  184. click_button("btn-login-submit")
  185. response.should(have_tag("#logged-in-status", name.to_regexp))
  186. end
  187.  
  188. Given(/^I log in as a merchant/) do
  189. password = "password"
  190. @login = make_account_with_role("merchant", password)
  191. @merchant = Factory.create_merchant
  192. @merchant.account = @login.account
  193. @merchant.account.save!
  194. @merchant.save!
  195. @login.save!
  196. visit("/login")
  197. fill_in("login_email", :with => (@login.email))
  198. fill_in("login_password", :with => (password))
  199. click_button("btn-login-submit")
  200. end
  201.  
  202. Given(/^I log in as an admin/) do
  203. password = "password"
  204. @login = make_account_with_role("admin", password)
  205. name = @login.name
  206. mailbox = Factory.create_mailbox
  207. (@login.account.mailboxes << mailbox)
  208. @login.account.save!
  209. @login.save!
  210. visit("/login")
  211. fill_in("login_email", :with => (@login.email))
  212. fill_in("login_password", :with => (password))
  213. click_button("btn-login-submit")
  214. response.should(have_tag("#logged-in-status", name.to_regexp))
  215. end
  216.  
  217. Given(/^I log in as the merchant "(.*)"/) do |merchant_name|
  218. visit("/logout")
  219. merchant = Merchant.find_by_name(merchant_name)
  220. visit("/login")
  221. fill_in("login[email]", :with => (merchant.login.email))
  222. fill_in("login[password]", :with => "password")
  223. click_button("btn-login-submit")
  224. response.should(have_tag("title", "Merchants".to_regexp))
  225. end
  226.  
  227. Given(/^I log in as the user "(.*)"/) do |email_address|
  228. Given("there exists a verified user with the email \"#{email_address}\"")
  229. visit("/logout")
  230. visit("/login")
  231. fill_in("login[email]", :with => (email_address))
  232. fill_in("login[password]", :with => "password")
  233. click_button("btn-login-submit")
  234. end
  235.  
  236. Given(/^I should see an invitation to "(.*)"$/) do |merchant_name|
  237. response.should(have_tag("pre", "Dear #{merchant_name}".to_regexp))
  238. end
  239.  
  240. Given(/^I view the invitation for "(.*)"$/) do |merchant_name|
  241. merchant_id = look_up_merchant(merchant_name).id
  242. visit("/admin/merchants/#{merchant_id}/preview_invite")
  243. end
  244.  
  245. Given(/^Ultrasphinx has indexed all objects in the system$/) do
  246. `rake ultrasphinx:index RAILS_ENV=test`
  247. end
  248.  
  249. Given(/^a CSR has created a merchant account for "(.*)"$/) do |merchant_name|
  250. click_link("Merchants")
  251. click_link("New Merchant")
  252. fill_in("Name", :with => (merchant_name))
  253. fill_in("Address", :with => (/\d{4} \w+/.gen))
  254. fill_in("Email address", :with => (/\w{4,7}@testmail.com/.gen))
  255. fill_in("City", :with => "Madison")
  256. select("Michigan", :from => "State")
  257. fill_in("Zip code", :with => "53703")
  258. click_button("Create Merchant")
  259. response.body.should =~ /Successfully created merchant/m
  260. end
  261.  
  262. Given(/^change the contact email for "(.*)" to "(.*)"$/) do |merchant_name, email|
  263. visit(edit_url_for_merchant(merchant_name))
  264. fill_in("catalog[email_customer_service]", :with => (email))
  265. click_button("Save Changes")
  266. merchant = look_up_merchant(merchant_name)
  267. (merchant.email_customer_service.should == email)
  268. end
  269.  
  270. Given(/^send an invitation to "(.*)"$/) do |merchant_name|
  271. merchant_id = look_up_merchant(merchant_name).id
  272. put("/admin/merchants/#{merchant_id}/invite")
  273. end
  274.  
  275. Given(/^the CSR has logged out$/) { visit("/logout") }
  276.  
  277. Given(/^the catalog "(.*)" has the mail preference "(.*)"$/) do |catalog, mail_preference|
  278. catalog = (Catalog.find_by_title(catalog) or Factory.create_catalog(:title => (catalog)))
  279. mail_preference = Factory.create_mail_preference(:title => (mail_preference), :catalog_id => (catalog.id))
  280. (catalog.mail_preferences << mail_preference)
  281. catalog.save
  282. end
  283.  
  284. Given(/^the merchant "(.*)" is (not )?participating/) do |merchant_name, participation_string|
  285. @merchant = Merchant.find_by_name(merchant_name)
  286. participating_flag = (not (participation_string == "not "))
  287. if participating_flag then
  288. @merchant.relationship_status = Merchant::RELATIONSHIP_HEALTHY
  289. else
  290. @merchant.relationship_status = Merchant::RELATIONSHIP_REFUSED
  291. end
  292. @merchant.save!
  293. end
  294.  
  295. Given(/^the merchant for "(.*)" is "(.*)"/) do |catalog_title, merchant_relationship_string|
  296. @catalog = Factory.create_catalog(:title => (catalog_title))
  297. email_notification_method = Factory.create_email_notification_method(:optout_email => "professor_gryff@seventhgeneration.com")
  298. @catalog.notification_method = email_notification_method
  299. @catalog.save
  300. @merchant = @catalog.merchant
  301. case merchant_relationship_string
  302. when "participating" then
  303. merchant_relationship = Merchant::RELATIONSHIP_HEALTHY
  304. when "not participationg" then
  305. merchant_relationship = Merchant::RELATIONSHIP_REFUSED
  306. else
  307. # do nothing
  308. end
  309. @merchant.relationship_status = merchant_relationship
  310. @merchant.save
  311. end
  312.  
  313. Given(/^the merchant has sent an email "(.*)" my choice/) do |email_action|
  314. if (email_action == "confirming") then
  315. email_folder = "Confirmation"
  316. else
  317. email_folder = "Acceptance" if (email_action == "accepting")
  318. end
  319. @imap = ImapMock.new
  320. @imap.mock_add_email("Unprocessed/#{email_folder}", generate_mail(@choice))
  321. end
  322.  
  323. Given(/^there exists a catalog for "(.*)" called "(.*)"/) do |merchant_name, catalog_name|
  324. Given("I log in as a CSR")
  325. Given("there exists a merchant named \"#{merchant_name}\"")
  326. visit("/logout")
  327. Given("I log in as a CSR")
  328. Given("I go to create a new catalog for \"#{merchant_name}\"")
  329. fill_in("catalog[title]", :with => (catalog_name))
  330. click_button("Create")
  331. Given("I should see \"created the catalog\"")
  332. Given("I follow \"Log Out\"")
  333. end
  334.  
  335. Given(/^there exists a merchant named "(.*)"/) do |merchant_name|
  336. email = /\w{3,7}.\w{5,7}@cchoice.org/.gen
  337. Given("I follow \"Merchants & Catalogs\"")
  338. Given("I follow \"New Merchant\"")
  339. fill_in("Name", :with => (merchant_name))
  340. fill_in("Address", :with => "1234 Wealthy St.")
  341. fill_in("City", :with => "Grand Rapids")
  342. select("Michigan", :from => "State")
  343. fill_in("Zip code", :with => "49506")
  344. fill_in("merchant[email_customer_service]", :with => (email))
  345. fill_in("merchant[contact_name]", :with => "The Contact")
  346. fill_in("merchant[contact_title]", :with => "SVP")
  347. fill_in("merchant[contact_email]", :with => "merchantemail@cchoice.org")
  348. click_button("Create Merchant")
  349. Given("I should see \"created merchant\"")
  350. Given("I invite the merchant named \"#{merchant_name}\"")
  351. Given("the merchant named \"#{merchant_name}\" accepts the invitation")
  352. end
  353.  
  354. Given(/^there exists a verified user with the email "(.*)"$/) do |email_address|
  355. return if Login.find_by_email(email_address)
  356. visit("/logout")
  357. click_link("Get Started!")
  358. fill_in("Name", :with => (/\w{5,10}/.gen))
  359. fill_in("Email address", :with => (email_address))
  360. fill_in("login[email_confirmation]", :with => (email_address))
  361. password = "password"
  362. fill_in("New password", :with => (password))
  363. fill_in("login[password_confirmation]", :with => (password))
  364. fill_in("Address", :with => (/\d{3} \w{5} St./.gen))
  365. fill_in("City", :with => "Grand Rapids")
  366. fill_in("State", :with => "MI")
  367. fill_in("Zip code", :with => (/\d{5}/.gen))
  368. check("account[tos]")
  369. check("account[eighteen_or_older]")
  370. click_button("btn-signup-submit")
  371. verification_mail = VerificationMail.find_by_email_sent_to(email_address)
  372. visit("activate/#{verification_mail.encoded_key}")
  373. click_link("Log Out")
  374. end
  375.  
  376. Given(/^there is a catalog in the system called "(.*)"$/) do |catalog_title|
  377. account = Factory.create_account
  378. merchant = (@merchant or Factory.create_merchant(:name => ("The Catalogs Company #{/\w{2,5}/.gen}"), :account_id => (account.id)))
  379. catalog = Factory.create_catalog(:title => (catalog_title), :merchant_id => (merchant.id))
  380. catalog.save!
  381. @merchant.reload if @merchant
  382. end
  383.  
  384. Given(/the merchant "(.*)" has elected to receive instant notifications for opt-outs/) do |merchant_name|
  385. Given("I log in as the merchant \"#{merchant_name}\"")
  386. click_link("Manage Catalogs")
  387. click_link("Edit catalog")
  388. select("Email", :from => "notification_method")
  389. fill_in("email_notification_method[optout_email]", :with => ("#{merchant_name.gsub(/\W+/, "_").downcase}+optout@gmail.com"))
  390. click_button("btn-submit-edit-catalog")
  391. response.should(have_tag("#flash_content", "Changes to the catalog were successfully saved".to_regexp))
  392. end
  393.  
  394. Given(/the merchant "(.*)" sends the user "(.*)" a (Confirmation|Acknowledge) email/) do |merchant_name, user_email, email_type|
  395. @merchant = Merchant.find_by_name(merchant_name)
  396. @login = Login.find_by_email(user_email)
  397. @choice = @login.account.catalog_choices.first
  398. @imap = ImapMock.new
  399. @imap.mock_add_email("Unprocessed/#{email_type}", generate_mail(@choice))
  400. end
  401.  
  402. Given(/the merchant named "(.*)" accepts the invitation/) do |merchant_name|
  403. @merchant = Merchant.find_by_name(merchant_name)
  404. @verification_mail = VerificationMail.find_by_email_sent_to(@merchant.login.email)
  405. visit("/logout")
  406. visit("/merchants/#{@verification_mail.encoded_key}")
  407. click_link("Confirm Your Account")
  408. fill_in("Password", :with => "password")
  409. fill_in("Type it again", :with => "password")
  410. check("merchant[merchant_license]")
  411. click_button("btn-confirmaccount")
  412. Given("I should see \"Welcome, #{@merchant.name}!\"")
  413. end
  414.  
  415. Then(/I should be logged in/) { }
  416.  
  417. Then(/^I debug$/) { debugger }
  418.  
  419. Then(/^I fill in the merchant code$/) do
  420. visit("/merchants/account/confirm?id=#{@verification_mail.encoded_key}")
  421. end
  422.  
  423. Then(/^I puts$/) { puts(response.body) }
  424.  
  425. Then(/^I should not see "(.*)"$/) do |text|
  426. response.body.should_not(contain(text))
  427. end
  428.  
  429. Then(/^I should not see \/(.*)\/$/) do |text|
  430. response.body.should_not(contain(Regexp.new(text)))
  431. end
  432.  
  433. Then(/^I should see "(.*)" opted out of "(.*)" in the catalog choice updates file$/) do |user_name, catalog|
  434. require("fastercsv")
  435. header, user = FasterCSV.parse(download_sent_file)
  436. (header[(0..2)].should == ["Catalog", "Customer Number", "Name"])
  437. (user[0].should == catalog)
  438. (user[2].should == user_name)
  439. end
  440.  
  441. Then(/^I should see "(.*)"$/) { |text| response.body.should(contain(text)) }
  442.  
  443. Then(/^I should see \/(.*)\/$/) do |text|
  444. response.body.should(contain(Regexp.new(text)))
  445. end
  446.  
  447. Then(/^I should see a search result for "(.*)"$/) do |catalog_title|
  448. response.should(have_tag("li.catalog h3", catalog_title.to_regexp))
  449. end
  450.  
  451. Then(/^I should see that the status of my choice is "(.*)"$/) do |status_string|
  452. visit("/mail_preferences/#{@choice.id}")
  453. response.body.should(have_tag("a[href=/mail_preferences/#{@choice.id}]", :text => (status_string.titleize)))
  454. end
  455.  
  456. Then(/^the "(.*)" checkbox should be checked$/) do |label|
  457. field_labeled(label).should(be_checked)
  458. end
  459.  
  460. Then(/the imap server should have the invalid email in error category/) do
  461. box = @imap.fetch_all_for_box("Error/Acknowledge")
  462. (box.size.should == 1)
  463. (box[0].to_s.should == "INVALID")
  464. end
  465.  
  466. When(/^I attach the file at "(.*)" to "(.*)" $/) do |path, field|
  467. attach_file(field, path)
  468. end
  469.  
  470. When(/^I check "(.*)"$/) { |field| check(field) }
  471.  
  472. When(/^I choose "(.*)"$/) { |field| choose(field) }
  473.  
  474. When(/^I fill in "(.*)" with "(.*)"$/) do |field, value|
  475. fill_in(field, :with => (value))
  476. end
  477.  
  478. When(/^I follow "(.*)"$/) { |link| click_link(link) }
  479.  
  480. When(/^I follow Frequency Options for "(.*)"$/) do |catalog_title|
  481. catalog = Catalog.find_by_title(catalog_title)
  482. click_link("Frequency Options", :id => ("frequency_options_#{catalog.id}"))
  483. end
  484.  
  485. When(/^I follow the edit catalog link for "(.*)"$/) do |title|
  486. catalog = Catalog.find_by_title(title)
  487. click_link("Edit catalog", :id => ("edit_catalog_#{catalog.id}"))
  488. end
  489.  
  490. When(/^I follow the merchant invitation for "(.*)"$/) do |merchant_name|
  491. merchant = Merchant.find_by_name(merchant_name)
  492. @verification_mail = VerificationMail.find_by_login_id(merchant.login.id)
  493. @verification_mail.should_not(be_nil)
  494. visit("/merchants")
  495. end
  496.  
  497. When(/^I press "(.*)"$/) { |button| click_button(button) }
  498.  
  499. When(/^I select "(.*)" as the "(.*)" date and time$/) do |datetime, datetime_label|
  500. select_datetime(datetime, :from => (datetime_label))
  501. end
  502.  
  503. When(/^I select "(.*)" as the "(.*)" date$/) do |date, date_label|
  504. select_date(date, :from => (date_label))
  505. end
  506.  
  507. When(/^I select "(.*)" as the "(.*)" time$/) do |time, time_label|
  508. select_time(time, :from => (time_label))
  509. end
  510.  
  511. When(/^I select "(.*)" as the date and time$/) do |time|
  512. select_datetime(time)
  513. end
  514.  
  515. When(/^I select "(.*)" as the date$/) { |date| select_date(date) }
  516.  
  517. When(/^I select "(.*)" as the time$/) { |time| select_time(time) }
  518.  
  519. When(/^I select "(.*)" from "(.*)"$/) do |value, field|
  520. select(value, :from => (field))
  521. end
  522.  
  523. When(/^I uncheck "(.*)"$/) { |field| uncheck(field) }
  524.  
  525. When(/^I view my dashboard/) do
  526. login_as(@login)
  527. visit("/")
  528. end
  529.  
  530. When(/^I visit 'My Choices'/) do
  531. login_as(@login)
  532. visit("/mail_preferences")
  533. end
  534.  
  535. When(/^the CSR sends an invitation for "(.*)"$/) do |merchant_name|
  536. invite_merchant(merchant_name)
  537. end
  538.  
  539. When(/^the mail processing daemon runs/) do
  540. MerchantEmailProcessing::MailProcessor.construct(:imap => (@imap), :logger => (stub_everything)).process
  541. end
Add Comment
Please, Sign In to add comment