member_spec.rb 7.98 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
3
describe Member, models: true do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
4
  describe "Associations" do
5
    it { is_expected.to belong_to(:user) }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6 7 8 9 10
  end

  describe "Validation" do
    subject { Member.new(access_level: Member::GUEST) }

11 12 13
    it { is_expected.to validate_presence_of(:user) }
    it { is_expected.to validate_presence_of(:source) }
    it { is_expected.to validate_inclusion_of(:access_level).in_array(Gitlab::Access.values) }
Douwe Maan's avatar
Douwe Maan committed
14

15 16 17 18
    it_behaves_like 'an object with email-formated attributes', :invite_email do
      subject { build(:project_member) }
    end

Douwe Maan's avatar
Douwe Maan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    context "when an invite email is provided" do
      let(:member) { build(:project_member, invite_email: "user@example.com", user: nil) }

      it "doesn't require a user" do
        expect(member).to be_valid
      end

      it "requires a valid invite email" do
        member.invite_email = "nope"

        expect(member).not_to be_valid
      end

      it "requires a unique invite email scoped to this source" do
        create(:project_member, source: member.source, invite_email: member.invite_email)

        expect(member).not_to be_valid
      end

      it "is valid otherwise" do
        expect(member).to be_valid
      end
    end

    context "when an invite email is not provided" do
      let(:member) { build(:project_member) }

      it "requires a user" do
        member.user = nil

        expect(member).not_to be_valid
      end

      it "is valid otherwise" do
        expect(member).to be_valid
      end
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
56
  end
57

58
  describe 'Scopes & finders' do
59 60
    before do
      project = create(:project)
61 62 63 64 65 66 67 68 69 70 71 72 73
      group = create(:group)
      @owner_user = create(:user).tap { |u| group.add_owner(u) }
      @owner = group.members.find_by(user_id: @owner_user.id)

      @master_user = create(:user).tap { |u| project.team << [u, :master] }
      @master = project.members.find_by(user_id: @master_user.id)

      ProjectMember.add_user(project.members, 'toto1@example.com', Gitlab::Access::DEVELOPER, @master_user)
      @invited_member = project.members.invite.find_by_invite_email('toto1@example.com')

      accepted_invite_user = build(:user)
      ProjectMember.add_user(project.members, 'toto2@example.com', Gitlab::Access::DEVELOPER, @master_user)
      @accepted_invite_member = project.members.invite.find_by_invite_email('toto2@example.com').tap { |u| u.accept_invite!(accepted_invite_user) }
74 75

      requested_user = create(:user).tap { |u| project.request_access(u) }
76
      @requested_member = project.requesters.find_by(user_id: requested_user.id)
77

78
      accepted_request_user = create(:user).tap { |u| project.request_access(u) }
79
      @accepted_request_member = project.requesters.find_by(user_id: accepted_request_user.id).tap { |m| m.accept_request }
80 81
    end

82 83
    describe '.invite' do
      it { expect(described_class.invite).not_to include @master }
84 85 86 87 88 89
      it { expect(described_class.invite).to include @invited_member }
      it { expect(described_class.invite).not_to include @accepted_invite_member }
      it { expect(described_class.invite).not_to include @requested_member }
      it { expect(described_class.invite).not_to include @accepted_request_member }
    end

90 91 92 93 94 95 96 97 98 99
    describe '.non_invite' do
      it { expect(described_class.non_invite).to include @master }
      it { expect(described_class.non_invite).not_to include @invited_member }
      it { expect(described_class.non_invite).to include @accepted_invite_member }
      it { expect(described_class.non_invite).to include @requested_member }
      it { expect(described_class.non_invite).to include @accepted_request_member }
    end

    describe '.request' do
      it { expect(described_class.request).not_to include @master }
100 101 102 103 104 105
      it { expect(described_class.request).not_to include @invited_member }
      it { expect(described_class.request).not_to include @accepted_invite_member }
      it { expect(described_class.request).to include @requested_member }
      it { expect(described_class.request).not_to include @accepted_request_member }
    end

106 107 108 109 110 111 112 113
    describe '.owners_and_masters' do
      it { expect(described_class.owners_and_masters).to include @owner }
      it { expect(described_class.owners_and_masters).to include @master }
      it { expect(described_class.owners_and_masters).not_to include @invited_member }
      it { expect(described_class.owners_and_masters).not_to include @accepted_invite_member }
      it { expect(described_class.owners_and_masters).not_to include @requested_member }
      it { expect(described_class.owners_and_masters).not_to include @accepted_request_member }
    end
114 115
  end

116
  describe "Delegate methods" do
117 118
    it { is_expected.to respond_to(:user_name) }
    it { is_expected.to respond_to(:user_email) }
119
  end
Douwe Maan's avatar
Douwe Maan committed
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  describe ".add_user" do
    let!(:user)    { create(:user) }
    let(:project) { create(:project) }

    context "when called with a user id" do
      it "adds the user as a member" do
        Member.add_user(project.project_members, user.id, ProjectMember::MASTER)

        expect(project.users).to include(user)
      end
    end

    context "when called with a user object" do
      it "adds the user as a member" do
        Member.add_user(project.project_members, user, ProjectMember::MASTER)

        expect(project.users).to include(user)
      end
    end

    context "when called with a known user email" do
      it "adds the user as a member" do
        Member.add_user(project.project_members, user.email, ProjectMember::MASTER)

        expect(project.users).to include(user)
      end
    end

    context "when called with an unknown user email" do
      it "adds a member invite" do
        Member.add_user(project.project_members, "user@example.com", ProjectMember::MASTER)

        expect(project.project_members.invite.pluck(:invite_email)).to include("user@example.com")
      end
    end
  end

158
  describe '#accept_request' do
159
    let(:member) { create(:project_member, requested_at: Time.now.utc) }
160

161
    it { expect(member.accept_request).to be_truthy }
162 163 164 165 166 167 168 169 170 171 172 173 174 175

    it 'clears requested_at' do
      member.accept_request

      expect(member.requested_at).to be_nil
    end

    it 'calls #after_accept_request' do
      expect(member).to receive(:after_accept_request)

      member.accept_request
    end
  end

176 177
  describe '#invite?' do
    subject { create(:project_member, invite_email: "user@example.com", user: nil) }
178

179 180
    it { is_expected.to be_invite }
  end
181

182 183
  describe '#request?' do
    subject { create(:project_member, requested_at: Time.now.utc) }
184

185 186
    it { is_expected.to be_request }
  end
187

188 189 190
  describe '#pending?' do
    let(:invited_member) { create(:project_member, invite_email: "user@example.com", user: nil) }
    let(:requester) { create(:project_member, requested_at: Time.now.utc) }
191

192 193
    it { expect(invited_member).to be_invite }
    it { expect(requester).to be_pending }
194 195
  end

Douwe Maan's avatar
Douwe Maan committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  describe "#accept_invite!" do
    let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
    let(:user) { create(:user) }

    it "resets the invite token" do
      member.accept_invite!(user)

      expect(member.invite_token).to be_nil
    end

    it "sets the invite accepted timestamp" do
      member.accept_invite!(user)

      expect(member.invite_accepted_at).not_to be_nil
    end

    it "sets the user" do
      member.accept_invite!(user)

      expect(member.user).to eq(user)
    end

    it "calls #after_accept_invite" do
      expect(member).to receive(:after_accept_invite)

      member.accept_invite!(user)
    end
  end

Douwe Maan's avatar
Douwe Maan committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  describe "#decline_invite!" do
    let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }

    it "destroys the member" do
      member.decline_invite!

      expect(member).to be_destroyed
    end

    it "calls #after_decline_invite" do
      expect(member).to receive(:after_decline_invite)

      member.decline_invite!
    end
  end

Douwe Maan's avatar
Douwe Maan committed
241 242
  describe "#generate_invite_token" do
    let!(:member) { create(:project_member, invite_email: "user@example.com", user: nil) }
243

Douwe Maan's avatar
Douwe Maan committed
244 245 246 247
    it "sets the invite token" do
      expect { member.generate_invite_token }.to change { member.invite_token}
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
248
end