Skip to content

Commit

Permalink
[frontend][ci] Add specs for Relationship adding methods
Browse files Browse the repository at this point in the history
Added specs for the #add_group and #add_user for increasing the
coverage of this class.
  • Loading branch information
Moises Deniz Aleman committed Feb 12, 2018
1 parent d8ac290 commit 72d6d24
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/api/app/models/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def self.add_user(obj, user, role, ignore_lock = nil, check = nil)
role = Role.find_by_title!(role) unless role.is_a? Role
if role.global
# only nonglobal roles may be set in an object
raise SaveError, "tried to set global role '#{role.title}' for user '#{user}' in #{obj.class} '#{name}'"
raise SaveError, "tried to set global role '#{role.title}' for user '#{user}' in #{obj.class} '#{obj.name}'"
end

user = User.find_by_login!(user) unless user.is_a? User
Expand All @@ -89,7 +89,7 @@ def self.add_group(obj, group, role, ignore_lock = nil, check = nil)

if role.global
# only nonglobal roles may be set in an object
raise SaveError, "tried to set global role '#{role_title}' for group '#{group}' in #{obj.class} '#{name}'"
raise SaveError, "tried to set global role '#{role.title}' for group '#{group}' in #{obj.class} '#{obj.name}'"
end

group = Group.find_by_title(group.to_s) unless group.is_a? Group
Expand Down
83 changes: 77 additions & 6 deletions src/api/spec/models/relationship_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'rails_helper'

RSpec.describe Relationship do
let(:admin_user) { create(:admin_user, login: 'admin') }
let(:global_role) { create(:role, title: 'global_role', global: true) }
let(:normal_role) { create(:role, title: 'normal_role', global: false) }

before(:all) do
@caching_state = ActionController::Base.perform_caching
ActionController::Base.perform_caching = true
Expand All @@ -10,21 +14,88 @@
ActionController::Base.perform_caching = @caching_state
end

it '.add_user' do
skip
describe '.add_user' do
let(:role) { normal_role }
let(:user) { create(:confirmed_user, login: 'other_user') }
let(:project) { user.home_project }

before do
login(admin_user)
end

subject { Relationship.add_user(project, user, role, true, true) }

context 'with a global role' do
let(:role) { global_role }

it { expect { subject }.to raise_error(Relationship::SaveError, /tried to set global role/) }
end

context 'with an already existing relationship' do
before do
project.relationships.create(user: user, role: role)
end

it { expect { subject }.to raise_error(Relationship::SaveError, 'Relationship already exists') }
end

context 'with invalid relationship data' do
skip('This is imposible to happen with the actual validations and how the object is created')
end

context 'with valid data' do
before do
subject
end

it { expect { project.store }.to change { Relationship.count }.by(1) }
end
end

it '.add_group' do
skip
describe '.add_group' do
let(:role) { normal_role }
let(:user) { admin_user }
let(:project) { user.home_project }
let(:group) { create(:group) }

before do
login(admin_user)
end

subject { Relationship.add_group(project, group, role, true, true) }

context 'with a global role' do
let(:role) { global_role }

it { expect { subject }.to raise_error(Relationship::SaveError, /tried to set global role/) }
end

context 'with an already existing relationship' do
before do
project.relationships.create(group: group, role: role)
end

it { expect { subject }.to raise_error(Relationship::SaveError, 'Relationship already exists') }
end

context 'with invalid relationship data' do
skip('This is imposible to happen with the actual validations and how the object is created')
end

context 'with valid data' do
before do
subject
end

it { expect { project.store }.to change { Relationship.count }.by(1) }
end
end

describe '.forbidden_project_ids' do
let(:confirmed_user) { create(:confirmed_user) }
let(:project) { create(:forbidden_project) }

context 'for admins' do
let(:admin_user) { create(:admin_user) }

before do
login(admin_user)
end
Expand Down

0 comments on commit 72d6d24

Please sign in to comment.