require 'rails_helper' RSpec.feature "Users", type: :feature do context "when the user is a regular user" do let!(:user) { create(:user) } let!(:contest) { create(:contest, user: user) } before do login(user) end it "should not see a link to all users" do visit root_path expect(page).not_to have_content(I18n.t("nav.users")) end it "should not be able to see the user list" do visit users_path expect(page).not_to have_content(I18n.t("users.index.title")) end it "should be able to open their account info" do visit root_path click_link I18n.t("nav.settings") expect(page).to have_current_path(edit_user_path(user)) end context "when updating their account info" do let!(:existing_user) { create(:user, username: "taken_username") } it "should allow changing to an untaken username" do visit edit_user_path(user) fill_in I18n.t("activerecord.attributes.user.username"), with: "untaken_username" expect { click_button(I18n.t("helpers.buttons.save")); user.reload }.to change(user, :username).to("untaken_username") end it "should prevent changing to an already taken username" do visit edit_user_path(user) fill_in I18n.t("activerecord.attributes.user.username"), with: "taken_username" expect { click_button(I18n.t("helpers.buttons.save")); user.reload }.not_to change(user, :username) expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.username.taken")) end it "should prevent changing to a blank username" do visit edit_user_path(user) fill_in I18n.t("activerecord.attributes.user.username"), with: "" expect { click_button(I18n.t("helpers.buttons.save")); user.reload }.not_to change(user, :username) expect(page).to have_content(I18n.t("activerecord.errors.models.user.attributes.username.blank")) end it "should allow changing to a non-blank password" do visit edit_user_path(user) fill_in I18n.t("activerecord.attributes.user.password"), with: "new_password" expect { click_button(I18n.t("helpers.buttons.save_password")); user.reload } .to change(user, :password_digest) .and change { user.authenticate("new_password") }.from(false).to(user) end end end context "when the user is an admin" do let!(:admin) { create(:user, :admin) } let!(:user) { create(:user) } before do login(admin) end it "should see a link to all users" do visit root_path expect(page).to have_content(I18n.t("nav.users")) end it "should be able to see the user list" do visit users_path expect(page).to have_content(I18n.t("users.index.title")) expect(page).to have_content(user.username) end end end