main
  1# frozen_string_literal: true
  2
  3require 'rails_helper'
  4
  5RSpec.describe SCIM::User do
  6  describe "#valid?" do
  7    specify { expect(build(:scim_user)).to be_valid }
  8    specify { expect(build(:scim_user, id: 1)).to be_invalid }
  9
 10    specify do
 11      subject = build(:scim_user, schemas: ["urn:ietf:params:scim:schemas:core:2.0:Blah"])
 12      expect(subject).not_to be_valid
 13      expect(subject.errors[:schemas]).to be_present
 14    end
 15
 16    specify do
 17      subject = build(:scim_user, userName: nil)
 18      expect(subject).not_to be_valid
 19      expect(subject.errors[:userName]).to be_present
 20    end
 21
 22    specify do
 23      subject = build(:scim_user, userName: 'notanemail')
 24      expect(subject).not_to be_valid
 25      expect(subject.errors[:userName]).to be_present
 26    end
 27
 28    specify do
 29      subject = build(:scim_user, locale: '')
 30      expect(subject).not_to be_valid
 31      expect(subject.errors[:locale]).to be_present
 32    end
 33
 34    specify do
 35      subject = build(:scim_user, locale: 'de')
 36      expect(subject).not_to be_valid
 37      expect(subject.errors[:locale]).to be_present
 38    end
 39
 40    specify do
 41      subject = build(:scim_user, timezone: '')
 42      expect(subject).not_to be_valid
 43      expect(subject.errors[:timezone]).to be_present
 44    end
 45
 46    specify do
 47      subject = build(:scim_user, timezone: 'etc/unknown')
 48      expect(subject).not_to be_valid
 49      expect(subject.errors[:timezone]).to be_present
 50    end
 51  end
 52
 53  describe "#save!" do
 54    context "when the user is new" do
 55      let(:current_user) { create(:user) }
 56
 57      before { allow(Current).to receive(:user).and_return(current_user) }
 58
 59      context "when creating a user" do
 60        subject { build(:scim_user) }
 61
 62        specify { expect(subject.save!).to be_persisted }
 63        specify { expect(subject.save!.email).to eql(subject.userName) }
 64        specify { expect(subject.save!.locale).to eql(subject.locale) }
 65        specify { expect(subject.save!.timezone).to eql(subject.timezone) }
 66        specify { expect(subject.save!.password_digest).to be_present }
 67      end
 68    end
 69
 70    context "when one user is updating another user" do
 71      subject { build(:scim_user, id: other_user.to_param) }
 72
 73      let(:current_user) { create(:user) }
 74      let(:other_user) { create(:user) }
 75
 76      before { allow(Current).to receive(:user).and_return(current_user) }
 77
 78      specify { expect(subject.save!.to_param).to eql(other_user.to_param) }
 79      specify { expect(subject.save!.email).to eql(subject.userName) }
 80      specify { expect(subject.save!.locale).to eql(subject.locale) }
 81      specify { expect(subject.save!.timezone).to eql(subject.timezone) }
 82    end
 83
 84    context "when one user attempts to change the password of another user" do
 85      subject { build(:scim_user, id: other_user.to_param, password: generate(:password)) }
 86
 87      let(:current_user) { create(:user) }
 88      let(:other_user) { create(:user) }
 89
 90      before { allow(Current).to receive(:user).and_return(current_user) }
 91
 92      specify { expect { subject.save! }.to raise_error(StandardError) }
 93    end
 94
 95    context "when a user changes their own password" do
 96      subject { build(:scim_user, id: current_user.to_param, password: password) }
 97
 98      let!(:current_user) { create(:user) }
 99      let(:password) { generate(:password) }
100
101      before do
102        freeze_time
103        allow(Current).to receive(:user).and_return(current_user)
104      end
105
106      specify { expect(subject.save!.authenticate(password)).to be_truthy }
107    end
108  end
109
110  describe "#humanAttributeName" do
111    subject { described_class }
112
113    specify { expect(subject.human_attribute_name('userName')).to eql('userName') }
114    specify { expect(subject.human_attribute_name('schemas')).to eql('schemas') }
115    specify { expect(subject.human_attribute_name('locale')).to eql('locale') }
116    specify { expect(subject.human_attribute_name('password')).to eql('password') }
117  end
118end