Skip to content

Commit

Permalink
Fix paper trail when user removed from group (#178)
Browse files Browse the repository at this point in the history
* group#remove_user should create paper trail with event destroy

* GroupAssociation.revoke_expired should create paper trail with event destroy when found expired associations
  • Loading branch information
walbertus authored and giosakti committed Jan 24, 2020
1 parent 78b23a9 commit 9178ca7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def add_user_with_expiration(user_id, expiration_date)
end

def remove_user(user_id)
group_associations.where(user_id: user_id).delete_all
group_associations.where(user_id: user_id).destroy_all
burst_host_cache
end
end
2 changes: 1 addition & 1 deletion app/models/group_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class GroupAssociation < ApplicationRecord
belongs_to :group

def self.revoke_expired(date = Date.today)
where('expiration_date < ?', date).delete_all
where('expiration_date < ?', date).destroy_all
end
end
32 changes: 23 additions & 9 deletions spec/models/group_association_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
require 'rails_helper'

describe GroupAssociation, type: :model do
describe '.revoke_expired' do
it 'should revoke associations that expired yesterday' do
user = create(:user)
group = create(:group)
group.add_user_with_expiration(user, Date.today - 1)
let(:user) { create(:user) }
let(:group) { create(:group) }

GroupAssociation.revoke_expired
context 'when found expired associations' do
it 'should revoke associations' do
group.add_user_with_expiration(user, Date.today - 1)

GroupAssociation.revoke_expired

expired_association = GroupAssociation.where('expiration_date < ?', Date.today).count
expect(expired_association).to eq 0
end

it 'should create paper trail with event destroy' do
group_association = group.add_user_with_expiration(user, Date.today - 1)

GroupAssociation.revoke_expired

expired_association = GroupAssociation.where('expiration_date < ?', Date.today).count
expect(expired_association).to eq 0
versions = PaperTrail::Version.
with_item_keys(GroupAssociation.name, group_association.id).
where(event: 'destroy')
expect(versions.length).to eq(1)
end
end

it 'should not revoke associations that not expired yet' do
user = create(:user)
group = create(:group)
group.add_user_with_expiration(user, Date.today)

GroupAssociation.revoke_expired
Expand Down
11 changes: 11 additions & 0 deletions spec/models/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@
expect(group.users.map(&:id).include?(user.id)).to eq(false)
end

it 'should create paper trail with event destroy' do
group.add_user(user.id)
group_association_id = group.group_associations.where(user_id: user.id).first.id
group.remove_user(user.id)

versions = PaperTrail::Version.
with_item_keys(GroupAssociation.name, group_association_id).
where(event: 'destroy')
expect(versions.length).to eq(1)
end

it 'should burst host cache' do
group.add_user(user.id)

Expand Down

0 comments on commit 9178ca7

Please sign in to comment.