# frozen_string_literal: true

require 'rails_helper'

RSpec.describe AdminUser, type: :model do
  shared_examples 'admin' do
    it { is_expected.to have_abilities(:manage, AdminUser) }
    it { is_expected.to have_abilities(:manage, Visitor) }
    it { is_expected.to have_abilities(:manage, Picture) }
    it { is_expected.to have_abilities(:manage, Guest) }
  end

  subject(:ability) { Ability.new(user) }

  describe 'admin role' do
    let(:user) { AdminUser.new(role: :admin) }
    it_behaves_like 'admin'
  end

  describe 'no role == admin' do
    let(:user) { AdminUser.new }
    it_behaves_like 'admin'
  end

  describe 'user role' do
    let(:user) { AdminUser.new(role: :user) }
    let(:any_visitor) { Visitor.new }
    let(:any_picture) { Picture.new }
    let(:users_picture) { Picture.new(imageable: user) }

    it { is_expected.to have_abilities({ manage: false, read: true }, AdminUser) }
    it { is_expected.to have_abilities(:manage, Visitor) }
    it { is_expected.to have_abilities(:manage, Picture) }
    it { is_expected.to not_have_abilities([:manage, :read], any_visitor) }
    it { is_expected.to not_have_abilities([:manage, :read], any_picture) }
    it { is_expected.to have_abilities(:manage, users_picture) }

    describe "user's visitors" do
      let(:user) { create(:admin_user) }
      let(:visitors_picture) { create(:picture) }
      let(:users_visitor) { create(:visitor, picture: visitors_picture) }
      let!(:guest_list) { create(:guest_list, admin_user: user, visitor: users_visitor) }
      it { is_expected.to have_abilities(:manage, users_visitor) }
      it { is_expected.to have_abilities(:manage, visitors_picture) }
    end
  end
end
