tabletop-companion/test/controllers/users_controller_test.rb

76 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class UsersControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get new_user_url
assert_response :success
end
test "should get show" do
sign_in users(:trevor)
get user_url(users(:trevor))
assert_response :success
end
test "should create user" do
assert_changes("User.count", +1) do
post(users_url, params: { user: user_params })
end
assert_redirected_to login_path
end
test "should email user for confirmation" do
assert_emails(+1) do
post(users_url, params: { user: user_params })
end
end
test "should alert to invalid user" do
assert_no_changes("User.count") do
post(users_url, params: { user: { username: "new_user" } })
end
assert_response :unprocessable_entity
end
test "should get edit" do
sign_in users(:trevor)
get edit_user_url(users(:trevor))
assert_response :success
end
test "can only edit own user" do
sign_in users(:trevor)
get edit_user_url(users(:gimli))
assert_response :forbidden
end
test "should update user" do
sign_in users(:trevor)
patch user_url(users(:trevor)), params: { user: { profile: "All about me" } }
assert_redirected_to user_path(users(:trevor))
end
test "can delete avatar" do
user = users(:trevor)
assert user.avatar.attached?
sign_in users(:trevor)
patch(user_url(user), params: { user: { delete_avatar: "1" } })
assert_not user.reload.avatar.attached?
end
private
def user_params
{
username: "new_user",
password: "password",
email: "new_user@ex.com",
first_name: "new",
last_name: "user",
}
end
end