require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  context "The users controller" do
    setup do
      @user = FactoryGirl.create(:user)
      CurrentUser.user = @user
      CurrentUser.ip_addr = "127.0.0.1"
    end

    teardown do
      CurrentUser.user = nil
    end

    context "index action" do
      setup do
        FactoryGirl.create(:user, :name => "abc")
      end

      should "list all users" do
        get :index
        assert_response :success
      end

      should "list all users (with search)" do
        get :index, {:search => {:name_matches => "abc"}}
        assert_response :success
      end
    end

    context "show action" do
      setup do
        @user = FactoryGirl.create(:user)
      end

      should "render" do
        get :show, {:id => @user.id}
        assert_response :success
      end
    end

    context "create action" do
      should "create a user" do
        assert_difference("User.count", 1) do
          post :create, {:user => {:name => "xxx", :password => "xxxxx1", :password_confirmation => "xxxxx1"}}, {:user_id => @user.id}
          assert_not_nil(assigns(:user))
          assert_equal([], assigns(:user).errors.full_messages)
        end
      end
    end

    context "edit action" do
      setup do
        @user = FactoryGirl.create(:user)
      end

      should "render" do
        get :edit, {:id => @user.id}, {:user_id => @user.id}
        assert_response :success
      end
    end

    context "update action" do
      setup do
        @user = FactoryGirl.create(:user)
      end

      should "update a user" do
        post :update, {:id => @user.id, :user => {:name => "xyz"}}, {:user_id => @user.id}
        @user.reload
        assert_equal("xyz", @user.name)
      end
    end
  end
end
