From f19eeba8dc9a2c8c5b5c811db6f48532727d20fc Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Wed, 8 May 2019 15:28:07 +0200 Subject: [PATCH] Add factory valid check for User's associations --- app/models/user.rb | 2 ++ app/models/user_identity.rb | 2 ++ app/models/user_my_module.rb | 13 +++----- app/models/user_notification.rb | 2 ++ app/models/user_project.rb | 11 +++---- app/models/user_team.rb | 15 ++++----- spec/factories/user_identities.rb | 3 ++ ..._notification.rb => user_notifications.rb} | 2 ++ spec/factories/user_teams.rb | 2 ++ spec/factories/view_states.rb | 4 +-- spec/models/user_identity_spec.rb | 31 +++++++++++++++++++ spec/models/user_my_module_spec.rb | 10 +++++- spec/models/user_notification_spec.rb | 7 +++++ spec/models/user_project_spec.rb | 13 ++++++++ spec/models/user_spec.rb | 7 ++++- spec/models/user_team_spec.rb | 10 +++++- spec/models/view_state_spec.rb | 6 ++++ 17 files changed, 112 insertions(+), 28 deletions(-) rename spec/factories/{user_notification.rb => user_notifications.rb} (72%) create mode 100644 spec/models/user_identity_spec.rb diff --git a/app/models/user.rb b/app/models/user.rb index b60721274..8228f67e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class User < ApplicationRecord include SearchableModel include SettingsModel diff --git a/app/models/user_identity.rb b/app/models/user_identity.rb index 2d1f46a23..01775e1bd 100644 --- a/app/models/user_identity.rb +++ b/app/models/user_identity.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class UserIdentity < ActiveRecord::Base belongs_to :user validates :provider, presence: true, uniqueness: { scope: :user_id } diff --git a/app/models/user_my_module.rb b/app/models/user_my_module.rb index 5b3edabb4..3ba35879d 100644 --- a/app/models/user_my_module.rb +++ b/app/models/user_my_module.rb @@ -1,13 +1,10 @@ +# frozen_string_literal: true + class UserMyModule < ApplicationRecord validates :user, presence: true, uniqueness: { scope: :my_module } validates :my_module, presence: true - belongs_to :user, inverse_of: :user_my_modules, touch: true, optional: true - belongs_to :assigned_by, - foreign_key: 'assigned_by_id', - class_name: 'User', - optional: true - belongs_to :my_module, inverse_of: :user_my_modules, - touch: true, - optional: true + belongs_to :user, inverse_of: :user_my_modules, touch: true + belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User', optional: true + belongs_to :my_module, inverse_of: :user_my_modules, touch: true end diff --git a/app/models/user_notification.rb b/app/models/user_notification.rb index d034a563c..83226548f 100644 --- a/app/models/user_notification.rb +++ b/app/models/user_notification.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class UserNotification < ApplicationRecord include NotificationsHelper diff --git a/app/models/user_project.rb b/app/models/user_project.rb index cbaeb4ac0..b63ede753 100644 --- a/app/models/user_project.rb +++ b/app/models/user_project.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class UserProject < ApplicationRecord enum role: { owner: 0, normal_user: 1, technician: 2, viewer: 3 } @@ -5,12 +7,9 @@ class UserProject < ApplicationRecord validates :user, presence: true, uniqueness: { scope: :project } validates :project, presence: true - belongs_to :user, inverse_of: :user_projects, touch: true, optional: true - belongs_to :assigned_by, - foreign_key: 'assigned_by_id', - class_name: 'User', - optional: true - belongs_to :project, inverse_of: :user_projects, touch: true, optional: true + belongs_to :user, inverse_of: :user_projects, touch: true + belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User', optional: true + belongs_to :project, inverse_of: :user_projects, touch: true before_destroy :destroy_associations validates_uniqueness_of :user_id, scope: :project_id diff --git a/app/models/user_team.rb b/app/models/user_team.rb index 87cc5fd25..45f5f7f70 100644 --- a/app/models/user_team.rb +++ b/app/models/user_team.rb @@ -1,16 +1,13 @@ +# frozen_string_literal: true + class UserTeam < ApplicationRecord enum role: { guest: 0, normal_user: 1, admin: 2 } - validates :role, presence: true - validates :user, presence: true - validates :team, presence: true + validates :role, :user, :team, presence: true - belongs_to :user, inverse_of: :user_teams, touch: true, optional: true - belongs_to :assigned_by, - foreign_key: 'assigned_by_id', - class_name: 'User', - optional: true - belongs_to :team, inverse_of: :user_teams, optional: true + belongs_to :user, inverse_of: :user_teams, touch: true + belongs_to :assigned_by, foreign_key: 'assigned_by_id', class_name: 'User', optional: true + belongs_to :team, inverse_of: :user_teams before_destroy :destroy_associations after_create :create_samples_table_state diff --git a/spec/factories/user_identities.rb b/spec/factories/user_identities.rb index c9035a241..c33e17570 100644 --- a/spec/factories/user_identities.rb +++ b/spec/factories/user_identities.rb @@ -1,5 +1,8 @@ +# frozen_string_literal: true + FactoryBot.define do factory :user_identity do + user uid Faker::Crypto.unique.sha1 provider Faker::App.unique.name end diff --git a/spec/factories/user_notification.rb b/spec/factories/user_notifications.rb similarity index 72% rename from spec/factories/user_notification.rb rename to spec/factories/user_notifications.rb index 3eee80e4a..579e8cdf8 100644 --- a/spec/factories/user_notification.rb +++ b/spec/factories/user_notifications.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.define do factory :user_notification do checked false diff --git a/spec/factories/user_teams.rb b/spec/factories/user_teams.rb index ee52a9af9..c13c75db5 100644 --- a/spec/factories/user_teams.rb +++ b/spec/factories/user_teams.rb @@ -2,6 +2,8 @@ FactoryBot.define do factory :user_team do + user + team trait :admin do role 'admin' end diff --git a/spec/factories/view_states.rb b/spec/factories/view_states.rb index eb819d592..d898bef59 100644 --- a/spec/factories/view_states.rb +++ b/spec/factories/view_states.rb @@ -3,7 +3,7 @@ FactoryBot.define do factory :view_state do state {} - user { User.first || create(:user) } - viewable { Team.first || create(:team) } + user + viewable { create :team } end end diff --git a/spec/models/user_identity_spec.rb b/spec/models/user_identity_spec.rb new file mode 100644 index 000000000..23179a1ba --- /dev/null +++ b/spec/models/user_identity_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe UserIdentity, type: :model do + let(:user_identity) { build :user_identity } + + it 'is valid' do + expect(user_identity).to be_valid + end + + it 'should be of class UserIdentity' do + expect(subject.class).to eq UserIdentity + end + + describe 'Relations' do + it { is_expected.to belong_to :user } + end + + describe 'Validations' do + describe '#uid' do + it { is_expected.to validate_presence_of :uid } + it { expect(user_identity).to validate_uniqueness_of(:uid).scoped_to(:provider) } + end + + describe '#provider' do + it { is_expected.to validate_presence_of :provider } + it { expect(user_identity).to validate_uniqueness_of(:provider).scoped_to(:user_id) } + end + end +end diff --git a/spec/models/user_my_module_spec.rb b/spec/models/user_my_module_spec.rb index 5ba89fdef..466a74b92 100644 --- a/spec/models/user_my_module_spec.rb +++ b/spec/models/user_my_module_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe UserMyModule, type: :model do + let(:user_my_module) { build :user_my_module } + + it 'is valid' do + expect(user_my_module).to be_valid + end + it 'should be of class UserMyModule' do expect(subject.class).to eq UserMyModule end @@ -19,7 +27,7 @@ describe UserMyModule, type: :model do it { should belong_to(:assigned_by).class_name('User') } end - describe 'Should be a valid object' do + describe 'Validations' do it { should validate_presence_of :user } it { should validate_presence_of :my_module } end diff --git a/spec/models/user_notification_spec.rb b/spec/models/user_notification_spec.rb index a7ba560f2..c59645ee3 100644 --- a/spec/models/user_notification_spec.rb +++ b/spec/models/user_notification_spec.rb @@ -1,7 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe UserNotification, type: :model do let(:user) { create :user } + let(:user_notification) { build :user_notification } + + it 'is valid' do + expect(user_notification).to be_valid + end it 'should be of class UserNotification' do expect(subject.class).to eq UserNotification diff --git a/spec/models/user_project_spec.rb b/spec/models/user_project_spec.rb index 18883e775..3c8a29c1f 100644 --- a/spec/models/user_project_spec.rb +++ b/spec/models/user_project_spec.rb @@ -1,6 +1,19 @@ +# frozen_string_literal: true + require 'rails_helper' describe UserProject, type: :model do + let(:user_project_without_role) { build :user_project } + let(:user_project) { build :user_project, :owner } + + it 'is invalid without role' do + expect(user_project_without_role).not_to be_valid + end + + it 'is valid with role' do + expect(user_project).to be_valid + end + it 'should be of class UserProject' do expect(subject.class).to eq UserProject end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 20c46deab..7db2650c0 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -3,6 +3,11 @@ require 'rails_helper' describe User, type: :model do + let(:user) { build :user } + + it 'is valid' do + expect(user).to be_valid + end it 'should be of class User' do expect(subject.class).to eq User end @@ -120,7 +125,7 @@ describe User, type: :model do end end - describe 'Should be a valid object' do + describe 'Validations' do it { should validate_presence_of :full_name } it { should validate_presence_of :initials } it { should validate_presence_of :email } diff --git a/spec/models/user_team_spec.rb b/spec/models/user_team_spec.rb index 8ca01063f..1f98c469d 100644 --- a/spec/models/user_team_spec.rb +++ b/spec/models/user_team_spec.rb @@ -1,6 +1,14 @@ +# frozen_string_literal: true + require 'rails_helper' describe UserTeam, type: :model do + let(:user_team) { build :user_team } + + it 'is valid' do + expect(user_team).to be_valid + end + it 'should be of class UserTeam' do expect(subject.class).to eq UserTeam end @@ -20,7 +28,7 @@ describe UserTeam, type: :model do it { should belong_to(:assigned_by).class_name('User') } end - describe 'Should be a valid object' do + describe 'Validations' do it { should validate_presence_of :role } it { should validate_presence_of :user } it { should validate_presence_of :team } diff --git a/spec/models/view_state_spec.rb b/spec/models/view_state_spec.rb index 532ed1db3..3ededbfd5 100644 --- a/spec/models/view_state_spec.rb +++ b/spec/models/view_state_spec.rb @@ -3,6 +3,12 @@ require 'rails_helper' RSpec.describe ViewState, type: :model do + let(:view_state) { build :view_state } + + it 'is valid' do + expect(view_state).to be_valid + end + it 'should be of class ViewState' do expect(subject.class).to eq ViewState end