require 'rails_helper'

describe Member, type: :model do
  let(:company) { ActsAsTenant.current_tenant }
  let(:member) { create :member }
  let(:active_status) { create :member_status, :active }
  let(:inactive_status) { create :member_status, :inactive }

  let(:attr) {
    { user: create(:user),
      first_name: "Example",
      last_name: "User",
      company: company,
      member_status: active_status,
      security_group: SecurityGroup.find_by_name("User") }
  }

  it "should create a new instance given a valid attribute", :vcr do
    Member.create!(attr)
  end

  it "should reject invalid email addresses for active members" do
    addresses = %w[user@bar.com user@foo.com]

    addresses.each do |address|
      user = build(:user, email: address)
      member = Member.new(attr.merge(user: user))
      expect(member).not_to be_valid
      expect(member.errors.full_messages.to_s).to include "not valid for this app"
    end
  end

  it "should allow invalid email addresses for inactive members" do
    addresses = %w[user@bar.com user@foo.com]

    addresses.each do |address|
      user = build(:user, email: address)
      member = Member.new(attr.merge(user: user, member_status: inactive_status))
      expect(member).to be_valid
    end
  end

  it "should allow valid email addresses" do
    addresses = ["joe@radbear.net", "bob@radbear.net", "sally@radbear.net"]

    addresses.each do |address|
      user = build(:user, email: address)
      member = Member.new(attr.merge(user: user))
      expect(member).to be_valid
    end
  end
end
