Skip to content

Commit

Permalink
[ajey] Fixed #118 It was not creating duplicate grouops, but adding s…
Browse files Browse the repository at this point in the history
…ame group twice, add check in host before adding same group to host groups
  • Loading branch information
ajeygore committed Aug 24, 2018
1 parent c128db0 commit 1a0da5d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
13 changes: 8 additions & 5 deletions app/models/host_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ def sysadmins
end

def add_host_group(name)
if name.squish.present?
name = "#{name.squish}_host_group"
self.add_group(name.squish.downcase)
name = name.squish
if name.present?
name = "#{name}_host_group"
self.add_group(name.downcase)
end
end

def add_group(name)
if name.squish.present?
self.groups << Group.find_or_initialize_by(name: name.squish.downcase)
name = name.squish
if name.present?
group = Group.find_or_initialize_by(name: name.downcase)
self.groups << group unless self.groups.include? group
self.save
end
end
Expand Down
18 changes: 17 additions & 1 deletion spec/controllers/nss_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,29 @@
expect(data["success"]).to eq(true)
end

it 'it shouldn\'t return sysadmins for invalid token' do
it 'should not return sysadmins for invalid token' do
json = { token: '', name: 'random_host', group_name: '', format: :json }
post 'add_host', json
body = response.body
expect(JSON.parse(body)['success']).to eq(false)
end

it "should not create groups and admins again for the same host name" do

create(:access_token, token: access_token)
json = { token: access_token, name: 'random_host', group_name: 'duplicate_group', format: :json }
post 'add_host', json
body = response.body
expect(JSON.parse(body)['success']).to eq(true)
expect(JSON.parse(body)['groups'].count).to eq 2

json = { token: access_token, name: 'random_host', group_name: 'duplicate_group', format: :json }
post 'add_host', json
body = response.body
expect(JSON.parse(body)['success']).to eq(true)
expect(JSON.parse(body)['groups'].count).to eq 2
end

it "should return sysadmins for that host" do
sign_in user
access_token = create(:access_token)
Expand Down
4 changes: 2 additions & 2 deletions spec/models/host_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
expect(groups.include?("#{host_machine.name.downcase}_host_group")).to eq(true)
end

it 'shouldn\'t add the group if the name is invalid' do
it 'should not add the group if the name is invalid' do
host_machine.add_host_group('')
groups = host_machine.groups.map(&:name)
expect(groups.include?("")).to eq(false)
Expand All @@ -119,7 +119,7 @@
expect(groups.include?(group_name.downcase)).to eq(true)
end

it 'shouldn\'t add the group if the name is invalid' do
it 'should not add the group if the name is invalid' do
host_machine.add_group('')
groups = host_machine.groups.map(&:name)
expect(groups.include?("")).to eq(false)
Expand Down

0 comments on commit 1a0da5d

Please sign in to comment.