Si vous constatez la perte de tickets, de commentaires ou de projets, veuillez consulter la page https://status.framasoft.org/incident/626.

adherent_spec.rb 6.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# OLIGAA - Stands for "Outil en Ligne d'Information et de Gestion des Activités
# des Associations" which is an online tool for associations to manage news,
# actors and activities.
# Copyright (C) 2010-2018 Comité Départemental du Sport Adapté de la Gironde (CDSA
# 33) <contact@cdsa33.org>
#
# This file is part of OLIGAA.
#
# OLIGAA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OLIGAA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OLIGAA.  If not, see <http://www.gnu.org/licenses/>.
#
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
require 'rails_helper'

RSpec.describe Adherent, type: :model do
  describe "associations" do
    it {is_expected.to have_many(:subscriptions)}
  end

  describe "validations" do
    it "default factory is valid" do
      expect(build(:adherent)).to be_valid
    end
    it { is_expected.to validate_presence_of(:last_name) }
    it { is_expected.to validate_presence_of(:first_name) }
    it { is_expected.to validate_presence_of(:born_on) }
    it { is_expected.to validate_presence_of(:permit_number) }
38 39 40 41
    it "two adherents can't have the same permit number" do
      create :adherent, permit_number: "123456"
      new_adherent = build :adherent, permit_number: "123456"
      expect(new_adherent).not_to be_valid
42
      expect(new_adherent.errors[:permit_number].first).to eq "Ce numéro de licence est déjà pris pour cette saison. Si vous pensez qu'il s'agit du bon numéro, merci de prendre contact avec le CDSA33."
43
    end
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    context "regarding medicosocial org" do
      it "can be associated to an existing one" do
        medico_social_org = create :member
        adherent = create :adherent, medico_social_org: medico_social_org, other_medicosocial_org: ""
        expect(adherent.medico_social_org).to eq medico_social_org
        expect(adherent.other_medicosocial_org).to be_blank
      end
      it "can have a custom one" do
        adherent = build :adherent, medico_social_org: nil, other_medicosocial_org: ""
        expect(adherent).not_to be_valid
        adherent.other_medicosocial_org = "Mon asso"
        adherent.save! and adherent.reload
        expect(adherent.medico_social_org).to be_nil
        expect(adherent.other_medicosocial_org).to eq "Mon asso"
      end
      it "forces to the existing one if a custom one is also provided" do
        medico_social_org = create :member
        adherent = create :adherent, medico_social_org: medico_social_org, other_medicosocial_org: "Mon asso"
        expect(adherent.medico_social_org).to eq medico_social_org
        expect(adherent.other_medicosocial_org).to be_blank
      end
    end
66 67 68 69 70 71 72
  end

  describe "behaviors" do
    it "#name returns concatenated value" do
      adherent = build :adherent, last_name: "Durand", first_name: "Nicolas"
      expect(adherent.name).to eq "Nicolas Durand"
    end
73 74 75 76 77 78 79 80

    it "#medico_social_org_name returns the name of the associated medico-social org" do
      medico_social_org = create :member, name: "IME one"
      adherent = build :adherent, medico_social_org: medico_social_org
      expect(adherent.medico_social_org_name).to eq "IME one"
      adherent = build :adherent, medico_social_org: nil, other_medicosocial_org: "IME two"
      expect(adherent.medico_social_org_name).to eq "IME two"
    end
81
  end
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

  describe "scopes" do

    context ".available_for_current_season" do

      let(:params) {
        {
          first_name: "Jean",
          last_name: "Dupont",
          born_on: "14/07/1989",
          permit_number: "123456",
          medico_social_org_id: nil,
          other_medicosocial_org: nil
        }
      }

      context "adherent does not exist" do
        it "returns a just built one" do
          # Given
          # When
          result = Adherent.available_for_current_season(params)
          # Then
          expect(result).to be_a Adherent
          expect(result).not_to be_persisted
        end
      end

      context "adherent alreaday exists" do

        let!(:existing_adherent) { create :adherent, id: 42, permit_number: "123456" }

        context "within another season" do

          before do
            Timecop.freeze(Date.new(2016,9,10))
            create :subscription, adherent: existing_adherent
          end

          after { Timecop.return }

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
          context "when :id provided (renewal)" do
            it "returns the adherent" do
              # Given
              Timecop.travel(Date.new(2017,9,10))
              params[:id] = 42
              # When
              result = Adherent.available_for_current_season(params)
              # Then
              expect(result).to eq existing_adherent
              expect(result).to be_persisted
            end
          end

          context "when :id not provided (subscription wihtin another club)" do
            it "returns the adherent" do
              # Given
              Timecop.travel(Date.new(2017,9,10))
              # When
              result = Adherent.available_for_current_season(params)
              # Then
              expect(result).to eq existing_adherent
              expect(result).to be_persisted
            end
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
          end
        end

        context "Within the same season" do

          before do
            Timecop.freeze(Date.new(2018,9,10))
            create :subscription, adherent: existing_adherent
          end

          after { Timecop.return }

          it "returns a just built one (for validations purpose)" do
            # Given
            params[:id] = 42
            # When
            result = Adherent.available_for_current_season(params)
            # Then
            expect(result).not_to eq existing_adherent
            expect(result).not_to be_persisted
            expect(result.id).to be_nil
          end
        end
      end
    end
  end
171
end