Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

547 lines
18 KiB
Ruby
Raw Permalink Normal View History

2025-05-05 09:29:55 +02:00
# frozen_string_literal: true
2014-07-24 17:42:26 +02:00
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2014-07-24 17:42:26 +02:00
#
# 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:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2014-07-24 17:42:26 +02:00
# 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.
2014-07-24 17:42:26 +02:00
#++
require "spec_helper"
2023-05-31 12:15:15 +02:00
RSpec.describe SysController, with_settings: { sys_api_enabled: true } do
2021-02-11 16:02:18 +01:00
let(:commit_role) do
2023-10-05 15:28:31 +02:00
create(:project_role, permissions: %i[commit_access browse_repository])
2021-02-11 16:02:18 +01:00
end
2023-10-05 15:28:31 +02:00
let(:browse_role) { create(:project_role, permissions: [:browse_repository]) }
let(:guest_role) { create(:project_role, permissions: []) }
let(:valid_user_password) { "Top Secret Password" }
2021-02-11 16:02:18 +01:00
let(:valid_user) do
2022-01-24 19:22:35 +01:00
create(:user,
login: "johndoe",
password: valid_user_password,
password_confirmation: valid_user_password)
2021-02-11 16:02:18 +01:00
end
let(:api_key) { "12345678" }
let(:public) { false }
2022-01-24 19:22:35 +01:00
let(:project) { create(:project, public:) }
2021-05-04 16:55:34 +02:00
let!(:repository_project) do
2022-01-24 19:22:35 +01:00
create(:project, public: false, members: { valid_user => [browse_role] })
2021-05-04 16:55:34 +02:00
end
before do
2022-01-24 19:22:35 +01:00
create(:non_member, permissions: [:browse_repository])
DeletedUser.first # creating it first in order to avoid problems with should_receive
allow(Setting).to receive(:sys_api_key).and_return(api_key)
2016-04-25 09:06:13 +02:00
Rails.cache.clear
RequestStore.clear!
end
describe "svn" do
2022-01-24 19:22:35 +01:00
let!(:repository) { create(:repository_subversion, project:) }
describe "repo_auth" do
context "for valid login, but no access to repo_auth" do
before do
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: "without-access",
method: "GET" }
end
it "responds 403 not allowed" do
expect(response.code).to eq("403")
expect(response.body).to eq("Not allowed")
end
end
context "for valid login and user has read permission (role reporter) for project" do
before do
2022-01-24 19:22:35 +01:00
create(:member,
user: valid_user,
roles: [browse_role],
project:)
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
end
it "responds 200 okay dokay for GET" do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET" }
expect(response.code).to eq("200")
end
it "responds 403 not allowed for POST" do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "POST" }
expect(response.code).to eq("403")
end
end
context "for valid login and user has rw permission (role developer) for project" do
before do
2022-01-24 19:22:35 +01:00
create(:member,
user: valid_user,
roles: [commit_role],
project:)
valid_user.save
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
end
it "responds 200 okay dokay for GET" do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET" }
expect(response.code).to eq("200")
end
it "responds 200 okay dokay for POST" do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "POST" }
expect(response.code).to eq("200")
end
end
context "for invalid login and user has role manager for project" do
before do
2022-01-24 19:22:35 +01:00
create(:member,
user: valid_user,
roles: [commit_role],
project:)
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password + "made invalid"
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET" }
end
it "responds 401 auth required" do
expect(response.code).to eq("401")
end
end
context "for valid login and user is not member for project" do
before do
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET" }
end
it "responds 403 not allowed" do
expect(response.code).to eq("403")
end
end
context "for valid login and project is public" do
let(:public) { true }
before do
2022-01-24 19:22:35 +01:00
random_project = create(:project, public: false)
create(:member,
user: valid_user,
roles: [browse_role],
project: random_project)
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET" }
end
it "responds 200 OK" do
expect(response.code).to eq("200")
end
end
context "for invalid credentials" do
before do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: "any-repo",
method: "GET" }
end
it "responds 401 auth required" do
expect(response.code).to eq("401")
expect(response.body).to eq("Authorization required")
end
end
context "for invalid api key" do
it "responds 403 for valid username/password" do
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: "not_the_api_key",
repository: "any-repo",
method: "GET" }
expect(response.code).to eq("403")
expect(response.body)
.to eq("Access denied. Repository management WS is disabled or key is invalid.")
end
it "responds 403 for invalid username/password" do
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
"invalid",
"invalid"
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: "not_the_api_key",
repository: "any-repo",
method: "GET" }
expect(response.code).to eq("403")
expect(response.body)
.to eq("Access denied. Repository management WS is disabled or key is invalid.")
end
end
end
end
describe "git" do
2022-01-24 19:22:35 +01:00
let!(:repository) { create(:repository_git, project:) }
describe "repo_auth" do
context "for valid login, but no access to repo_auth" do
before do
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: "without-access",
method: "GET",
git_smart_http: "1",
uri: "/git",
location: "/git" }
end
it "responds 403 not allowed" do
expect(response.code).to eq("403")
expect(response.body).to eq("Not allowed")
end
end
context "for valid login and user has read permission (role reporter) for project" do
before do
2022-01-24 19:22:35 +01:00
create(:member,
user: valid_user,
roles: [browse_role],
project:)
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
end
it "responds 200 okay dokay for read-only access" do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET",
git_smart_http: "1",
uri: "/git",
location: "/git" }
expect(response.code).to eq("200")
end
it "responds 403 not allowed for write (push)" do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "POST",
git_smart_http: "1",
uri: "/git/#{project.identifier}/git-receive-pack",
location: "/git" }
expect(response.code).to eq("403")
end
end
context "for valid login and user has rw permission (role developer) for project" do
before do
2022-01-24 19:22:35 +01:00
create(:member,
user: valid_user,
roles: [commit_role],
project:)
valid_user.save
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
end
it "responds 200 okay dokay for GET" do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET",
git_smart_http: "1",
uri: "/git",
location: "/git" }
expect(response.code).to eq("200")
end
it "responds 200 okay dokay for POST" do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "POST",
git_smart_http: "1",
uri: "/git/#{project.identifier}/git-receive-pack",
location: "/git" }
expect(response.code).to eq("200")
end
end
context "for invalid login and user has role manager for project" do
before do
2022-01-24 19:22:35 +01:00
create(:member,
user: valid_user,
roles: [commit_role],
project:)
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password + "made invalid"
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET",
git_smart_http: "1",
uri: "/git",
location: "/git" }
end
it "responds 401 auth required" do
expect(response.code).to eq("401")
end
end
context "for valid login and user is not member for project" do
before do
2022-01-24 19:22:35 +01:00
project = create(:project, public: false)
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET",
git_smart_http: "1",
uri: "/git",
location: "/git" }
end
it "responds 403 not allowed" do
expect(response.code).to eq("403")
end
end
context "for valid login and project is public" do
let(:public) { true }
before do
2022-01-24 19:22:35 +01:00
random_project = create(:project, public: false)
create(:member,
user: valid_user,
roles: [browse_role],
project: random_project)
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: project.identifier,
method: "GET",
git_smart_http: "1",
uri: "/git",
location: "/git" }
end
it "responds 200 OK" do
expect(response.code).to eq("200")
end
end
context "for invalid credentials" do
before do
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: api_key,
repository: "any-repo",
method: "GET",
git_smart_http: "1",
uri: "/git",
location: "/git" }
end
it "responds 401 auth required" do
expect(response.code).to eq("401")
expect(response.body).to eq("Authorization required")
end
end
context "for invalid api key" do
it "responds 403 for valid username/password" do
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: "not_the_api_key",
repository: "any-repo",
method: "GET",
git_smart_http: "1",
uri: "/git",
location: "/git" }
expect(response.code).to eq("403")
expect(response.body)
.to eq("Access denied. Repository management WS is disabled or key is invalid.")
end
it "responds 403 for invalid username/password" do
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
"invalid",
"invalid"
)
2021-02-11 16:02:18 +01:00
post "repo_auth", params: { key: "not_the_api_key",
repository: "any-repo",
method: "GET",
git_smart_http: "1",
uri: "/git",
location: "/git" }
expect(response.code).to eq("403")
expect(response.body)
.to eq("Access denied. Repository management WS is disabled or key is invalid.")
end
end
end
end
2024-12-06 09:14:42 +01:00
describe "#fetch_changesets" do
let(:params) { { id: repository_project.identifier } }
before do
request.env["HTTP_AUTHORIZATION"] =
ActionController::HttpAuthentication::Basic.encode_credentials(
valid_user.login,
valid_user_password
)
allow_any_instance_of(Repository::Subversion).to receive(:fetch_changesets).and_return(true)
get "fetch_changesets", params: params.merge({ key: api_key })
end
context "with a project identifier" do
it "is successful" do
expect(response)
.to have_http_status(:ok)
end
end
context "without a project identifier" do
let(:params) { {} }
it "is successful" do
expect(response)
.to have_http_status(:ok)
end
end
context "for an unknown project" do
let(:params) { { id: 0 } }
it "returns 404" do
expect(response)
.to have_http_status(:not_found)
end
end
context "when disabled", with_settings: { sys_api_enabled?: false } do
it "is 403 forbidden" do
expect(response)
.to have_http_status(:forbidden)
end
end
end
end