Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
getfitter-core / spec / models / core / user_spec.rb
Size: Mime:
require 'rails_helper'

RSpec.describe Core::User, type: :model do
  it 'has a valid factory' do
    expect(create(:core_user)).to be_valid
  end

  it 'is invalid without a name' do
    expect(build(:core_user, name: nil)).to be_invalid
  end

  it 'is invalid without an email' do
    expect(build(:core_user, email: nil)).to be_invalid
  end

  it 'cannot create a new user with a existing email' do
    expect(create(:core_user, email: 'joe@example.com')).to be_valid
    expect(build(:core_user, email: 'joe@example.com')).to be_invalid
  end

  it 'can have listed medical issues' do
    user = create(:core_user, medical_issues: 'Some notable conditions')

    expect(user.medical_issues).to eq 'Some notable conditions'
  end

  it 'can have notes' do
    user = create(:core_user, notes: 'Something notable')

    expect(user.notes).to eq 'Something notable'
  end

  it 'has a custom booking email' do
    user = create(:core_user, email: 'user@example.com')
    hash = Zlib.crc32('user@example.com')

    expect(user.booking_email).to eq "bookings+#{hash}@getfitter.co"
  end

  it 'can have an organisation' do
    user = create(:core_user)
    organisation = create(:core_organisation)
    user.organisation = organisation

    expect(user.organisation).to eq organisation
  end

  it 'can have many bookings' do
    user = create(:core_user)
    booking = user.bookings.create(attributes_for(:core_booking))

    expect(user.bookings).to eq([booking])
  end

  it 'can have many oauth applications' do
    user = create(:core_user)
    app = user.oauth_applications.create(attributes_for(:oauth_application))

    expect(user.oauth_applications).to eq([app])
  end

  it 'will delete associated bookings when the user is deleted' do
    user = create(:core_user)
    create(:core_booking, user: user)

    expect(described_class.all.count).to eq 1
    expect(Core::Booking.all.count).to eq 1

    user.destroy!

    expect(described_class.all.count).to eq 0
    expect(Core::Booking.all.count).to eq 0
  end

  it 'will delete associated oauth applictions when the user is deleted' do
    user = create(:core_user)
    user.oauth_applications.create(attributes_for(:oauth_application))

    expect(described_class.all.count).to eq 1
    expect(Doorkeeper::Application.all.count).to eq 1

    user.destroy!

    expect(described_class.all.count).to eq 0
    expect(Doorkeeper::Application.all.count).to eq 0
  end

  it 'can have passes' do
    organisation = create(:core_organisation)
    pass = create(:core_pass, organisation: organisation)
    user = create(:core_user)
    pass_purchase = create(:core_pass_purchase, user: user, pass: pass)

    expect(user.pass_purchases).to eq [pass_purchase]
  end

  it 'has a helper method for returning active passes' do
    organisation = create(:core_organisation)
    pass_one = create(:core_pass, organisation: organisation, duration: -1)
    pass_two = create(:core_pass, organisation: organisation, duration: 30)

    user = create(:core_user)
    create(:core_pass_purchase, user: user, pass: pass_one)
    create(:core_pass_purchase, user: user, pass: pass_two)

    passes = user.active_passes

    expect(passes.count).to eq 1
    expect(passes).to eq [pass_two]
  end

  it 'has a helper for determining if a user has a pass valid for an event' do
    organisation = create(:core_organisation)
    event = create(:core_event, organisation: organisation)
    pass = create(:core_pass, organisation: organisation, duration: 30)
    user = create(:core_user)
    create(:core_pass_purchase, user: user, pass: pass)

    # with a valid event
    expect(user.valid_pass?(event)).to eq true

    # make a random event and check that, too
    event_two = create(:core_event)
    expect(user.valid_pass?(event_two)).to eq false
  end

  it 'can be restricted in purchasing ability' do
    user = create(:core_user, purchases_allowed: false)

    expect(user.purchases_allowed?).to be_falsey
  end
end