require 'test_helper'

class UsersEditTest < ActionDispatch::IntegrationTest
  
  def setup
    @user = users :michael
  end

  test "unsuccessful edit" do
    log_in_as @user
    get edit_user_path(@user)
    assert_template 'users/edit'

    patch user_path(@user), user: { name: "", email: "", password: "foo", password_confirmation: "bar" }
    assert_template 'users/edit'
  end

  test "successful edit" do
    log_in_as @user
    get edit_user_path(@user)
    assert_template 'users/edit'

    name = 'Foo Bar'
    email = 'foo@example.com'
    password = "password1"

    patch user_path(@user), user: { name: name, email: email, password: password, password_confirmation: password }
    assert_not flash.empty?
    assert_redirected_to @user

    @user.reload
    assert_equal name, @user.name
    assert_equal email, @user.email
  end

  test 'successful edit without resetting password' do
    log_in_as @user
    get edit_user_path(@user)
    assert_template 'users/edit'

    name = 'Foo Bar'
    email = 'foo@example.com'

    patch user_path(@user), user: { name: name, email: email }
    assert_not flash.empty?
    assert_redirected_to @user

    @user.reload
    assert_equal name, @user.name
    assert_equal email, @user.email
  end

  test 'successful edit with friendly forwarding' do
    get edit_user_path(@user)
    log_in_as(@user)

    assert_redirected_to edit_user_path(@user)

    name = "Foo Bar"
    email = "foo@example.com"

    patch user_path(@user), user: { name: name, email: email }
    assert_not flash.empty?
    assert_redirected_to @user

    @user.reload
    assert_equal name, @user.name
    assert_equal email, @user.email
  end

end
