Implements tests for Shareable_links related Models [SCI-8755] (#5867)

This commit is contained in:
Soufiane 2023-07-26 15:21:55 +02:00 committed by GitHub
parent 44c919d3dc
commit fc4a48cd29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 107 additions and 21 deletions

View file

@ -0,0 +1,13 @@
FactoryBot.define do
factory :shareable_link do
description { Faker::Lorem.sentence }
association :shareable, factory: :my_module
team { shareable.team }
association :created_by, factory: :user
association :last_modified_by, factory: :user
after(:build) do |shareable_link|
shareable_link.uuid = shareable_link.shareable.signed_id
end
end
end

View file

@ -10,5 +10,8 @@ FactoryBot.define do
trait :with_members do
users { create_list :user, 3 }
end
trait :shareable_links_enabled do
shareable_links_enabled { true }
end
end
end

View file

@ -60,6 +60,7 @@ describe MyModule, type: :model do
it { should have_many(:inputs).class_name('Connection') }
it { should have_many(:outputs).class_name('Connection') }
it { should have_many(:my_module_antecessors).class_name('MyModule') }
it { should have_one(:shareable_link).dependent(:destroy) }
end
describe 'Validations' do

View file

@ -0,0 +1,46 @@
require 'rails_helper'
describe ShareableLink, type: :model do
let(:shareable_link) { build :shareable_link }
it 'is valid' do
expect(shareable_link).to be_valid
end
it 'should be of class ShareableLink' do
expect(subject.class).to eq ShareableLink
end
describe 'Database table' do
it { should have_db_column :uuid }
it { should have_db_column :created_at }
it { should have_db_column :updated_at }
it { should have_db_column :created_by_id }
it { should have_db_column :last_modified_by_id }
it { should have_db_column :description }
it { should have_db_column :team_id }
it { should have_db_column :shareable_type }
it { should have_db_column :shareable_id }
end
describe 'Relations' do
it { should belong_to(:shareable) }
it { should belong_to(:team) }
it { should belong_to(:created_by).class_name('User') }
it { should belong_to(:last_modified_by).class_name('User').optional }
end
describe 'Validations' do
it { should validate_length_of(:description).is_at_most(Constants::RICH_TEXT_MAX_LENGTH) }
it 'validates uniqueness of shareable_type scoped to shareable_id' do
shareable_link1 = create(:shareable_link)
shareable_link2 = build(:shareable_link,
shareable_type: shareable_link1.shareable_type,
shareable_id: shareable_link1.shareable_id)
expect(shareable_link2).not_to be_valid
expect(shareable_link2.errors[:shareable_type]).to include('has already been taken')
end
end
end

View file

@ -21,12 +21,12 @@ describe Team, type: :model do
it { should have_db_column :last_modified_by_id }
it { should have_db_column :description }
it { should have_db_column :space_taken }
it { should have_db_column :shareable_links_enabled }
end
describe 'Relations' do
it { should belong_to(:created_by).class_name('User').optional }
it { should belong_to(:last_modified_by).class_name('User').optional }
it { should have_many :user_teams }
it { should have_many :users }
it { should have_many :projects }
it { should have_many :protocols }
@ -36,6 +36,7 @@ describe Team, type: :model do
it { should have_many :reports }
it { should have_many(:team_shared_objects).dependent(:destroy) }
it { should have_many :shared_repositories }
it { should have_many(:shareable_links).dependent(:destroy) }
end
describe 'Validations' do
@ -49,4 +50,42 @@ describe Team, type: :model do
.is_at_most(Constants::TEXT_MAX_LENGTH)
end
end
describe 'Callbacks' do
it 'does not triggers destroy_all on shareable_links before save when shareable_links_enabled? is false' do
allow(team).to receive(:shareable_links_enabled?).and_return(true)
expect(team.shareable_links).not_to receive(:destroy_all)
team.save
end
context 'when shareable_links_enabled is true' do
it 'does not destroy shareable_links before saving' do
team.shareable_links_enabled = true
create_list(:shareable_link, 3, team: team)
team.save
expect(team.shareable_links.count).to eq(3)
end
end
context 'when shareable_links_enabled is false' do
it 'triggers destroy_all on shareable_links before save when shareable_links_enabled? is false' do
allow(team).to receive(:shareable_links_enabled?).and_return(false)
expect(team.shareable_links).to receive(:destroy_all)
team.save
end
it 'destroys all shareable_links before saving' do
team.shareable_links_enabled = true
create_list(:shareable_link, 3, team: team)
team.save
team.shareable_links_enabled = false
create_list(:shareable_link, 3, team: team)
team.save
expect(team.shareable_links.count).to eq(0)
end
end
end
end

View file

@ -46,7 +46,6 @@ describe User, type: :model do
end
describe 'Relations' do
it { should have_many :user_teams }
it { should have_many :teams }
it { should have_many :user_projects }
it { should have_many :projects }
@ -85,7 +84,6 @@ describe User, type: :model do
it { should have_many :tokens }
it { should have_many :modified_tags }
it { should have_many :assigned_user_my_modules }
it { should have_many :assigned_user_teams }
it { should have_many :assigned_user_projects }
it { should have_many :added_protocols }
it { should have_many :archived_protocols }
@ -94,6 +92,7 @@ describe User, type: :model do
it { should have_many :user_notifications }
it { should have_many :notifications }
it { should have_many :zip_exports }
it { should have_many(:shareable_links).dependent(:destroy) }
it 'have many repositories' do
table = User.reflect_on_association(:repositories)
@ -149,21 +148,6 @@ describe User, type: :model do
end
end
describe 'teams_data should return a list of teams' do
# needs persistence because is testing a sql query
let(:team) { create :team }
let(:user_one) do
create :user, email: 'user1@asdf.com', current_team_id: team.id
end
let(:user_two) { create :user, email: 'user2@asdf.com' }
it 'should return correct number of team members' do
create :user_team, team: team, user: user_one
create :user_team, team: team, user: user_two
expect(user_one.datatables_teams.first.members).to eq 2
end
end
describe 'user settings' do
it { is_expected.to respond_to(:time_zone) }
it { is_expected.to respond_to(:assignments_notification) }