Skip to content

Commit

Permalink
Adds missing collaborations features (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhocquet authored Nov 29, 2019
1 parent 15f583f commit f744c42
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
30 changes: 23 additions & 7 deletions lib/boxr/collaborations.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
module Boxr
class Client

def folder_collaborations(folder, fields: [])
def folder_collaborations(folder, fields: [], offset: 0, limit: DEFAULT_LIMIT)
folder_id = ensure_id(folder)
query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
uri = "#{FOLDERS_URI}/#{folder_id}/collaborations"
collaborations = get_all_with_pagination(uri, query: query, offset: offset, limit: limit)
end

def file_collaborations(file, fields: [], limit: DEFAULT_LIMIT, marker: nil)
file_id = ensure_id(file)
query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
query[:limit] = limit
query[:marker] = marker unless marker.nil?

uri = "#{FILES_URI}/#{file_id}/collaborations"

collaborations, response = get(uri, query: query)
collaborations['entries']
end

def add_collaboration(folder, accessible_by, role, fields: [], notify: nil)
folder_id = ensure_id(folder)
def group_collaborations(group, offset: 0, limit: DEFAULT_LIMIT)
group_id = ensure_id(group)
uri = "#{GROUPS_URI}/#{group_id}/collaborations"

collaborations = get_all_with_pagination(uri, offset: offset, limit: limit)
end

def add_collaboration(item, accessible_by, role, fields: [], notify: nil, type: :folder)
item_id = ensure_id(item)
query = build_fields_query(fields, COLLABORATION_FIELDS_QUERY)
query[:notify] = notify unless notify.nil?

attributes = {item: {id: folder_id, type: :folder}}
attributes = {item: {id: item_id, type: type}}
attributes[:accessible_by] = accessible_by
attributes[:role] = validate_role(role)

Expand Down Expand Up @@ -60,9 +77,8 @@ def pending_collaborations(fields: [])
pending_collaborations['entries']
end


private

def validate_role(role)
case role
when :previewer_uploader
Expand All @@ -75,7 +91,7 @@ def validate_role(role)

role = role.to_s
raise BoxrError.new(boxr_message: "Invalid collaboration role: '#{role}'") unless VALID_COLLABORATION_ROLES.include?(role)

role
end
end
Expand Down
6 changes: 0 additions & 6 deletions lib/boxr/groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,5 @@ def delete_group_membership(membership)
result
end

def group_collaborations(group, offset: 0, limit: DEFAULT_LIMIT)
group_id = ensure_id(group)
uri = "#{GROUPS_URI}/#{group_id}/collaborations"
collaborations = get_all_with_pagination(uri, offset: offset, limit: limit)
end

end
end
2 changes: 1 addition & 1 deletion lib/boxr/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Boxr
VERSION = "1.13.1".freeze
VERSION = "1.14.0".freeze
end
31 changes: 29 additions & 2 deletions spec/boxr/collaborations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#rake spec SPEC_OPTS="-e \"invokes collaborations operations"\"
describe 'collaborations operations' do
it "invokes collaborations operations" do
puts "test setup"
new_file = BOX_CLIENT.upload_file("./spec/test_files/#{TEST_FILE_NAME}", @test_folder)
test_group = BOX_CLIENT.create_group(TEST_GROUP_NAME)
second_test_user = BOX_CLIENT.create_user("Second Test User", login: "second_test_user@#{('a'..'z').to_a.shuffle[0,10].join}.com", role: 'coadmin')

puts "add collaboration"
collaboration = BOX_CLIENT.add_collaboration(@test_folder, {id: @test_user.id, type: :user}, :viewer_uploader)
file_collaboration = BOX_CLIENT.add_collaboration(new_file, {id: second_test_user.id, type: :user}, :viewer, type: :file)
group_collaboration = BOX_CLIENT.add_collaboration(@test_folder, {id: test_group.id, type: :group}, :editor)
expect(collaboration.accessible_by.id).to eq(@test_user.id)
expect(file_collaboration.accessible_by.id).to eq(second_test_user.id)
expect(group_collaboration.accessible_by.id).to eq(test_group.id)

COLLABORATION = collaboration
FILE_COLLABORATION = file_collaboration
GROUP_COLLABORATION = group_collaboration

puts "inspect collaboration"
collaboration = BOX_CLIENT.collaboration(COLLABORATION)
Expand All @@ -16,20 +28,35 @@

puts "inspect folder collaborations"
collaborations = BOX_CLIENT.folder_collaborations(@test_folder)
expect(collaborations.count).to eq(1)
expect(collaborations.count).to eq(2)
expect(collaborations[0].id).to eq(COLLABORATION.id)

puts "inspect file collaborations"
collaborations = BOX_CLIENT.file_collaborations(new_file)
expect(collaborations.count).to eq(3)
expect(collaborations[0].id).to eq(FILE_COLLABORATION.id)

puts "inspect group collaborations"
collaborations = BOX_CLIENT.group_collaborations(test_group)
expect(collaborations.count).to eq(1)
expect(collaborations[0].id).to eq(GROUP_COLLABORATION.id)

puts "remove collaboration"
result = BOX_CLIENT.remove_collaboration(COLLABORATION)
expect(result).to eq({})
collaborations = BOX_CLIENT.folder_collaborations(@test_folder)
expect(collaborations.count).to eq(0)
expect(collaborations.count).to eq(1)

puts "inspect pending collaborations"
pending_collaborations = BOX_CLIENT.pending_collaborations
expect(pending_collaborations).to eq([])

puts "add invalid collaboration"
expect { BOX_CLIENT.add_collaboration(@test_folder, {id: @test_user.id, type: :user}, :invalid_role)}.to raise_error{Boxr::BoxrError}

puts "test teardown"
BOX_CLIENT.delete_file(new_file)
BOX_CLIENT.delete_group(test_group)
BOX_CLIENT.delete_user(second_test_user, force: true)
end
end
4 changes: 0 additions & 4 deletions spec/boxr/groups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@
group_memberships = BOX_CLIENT.group_memberships_for_user(@test_user)
expect(group_memberships.count).to eq(0)

puts "inspect group collaborations"
group_collaboration = BOX_CLIENT.add_collaboration(@test_folder, {id: test_group.id, type: :group}, :editor)
expect(group_collaboration.accessible_by.id).to eq(test_group.id)

puts "delete group"
response = BOX_CLIENT.delete_group(test_group)
expect(response).to eq({})
Expand Down

0 comments on commit f744c42

Please sign in to comment.