Skip to content

Commit 9b00cfe

Browse files
authored
Enable authorization handling in actionMany mutations (#160)
# Description ✍️ This PR enables the feature to authorize the operation of `updateMany` and `deleteMany` mutations of the Graphoid gem. #### `updateMany` mutations 1. `updateManyProjects` 2. `updateManySheets` 3. `updateManyGroups` 4. `updateManyColumns` 5. `updateManyRecords` 6. `updateManyUsers` 7. `updateManyRoles` 8. `updateManyAttachments` 9. `updateManyTriggers` 10. `updateManyFilters` 11. `updateManySheetModules` #### `deleteMany` mutations 13. `deleteManyProjects` 14. `deleteManySheets` 15. `deleteManyGroups` 16. `deleteManyColumns` 17. `deleteManyRecords` 18. `deleteManyUsers` 19. `deleteManyRoles` 20. `deleteManyAttachments` 21. `deleteManyTriggers` 22. `deleteManyFilters` 23. `deleteManySheetModules` # Checks ☑️ - [x] Enable authorization handling in actionMany mutations
1 parent 110d092 commit 9b00cfe

3 files changed

Lines changed: 77 additions & 21 deletions

File tree

lib/graphoid/mutations/update.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def self.build(model)
4747

4848
begin
4949
objects = Graphoid::Queries::Processor.execute(model, where.to_h)
50+
objects = model.resolve_filter(self, objects) if model.respond_to?(:resolve_filter)
5051
objects.update_all(attrs)
5152
objects.all.to_a
5253
rescue Exception => ex

spec/tester_mongo/spec/graphoid/mutations/delete_many_spec.rb

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,53 @@
33
require 'rails_helper'
44

55
describe 'MutationDeleteMany', type: :request do
6+
def query(value)
7+
%{
8+
mutation {
9+
deleteManyAccounts(where: { stringField: "#{value}" }){
10+
id
11+
stringField
12+
}
13+
}
14+
}
15+
end
16+
617
before { Account.delete_all }
7-
subject { Helper.resolve(self, @action, @query) }
18+
subject { Helper.resolve(self, 'deleteManyAccounts', query('bob')) }
819

920
let!(:a0) { Account.create!(string_field: 'bob') }
1021
let!(:a1) { Account.create!(string_field: 'bob') }
1122
let!(:a2) { Account.create!(string_field: 'oob') }
1223

1324
it 'deletes many objects by condition' do
14-
@action = 'deleteManyAccounts'
15-
16-
@query = %{
17-
mutation {
18-
deleteManyAccounts(where: { stringField: "bob" }){
19-
id
20-
}
21-
}
22-
}
23-
2425
expect(Account.count).to eq(3)
2526
subject
2627
expect(Account.count).to eq(1)
2728
expect(Account.first.string_field).to eq('oob')
2829
end
30+
31+
describe 'when there are errors in the mutation execution' do
32+
before do
33+
Account.class_eval do
34+
def self.resolve_filter(resolver, result)
35+
msg = 'User has insufficient privileges for this operation'
36+
raise GraphQL::ExecutionError, msg
37+
end
38+
end
39+
40+
@result = Helper.resolve(self, 'deleteManyAccounts', query('edimar'))
41+
end
42+
43+
after do
44+
Account.class_eval do
45+
def self.resolve_filter(resolver, result)
46+
result
47+
end
48+
end
49+
end
50+
51+
it 'should block the operation' do
52+
expect(response.body).to include('User has insufficient privileges for this operation')
53+
end
54+
end
2955
end

spec/tester_mongo/spec/graphoid/mutations/update_many_spec.rb

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,54 @@
33
require 'rails_helper'
44

55
describe 'MutationUpdateMany', type: :request do
6-
before { Account.delete_all }
7-
subject { Helper.resolve(self, 'updateManyAccounts', @query) }
8-
9-
let!(:a0) { Account.create!(string_field: 'account0', snake_case: 'snake', camelCase: 'camel') }
10-
let!(:a1) { Account.create!(string_field: 'account1', snake_case: 'snake', camelCase: 'camel') }
11-
let!(:a2) { Account.create!(string_field: 'account2', snake_case: 'snaki', camelCase: 'camel') }
12-
13-
it 'updates many objects by condition' do
14-
@query = %{
6+
def query(value)
7+
%{
158
mutation M {
16-
updateManyAccounts(where: { snakeCase: "snake" }, data: { camelCase: "updated" }){
9+
updateManyAccounts(where: { snakeCase: "snake" }, data: { camelCase: "#{value}" }){
1710
id
1811
camelCase
1912
}
2013
}
2114
}
15+
end
16+
17+
before { Account.delete_all }
18+
subject { Helper.resolve(self, 'updateManyAccounts', query('updated')) }
2219

20+
let!(:a0) { Account.create!(string_field: 'account0', snake_case: 'snake', camelCase: 'camel') }
21+
let!(:a1) { Account.create!(string_field: 'account1', snake_case: 'snake', camelCase: 'camel') }
22+
let!(:a2) { Account.create!(string_field: 'account2', snake_case: 'snaki', camelCase: 'camel') }
23+
24+
it 'updates many objects by condition' do
2325
expect(subject[0]['camelCase']).to eq('updated')
2426
expect(subject[1]['camelCase']).to eq('updated')
27+
expect(a0.reload.camelCase).to eq('updated')
28+
expect(a1.reload.camelCase).to eq('updated')
2529
expect(a2.reload.camelCase).to eq('camel')
2630
end
31+
32+
describe 'when there are errors in the mutation execution' do
33+
before do
34+
Account.class_eval do
35+
def self.resolve_filter(resolver, result)
36+
msg = 'User has insufficient privileges for this operation'
37+
raise GraphQL::ExecutionError, msg
38+
end
39+
end
40+
41+
@result = Helper.resolve(self, 'updateManyAccounts', query('edimar'))
42+
end
43+
44+
after do
45+
Account.class_eval do
46+
def self.resolve_filter(resolver, result)
47+
result
48+
end
49+
end
50+
end
51+
52+
it 'should block the operation' do
53+
expect(response.body).to include('User has insufficient privileges for this operation')
54+
end
55+
end
2756
end

0 commit comments

Comments
 (0)