# == Schema Information
#
# Table name: users
#
#  id                     :integer          not null, primary key
#  email                  :string           default(""), not null
#  encrypted_password     :string           default(""), not null
#  reset_password_token   :string
#  reset_password_sent_at :datetime
#  remember_created_at    :datetime
#  sign_in_count          :integer          default(0)
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string
#  last_sign_in_ip        :string
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#  first_name             :string           default("Joe")
#  last_name              :string           default("Bloggs")
#

require 'rails_helper'

describe User do

    # ActiveRecord relations
    it { expect(subject).to have_one(:attachment).dependent(:destroy) }
    it { expect(subject).to have_many(:notifications).dependent(:destroy) }
    it { expect(subject).to have_many(:permissions).dependent(:destroy) }
    it { expect(subject).to have_many(:roles).through(:permissions) }

    # Validations
    it { expect(subject).to validate_presence_of(:first_name) }
    it { expect(subject).to validate_presence_of(:last_name) }

    it { expect(subject).to accept_nested_attributes_for(:attachment) }

    describe "When querying a user's authentication level" do
        let(:admin) { create(:admin) }
        let(:user) { create(:standard_user) }

        it "should return true if the symbol parameter matches a role attached to the user" do
            expect(admin.role?(:admin)).to eq true
        end

        it "should return false if the symbol parameter doesn't match a role attached to the user" do
            expect(user.role?(:admin)).to eq false
        end
    end

    describe "When displaying a user's name" do
        let(:user) { create(:user, first_name: 'Tom', last_name: 'Dallimore') }

        it "should concatenate the user's first and last name to a string" do
            expect(user.full_name).to eq 'Tom Dallimore'
        end

    end
end
