Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shared_examples "user without access to user management" do
- it "cannot access the user management page" do
- visit users_path
- expect(page).to have_content "You are not authorized to access this page."
- end
- end
- Role::NAMES.each do |role|
- shared_examples "user who can manage #{role} accounts" do
- # set the name of the additional user to the alphabetically first one,
- # so that the sortable table won't change the position
- # and wreck the test
- let!(:additional_user) { create(:user, role.to_sym) }
- before do
- if additional_user.current_role_name == "student"
- additional_user.student = create(:student, user: additional_user)
- term_class.students << additional_user.student
- elsif additional_user.current_role_name == "teacher"
- additional_user.teacher = create(:teacher, user: additional_user)
- elsif additional_user.current_role_name == "caregiver"
- additional_user.caregiver = create(:caregiver, user: additional_user)
- end
- visit users_path
- end
- it "can see '#{role.pluralize.humanize}' tab" do
- within ".nav-tabs" do
- expect(page).to have_link role.humanize
- end
- end
- scenario "clicks on '#{role.pluralize.humanize}' tab", :js do
- click_on_role_tab(additional_user, role.to_sym)
- expect(page).to have_selector("img[src$='#{additional_user.avatar.square_32.url}']")
- %i(first_name last_name email unique_id).each do |attribute|
- expect(page).to have_content additional_user.send(attribute)
- end
- end
- context "after clicking on 'Add New User'" do
- before do
- click_link "Add New User"
- end
- it "can see #{role.humanize} role checkbox" do
- expect(page).to have_xpath "//input[@value='#{Role.find_by(name: role).id}']"
- end
- context "after submitting valid user form", :js do
- let!(:previous_user_count) { User.count }
- before do
- %w(last_name first_name phone address1 address2 zip_code city place_of_birth).each do |text_field|
- fill_in "user[#{text_field}]", with: text_field.capitalize.to_s
- end
- fill_in "user[email]", with: "test_user@example.com"
- find("##{role}-role-button").click
- find("#gender_female").click
- find(:xpath, "//input[@name='user[date_of_birth]']").click
- find(:xpath, "//td[@data-day='1']").click
- click_button "Save"
- end
- it "is redirected to next step for teachers and students and then to profile page" do
- created_user = User.last
- expect(find(".alert-success")).to have_content "User was successfully saved."
- expect(User.count).to eq previous_user_count + 1
- if role == "student"
- expect(current_path).to eq edit_student_path(created_user.reload.student)
- expect(created_user.student).to_not be_nil
- find(".save-btn").click
- elsif role == "teacher"
- expect(current_path).to eq edit_teacher_path(created_user.reload.teacher)
- expect(created_user.teacher).to_not be_nil
- find(".save-btn").click
- elsif role == "caregiver"
- expect(created_user.caregiver).to_not be_nil
- end
- expect(current_path).to eq user_path(created_user)
- end
- end
- end
- context "after visiting user's profile page" do
- before do
- if additional_user.current_role_name == "student" || additional_user.current_role_name == "teacher"
- parent = additional_user.student || additional_user.teacher
- EmergencyContact.create(first_name: "first_name", last_name: "last_name", email: "email", phone: "phone", parent: parent)
- end
- if additional_user.current_role_name == "student"
- relationship_type = RelationshipType.create(description: "Description")
- caregiver = create(:caregiver)
- StudentCaregiver.create(student: additional_user.student, caregiver: caregiver, relationship_type: relationship_type)
- end
- visit user_path(additional_user)
- end
- scenario "clicks on 'Edit Details' button" do
- click_on "Edit Details"
- expect(current_path).to eq edit_user_path(additional_user)
- end
- scenario "clicks on 'Delete' button", :js, retry: 5 do
- expect { click_on "Delete" }.to change { User.count }.by(-1)
- expect(current_path).to eq users_path
- expect(page).to have_content "User was successfully deleted"
- end
- if role == "teacher"
- scenario "cannot delete a headroom teacher" do
- create :grade_level_class, headroom_teacher: additional_user.teacher
- expect { click_on "Delete" }.to change { User.count }.by(0)
- expect(current_path).to match(/\/users\/.*/)
- expect(page).to have_content "Teacher is currently headroom teacher of"
- end
- scenario "cannot delete a course teacher" do
- create :course, teacher: additional_user.teacher
- expect { click_on "Delete" }.to change { User.count }.by(0)
- expect(current_path).to match(/\/users\/.*/)
- expect(page).to have_content "Teacher is currently teaching 1 course. Please replace the teacher of this course before."
- end
- end
- it "sees the user's details", retry: 5 do
- %i(unique_id last_name first_name gender place_of_birth date_of_birth email phone address1 address2 zip_code city).each do |attr|
- expect(page).to have_content additional_user.send(attr)
- end
- expect(page).to have_selector("img[src$='#{additional_user.avatar.square_600.url}']")
- additional_user.roles.each do |user_role|
- expect(page).to have_content user_role.name.humanize
- end
- if additional_user.current_role_name == "student" || additional_user.current_role_name == "teacher"
- additional_user.current_role_from_name.emergency_contacts.each do |emergency_contact|
- %i(first_name last_name email phone).each do |attr|
- expect(page).to have_content emergency_contact.send(attr)
- end
- end
- end
- if additional_user.current_role_name == "student"
- student_caregivers = additional_user.student.student_caregivers
- student_caregivers.each do |student_caregiver|
- expect(page).to have_link "#{student_caregiver.caregiver.first_name} #{student_caregiver.caregiver.last_name}", href: user_path(student_caregiver.caregiver.user)
- expect(page).to have_content student_caregiver.relationship_type.description
- end
- end
- if additional_user.current_role_name == "caregiver"
- StudentCaregiver.create(
- caregiver: additional_user.caregiver,
- student: create(:student),
- relationship_type: RelationshipType.first
- )
- caregiver_students = additional_user.caregiver.students
- visit current_path
- expect(caregiver_students).to_not be_empty
- caregiver_students.each do |caregiver_student|
- expect(page).to have_link caregiver_student.name, href: user_path(caregiver_student.user)
- end
- end
- end
- it "clicks on 'Edit Picture' button" do
- click_on "Edit Picture"
- expect(current_path).to eq avatar_upload_user_path(additional_user)
- end
- end
- context "after visiting user edit page", :js do
- before do
- visit edit_user_path(additional_user)
- end
- it "sees a gender option selected" do
- within ".form-group.gender-selection" do
- expect(find("label.btn.btn-default.active").text.downcase).to eq(additional_user.gender.downcase)
- end
- end
- it "changes role" do
- expect(page).to have_css(".role_checkboxes")
- within(".role_checkboxes") do
- # All are roles that are accessible for this user.
- expect(all("input[type=checkbox]").length).to eq(Role.accessible_by(Ability.new(user)).count)
- # All are enabled.
- expect(all("input[type=checkbox][disabled]").length).to eq(0)
- end
- end
- end
- context "after visiting 'Edit Profile Picture' page" do
- before do
- visit avatar_upload_user_path(additional_user)
- end
- it "uploads and crops image", :js, retry: 5 do
- expect(page).to have_selector("#upload-panel", visible: true)
- expect(page).to have_selector("#crop-panel", visible: false)
- attach_file("user_avatar", "app/assets/images/dummy/square_600_dummy.png")
- expect(page).to have_selector("#crop-panel", visible: true)
- expect(find("#upload-panel", visible: false)).to_not be_visible
- expect(page).to have_selector("#user_avatar_crop_x", visible: false)
- expect(page).to have_selector('#user_avatar_crop_x[value="0"]', visible: false)
- find(".ord-w.jcrop-dragbar").drag_to(find(".ord-s.jcrop-handle"))
- expect(page).to_not have_selector('#user_avatar_crop_x[value="0"]', visible: false)
- crop_values = {}
- %i(x y w h).each do |c|
- crop_values[c] = find("#user_avatar_crop_#{c}", visible: false).value.to_i
- end
- click_button "Save"
- expect(page).to have_content("User was successfully saved.")
- expect(page).not_to have_selector("small", text: "Profile Picture Upload") # Need to wait for the title to disappear (new page loaded)
- additional_user.reload
- %i(x y w h).each do |c|
- expect(additional_user.send("avatar_crop_#{c}")).to eq crop_values[c]
- end
- end
- it "selects image for upload and then decides to chose another picture", :js do
- attach_file("user_avatar", "app/assets/images/dummy/square_600_dummy.png")
- expect(page).to have_selector("#crop-panel", visible: true)
- expect(page).to have_selector(".jcrop-holder img")
- previous_image = all(".jcrop-holder img").first[:src]
- click_on "Chose a different picture"
- expect(page).to have_selector("#crop-panel", visible: false)
- attach_file("user_avatar", "app/assets/images/dummy/square_300_dummy.png")
- expect(page).to have_selector("#crop-panel", visible: true)
- expect(page).to have_selector(".jcrop-holder img")
- expect(all(".jcrop-holder img").first[:src]).to_not eq previous_image
- end
- end
- context "after clicking on '#{role.pluralize.humanize}' tab", :js do
- let(:user_table_row_selector) do
- ".#{ActionView::RecordIdentifier.dom_id(additional_user)}"
- end
- before do
- click_on_role_tab(additional_user, role.to_sym)
- app.users_index_page.wait_for_user_rows
- end
- it "clicks 'Edit' button" do
- within user_table_row_selector do
- expect(page).to have_content "user4@example.com"
- click_link "Edit"
- end
- expect(app.users_edit_page).to be_displayed(id: additional_user.id)
- expect(current_path).to eq edit_user_path(additional_user)
- end
- it "clicks 'Delete' button" do
- user_row = app.users_index_page.user_row_for_user(additional_user)
- if role == "teacher"
- # FIXME: Cannot get this test working (by Can)
- # expect { delete_button.click }.to change{ User.count }.by(0)
- # app.users_index_page.wait_for_flash_error
- # expect(app.users_index_page).to have_flash_error text: "Teacher is currently headroom teacher of"
- # expect(app.users_index_page).to have_flash_error text: "Teacher is currently teaching #{additional_user.teacher.courses.count} courses"
- else
- if role == "student"
- previous_role_users_count = additional_user.student.current_grade_level.students.count
- else
- previous_role_users_count = additional_user.current_role.users.count
- end
- expect(app.users_index_page).to have_user_rows count: previous_role_users_count
- previous_users_count = User.count
- user_row.delete_button.click
- expect(app.users_index_page).to have_user_rows count: previous_role_users_count - 1
- expect(User.count).to eq previous_users_count - 1
- end
- end
- end
- if role == "student" || role == "teacher"
- context "after visiting #{role} edit page", :js do
- let!(:parent) { additional_user.send(role.to_s) }
- it "can see the edit details button with an apporpriate text for teachers and students" do
- visit edit_user_path(additional_user)
- if role == "student"
- expect(find("#edit_btn")).to have_content "Add Caregiver & Emergency Contacts"
- elsif role == "teacher"
- expect(find("#edit_btn")).to have_content "Add Subjects & Emergency Contacts"
- end
- end
- context "without existing emergency contact" do
- before do
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- end
- scenario "clicks on 'Add emergency contact' button 3 times and removes the last one" do
- expect(page).not_to have_css ".emergency-contact-panel"
- find(".add-emergency-contact-btn").click
- within ".emergency-contacts-accordion" do
- expect(page).to have_css ".emergency-contact-panel"
- expect(page).to have_css ".panel-info"
- end
- expect(page).to have_content "New emergency contact #1"
- %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
- expect(page).to have_css "##{role}_emergency_contacts_attributes_0_#{text_field}"
- end
- 2.times do |index|
- find(".add-emergency-contact-btn").click
- expect(page).to have_content "New emergency contact ##{index + 2}"
- %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
- expect(page).to have_css "##{role}_emergency_contacts_attributes_#{index + 1}_#{text_field}"
- expect(page).to have_field "#{role}[emergency_contacts_attributes][#{index + 1}][#{text_field}]"
- end
- end
- all(".delete-emergency-contact")[2].click
- %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
- expect(page).not_to have_css "##{role}_emergency_contacts_attributes_2_#{text_field}"
- expect(page).not_to have_field "#{role}[emergency_contacts_attributes][2][#{text_field}]"
- end
- expect(page).not_to have_content "New emergency contact #3"
- title = "New emergency contact #1"
- expect(page).to have_content title
- click_link(title)
- %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
- expect(page).to have_css "##{role}_emergency_contacts_attributes_0_#{text_field}"
- expect(page).to have_field "#{role}[emergency_contacts_attributes][0][#{text_field}]"
- end
- end
- scenario "creates emergency contact" do
- find(".add-emergency-contact-btn").click
- panel = find(".emergency-contact-panel")
- within panel do
- %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
- fill_in("#{role}[emergency_contacts_attributes][0][#{text_field}]", with: text_field)
- end
- fill_in("#{role}[emergency_contacts_attributes][0][email]", with: "valid@email.com")
- end
- expect { find(".save-btn").click }.to change { EmergencyContact.count }.from(0).to(1)
- expect(current_path).to eq user_path(additional_user)
- expect(page).to have_content "#{role.humanize} successfully saved."
- end
- scenario "submits empty emergency contact form" do
- find(".add-emergency-contact-btn").click
- find(".save-btn").click
- expect(all(".has-error").count).to eq 2 # first_name and last_name are mandatory
- expect(page).to have_css ".panel-danger"
- expect(page).to have_content "This field is required"
- end
- scenario "submits malformed emergency contact email" do
- find(".add-emergency-contact-btn").click
- within find(".emergency-contact-panel") do
- %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
- fill_in("#{role}[emergency_contacts_attributes][0][#{text_field}]", with: text_field)
- end
- fill_in("#{role}[emergency_contacts_attributes][0][email]", with: "badEmail.com")
- end
- find(".save-btn").click
- expect(page).to have_content "first_name last_name"
- expect(all(".has-error").count).to eq 1
- expect(page).to have_css ".panel-danger"
- expect(page).to have_content "is invalid"
- end
- end
- case role
- when "teacher"
- context "without existing subjects added to teacher" do
- let!(:subjects) { Array.new(5) { create :subject } }
- let(:subject_1) { "Subject 1" }
- let(:subject_2) { "Subject 2" }
- let(:subject_3) { "Subject 3" }
- before do
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- end
- scenario "clicks on 'Add subject' button 3 times and removes the second one" do
- expect(page).not_to have_css ".subject-form"
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- within ".teacher-subjects-accordion" do
- expect(page).to have_css ".subject-panel"
- expect(page).to have_css ".panel-info"
- end
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_1)
- find(".add-subject-btn").click
- select subject_2, from: "subjects-select"
- find(".attach-subject-btn").click
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_2)
- find(".add-subject-btn").click
- select subject_3, from: "subjects-select"
- find(".attach-subject-btn").click
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_3)
- all(".delete-subject")[1].click
- expect(page).not_to have_selector(".subject-panel .panel-title", text: subject_2)
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_1)
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_3)
- end
- scenario "Saving subjects" do
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- find(".add-subject-btn").click
- select subject_2, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- find(".add-subject-btn").click
- select subject_3, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_3).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(3)
- expect(page).to have_content "Teacher successfully saved"
- end
- scenario "Saving subject without grade_level" do
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- expect { click_button "Save" }.to change { parent.reload.subjects.count }.by(1)
- expect(page).to have_content "Teacher successfully saved"
- end
- scenario "Deleting subjects" do
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- check "#{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}"
- find(".add-subject-btn").click
- select subject_2, from: "subjects-select"
- find(".attach-subject-btn").click
- check "#{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}"
- expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(2)
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- all(".delete-subject")[0].click
- sleep(1)
- expect(current_path).to eq edit_teacher_path(additional_user.teacher)
- expect(all(".subject-panel").count).to eq 1
- end
- scenario "Saving new subjects with existing subjects" do
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- find(".add-subject-btn").click
- select subject_2, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(2)
- end
- end
- when "student"
- context "Visits student edit page to manipulate caregivers" do
- let(:student) { create(:student) }
- before do
- 4.times { create(:caregiver) }
- visit edit_student_path(student)
- end
- def create_typeahead_results(count, query = "Joh")
- (1..count).each { |i| typeahead_and_click_result("#caregiver-search", query, i) }
- expect { find(:css, ".save-btn").click }.to change {
- StudentCaregiver.count
- }.from(0).to(count)
- end
- scenario "Creates 2 student caregivers", :js do
- create_typeahead_results(2)
- end
- scenario "Tries to add same caregiver 2 times", :js do
- 2.times { |_i| typeahead_and_click_result("#caregiver-search", "Joh", 1) }
- expect { find(:css, ".save-btn").click }.to change { StudentCaregiver.count }.from(0).to(1)
- end
- scenario "Creates 2 student caregivers, then deletes one", :js do
- create_typeahead_results(2)
- visit edit_student_path(student)
- expect(page).to have_selector(".remove-caregiver")
- first(".remove-caregiver").click
- expect { find(:css, ".save-btn").click }.to change { StudentCaregiver.count }.from(2).to(1)
- end
- scenario "Adds a caregiver, removes it and then hits save", :js do
- typeahead_and_click_result("#caregiver-search", "Joh", 1)
- expect(page).to have_selector(".remove-caregiver")
- first(".remove-caregiver").click
- expect { find(:css, ".save-btn").click }.not_to change { StudentCaregiver.count }
- end
- scenario "Creates 2 student caregivers then edits their relationship types", :js do
- create_typeahead_results(2)
- visit edit_student_path(student)
- 2.times do |i| # i is 0 first
- select "Others", from: "student_student_caregivers_attributes_#{i}_relationship_type_id"
- end
- expect { find(:css, ".save-btn").click }.to change {
- StudentCaregiver.joins(:relationship_type).pluck(:description)
- }.from(%w(Father Father)).to(%w(Others Others))
- end
- end
- end
- context "with existing emergency contact" do
- let!(:emergency_contact) { create(:emergency_contact, first_name: "first_name", last_name: "last_name", email: "email@email.com", phone: "phone", parent: parent) }
- before do
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- end
- it "edits existing record", :js, retry: 2 do
- attributes = %i(last_name first_name email phone address1 address2 zip_code city relation)
- new_values = %w(last first lf@www.com 123 avenue 5th 0000 neverland father)
- expect(parent.emergency_contacts.count).to eq 1
- expect(page).to have_content "first_name last_name"
- find(".title-link").click
- within ".emergency-contact-panel" do
- all("input[type=text]").each_with_index do |field, index|
- expect(field.value).to eq emergency_contact.send(attributes[index])
- field.set new_values[index]
- end
- end
- find(".save-btn").click
- expect(current_path).to eq user_path(additional_user)
- expect(page).to have_content "#{role.humanize} successfully saved."
- expect(parent.emergency_contacts.count).to eq 1
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- attributes.each_with_index do |attribute, index|
- expect(parent.emergency_contacts.first.send(attribute)).to eq new_values[index]
- end
- expect(all(".emergency-contact-panel").count).to eq 1
- end
- it "deletes existing record" do
- find(".delete-emergency-contact").click
- sleep(1)
- expect(parent.emergency_contacts.count).to eq 0
- end
- end
- end
- end
- end
- shared_examples "user who can manage without delete #{role} accounts" do
- # set the name of the additional user to the alphabetically first one,
- # so that the sortable table won't change the position
- # and wreck the test
- let!(:additional_user) { create(:user, role.to_sym) }
- before do
- if additional_user.current_role_name == "student"
- additional_user.student = create(:student, user: additional_user)
- term_class.students << additional_user.student
- elsif additional_user.current_role_name == "teacher"
- additional_user.teacher = create(:teacher, user: additional_user)
- elsif additional_user.current_role_name == "caregiver"
- additional_user.caregiver = create(:caregiver, user: additional_user)
- end
- visit users_path
- end
- it "can see '#{role.pluralize.humanize}' tab" do
- within ".nav-tabs" do
- expect(page).to have_link role.humanize
- end
- end
- scenario "clicks on '#{role.pluralize.humanize}' tab", :js do
- click_on_role_tab(additional_user, role.to_sym)
- expect(page).to have_selector("img[src$='#{additional_user.avatar.square_32.url}']")
- %i(first_name last_name email unique_id).each do |attribute|
- expect(page).to have_content additional_user.send(attribute)
- end
- end
- context "after clicking on 'Add New User'" do
- before do
- click_link "Add New User"
- end
- it "can see #{role.humanize} role checkbox" do
- expect(page).to have_xpath "//input[@value='#{Role.find_by(name: role).id}']"
- end
- context "after submitting valid user form", :js do
- let!(:previous_user_count) { User.count }
- before do
- %w(last_name first_name phone address1 address2 zip_code city place_of_birth).each do |text_field|
- fill_in "user[#{text_field}]", with: text_field.capitalize.to_s
- end
- fill_in "user[email]", with: "test_user@example.com"
- find("##{role}-role-button").click
- find("#gender_female").click
- find(:xpath, "//input[@name='user[date_of_birth]']").click
- find(:xpath, "//td[@data-day='1']").click
- click_button "Save"
- end
- it "is redirected to next step for teachers and students and then to profile page" do
- created_user = User.last
- expect(find(".alert-success")).to have_content "User was successfully saved."
- expect(User.count).to eq previous_user_count + 1
- if role == "student"
- expect(current_path).to eq edit_student_path(created_user.reload.student)
- expect(created_user.student).to_not be_nil
- find(".save-btn").click
- elsif role == "teacher"
- expect(current_path).to eq edit_teacher_path(created_user.reload.teacher)
- expect(created_user.teacher).to_not be_nil
- find(".save-btn").click
- elsif role == "caregiver"
- expect(created_user.caregiver).to_not be_nil
- end
- expect(current_path).to eq user_path(created_user)
- end
- end
- end
- context "after visiting user's profile page" do
- before do
- if additional_user.current_role_name == "student" || additional_user.current_role_name == "teacher"
- parent = additional_user.student || additional_user.teacher
- EmergencyContact.create(first_name: "first_name", last_name: "last_name", email: "email", phone: "phone", parent: parent)
- end
- if additional_user.current_role_name == "student"
- relationship_type = RelationshipType.create(description: "Description")
- caregiver = create(:caregiver)
- StudentCaregiver.create(student: additional_user.student, caregiver: caregiver, relationship_type: relationship_type)
- end
- visit user_path(additional_user)
- end
- scenario "clicks on 'Edit Details' button" do
- click_on "Edit Details"
- expect(current_path).to eq edit_user_path(additional_user)
- end
- it "can't see 'Delete' button" do
- expect(page).not_to have_button('Delete')
- end
- it "sees the user's details", retry: 5 do
- %i(unique_id last_name first_name gender place_of_birth date_of_birth email phone address1 address2 zip_code city).each do |attr|
- expect(page).to have_content additional_user.send(attr)
- end
- expect(page).to have_selector("img[src$='#{additional_user.avatar.square_600.url}']")
- additional_user.roles.each do |user_role|
- expect(page).to have_content user_role.name.humanize
- end
- if additional_user.current_role_name == "student" || additional_user.current_role_name == "teacher"
- additional_user.current_role_from_name.emergency_contacts.each do |emergency_contact|
- %i(first_name last_name email phone).each do |attr|
- expect(page).to have_content emergency_contact.send(attr)
- end
- end
- end
- if additional_user.current_role_name == "student"
- student_caregivers = additional_user.student.student_caregivers
- student_caregivers.each do |student_caregiver|
- expect(page).to have_link "#{student_caregiver.caregiver.first_name} #{student_caregiver.caregiver.last_name}", href: user_path(student_caregiver.caregiver.user)
- expect(page).to have_content student_caregiver.relationship_type.description
- end
- end
- if additional_user.current_role_name == "caregiver"
- StudentCaregiver.create(
- caregiver: additional_user.caregiver,
- student: create(:student),
- relationship_type: RelationshipType.first
- )
- caregiver_students = additional_user.caregiver.students
- visit current_path
- expect(caregiver_students).to_not be_empty
- caregiver_students.each do |caregiver_student|
- expect(page).to have_link caregiver_student.name, href: user_path(caregiver_student.user)
- end
- end
- end
- it "clicks on 'Edit Picture' button" do
- click_on "Edit Picture"
- expect(current_path).to eq avatar_upload_user_path(additional_user)
- end
- end
- context "after visiting user edit page", :js do
- before do
- visit edit_user_path(additional_user)
- end
- it "sees a gender option selected" do
- within ".form-group.gender-selection" do
- expect(find("label.btn.btn-default.active").text.downcase).to eq(additional_user.gender.downcase)
- end
- end
- it "changes role" do
- expect(page).to have_css(".role_checkboxes")
- within(".role_checkboxes") do
- # All are roles that are accessible for this user.
- expect(all("input[type=checkbox]").length).to eq(Role.accessible_by(Ability.new(user)).count)
- # All are enabled.
- expect(all("input[type=checkbox][disabled]").length).to eq(0)
- end
- end
- end
- context "after visiting 'Edit Profile Picture' page" do
- before do
- visit avatar_upload_user_path(additional_user)
- end
- it "uploads and crops image", :js, retry: 5 do
- expect(page).to have_selector("#upload-panel", visible: true)
- expect(page).to have_selector("#crop-panel", visible: false)
- attach_file("user_avatar", "app/assets/images/dummy/square_600_dummy.png")
- expect(page).to have_selector("#crop-panel", visible: true)
- expect(find("#upload-panel", visible: false)).to_not be_visible
- expect(page).to have_selector("#user_avatar_crop_x", visible: false)
- expect(page).to have_selector('#user_avatar_crop_x[value="0"]', visible: false)
- find(".ord-w.jcrop-dragbar").drag_to(find(".ord-s.jcrop-handle"))
- expect(page).to_not have_selector('#user_avatar_crop_x[value="0"]', visible: false)
- crop_values = {}
- %i(x y w h).each do |c|
- crop_values[c] = find("#user_avatar_crop_#{c}", visible: false).value.to_i
- end
- click_button "Save"
- expect(page).to have_content("User was successfully saved.")
- expect(page).not_to have_selector("small", text: "Profile Picture Upload") # Need to wait for the title to disappear (new page loaded)
- additional_user.reload
- %i(x y w h).each do |c|
- expect(additional_user.send("avatar_crop_#{c}")).to eq crop_values[c]
- end
- end
- it "selects image for upload and then decides to chose another picture", :js do
- attach_file("user_avatar", "app/assets/images/dummy/square_600_dummy.png")
- expect(page).to have_selector("#crop-panel", visible: true)
- expect(page).to have_selector(".jcrop-holder img")
- previous_image = all(".jcrop-holder img").first[:src]
- click_on "Chose a different picture"
- expect(page).to have_selector("#crop-panel", visible: false)
- attach_file("user_avatar", "app/assets/images/dummy/square_300_dummy.png")
- expect(page).to have_selector("#crop-panel", visible: true)
- expect(page).to have_selector(".jcrop-holder img")
- expect(all(".jcrop-holder img").first[:src]).to_not eq previous_image
- end
- end
- context "after clicking on '#{role.pluralize.humanize}' tab", :js do
- let(:user_table_row_selector) do
- ".#{ActionView::RecordIdentifier.dom_id(additional_user)}"
- end
- before do
- click_on_role_tab(additional_user, role.to_sym)
- app.users_index_page.wait_for_user_rows
- end
- it "clicks 'Edit' button" do
- within user_table_row_selector do
- expect(page).to have_content "user4@example.com"
- click_link "Edit"
- end
- expect(app.users_edit_page).to be_displayed(id: additional_user.id)
- expect(current_path).to eq edit_user_path(additional_user)
- end
- end
- if role == "student" || role == "teacher"
- context "after visiting #{role} edit page", :js do
- let!(:parent) { additional_user.send(role.to_s) }
- it "can see the edit details button with an apporpriate text for teachers and students" do
- visit edit_user_path(additional_user)
- if role == "student"
- expect(find("#edit_btn")).to have_content "Add Caregiver & Emergency Contacts"
- elsif role == "teacher"
- expect(find("#edit_btn")).to have_content "Add Subjects & Emergency Contacts"
- end
- end
- context "without existing emergency contact" do
- before do
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- end
- scenario "clicks on 'Add emergency contact' button 3 times and removes the last one" do
- expect(page).not_to have_css ".emergency-contact-panel"
- find(".add-emergency-contact-btn").click
- within ".emergency-contacts-accordion" do
- expect(page).to have_css ".emergency-contact-panel"
- expect(page).to have_css ".panel-info"
- end
- expect(page).to have_content "New emergency contact #1"
- %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
- expect(page).to have_css "##{role}_emergency_contacts_attributes_0_#{text_field}"
- end
- 2.times do |index|
- find(".add-emergency-contact-btn").click
- expect(page).to have_content "New emergency contact ##{index + 2}"
- %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
- expect(page).to have_css "##{role}_emergency_contacts_attributes_#{index + 1}_#{text_field}"
- expect(page).to have_field "#{role}[emergency_contacts_attributes][#{index + 1}][#{text_field}]"
- end
- end
- all(".delete-emergency-contact")[2].click
- %w(first_name last_name phone gender address1 address2 zip_code city relation).each do |text_field|
- expect(page).not_to have_css "##{role}_emergency_contacts_attributes_2_#{text_field}"
- expect(page).not_to have_field "#{role}[emergency_contacts_attributes][2][#{text_field}]"
- end
- expect(page).not_to have_content "New emergency contact #3"
- title = "New emergency contact #1"
- expect(page).to have_content title
- click_link(title)
- %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
- expect(page).to have_css "##{role}_emergency_contacts_attributes_0_#{text_field}"
- expect(page).to have_field "#{role}[emergency_contacts_attributes][0][#{text_field}]"
- end
- end
- scenario "creates emergency contact" do
- find(".add-emergency-contact-btn").click
- panel = find(".emergency-contact-panel")
- within panel do
- %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
- fill_in("#{role}[emergency_contacts_attributes][0][#{text_field}]", with: text_field)
- end
- fill_in("#{role}[emergency_contacts_attributes][0][email]", with: "valid@email.com")
- end
- expect { find(".save-btn").click }.to change { EmergencyContact.count }.from(0).to(1)
- expect(current_path).to eq user_path(additional_user)
- expect(page).to have_content "#{role.humanize} successfully saved."
- end
- scenario "submits empty emergency contact form" do
- find(".add-emergency-contact-btn").click
- find(".save-btn").click
- expect(all(".has-error").count).to eq 2 # first_name and last_name are mandatory
- expect(page).to have_css ".panel-danger"
- expect(page).to have_content "This field is required"
- end
- scenario "submits malformed emergency contact email" do
- find(".add-emergency-contact-btn").click
- within find(".emergency-contact-panel") do
- %w(first_name last_name phone address1 address2 zip_code city relation).each do |text_field|
- fill_in("#{role}[emergency_contacts_attributes][0][#{text_field}]", with: text_field)
- end
- fill_in("#{role}[emergency_contacts_attributes][0][email]", with: "badEmail.com")
- end
- find(".save-btn").click
- expect(page).to have_content "first_name last_name"
- expect(all(".has-error").count).to eq 1
- expect(page).to have_css ".panel-danger"
- expect(page).to have_content "is invalid"
- end
- end
- case role
- when "teacher"
- context "without existing subjects added to teacher" do
- let!(:subjects) { Array.new(5) { create :subject } }
- let(:subject_1) { "Subject 1" }
- let(:subject_2) { "Subject 2" }
- let(:subject_3) { "Subject 3" }
- before do
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- end
- scenario "clicks on 'Add subject' button 3 times and removes the second one" do
- expect(page).not_to have_css ".subject-form"
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- within ".teacher-subjects-accordion" do
- expect(page).to have_css ".subject-panel"
- expect(page).to have_css ".panel-info"
- end
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_1)
- find(".add-subject-btn").click
- select subject_2, from: "subjects-select"
- find(".attach-subject-btn").click
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_2)
- find(".add-subject-btn").click
- select subject_3, from: "subjects-select"
- find(".attach-subject-btn").click
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_3)
- all(".delete-subject")[1].click
- expect(page).not_to have_selector(".subject-panel .panel-title", text: subject_2)
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_1)
- expect(page).to have_selector(".subject-panel .panel-title", text: subject_3)
- end
- scenario "Saving subjects" do
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- find(".add-subject-btn").click
- select subject_2, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- find(".add-subject-btn").click
- select subject_3, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_3).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(3)
- expect(page).to have_content "Teacher successfully saved"
- end
- scenario "Saving subject without grade_level" do
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- expect { click_button "Save" }.to change { parent.reload.subjects.count }.by(1)
- expect(page).to have_content "Teacher successfully saved"
- end
- scenario "Deleting subjects" do
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- check "#{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}"
- find(".add-subject-btn").click
- select subject_2, from: "subjects-select"
- find(".attach-subject-btn").click
- check "#{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}"
- expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(2)
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- all(".delete-subject")[0].click
- sleep(1)
- expect(current_path).to eq edit_teacher_path(additional_user.teacher)
- expect(all(".subject-panel").count).to eq 1
- end
- scenario "Saving new subjects with existing subjects" do
- find(".add-subject-btn").click
- select subject_1, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_1).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- find(".add-subject-btn").click
- select subject_2, from: "subjects-select"
- find(".attach-subject-btn").click
- check_box = find("##{role}_teacher_subjects_attributes_#{Subject.find_by(name: subject_2).id}_grade_level_id_#{GradeLevel.accessible_by(user_ability).sample.id}")
- check_box.set(true)
- expect { click_button "Save" }.to change { parent.reload.subjects.count }.from(0).to(2)
- end
- end
- when "student"
- context "Visits student edit page to manipulate caregivers" do
- let(:student) { create(:student) }
- before do
- 4.times { create(:caregiver) }
- visit edit_student_path(student)
- end
- def create_typeahead_results(count, query = "Joh")
- (1..count).each { |i| typeahead_and_click_result("#caregiver-search", query, i) }
- expect { find(:css, ".save-btn").click }.to change {
- StudentCaregiver.count
- }.from(0).to(count)
- end
- scenario "Creates 2 student caregivers", :js do
- create_typeahead_results(2)
- end
- scenario "Tries to add same caregiver 2 times", :js do
- 2.times { |_i| typeahead_and_click_result("#caregiver-search", "Joh", 1) }
- expect { find(:css, ".save-btn").click }.to change { StudentCaregiver.count }.from(0).to(1)
- end
- scenario "Creates 2 student caregivers, then deletes one", :js do
- create_typeahead_results(2)
- visit edit_student_path(student)
- expect(page).to have_selector(".remove-caregiver")
- first(".remove-caregiver").click
- expect { find(:css, ".save-btn").click }.to change { StudentCaregiver.count }.from(2).to(1)
- end
- scenario "Adds a caregiver, removes it and then hits save", :js do
- typeahead_and_click_result("#caregiver-search", "Joh", 1)
- expect(page).to have_selector(".remove-caregiver")
- first(".remove-caregiver").click
- expect { find(:css, ".save-btn").click }.not_to change { StudentCaregiver.count }
- end
- scenario "Creates 2 student caregivers then edits their relationship types", :js do
- create_typeahead_results(2)
- visit edit_student_path(student)
- 2.times do |i| # i is 0 first
- select "Others", from: "student_student_caregivers_attributes_#{i}_relationship_type_id"
- end
- expect { find(:css, ".save-btn").click }.to change {
- StudentCaregiver.joins(:relationship_type).pluck(:description)
- }.from(%w(Father Father)).to(%w(Others Others))
- end
- end
- end
- context "with existing emergency contact" do
- let!(:emergency_contact) { create(:emergency_contact, first_name: "first_name", last_name: "last_name", email: "email@email.com", phone: "phone", parent: parent) }
- before do
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- end
- it "edits existing record", :js, retry: 2 do
- attributes = %i(last_name first_name email phone address1 address2 zip_code city relation)
- new_values = %w(last first lf@www.com 123 avenue 5th 0000 neverland father)
- expect(parent.emergency_contacts.count).to eq 1
- expect(page).to have_content "first_name last_name"
- find(".title-link").click
- within ".emergency-contact-panel" do
- all("input[type=text]").each_with_index do |field, index|
- expect(field.value).to eq emergency_contact.send(attributes[index])
- field.set new_values[index]
- end
- end
- find(".save-btn").click
- expect(current_path).to eq user_path(additional_user)
- expect(page).to have_content "#{role.humanize} successfully saved."
- expect(parent.emergency_contacts.count).to eq 1
- visit edit_user_path(additional_user)
- click_link "edit_btn"
- attributes.each_with_index do |attribute, index|
- expect(parent.emergency_contacts.first.send(attribute)).to eq new_values[index]
- end
- expect(all(".emergency-contact-panel").count).to eq 1
- end
- it "deletes existing record" do
- find(".delete-emergency-contact").click
- sleep(1)
- expect(parent.emergency_contacts.count).to eq 0
- end
- end
- end
- end
- end
- shared_examples "user who cannot manage #{role} accounts" do
- before do
- visit users_path
- end
- it "cannot see '#{role.pluralize.humanize}' tab" do
- within ".nav-tabs" do
- expect(page).not_to have_link role.pluralize.humanize
- end
- end
- it "cannot destroy and update profile of #{role} user" do
- additional_user = create(:user, role.to_sym)
- visit user_path(additional_user)
- expect(current_path).to eq user_path(additional_user)
- expect(page).not_to have_css ".edit-details-btn"
- expect(page).not_to have_css ".edit-picture-btn"
- expect(page).not_to have_css ".delete-btn"
- end
- end
- shared_examples "user who can only read #{role} accounts" do
- before do
- visit users_path
- end
- it "can see '#{role.pluralize.humanize}' tab" do
- within ".nav-tabs" do
- expect(page).to have_link role.pluralize.humanize
- end
- end
- it "can access profile page of #{role} user" do
- additional_user = create(:user, role.to_sym)
- visit user_path(additional_user)
- expect(current_path).to eq user_path(additional_user)
- end
- end
- end
- shared_examples "user who cannot add new user" do
- before do
- visit users_path
- end
- it "cannot see 'Add New User' link" do
- expect(page).not_to have_link "Add New User"
- end
- end
- def click_on_role_tab(additional_user, role, grade_level = nil)
- within ".roles" do
- click_link role.to_s.pluralize.humanize
- end
- if additional_user.current_role_name == "student"
- gl = grade_level || additional_user.student.current_grade_level
- within ".grade_levels" do
- if gl
- selector = "." + ActionView::RecordIdentifier.dom_id(gl)
- find(selector).click
- else
- app.users_index_page.no_grade_level_tab.click
- end
- end
- end
- end
- def user_ability
- Ability.new(user)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement