Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 52.10 KB | None | 0 0
  1. shared_examples "user without access to user management" do
  2. it "cannot access the user management page" do
  3. visit users_path
  4. expect(page).to have_content "You are not authorized to access this page."
  5. end
  6. end
  7.  
  8. Role::NAMES.each do |role|
  9. shared_examples "user who can manage #{role} accounts" do
  10. # set the name of the additional user to the alphabetically first one,
  11. # so that the sortable table won't change the position
  12. # and wreck the test
  13. let!(:additional_user) { create(:user, role.to_sym) }
  14.  
  15. before do
  16. if additional_user.current_role_name == "student"
  17. additional_user.student = create(:student, user: additional_user)
  18. term_class.students << additional_user.student
  19. elsif additional_user.current_role_name == "teacher"
  20. additional_user.teacher = create(:teacher, user: additional_user)
  21. elsif additional_user.current_role_name == "caregiver"
  22. additional_user.caregiver = create(:caregiver, user: additional_user)
  23. end
  24. visit users_path
  25. end
  26.  
  27. it "can see '#{role.pluralize.humanize}' tab" do
  28. within ".nav-tabs" do
  29. expect(page).to have_link role.humanize
  30. end
  31. end
  32.  
  33. scenario "clicks on '#{role.pluralize.humanize}' tab", :js do
  34. click_on_role_tab(additional_user, role.to_sym)
  35. expect(page).to have_selector("img[src$='#{additional_user.avatar.square_32.url}']")
  36. %i(first_name last_name email unique_id).each do |attribute|
  37. expect(page).to have_content additional_user.send(attribute)
  38. end
  39. end
  40.  
  41. context "after clicking on 'Add New User'" do
  42. before do
  43. click_link "Add New User"
  44. end
  45.  
  46. it "can see #{role.humanize} role checkbox" do
  47. expect(page).to have_xpath "//input[@value='#{Role.find_by(name: role).id}']"
  48. end
  49.  
  50. context "after submitting valid user form", :js do
  51. let!(:previous_user_count) { User.count }
  52.  
  53. before do
  54. %w(last_name first_name phone address1 address2 zip_code city place_of_birth).each do |text_field|
  55. fill_in "user[#{text_field}]", with: text_field.capitalize.to_s
  56. end
  57.  
  58. fill_in "user[email]", with: "test_user@example.com"
  59. find("##{role}-role-button").click
  60. find("#gender_female").click
  61. find(:xpath, "//input[@name='user[date_of_birth]']").click
  62. find(:xpath, "//td[@data-day='1']").click
  63. click_button "Save"
  64. end
  65.  
  66. it "is redirected to next step for teachers and students and then to profile page" do
  67. created_user = User.last
  68. expect(find(".alert-success")).to have_content "User was successfully saved."
  69. expect(User.count).to eq previous_user_count + 1
  70.  
  71. if role == "student"
  72. expect(current_path).to eq edit_student_path(created_user.reload.student)
  73. expect(created_user.student).to_not be_nil
  74. find(".save-btn").click
  75. elsif role == "teacher"
  76. expect(current_path).to eq edit_teacher_path(created_user.reload.teacher)
  77. expect(created_user.teacher).to_not be_nil
  78. find(".save-btn").click
  79. elsif role == "caregiver"
  80. expect(created_user.caregiver).to_not be_nil
  81. end
  82.  
  83. expect(current_path).to eq user_path(created_user)
  84. end
  85. end
  86. end
  87.  
  88. context "after visiting user's profile page" do
  89. before do
  90. if additional_user.current_role_name == "student" || additional_user.current_role_name == "teacher"
  91. parent = additional_user.student || additional_user.teacher
  92. EmergencyContact.create(first_name: "first_name", last_name: "last_name", email: "email", phone: "phone", parent: parent)
  93. end
  94.  
  95. if additional_user.current_role_name == "student"
  96. relationship_type = RelationshipType.create(description: "Description")
  97. caregiver = create(:caregiver)
  98. StudentCaregiver.create(student: additional_user.student, caregiver: caregiver, relationship_type: relationship_type)
  99. end
  100.  
  101. visit user_path(additional_user)
  102. end
  103.  
  104. scenario "clicks on 'Edit Details' button" do
  105. click_on "Edit Details"
  106. expect(current_path).to eq edit_user_path(additional_user)
  107. end
  108.  
  109. scenario "clicks on 'Delete' button", :js, retry: 5 do
  110. expect { click_on "Delete" }.to change { User.count }.by(-1)
  111. expect(current_path).to eq users_path
  112. expect(page).to have_content "User was successfully deleted"
  113. end
  114.  
  115. if role == "teacher"
  116. scenario "cannot delete a headroom teacher" do
  117. create :grade_level_class, headroom_teacher: additional_user.teacher
  118. expect { click_on "Delete" }.to change { User.count }.by(0)
  119. expect(current_path).to match(/\/users\/.*/)
  120. expect(page).to have_content "Teacher is currently headroom teacher of"
  121. end
  122.  
  123. scenario "cannot delete a course teacher" do
  124. create :course, teacher: additional_user.teacher
  125. expect { click_on "Delete" }.to change { User.count }.by(0)
  126. expect(current_path).to match(/\/users\/.*/)
  127. expect(page).to have_content "Teacher is currently teaching 1 course. Please replace the teacher of this course before."
  128. end
  129. end
  130.  
  131. it "sees the user's details", retry: 5 do
  132. %i(unique_id last_name first_name gender place_of_birth date_of_birth email phone address1 address2 zip_code city).each do |attr|
  133. expect(page).to have_content additional_user.send(attr)
  134. end
  135.  
  136. expect(page).to have_selector("img[src$='#{additional_user.avatar.square_600.url}']")
  137. additional_user.roles.each do |user_role|
  138. expect(page).to have_content user_role.name.humanize
  139. end
  140.  
  141. if additional_user.current_role_name == "student" || additional_user.current_role_name == "teacher"
  142. additional_user.current_role_from_name.emergency_contacts.each do |emergency_contact|
  143. %i(first_name last_name email phone).each do |attr|
  144. expect(page).to have_content emergency_contact.send(attr)
  145. end
  146. end
  147. end
  148.  
  149. if additional_user.current_role_name == "student"
  150. student_caregivers = additional_user.student.student_caregivers
  151. student_caregivers.each do |student_caregiver|
  152. expect(page).to have_link "#{student_caregiver.caregiver.first_name} #{student_caregiver.caregiver.last_name}", href: user_path(student_caregiver.caregiver.user)
  153. expect(page).to have_content student_caregiver.relationship_type.description
  154. end
  155. end
  156.  
  157. if additional_user.current_role_name == "caregiver"
  158. StudentCaregiver.create(
  159. caregiver: additional_user.caregiver,
  160. student: create(:student),
  161. relationship_type: RelationshipType.first
  162. )
  163. caregiver_students = additional_user.caregiver.students
  164. visit current_path
  165. expect(caregiver_students).to_not be_empty
  166. caregiver_students.each do |caregiver_student|
  167. expect(page).to have_link caregiver_student.name, href: user_path(caregiver_student.user)
  168. end
  169. end
  170. end
  171.  
  172. it "clicks on 'Edit Picture' button" do
  173. click_on "Edit Picture"
  174. expect(current_path).to eq avatar_upload_user_path(additional_user)
  175. end
  176. end
  177.  
  178. context "after visiting user edit page", :js do
  179. before do
  180. visit edit_user_path(additional_user)
  181. end
  182.  
  183. it "sees a gender option selected" do
  184. within ".form-group.gender-selection" do
  185. expect(find("label.btn.btn-default.active").text.downcase).to eq(additional_user.gender.downcase)
  186. end
  187. end
  188.  
  189. it "changes role" do
  190. expect(page).to have_css(".role_checkboxes")
  191. within(".role_checkboxes") do
  192. # All are roles that are accessible for this user.
  193. expect(all("input[type=checkbox]").length).to eq(Role.accessible_by(Ability.new(user)).count)
  194. # All are enabled.
  195. expect(all("input[type=checkbox][disabled]").length).to eq(0)
  196. end
  197. end
  198. end
  199.  
  200. context "after visiting 'Edit Profile Picture' page" do
  201. before do
  202. visit avatar_upload_user_path(additional_user)
  203. end
  204.  
  205. it "uploads and crops image", :js, retry: 5 do
  206. expect(page).to have_selector("#upload-panel", visible: true)
  207. expect(page).to have_selector("#crop-panel", visible: false)
  208. attach_file("user_avatar", "app/assets/images/dummy/square_600_dummy.png")
  209. expect(page).to have_selector("#crop-panel", visible: true)
  210. expect(find("#upload-panel", visible: false)).to_not be_visible
  211. expect(page).to have_selector("#user_avatar_crop_x", visible: false)
  212. expect(page).to have_selector('#user_avatar_crop_x[value="0"]', visible: false)
  213. find(".ord-w.jcrop-dragbar").drag_to(find(".ord-s.jcrop-handle"))
  214. expect(page).to_not have_selector('#user_avatar_crop_x[value="0"]', visible: false)
  215. crop_values = {}
  216. %i(x y w h).each do |c|
  217. crop_values[c] = find("#user_avatar_crop_#{c}", visible: false).value.to_i
  218. end
  219. click_button "Save"
  220. expect(page).to have_content("User was successfully saved.")
  221. expect(page).not_to have_selector("small", text: "Profile Picture Upload") # Need to wait for the title to disappear (new page loaded)
  222. additional_user.reload
  223. %i(x y w h).each do |c|
  224. expect(additional_user.send("avatar_crop_#{c}")).to eq crop_values[c]
  225. end
  226. end
  227.  
  228. it "selects image for upload and then decides to chose another picture", :js do
  229. attach_file("user_avatar", "app/assets/images/dummy/square_600_dummy.png")
  230. expect(page).to have_selector("#crop-panel", visible: true)
  231. expect(page).to have_selector(".jcrop-holder img")
  232. previous_image = all(".jcrop-holder img").first[:src]
  233. click_on "Chose a different picture"
  234. expect(page).to have_selector("#crop-panel", visible: false)
  235. attach_file("user_avatar", "app/assets/images/dummy/square_300_dummy.png")
  236. expect(page).to have_selector("#crop-panel", visible: true)
  237. expect(page).to have_selector(".jcrop-holder img")
  238. expect(all(".jcrop-holder img").first[:src]).to_not eq previous_image
  239. end
  240. end
  241.  
  242. context "after clicking on '#{role.pluralize.humanize}' tab", :js do
  243. let(:user_table_row_selector) do
  244. ".#{ActionView::RecordIdentifier.dom_id(additional_user)}"
  245. end
  246.  
  247. before do
  248. click_on_role_tab(additional_user, role.to_sym)
  249. app.users_index_page.wait_for_user_rows
  250. end
  251.  
  252. it "clicks 'Edit' button" do
  253. within user_table_row_selector do
  254. expect(page).to have_content "user4@example.com"
  255. click_link "Edit"
  256. end
  257. expect(app.users_edit_page).to be_displayed(id: additional_user.id)
  258. expect(current_path).to eq edit_user_path(additional_user)
  259. end
  260.  
  261. it "clicks 'Delete' button" do
  262. user_row = app.users_index_page.user_row_for_user(additional_user)
  263.  
  264. if role == "teacher"
  265. # FIXME: Cannot get this test working (by Can)
  266. # expect { delete_button.click }.to change{ User.count }.by(0)
  267. # app.users_index_page.wait_for_flash_error
  268. # expect(app.users_index_page).to have_flash_error text: "Teacher is currently headroom teacher of"
  269. # expect(app.users_index_page).to have_flash_error text: "Teacher is currently teaching #{additional_user.teacher.courses.count} courses"
  270. else
  271. if role == "student"
  272. previous_role_users_count = additional_user.student.current_grade_level.students.count
  273. else
  274. previous_role_users_count = additional_user.current_role.users.count
  275. end
  276. expect(app.users_index_page).to have_user_rows count: previous_role_users_count
  277. previous_users_count = User.count
  278. user_row.delete_button.click
  279. expect(app.users_index_page).to have_user_rows count: previous_role_users_count - 1
  280. expect(User.count).to eq previous_users_count - 1
  281. end
  282. end
  283. end
  284.  
  285. if role == "student" || role == "teacher"
  286. context "after visiting #{role} edit page", :js do
  287. let!(:parent) { additional_user.send(role.to_s) }
  288.  
  289. it "can see the edit details button with an apporpriate text for teachers and students" do
  290. visit edit_user_path(additional_user)
  291. if role == "student"
  292. expect(find("#edit_btn")).to have_content "Add Caregiver & Emergency Contacts"
  293. elsif role == "teacher"
  294. expect(find("#edit_btn")).to have_content "Add Subjects & Emergency Contacts"
  295. end
  296. end
  297.  
  298. context "without existing emergency contact" do
  299. before do
  300. visit edit_user_path(additional_user)
  301. click_link "edit_btn"
  302. end
  303.  
  304. scenario "clicks on 'Add emergency contact' button 3 times and removes the last one" do
  305. expect(page).not_to have_css ".emergency-contact-panel"
  306. find(".add-emergency-contact-btn").click
  307. within ".emergency-contacts-accordion" do
  308. expect(page).to have_css ".emergency-contact-panel"
  309. expect(page).to have_css ".panel-info"
  310. end
  311.  
  312. expect(page).to have_content "New emergency contact #1"
  313. %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
  314. expect(page).to have_css "##{role}_emergency_contacts_attributes_0_#{text_field}"
  315. end
  316.  
  317. 2.times do |index|
  318. find(".add-emergency-contact-btn").click
  319. expect(page).to have_content "New emergency contact ##{index + 2}"
  320. %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
  321. expect(page).to have_css "##{role}_emergency_contacts_attributes_#{index + 1}_#{text_field}"
  322. expect(page).to have_field "#{role}[emergency_contacts_attributes][#{index + 1}][#{text_field}]"
  323. end
  324. end
  325.  
  326. all(".delete-emergency-contact")[2].click
  327. %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
  328. expect(page).not_to have_css "##{role}_emergency_contacts_attributes_2_#{text_field}"
  329. expect(page).not_to have_field "#{role}[emergency_contacts_attributes][2][#{text_field}]"
  330. end
  331.  
  332. expect(page).not_to have_content "New emergency contact #3"
  333.  
  334. title = "New emergency contact #1"
  335. expect(page).to have_content title
  336. click_link(title)
  337. %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
  338. expect(page).to have_css "##{role}_emergency_contacts_attributes_0_#{text_field}"
  339. expect(page).to have_field "#{role}[emergency_contacts_attributes][0][#{text_field}]"
  340. end
  341. end
  342.  
  343. scenario "creates emergency contact" do
  344. find(".add-emergency-contact-btn").click
  345. panel = find(".emergency-contact-panel")
  346.  
  347. within panel do
  348. %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
  349. fill_in("#{role}[emergency_contacts_attributes][0][#{text_field}]", with: text_field)
  350. end
  351.  
  352. fill_in("#{role}[emergency_contacts_attributes][0][email]", with: "valid@email.com")
  353. end
  354.  
  355. expect { find(".save-btn").click }.to change { EmergencyContact.count }.from(0).to(1)
  356. expect(current_path).to eq user_path(additional_user)
  357. expect(page).to have_content "#{role.humanize} successfully saved."
  358. end
  359.  
  360. scenario "submits empty emergency contact form" do
  361. find(".add-emergency-contact-btn").click
  362. find(".save-btn").click
  363. expect(all(".has-error").count).to eq 2 # first_name and last_name are mandatory
  364. expect(page).to have_css ".panel-danger"
  365. expect(page).to have_content "This field is required"
  366. end
  367.  
  368. scenario "submits malformed emergency contact email" do
  369. find(".add-emergency-contact-btn").click
  370. within find(".emergency-contact-panel") do
  371. %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
  372. fill_in("#{role}[emergency_contacts_attributes][0][#{text_field}]", with: text_field)
  373. end
  374.  
  375. fill_in("#{role}[emergency_contacts_attributes][0][email]", with: "badEmail.com")
  376. end
  377.  
  378. find(".save-btn").click
  379. expect(page).to have_content "first_name last_name"
  380. expect(all(".has-error").count).to eq 1
  381. expect(page).to have_css ".panel-danger"
  382. expect(page).to have_content "is invalid"
  383. end
  384. end
  385.  
  386. case role
  387. when "teacher"
  388. context "without existing subjects added to teacher" do
  389. let!(:subjects) { Array.new(5) { create :subject } }
  390. let(:subject_1) { "Subject 1" }
  391. let(:subject_2) { "Subject 2" }
  392. let(:subject_3) { "Subject 3" }
  393.  
  394. before do
  395. visit edit_user_path(additional_user)
  396. click_link "edit_btn"
  397. end
  398.  
  399. scenario "clicks on 'Add subject' button 3 times and removes the second one" do
  400. expect(page).not_to have_css ".subject-form"
  401. find(".add-subject-btn").click
  402. select subject_1, from: "subjects-select"
  403. find(".attach-subject-btn").click
  404.  
  405. within ".teacher-subjects-accordion" do
  406. expect(page).to have_css ".subject-panel"
  407. expect(page).to have_css ".panel-info"
  408. end
  409.  
  410. expect(page).to have_selector(".subject-panel .panel-title", text: subject_1)
  411.  
  412. find(".add-subject-btn").click
  413. select subject_2, from: "subjects-select"
  414. find(".attach-subject-btn").click
  415. expect(page).to have_selector(".subject-panel .panel-title", text: subject_2)
  416.  
  417. find(".add-subject-btn").click
  418. select subject_3, from: "subjects-select"
  419. find(".attach-subject-btn").click
  420. expect(page).to have_selector(".subject-panel .panel-title", text: subject_3)
  421.  
  422. all(".delete-subject")[1].click
  423. expect(page).not_to have_selector(".subject-panel .panel-title", text: subject_2)
  424. expect(page).to have_selector(".subject-panel .panel-title", text: subject_1)
  425. expect(page).to have_selector(".subject-panel .panel-title", text: subject_3)
  426. end
  427.  
  428. scenario "Saving subjects" do
  429. find(".add-subject-btn").click
  430. select subject_1, from: "subjects-select"
  431. find(".attach-subject-btn").click
  432. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  433. check_box.set(true)
  434.  
  435. find(".add-subject-btn").click
  436. select subject_2, from: "subjects-select"
  437. find(".attach-subject-btn").click
  438. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  439. check_box.set(true)
  440.  
  441. find(".add-subject-btn").click
  442. select subject_3, from: "subjects-select"
  443. find(".attach-subject-btn").click
  444. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_3).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  445. check_box.set(true)
  446.  
  447. expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(3)
  448. expect(page).to have_content "Teacher successfully saved"
  449. end
  450.  
  451. scenario "Saving subject without grade_level" do
  452. find(".add-subject-btn").click
  453. select subject_1, from: "subjects-select"
  454. find(".attach-subject-btn").click
  455.  
  456. expect { click_button "Save" }.to change { parent.reload.subjects.count }.by(1)
  457. expect(page).to have_content "Teacher successfully saved"
  458. end
  459.  
  460. scenario "Deleting subjects" do
  461. find(".add-subject-btn").click
  462. select subject_1, from: "subjects-select"
  463. find(".attach-subject-btn").click
  464. check "#{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}"
  465.  
  466. find(".add-subject-btn").click
  467. select subject_2, from: "subjects-select"
  468. find(".attach-subject-btn").click
  469. check "#{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}"
  470.  
  471. expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(2)
  472. visit edit_user_path(additional_user)
  473. click_link "edit_btn"
  474.  
  475. all(".delete-subject")[0].click
  476. sleep(1)
  477. expect(current_path).to eq edit_teacher_path(additional_user.teacher)
  478. expect(all(".subject-panel").count).to eq 1
  479. end
  480.  
  481. scenario "Saving new subjects with existing subjects" do
  482. find(".add-subject-btn").click
  483. select subject_1, from: "subjects-select"
  484. find(".attach-subject-btn").click
  485. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  486. check_box.set(true)
  487.  
  488. find(".add-subject-btn").click
  489. select subject_2, from: "subjects-select"
  490. find(".attach-subject-btn").click
  491. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  492. check_box.set(true)
  493.  
  494. expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(2)
  495. end
  496. end
  497.  
  498. when "student"
  499. context "Visits student edit page to manipulate caregivers" do
  500. let(:student) { create(:student) }
  501.  
  502. before do
  503. 4.times { create(:caregiver) }
  504. visit edit_student_path(student)
  505. end
  506.  
  507. def create_typeahead_results(count, query = "Joh")
  508. (1..count).each { |i| typeahead_and_click_result("#caregiver-search", query, i) }
  509. expect { find(:css, ".save-btn").click }.to change {
  510. StudentCaregiver.count
  511. }.from(0).to(count)
  512. end
  513.  
  514. scenario "Creates 2 student caregivers", :js do
  515. create_typeahead_results(2)
  516. end
  517.  
  518. scenario "Tries to add same caregiver 2 times", :js do
  519. 2.times { |_i| typeahead_and_click_result("#caregiver-search", "Joh", 1) }
  520. expect { find(:css, ".save-btn").click }.to change { StudentCaregiver.count }.from(0).to(1)
  521. end
  522.  
  523. scenario "Creates 2 student caregivers, then deletes one", :js do
  524. create_typeahead_results(2)
  525. visit edit_student_path(student)
  526. expect(page).to have_selector(".remove-caregiver")
  527. first(".remove-caregiver").click
  528. expect { find(:css, ".save-btn").click }.to change { StudentCaregiver.count }.from(2).to(1)
  529. end
  530.  
  531. scenario "Adds a caregiver, removes it and then hits save", :js do
  532. typeahead_and_click_result("#caregiver-search", "Joh", 1)
  533. expect(page).to have_selector(".remove-caregiver")
  534. first(".remove-caregiver").click
  535. expect { find(:css, ".save-btn").click }.not_to change { StudentCaregiver.count }
  536. end
  537.  
  538. scenario "Creates 2 student caregivers then edits their relationship types", :js do
  539. create_typeahead_results(2)
  540. visit edit_student_path(student)
  541.  
  542. 2.times do |i| # i is 0 first
  543. select "Others", from: "student_student_caregivers_attributes_#{i}_relationship_type_id"
  544. end
  545.  
  546. expect { find(:css, ".save-btn").click }.to change {
  547. StudentCaregiver.joins(:relationship_type).pluck(:description)
  548. }.from(%w(Father Father)).to(%w(Others Others))
  549. end
  550. end
  551. end
  552.  
  553. context "with existing emergency contact" do
  554. let!(:emergency_contact) { create(:emergency_contact, first_name: "first_name", last_name: "last_name", email: "email@email.com", phone: "phone", parent: parent) }
  555.  
  556. before do
  557. visit edit_user_path(additional_user)
  558. click_link "edit_btn"
  559. end
  560.  
  561. it "edits existing record", :js, retry: 2 do
  562. attributes = %i(last_name first_name email phone address1 address2 zip_code city relation)
  563. new_values = %w(last first lf@www.com 123 avenue 5th 0000 neverland father)
  564. expect(parent.emergency_contacts.count).to eq 1
  565. expect(page).to have_content "first_name last_name"
  566.  
  567. find(".title-link").click
  568. within ".emergency-contact-panel" do
  569. all("input[type=text]").each_with_index do |field, index|
  570. expect(field.value).to eq emergency_contact.send(attributes[index])
  571. field.set new_values[index]
  572. end
  573. end
  574.  
  575. find(".save-btn").click
  576. expect(current_path).to eq user_path(additional_user)
  577. expect(page).to have_content "#{role.humanize} successfully saved."
  578. expect(parent.emergency_contacts.count).to eq 1
  579. visit edit_user_path(additional_user)
  580. click_link "edit_btn"
  581.  
  582. attributes.each_with_index do |attribute, index|
  583. expect(parent.emergency_contacts.first.send(attribute)).to eq new_values[index]
  584. end
  585.  
  586. expect(all(".emergency-contact-panel").count).to eq 1
  587. end
  588.  
  589. it "deletes existing record" do
  590. find(".delete-emergency-contact").click
  591. sleep(1)
  592. expect(parent.emergency_contacts.count).to eq 0
  593. end
  594. end
  595. end
  596. end
  597. end
  598.  
  599. shared_examples "user who can manage without delete #{role} accounts" do
  600. # set the name of the additional user to the alphabetically first one,
  601. # so that the sortable table won't change the position
  602. # and wreck the test
  603. let!(:additional_user) { create(:user, role.to_sym) }
  604.  
  605. before do
  606. if additional_user.current_role_name == "student"
  607. additional_user.student = create(:student, user: additional_user)
  608. term_class.students << additional_user.student
  609. elsif additional_user.current_role_name == "teacher"
  610. additional_user.teacher = create(:teacher, user: additional_user)
  611. elsif additional_user.current_role_name == "caregiver"
  612. additional_user.caregiver = create(:caregiver, user: additional_user)
  613. end
  614. visit users_path
  615. end
  616.  
  617. it "can see '#{role.pluralize.humanize}' tab" do
  618. within ".nav-tabs" do
  619. expect(page).to have_link role.humanize
  620. end
  621. end
  622.  
  623. scenario "clicks on '#{role.pluralize.humanize}' tab", :js do
  624. click_on_role_tab(additional_user, role.to_sym)
  625. expect(page).to have_selector("img[src$='#{additional_user.avatar.square_32.url}']")
  626. %i(first_name last_name email unique_id).each do |attribute|
  627. expect(page).to have_content additional_user.send(attribute)
  628. end
  629. end
  630.  
  631. context "after clicking on 'Add New User'" do
  632. before do
  633. click_link "Add New User"
  634. end
  635.  
  636. it "can see #{role.humanize} role checkbox" do
  637. expect(page).to have_xpath "//input[@value='#{Role.find_by(name: role).id}']"
  638. end
  639.  
  640. context "after submitting valid user form", :js do
  641. let!(:previous_user_count) { User.count }
  642.  
  643. before do
  644. %w(last_name first_name phone address1 address2 zip_code city place_of_birth).each do |text_field|
  645. fill_in "user[#{text_field}]", with: text_field.capitalize.to_s
  646. end
  647.  
  648. fill_in "user[email]", with: "test_user@example.com"
  649. find("##{role}-role-button").click
  650. find("#gender_female").click
  651. find(:xpath, "//input[@name='user[date_of_birth]']").click
  652. find(:xpath, "//td[@data-day='1']").click
  653. click_button "Save"
  654. end
  655.  
  656. it "is redirected to next step for teachers and students and then to profile page" do
  657. created_user = User.last
  658. expect(find(".alert-success")).to have_content "User was successfully saved."
  659. expect(User.count).to eq previous_user_count + 1
  660.  
  661. if role == "student"
  662. expect(current_path).to eq edit_student_path(created_user.reload.student)
  663. expect(created_user.student).to_not be_nil
  664. find(".save-btn").click
  665. elsif role == "teacher"
  666. expect(current_path).to eq edit_teacher_path(created_user.reload.teacher)
  667. expect(created_user.teacher).to_not be_nil
  668. find(".save-btn").click
  669. elsif role == "caregiver"
  670. expect(created_user.caregiver).to_not be_nil
  671. end
  672.  
  673. expect(current_path).to eq user_path(created_user)
  674. end
  675. end
  676. end
  677.  
  678. context "after visiting user's profile page" do
  679. before do
  680. if additional_user.current_role_name == "student" || additional_user.current_role_name == "teacher"
  681. parent = additional_user.student || additional_user.teacher
  682. EmergencyContact.create(first_name: "first_name", last_name: "last_name", email: "email", phone: "phone", parent: parent)
  683. end
  684.  
  685. if additional_user.current_role_name == "student"
  686. relationship_type = RelationshipType.create(description: "Description")
  687. caregiver = create(:caregiver)
  688. StudentCaregiver.create(student: additional_user.student, caregiver: caregiver, relationship_type: relationship_type)
  689. end
  690.  
  691. visit user_path(additional_user)
  692. end
  693.  
  694. scenario "clicks on 'Edit Details' button" do
  695. click_on "Edit Details"
  696. expect(current_path).to eq edit_user_path(additional_user)
  697. end
  698.  
  699. it "can't see 'Delete' button" do
  700. expect(page).not_to have_button('Delete')
  701. end
  702.  
  703. it "sees the user's details", retry: 5 do
  704. %i(unique_id last_name first_name gender place_of_birth date_of_birth email phone address1 address2 zip_code city).each do |attr|
  705. expect(page).to have_content additional_user.send(attr)
  706. end
  707.  
  708. expect(page).to have_selector("img[src$='#{additional_user.avatar.square_600.url}']")
  709. additional_user.roles.each do |user_role|
  710. expect(page).to have_content user_role.name.humanize
  711. end
  712.  
  713. if additional_user.current_role_name == "student" || additional_user.current_role_name == "teacher"
  714. additional_user.current_role_from_name.emergency_contacts.each do |emergency_contact|
  715. %i(first_name last_name email phone).each do |attr|
  716. expect(page).to have_content emergency_contact.send(attr)
  717. end
  718. end
  719. end
  720.  
  721. if additional_user.current_role_name == "student"
  722. student_caregivers = additional_user.student.student_caregivers
  723. student_caregivers.each do |student_caregiver|
  724. expect(page).to have_link "#{student_caregiver.caregiver.first_name} #{student_caregiver.caregiver.last_name}", href: user_path(student_caregiver.caregiver.user)
  725. expect(page).to have_content student_caregiver.relationship_type.description
  726. end
  727. end
  728.  
  729. if additional_user.current_role_name == "caregiver"
  730. StudentCaregiver.create(
  731. caregiver: additional_user.caregiver,
  732. student: create(:student),
  733. relationship_type: RelationshipType.first
  734. )
  735. caregiver_students = additional_user.caregiver.students
  736. visit current_path
  737. expect(caregiver_students).to_not be_empty
  738. caregiver_students.each do |caregiver_student|
  739. expect(page).to have_link caregiver_student.name, href: user_path(caregiver_student.user)
  740. end
  741. end
  742. end
  743.  
  744. it "clicks on 'Edit Picture' button" do
  745. click_on "Edit Picture"
  746. expect(current_path).to eq avatar_upload_user_path(additional_user)
  747. end
  748. end
  749.  
  750. context "after visiting user edit page", :js do
  751. before do
  752. visit edit_user_path(additional_user)
  753. end
  754.  
  755. it "sees a gender option selected" do
  756. within ".form-group.gender-selection" do
  757. expect(find("label.btn.btn-default.active").text.downcase).to eq(additional_user.gender.downcase)
  758. end
  759. end
  760.  
  761. it "changes role" do
  762. expect(page).to have_css(".role_checkboxes")
  763. within(".role_checkboxes") do
  764. # All are roles that are accessible for this user.
  765. expect(all("input[type=checkbox]").length).to eq(Role.accessible_by(Ability.new(user)).count)
  766. # All are enabled.
  767. expect(all("input[type=checkbox][disabled]").length).to eq(0)
  768. end
  769. end
  770. end
  771.  
  772. context "after visiting 'Edit Profile Picture' page" do
  773. before do
  774. visit avatar_upload_user_path(additional_user)
  775. end
  776.  
  777. it "uploads and crops image", :js, retry: 5 do
  778. expect(page).to have_selector("#upload-panel", visible: true)
  779. expect(page).to have_selector("#crop-panel", visible: false)
  780. attach_file("user_avatar", "app/assets/images/dummy/square_600_dummy.png")
  781. expect(page).to have_selector("#crop-panel", visible: true)
  782. expect(find("#upload-panel", visible: false)).to_not be_visible
  783. expect(page).to have_selector("#user_avatar_crop_x", visible: false)
  784. expect(page).to have_selector('#user_avatar_crop_x[value="0"]', visible: false)
  785. find(".ord-w.jcrop-dragbar").drag_to(find(".ord-s.jcrop-handle"))
  786. expect(page).to_not have_selector('#user_avatar_crop_x[value="0"]', visible: false)
  787. crop_values = {}
  788. %i(x y w h).each do |c|
  789. crop_values[c] = find("#user_avatar_crop_#{c}", visible: false).value.to_i
  790. end
  791. click_button "Save"
  792. expect(page).to have_content("User was successfully saved.")
  793. expect(page).not_to have_selector("small", text: "Profile Picture Upload") # Need to wait for the title to disappear (new page loaded)
  794. additional_user.reload
  795. %i(x y w h).each do |c|
  796. expect(additional_user.send("avatar_crop_#{c}")).to eq crop_values[c]
  797. end
  798. end
  799.  
  800. it "selects image for upload and then decides to chose another picture", :js do
  801. attach_file("user_avatar", "app/assets/images/dummy/square_600_dummy.png")
  802. expect(page).to have_selector("#crop-panel", visible: true)
  803. expect(page).to have_selector(".jcrop-holder img")
  804. previous_image = all(".jcrop-holder img").first[:src]
  805. click_on "Chose a different picture"
  806. expect(page).to have_selector("#crop-panel", visible: false)
  807. attach_file("user_avatar", "app/assets/images/dummy/square_300_dummy.png")
  808. expect(page).to have_selector("#crop-panel", visible: true)
  809. expect(page).to have_selector(".jcrop-holder img")
  810. expect(all(".jcrop-holder img").first[:src]).to_not eq previous_image
  811. end
  812. end
  813.  
  814. context "after clicking on '#{role.pluralize.humanize}' tab", :js do
  815. let(:user_table_row_selector) do
  816. ".#{ActionView::RecordIdentifier.dom_id(additional_user)}"
  817. end
  818.  
  819. before do
  820. click_on_role_tab(additional_user, role.to_sym)
  821. app.users_index_page.wait_for_user_rows
  822. end
  823.  
  824. it "clicks 'Edit' button" do
  825. within user_table_row_selector do
  826. expect(page).to have_content "user4@example.com"
  827. click_link "Edit"
  828. end
  829. expect(app.users_edit_page).to be_displayed(id: additional_user.id)
  830. expect(current_path).to eq edit_user_path(additional_user)
  831. end
  832. end
  833.  
  834. if role == "student" || role == "teacher"
  835. context "after visiting #{role} edit page", :js do
  836. let!(:parent) { additional_user.send(role.to_s) }
  837.  
  838. it "can see the edit details button with an apporpriate text for teachers and students" do
  839. visit edit_user_path(additional_user)
  840. if role == "student"
  841. expect(find("#edit_btn")).to have_content "Add Caregiver & Emergency Contacts"
  842. elsif role == "teacher"
  843. expect(find("#edit_btn")).to have_content "Add Subjects & Emergency Contacts"
  844. end
  845. end
  846.  
  847. context "without existing emergency contact" do
  848. before do
  849. visit edit_user_path(additional_user)
  850. click_link "edit_btn"
  851. end
  852.  
  853. scenario "clicks on 'Add emergency contact' button 3 times and removes the last one" do
  854. expect(page).not_to have_css ".emergency-contact-panel"
  855. find(".add-emergency-contact-btn").click
  856. within ".emergency-contacts-accordion" do
  857. expect(page).to have_css ".emergency-contact-panel"
  858. expect(page).to have_css ".panel-info"
  859. end
  860.  
  861. expect(page).to have_content "New emergency contact #1"
  862. %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
  863. expect(page).to have_css "##{role}_emergency_contacts_attributes_0_#{text_field}"
  864. end
  865.  
  866. 2.times do |index|
  867. find(".add-emergency-contact-btn").click
  868. expect(page).to have_content "New emergency contact ##{index + 2}"
  869. %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
  870. expect(page).to have_css "##{role}_emergency_contacts_attributes_#{index + 1}_#{text_field}"
  871. expect(page).to have_field "#{role}[emergency_contacts_attributes][#{index + 1}][#{text_field}]"
  872. end
  873. end
  874.  
  875. all(".delete-emergency-contact")[2].click
  876. %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
  877. expect(page).not_to have_css "##{role}_emergency_contacts_attributes_2_#{text_field}"
  878. expect(page).not_to have_field "#{role}[emergency_contacts_attributes][2][#{text_field}]"
  879. end
  880.  
  881. expect(page).not_to have_content "New emergency contact #3"
  882.  
  883. title = "New emergency contact #1"
  884. expect(page).to have_content title
  885. click_link(title)
  886. %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
  887. expect(page).to have_css "##{role}_emergency_contacts_attributes_0_#{text_field}"
  888. expect(page).to have_field "#{role}[emergency_contacts_attributes][0][#{text_field}]"
  889. end
  890. end
  891.  
  892. scenario "creates emergency contact" do
  893. find(".add-emergency-contact-btn").click
  894. panel = find(".emergency-contact-panel")
  895.  
  896. within panel do
  897. %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
  898. fill_in("#{role}[emergency_contacts_attributes][0][#{text_field}]", with: text_field)
  899. end
  900.  
  901. fill_in("#{role}[emergency_contacts_attributes][0][email]", with: "valid@email.com")
  902. end
  903.  
  904. expect { find(".save-btn").click }.to change { EmergencyContact.count }.from(0).to(1)
  905. expect(current_path).to eq user_path(additional_user)
  906. expect(page).to have_content "#{role.humanize} successfully saved."
  907. end
  908.  
  909. scenario "submits empty emergency contact form" do
  910. find(".add-emergency-contact-btn").click
  911. find(".save-btn").click
  912. expect(all(".has-error").count).to eq 2 # first_name and last_name are mandatory
  913. expect(page).to have_css ".panel-danger"
  914. expect(page).to have_content "This field is required"
  915. end
  916.  
  917. scenario "submits malformed emergency contact email" do
  918. find(".add-emergency-contact-btn").click
  919. within find(".emergency-contact-panel") do
  920. %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
  921. fill_in("#{role}[emergency_contacts_attributes][0][#{text_field}]", with: text_field)
  922. end
  923.  
  924. fill_in("#{role}[emergency_contacts_attributes][0][email]", with: "badEmail.com")
  925. end
  926.  
  927. find(".save-btn").click
  928. expect(page).to have_content "first_name last_name"
  929. expect(all(".has-error").count).to eq 1
  930. expect(page).to have_css ".panel-danger"
  931. expect(page).to have_content "is invalid"
  932. end
  933. end
  934.  
  935. case role
  936. when "teacher"
  937. context "without existing subjects added to teacher" do
  938. let!(:subjects) { Array.new(5) { create :subject } }
  939. let(:subject_1) { "Subject 1" }
  940. let(:subject_2) { "Subject 2" }
  941. let(:subject_3) { "Subject 3" }
  942.  
  943. before do
  944. visit edit_user_path(additional_user)
  945. click_link "edit_btn"
  946. end
  947.  
  948. scenario "clicks on 'Add subject' button 3 times and removes the second one" do
  949. expect(page).not_to have_css ".subject-form"
  950. find(".add-subject-btn").click
  951. select subject_1, from: "subjects-select"
  952. find(".attach-subject-btn").click
  953.  
  954. within ".teacher-subjects-accordion" do
  955. expect(page).to have_css ".subject-panel"
  956. expect(page).to have_css ".panel-info"
  957. end
  958.  
  959. expect(page).to have_selector(".subject-panel .panel-title", text: subject_1)
  960.  
  961. find(".add-subject-btn").click
  962. select subject_2, from: "subjects-select"
  963. find(".attach-subject-btn").click
  964. expect(page).to have_selector(".subject-panel .panel-title", text: subject_2)
  965.  
  966. find(".add-subject-btn").click
  967. select subject_3, from: "subjects-select"
  968. find(".attach-subject-btn").click
  969. expect(page).to have_selector(".subject-panel .panel-title", text: subject_3)
  970.  
  971. all(".delete-subject")[1].click
  972. expect(page).not_to have_selector(".subject-panel .panel-title", text: subject_2)
  973. expect(page).to have_selector(".subject-panel .panel-title", text: subject_1)
  974. expect(page).to have_selector(".subject-panel .panel-title", text: subject_3)
  975. end
  976.  
  977. scenario "Saving subjects" do
  978. find(".add-subject-btn").click
  979. select subject_1, from: "subjects-select"
  980. find(".attach-subject-btn").click
  981. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  982. check_box.set(true)
  983.  
  984. find(".add-subject-btn").click
  985. select subject_2, from: "subjects-select"
  986. find(".attach-subject-btn").click
  987. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  988. check_box.set(true)
  989.  
  990. find(".add-subject-btn").click
  991. select subject_3, from: "subjects-select"
  992. find(".attach-subject-btn").click
  993. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_3).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  994. check_box.set(true)
  995.  
  996. expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(3)
  997. expect(page).to have_content "Teacher successfully saved"
  998. end
  999.  
  1000. scenario "Saving subject without grade_level" do
  1001. find(".add-subject-btn").click
  1002. select subject_1, from: "subjects-select"
  1003. find(".attach-subject-btn").click
  1004.  
  1005. expect { click_button "Save" }.to change { parent.reload.subjects.count }.by(1)
  1006. expect(page).to have_content "Teacher successfully saved"
  1007. end
  1008.  
  1009. scenario "Deleting subjects" do
  1010. find(".add-subject-btn").click
  1011. select subject_1, from: "subjects-select"
  1012. find(".attach-subject-btn").click
  1013. check "#{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}"
  1014.  
  1015. find(".add-subject-btn").click
  1016. select subject_2, from: "subjects-select"
  1017. find(".attach-subject-btn").click
  1018. check "#{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}"
  1019.  
  1020. expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(2)
  1021. visit edit_user_path(additional_user)
  1022. click_link "edit_btn"
  1023.  
  1024. all(".delete-subject")[0].click
  1025. sleep(1)
  1026. expect(current_path).to eq edit_teacher_path(additional_user.teacher)
  1027. expect(all(".subject-panel").count).to eq 1
  1028. end
  1029.  
  1030. scenario "Saving new subjects with existing subjects" do
  1031. find(".add-subject-btn").click
  1032. select subject_1, from: "subjects-select"
  1033. find(".attach-subject-btn").click
  1034. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  1035. check_box.set(true)
  1036.  
  1037. find(".add-subject-btn").click
  1038. select subject_2, from: "subjects-select"
  1039. find(".attach-subject-btn").click
  1040. check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
  1041. check_box.set(true)
  1042.  
  1043. expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(2)
  1044. end
  1045. end
  1046.  
  1047. when "student"
  1048. context "Visits student edit page to manipulate caregivers" do
  1049. let(:student) { create(:student) }
  1050.  
  1051. before do
  1052. 4.times { create(:caregiver) }
  1053. visit edit_student_path(student)
  1054. end
  1055.  
  1056. def create_typeahead_results(count, query = "Joh")
  1057. (1..count).each { |i| typeahead_and_click_result("#caregiver-search", query, i) }
  1058. expect { find(:css, ".save-btn").click }.to change {
  1059. StudentCaregiver.count
  1060. }.from(0).to(count)
  1061. end
  1062.  
  1063. scenario "Creates 2 student caregivers", :js do
  1064. create_typeahead_results(2)
  1065. end
  1066.  
  1067. scenario "Tries to add same caregiver 2 times", :js do
  1068. 2.times { |_i| typeahead_and_click_result("#caregiver-search", "Joh", 1) }
  1069. expect { find(:css, ".save-btn").click }.to change { StudentCaregiver.count }.from(0).to(1)
  1070. end
  1071.  
  1072. scenario "Creates 2 student caregivers, then deletes one", :js do
  1073. create_typeahead_results(2)
  1074. visit edit_student_path(student)
  1075. expect(page).to have_selector(".remove-caregiver")
  1076. first(".remove-caregiver").click
  1077. expect { find(:css, ".save-btn").click }.to change { StudentCaregiver.count }.from(2).to(1)
  1078. end
  1079.  
  1080. scenario "Adds a caregiver, removes it and then hits save", :js do
  1081. typeahead_and_click_result("#caregiver-search", "Joh", 1)
  1082. expect(page).to have_selector(".remove-caregiver")
  1083. first(".remove-caregiver").click
  1084. expect { find(:css, ".save-btn").click }.not_to change { StudentCaregiver.count }
  1085. end
  1086.  
  1087. scenario "Creates 2 student caregivers then edits their relationship types", :js do
  1088. create_typeahead_results(2)
  1089. visit edit_student_path(student)
  1090.  
  1091. 2.times do |i| # i is 0 first
  1092. select "Others", from: "student_student_caregivers_attributes_#{i}_relationship_type_id"
  1093. end
  1094.  
  1095. expect { find(:css, ".save-btn").click }.to change {
  1096. StudentCaregiver.joins(:relationship_type).pluck(:description)
  1097. }.from(%w(Father Father)).to(%w(Others Others))
  1098. end
  1099. end
  1100. end
  1101.  
  1102. context "with existing emergency contact" do
  1103. let!(:emergency_contact) { create(:emergency_contact, first_name: "first_name", last_name: "last_name", email: "email@email.com", phone: "phone", parent: parent) }
  1104.  
  1105. before do
  1106. visit edit_user_path(additional_user)
  1107. click_link "edit_btn"
  1108. end
  1109.  
  1110. it "edits existing record", :js, retry: 2 do
  1111. attributes = %i(last_name first_name email phone address1 address2 zip_code city relation)
  1112. new_values = %w(last first lf@www.com 123 avenue 5th 0000 neverland father)
  1113. expect(parent.emergency_contacts.count).to eq 1
  1114. expect(page).to have_content "first_name last_name"
  1115.  
  1116. find(".title-link").click
  1117. within ".emergency-contact-panel" do
  1118. all("input[type=text]").each_with_index do |field, index|
  1119. expect(field.value).to eq emergency_contact.send(attributes[index])
  1120. field.set new_values[index]
  1121. end
  1122. end
  1123.  
  1124. find(".save-btn").click
  1125. expect(current_path).to eq user_path(additional_user)
  1126. expect(page).to have_content "#{role.humanize} successfully saved."
  1127. expect(parent.emergency_contacts.count).to eq 1
  1128. visit edit_user_path(additional_user)
  1129. click_link "edit_btn"
  1130.  
  1131. attributes.each_with_index do |attribute, index|
  1132. expect(parent.emergency_contacts.first.send(attribute)).to eq new_values[index]
  1133. end
  1134.  
  1135. expect(all(".emergency-contact-panel").count).to eq 1
  1136. end
  1137.  
  1138. it "deletes existing record" do
  1139. find(".delete-emergency-contact").click
  1140. sleep(1)
  1141. expect(parent.emergency_contacts.count).to eq 0
  1142. end
  1143. end
  1144. end
  1145. end
  1146. end
  1147.  
  1148. shared_examples "user who cannot manage #{role} accounts" do
  1149. before do
  1150. visit users_path
  1151. end
  1152.  
  1153. it "cannot see '#{role.pluralize.humanize}' tab" do
  1154. within ".nav-tabs" do
  1155. expect(page).not_to have_link role.pluralize.humanize
  1156. end
  1157. end
  1158.  
  1159. it "cannot destroy and update profile of #{role} user" do
  1160. additional_user = create(:user, role.to_sym)
  1161. visit user_path(additional_user)
  1162. expect(current_path).to eq user_path(additional_user)
  1163. expect(page).not_to have_css ".edit-details-btn"
  1164. expect(page).not_to have_css ".edit-picture-btn"
  1165. expect(page).not_to have_css ".delete-btn"
  1166. end
  1167. end
  1168.  
  1169. shared_examples "user who can only read #{role} accounts" do
  1170. before do
  1171. visit users_path
  1172. end
  1173.  
  1174. it "can see '#{role.pluralize.humanize}' tab" do
  1175. within ".nav-tabs" do
  1176. expect(page).to have_link role.pluralize.humanize
  1177. end
  1178. end
  1179.  
  1180. it "can access profile page of #{role} user" do
  1181. additional_user = create(:user, role.to_sym)
  1182. visit user_path(additional_user)
  1183. expect(current_path).to eq user_path(additional_user)
  1184. end
  1185. end
  1186. end
  1187.  
  1188. shared_examples "user who cannot add new user" do
  1189. before do
  1190. visit users_path
  1191. end
  1192.  
  1193. it "cannot see 'Add New User' link" do
  1194. expect(page).not_to have_link "Add New User"
  1195. end
  1196. end
  1197.  
  1198. def click_on_role_tab(additional_user, role, grade_level = nil)
  1199. within ".roles" do
  1200. click_link role.to_s.pluralize.humanize
  1201. end
  1202.  
  1203. if additional_user.current_role_name == "student"
  1204. gl = grade_level || additional_user.student.current_grade_level
  1205.  
  1206. within ".grade_levels" do
  1207. if gl
  1208. selector = "." + ActionView::RecordIdentifier.dom_id(gl)
  1209. find(selector).click
  1210. else
  1211. app.users_index_page.no_grade_level_tab.click
  1212. end
  1213. end
  1214. end
  1215. end
  1216.  
  1217. def user_ability
  1218. Ability.new(user)
  1219. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement