Skip to content

Commit

Permalink
[#57707] added unit test for group users query
Browse files Browse the repository at this point in the history
  • Loading branch information
Kharonus committed Oct 7, 2024
1 parent 2755f61 commit be96a3c
Show file tree
Hide file tree
Showing 5 changed files with 658 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,12 @@ def http_options
Util.ocs_api_request
end

# rubocop:disable Metrics/AbcSize
def handle_response(response)
error_data = StorageErrorData.new(source: self.class, payload: response)

case response
in { status: 200..299 }
group_users = Nokogiri::XML(response.body.to_s).xpath("/ocs/data/users/element").map(&:text)
info "#{group_users.size} users found"
ServiceResult.success(result: group_users)
handle_success_response(response)
in { status: 405 }
Util.error(:not_allowed, "Outbound request method not allowed", error_data)
in { status: 401 }
Expand All @@ -82,7 +79,22 @@ def handle_response(response)
end
end

# rubocop:enable Metrics/AbcSize
def handle_success_response(response)
error_data = StorageErrorData.new(source: self.class, payload: response)
xml = Nokogiri::XML(response.body.to_s)
statuscode = xml.xpath("/ocs/meta/statuscode").text

case statuscode
when "100"
group_users = xml.xpath("/ocs/data/users/element").map(&:text)
info "#{group_users.size} users found"
ServiceResult.success(result: group_users)
when "404"
Util.error(:group_does_not_exist, "Group does not exist", error_data)
else
Util.error(:error, "Unknown response body", error_data)
end
end
end
end
end
Expand Down
50 changes: 0 additions & 50 deletions modules/storages/spec/common/storages/peripherals/registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,56 +56,6 @@
end
end

describe "#group_users_query" do
let(:expected_response_body) do
<<~XML
<?xml version="1.0"?>
<ocs>
<meta>
<status>ok</status>
<statuscode>100</statuscode>
<message>OK</message>
<totalitems></totalitems>
<itemsperpage></itemsperpage>
</meta>
<data>
<users>
<element>admin</element>
<element>OpenProject</element>
<element>reader</element>
<element>TestUser</element>
<element>TestUser34</element>
</users>
</data>
</ocs>
XML
end
let(:expected_response) do
{
status: 200,
body: expected_response_body,
headers: {}
}
end

before do
stub_request(:get, "https://example.com/ocs/v1.php/cloud/groups/#{storage.group}")
.with(
headers: {
"Authorization" => "Basic T3BlblByb2plY3Q6T3BlblByb2plY3RTZWN1cmVQYXNzd29yZA==",
"OCS-APIRequest" => "true"
}
)
.to_return(expected_response)
end

it "responds with a strings array with group users" do
result = registry.resolve("nextcloud.queries.group_users").call(storage:)
expect(result).to be_success
expect(result.result).to eq(%w[admin OpenProject reader TestUser TestUser34])
end
end

describe "#delete_folder_command" do
let(:auth_strategy) { Storages::Peripherals::StorageInteraction::AuthenticationStrategies::BasicAuth.strategy }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

require "spec_helper"
require_module_spec_helper

RSpec.describe Storages::Peripherals::StorageInteraction::Nextcloud::GroupUsersQuery, :webmock do
include NextcloudGroupUserHelper

let(:storage) { create(:nextcloud_storage_with_local_connection, :as_automatically_managed, username: "vcr") }
let(:auth_strategy) { Storages::Peripherals::Registry.resolve("nextcloud.authentication.userless").call }

describe "basic command setup" do
it "is registered as queries.group_users" do
expect(Storages::Peripherals::Registry
.resolve("#{storage}.queries.group_users")).to eq(described_class)
end

it "responds to #call with correct parameters" do
expect(described_class).to respond_to(:call)

method = described_class.method(:call)
expect(method.parameters).to contain_exactly(%i[keyreq storage],
%i[keyreq auth_strategy],
%i[keyreq group])
end
end

context "if group exists", vcr: "nextcloud/group_users_success" do
let(:user1) { "[email protected]" }
let(:user2) { "[email protected]" }
let(:user3) { "[email protected]" }
let(:group) { "Sith Assassins" }

before do
create_group(auth, storage, group)
add_user_to_group(user1, group)
add_user_to_group(user2, group)
end

after do
remove_group(auth, storage, group)
end

it "returns a success" do
result = described_class.call(storage:, auth_strategy:, group:)
expect(result).to be_success
expect(result.result).to include(user1, user2)
expect(result.result).not_to include(user3)
end
end

context "if group does not exist", vcr: "nextcloud/group_users_not_existing_group" do
let(:user) { "[email protected]" }
let(:group) { "Sith Assassins" }

it "returns a failure" do
result = described_class.call(storage:, auth_strategy:, group:)
expect(result).to be_failure

error = result.errors
expect(error.code).to eq(:group_does_not_exist)
expect(error.data.source).to eq(described_class)
end
end

private

def auth = Storages::Peripherals::StorageInteraction::Authentication[auth_strategy]

def add_user_to_group(user, group)
Storages::Peripherals::StorageInteraction::Nextcloud::AddUserToGroupCommand
.call(storage:, auth_strategy:, user:, group:)
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit be96a3c

Please sign in to comment.