Skip to content

Commit

Permalink
Update models spec
Browse files Browse the repository at this point in the history
  • Loading branch information
ifournight committed Apr 8, 2017
1 parent 9029a16 commit 5421068
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 35 deletions.
34 changes: 17 additions & 17 deletions app/models/access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion spec/models/access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@
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)
team = create(:team, owner: user)
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
)
Expand Down
11 changes: 4 additions & 7 deletions spec/models/activity_spec.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions spec/models/team_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions spec/models/todo_member_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'rails_helper'

RSpec.describe TodoMember, 'relations' do
it { should belong_to(:member) }
it { should belong_to(:todo) }
end
52 changes: 50 additions & 2 deletions spec/models/todo_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5421068

Please sign in to comment.