require 'test_helper'

class StatesCreationTest < ActionDispatch::IntegrationTest

  def setup
    @machine = machines(:dishwasher)
    @user = @machine.user
  end

  test 'successful create' do
    log_in_as @user
    get user_machine_path(user_id: @user, id: @machine)

    assert_difference 'State.count', 1 do
      post user_machine_states_path(user_id: @user, machine_id: @machine), state: {
        state_value: State.count,
        name: 'empty'
      }
    end
  end

  test 'unsuccessful create' do
    other_user = users(:archer)
    log_in_as other_user

    assert_no_difference 'State.count' do
      post user_machine_states_path(user_id: @user, machine_id: @machine), state: {
        state_value: -1,
        name: 'empty'
      }
    end

    assert_no_difference 'State.count' do
      post user_machine_states_path(user_id: @user, machine_id: @machine), state: {
        state_value: 0,
        name: ''
      }
    end
  end
end
