From 5421068f79d14a359ae645534b0e1d5579d1c7c4 Mon Sep 17 00:00:00 2001 From: ifournight Date: Sat, 8 Apr 2017 21:51:25 +0800 Subject: [PATCH] Update models spec --- app/models/access.rb | 34 ++++++++++----------- app/models/project.rb | 9 +++--- app/models/team.rb | 1 + db/seeds.rb | 1 - spec/models/access_spec.rb | 7 ++++- spec/models/activity_spec.rb | 11 +++---- spec/models/project_spec.rb | 6 ++-- spec/models/team_spec.rb | 2 ++ spec/models/todo_member_spec.rb | 6 ++++ spec/models/todo_spec.rb | 52 +++++++++++++++++++++++++++++++-- spec/models/user_spec.rb | 4 ++- 11 files changed, 98 insertions(+), 35 deletions(-) create mode 100644 spec/models/todo_member_spec.rb diff --git a/app/models/access.rb b/app/models/access.rb index f828642..28c8e8a 100644 --- a/app/models/access.rb +++ b/app/models/access.rb @@ -53,26 +53,26 @@ class Access < ApplicationRecord }.freeze ACCESS_GROUP_ADMIN = ACCESS_GROUP_MEMBER.deep_merge( - TEAM: - [ - AT[:INVITE_JOIN_TEAM], - AT[:CREATE_TEAM_PROJECTS], - AT[:READ_TEAM_MEMBER], - AT[:TEAM_MEMBER_MANAGE], - AT[:TEAM_MEMBER_AUTHORITY_MANAGE] - ] + TEAM: + [ + AT[:INVITE_JOIN_TEAM], + AT[:CREATE_TEAM_PROJECTS], + AT[:READ_TEAM_MEMBER], + AT[:TEAM_MEMBER_MANAGE], + AT[:TEAM_MEMBER_AUTHORITY_MANAGE] + ] ).freeze ACCESS_GROUP_SUPERADMIN = ACCESS_GROUP_ADMIN.deep_merge( - TEAM: - [ - AT[:INVITE_JOIN_TEAM], - AT[:CREATE_TEAM_PROJECTS], - AT[:READ_TEAM_MEMBER], - AT[:TEAM_MEMBER_MANAGE], - AT[:TEAM_MEMBER_AUTHORITY_MANAGE], - AT[:DELETE_TEAM_PROJECTS] - ] + TEAM: + [ + AT[:INVITE_JOIN_TEAM], + AT[:CREATE_TEAM_PROJECTS], + AT[:READ_TEAM_MEMBER], + AT[:TEAM_MEMBER_MANAGE], + AT[:TEAM_MEMBER_AUTHORITY_MANAGE], + AT[:DELETE_TEAM_PROJECTS] + ] ).freeze validates :access_type, presence: true diff --git a/app/models/project.rb b/app/models/project.rb index 7e0f700..dc404a0 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -11,14 +11,15 @@ def completed end end - def collaborators - Access + has_many :collaborators, + -> { + Access .where( subject_id: id, subject_type: self.class.name, access_type: Access::ACCESS_TYPE[:PROJECT_COLLABORATOR] - ).to_a.map(&:user) - end + ).map(&:user) + } validates :name, presence: true end diff --git a/app/models/team.rb b/app/models/team.rb index 9929fad..4a7fc2c 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -3,5 +3,6 @@ class Team < ApplicationRecord has_many :team_memberships has_many :members, through: :team_memberships has_many :projects + validates :name, presence: true end diff --git a/db/seeds.rb b/db/seeds.rb index 4cc5db2..0ceb866 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -68,7 +68,6 @@ # ifournight private - debugger ifournight_private = CreateTeam.new(creator_id: ifournight.id, team_name: 'ifournight_private').do AddTeamMember.new(team_id: ifournight_private.id, authorizer_id: ifournight.id, member_id: moerlang_cat.id, member_authority: TeamMembership::MEMBERSHIP_AUTHORITY[:MEMBER]).do diff --git a/spec/models/access_spec.rb b/spec/models/access_spec.rb index d351d63..2639009 100644 --- a/spec/models/access_spec.rb +++ b/spec/models/access_spec.rb @@ -8,6 +8,11 @@ end end +RSpec.describe Access, 'relations' do + it { should belong_to :user } + it { should belong_to :subject } +end + RSpec.describe Access, '.has_access' do it 'verify if user has centain types of access on subject' do user = create(:user) @@ -15,7 +20,7 @@ project = create(:project, team: team, creator: user) _access = Access.create( user_id: user.id, - type: Access::ACCESS_TYPE[:PROJECT_COLLABORATOR], + access_type: Access::ACCESS_TYPE[:PROJECT_COLLABORATOR], subject_id: project.id, subject_type: project.class.name ) diff --git a/spec/models/activity_spec.rb b/spec/models/activity_spec.rb index 253a134..3220979 100644 --- a/spec/models/activity_spec.rb +++ b/spec/models/activity_spec.rb @@ -1,10 +1,7 @@ require 'rails_helper' -RSpec.describe Activity, 'attributes' do - it 'serialize extra into Hash' do - extra = { key: 'value' } - activity = create(:activity, extra: extra) - - expect(activity.extra[:key]).to eq 'value' - end +RSpec.describe Activity, 'relations' do + it { should belong_to :user } + it { should belong_to :subject } + it { should belong_to :project } end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 0d9ed93..80909cb 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -6,7 +6,9 @@ end end -RSpec.describe Project, 'has many todos' do - it { should have_many :todos } +RSpec.describe Project, 'relations' do + it { should belong_to :team } it { should belong_to :creator } + it { should have_many :todos } + # it { should have_many :collaborators } end diff --git a/spec/models/team_spec.rb b/spec/models/team_spec.rb index e1e1ff7..1e37534 100644 --- a/spec/models/team_spec.rb +++ b/spec/models/team_spec.rb @@ -7,4 +7,6 @@ RSpec.describe Team, 'has many members' do it { should have_many :team_memberships } it { should have_many(:members).through(:team_memberships) } + it { should belong_to(:owner).class_name('User') } + it { should have_many(:projects) } end diff --git a/spec/models/todo_member_spec.rb b/spec/models/todo_member_spec.rb new file mode 100644 index 0000000..99421cd --- /dev/null +++ b/spec/models/todo_member_spec.rb @@ -0,0 +1,6 @@ +require 'rails_helper' + +RSpec.describe TodoMember, 'relations' do + it { should belong_to(:member) } + it { should belong_to(:todo) } +end diff --git a/spec/models/todo_spec.rb b/spec/models/todo_spec.rb index a5a5739..6417445 100644 --- a/spec/models/todo_spec.rb +++ b/spec/models/todo_spec.rb @@ -1,10 +1,58 @@ require 'rails_helper' -RSpec.describe Todo, '#validations' do +RSpec.describe Todo, 'validations' do it { is_expected.to validate_presence_of :title } end -RSpec.describe Todo, '#relations' do +RSpec.describe Todo, 'relations' do it { should belong_to(:creator) } it { should belong_to(:project) } + it { should have_many(:todo_members) } + it { should have_many(:members).through(:todo_members) } +end + +RSpec.describe Todo, '#over_due?' do + + before :each do + user = create(:user) + team = create(:team, owner: user) + project = create(:project, team: team, creator: user) + + @todo = create(:todo, project: project, creator: user) + end + + it 'return true when over_due' do + @todo.deadline = 1.day.ago + @todo.save! + + expect(@todo.over_due?).to eq true + end + + it 'return false when not over_due' do + @todo.deadline = Time.zone.now + 1.day + @todo.save! + + expect(@todo.over_due?).to eq false + end + +end + +RSpec.describe Todo, 'assigned_member' do + before :each do + @user = create(:user) + team = create(:team, owner: @user) + project = create(:project, team: team, creator: @user) + + @todo = create(:todo, project: project, creator: @user) + end + + it 'return nil when todo has no assigned' do + expect(@todo.assigned_member).to eq nil + end + + it 'return first of members if todo has any assigned members' do + TodoMember.create(todo_id: @todo.id, member_id: @user.id) + + expect(@todo.assigned_member.id).to eq @user.id + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 17b7976..b4560f4 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -11,5 +11,7 @@ RSpec.describe User, 'has many teams' do it { should have_many :team_memberships } it { should have_many :owned_teams } - it { should have_many(:joined_teams).through(:team_memberships).source(:member) } + it { should have_many(:joined_teams).through(:team_memberships).source(:team) } + it { should have_many(:todo_members) } + it { should have_many(:working_todos).through(:todo_members).source(:todo) } end