require 'spec_helper'

describe User do
  
  before { @user = User.new(name: "Matt") }

  subject { @user }

  it { should respond_to :name }
  it { should respond_to :ratings }
  it { should respond_to :submissions }

  it { should respond_to :admin }
  it { should respond_to :judge }

  it { should respond_to :email }
  it { should respond_to :confirmed }
  it { should respond_to :confirmation_code }
  it { should respond_to :ip }

  it { should respond_to :provider }
  it { should respond_to :uid }

  it { should be_valid }

  describe "without an email" do
    before { @user.email = " " }
    it { should be_valid }
  end

  describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.
                     foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not be_valid
      end
    end
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        @user.should be_valid
      end
    end
  end

  describe "when email address is already taken" do
    before do
      @user.email = "armin@mapc.net"
      user_with_same_email = @user.dup
      user_with_same_email.email = @user.email.upcase
      user_with_same_email.save
    end

    it { should be_valid }
  end
  
end
