Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

tests[Op#53117]: add gitlab model specs

Kabiru Mwenja6cbee5b3d574
diff --git a/modules/gitlab_integration/app/models/gitlab_issue.rb b/modules/gitlab_integration/app/models/gitlab_issue.rb
index 58ba450d1e4..61ad3c59f11 100644
--- a/modules/gitlab_integration/app/models/gitlab_issue.rb
+++ b/modules/gitlab_integration/app/models/gitlab_issue.rb
@@ -74,7 +74,7 @@ class GitlabIssue < ApplicationRecord
return if labels.nil?
return if labels.all? { |label| label.keys.sort == LABEL_KEYS }
- errors.add(:labels, 'invalid schema')
+ errors.add(:labels, :invalid_schema)
end
def with_logging
diff --git a/modules/gitlab_integration/config/locales/en.yml b/modules/gitlab_integration/config/locales/en.yml
index 76dbbd1af51..29eb8eaa3c9 100644
--- a/modules/gitlab_integration/config/locales/en.yml
+++ b/modules/gitlab_integration/config/locales/en.yml
@@ -28,6 +28,14 @@
#++
en:
+ activerecord:
+ errors:
+ models:
+ gitlab_issue:
+ attributes:
+ labels:
+ invalid_schema: "must be an array of hashes with keys: color, title"
+
project_module_gitlab: "Gitlab"
permission_show_gitlab_content: "Show Gitlab content"
@@ -47,32 +55,32 @@ en:
note_commit_referenced_comment: >
**Referenced in Commit:** [%{gitlab_user}](%{gitlab_user_url}) referenced this WP in
a Commit Note [%{commit_id}](%{commit_url}) on [%{repository}](%{repository_url}):
-
+
%{commit_note}
note_mr_referenced_comment: >
**Referenced in MR:** [%{gitlab_user}](%{gitlab_user_url}) referenced this WP in
Merge Request %{mr_number} [%{mr_title}](%{mr_url}) on [%{repository}](%{repository_url}):
-
+
%{mr_note}
note_mr_commented_comment: >
**Commented in MR:** [%{gitlab_user}](%{gitlab_user_url}) commented this WP in
Merge Request %{mr_number} [%{mr_title}](%{mr_url}) on [%{repository}](%{repository_url}):
-
+
%{mr_note}
note_issue_referenced_comment: >
**Referenced in Issue:** [%{gitlab_user}](%{gitlab_user_url}) referenced this WP in
Issue %{issue_number} [%{issue_title}](%{issue_url}) on [%{repository}](%{repository_url}):
-
+
%{issue_note}
note_issue_commented_comment: >
**Commented in Issue:** [%{gitlab_user}](%{gitlab_user_url}) commented this WP in
Issue %{issue_number} [%{issue_title}](%{issue_url}) on [%{repository}](%{repository_url}):
-
+
%{issue_note}
note_snippet_referenced_comment: >
**Referenced in Snippet:** [%{gitlab_user}](%{gitlab_user_url}) referenced this WP in
Snippet %{snippet_number} [%{snippet_title}](%{snippet_url}) on [%{repository}](%{repository_url}):
-
+
%{snippet_note}
issue_opened_referenced_comment: >
**Issue Opened:** Issue %{issue_number} [%{issue_title}](%{issue_url}) for [%{repository}](%{repository_url})
@@ -84,12 +92,12 @@ en:
**Issue Reopened:** Issue %{issue_number} [%{issue_title}](%{issue_url}) for [%{repository}](%{repository_url})
has been reopened by [%{gitlab_user}](%{gitlab_user_url}).
push_single_commit_comment: >
- **Pushed in MR:** [%{gitlab_user}](%{gitlab_user_url}) pushed [%{commit_number}](%{commit_url})
+ **Pushed in MR:** [%{gitlab_user}](%{gitlab_user_url}) pushed [%{commit_number}](%{commit_url})
to [%{repository}](%{repository_url}) at %{commit_timestamp}:
-
+
%{commit_note}
push_multiple_commits_comment: >
- **Pushed in MR:** [%{gitlab_user}](%{gitlab_user_url}) pushed multiple commits [%{commit_number}](%{commit_url})
+ **Pushed in MR:** [%{gitlab_user}](%{gitlab_user_url}) pushed multiple commits [%{commit_number}](%{commit_url})
to [%{repository}](%{repository_url}) at %{commit_timestamp}:
-
+
%{commit_note}
diff --git a/modules/gitlab_integration/spec/factories/gitlab_issues.rb b/modules/gitlab_integration/spec/factories/gitlab_issues.rb
new file mode 100644
index 00000000000..46516f3481f
--- /dev/null
+++ b/modules/gitlab_integration/spec/factories/gitlab_issues.rb
@@ -0,0 +1,56 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+FactoryBot.define do
+ factory :gitlab_issue do
+ gitlab_user
+
+ sequence(:number)
+ sequence(:gitlab_id)
+ state { 'opened' }
+ gitlab_html_url { "https://gitlab.com/test_user/test_repo/issues/#{number}" }
+
+ labels { [] }
+ gitlab_updated_at { Time.current }
+ sequence(:title) { |n| "Title of ISSUE #{n}" }
+ sequence(:body) { |n| "Body of ISSUE #{n}" }
+ sequence(:repository) { |n| "test_user/repo_#{n}" }
+
+ trait :partial do
+ body { nil }
+ end
+
+ trait :open do
+ state { 'opened' }
+ end
+
+ trait :closed do
+ state { 'closed' }
+ end
+ end
+end
diff --git a/modules/gitlab_integration/spec/factories/gitlab_users.rb b/modules/gitlab_integration/spec/factories/gitlab_users.rb
new file mode 100644
index 00000000000..34561c184af
--- /dev/null
+++ b/modules/gitlab_integration/spec/factories/gitlab_users.rb
@@ -0,0 +1,37 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+FactoryBot.define do
+ factory :gitlab_user do
+ sequence(:gitlab_id)
+ sequence(:gitlab_username) { |n| "user_#{n}" }
+ gitlab_name { "User #{gitlab_username}" }
+ gitlab_email { "[REDACTED]" }
+ gitlab_avatar_url { "https://gitlab.com/#{gitlab_username}_avatar.jpg" }
+ end
+end
diff --git a/modules/gitlab_integration/spec/models/gitlab_issue_spec.rb b/modules/gitlab_integration/spec/models/gitlab_issue_spec.rb
new file mode 100644
index 00000000000..5ca66a5e98f
--- /dev/null
+++ b/modules/gitlab_integration/spec/models/gitlab_issue_spec.rb
@@ -0,0 +1,179 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 GitlabIssue do
+ describe 'Associations' do
+ it { is_expected.to have_and_belong_to_many(:work_packages) }
+ it { is_expected.to belong_to(:gitlab_user).optional }
+ end
+
+ describe 'Validations' do
+ it { is_expected.to validate_presence_of :gitlab_html_url }
+ it { is_expected.to validate_presence_of :number }
+ it { is_expected.to validate_presence_of :repository }
+ it { is_expected.to validate_presence_of :state }
+ it { is_expected.to validate_presence_of :title }
+ it { is_expected.to validate_presence_of :gitlab_updated_at }
+
+ context 'when it is a partial issue' do
+ let(:issue) { build(:gitlab_issue, :partial) }
+
+ it { expect(issue).not_to validate_presence_of :body }
+ end
+
+ context 'when it is not a partial issue' do
+ let(:issue) { build(:gitlab_issue) }
+
+ it { expect(issue).to be_valid }
+ end
+
+ describe 'labels' do
+ it { is_expected.to allow_value(nil).for(:labels) }
+ it { is_expected.to allow_value([]).for(:labels) }
+ it { is_expected.to allow_value([{ 'color' => '#666', 'title' => 'grey' }]).for(:labels) }
+
+ it 'requires both color and title' do
+ expect(subject).not_to allow_value([{ 'title' => 'grey' }]).for(:labels)
+ end
+
+ it { is_expected.not_to allow_value([{}]).for(:labels) }
+
+ it 'returns an error message when invalid' do
+ issue = build(:gitlab_issue, labels: [{ 'title' => 'grey' }])
+ issue.valid?
+ expect(issue.errors[:labels]).to include('must be an array of hashes with keys: color, title')
+ end
+ end
+ end
+
+ describe 'Enums' do
+ let(:gitlab_issue) { build(:gitlab_issue) }
+
+ it do
+ expect(gitlab_issue).to define_enum_for(:state)
+ .with_values(opened: 'opened', closed: 'closed')
+ .backed_by_column_of_type(:string)
+ end
+ end
+
+ describe '.without_work_package' do
+ subject(:gitlab_issues_without_workpackages) { described_class.without_work_package }
+
+ let(:gitlab_issue_no_work_packages) { create(:gitlab_issue, work_packages: []) }
+ let(:gitlab_issue_with_work_packages) { create(:gitlab_issue, work_packages: create_list(:work_package, 1)) }
+
+ before do
+ gitlab_issue_no_work_packages
+ gitlab_issue_with_work_packages
+ end
+
+ it { expect(gitlab_issues_without_workpackages).to contain_exactly(gitlab_issue_no_work_packages) }
+ end
+
+ describe '.find_by_gitlab_identifiers' do
+ shared_let(:issue) { create(:gitlab_issue) }
+
+ it 'raises an ArgumentError when no id or url is provided' do
+ expect { described_class.find_by_gitlab_identifiers }.to raise_error(ArgumentError, 'needs an id or an url')
+ end
+
+ context 'when the gitlab_id attribute matches' do
+ it 'finds by gitlab_id' do
+ expect(described_class.find_by_gitlab_identifiers(id: issue.gitlab_id)).to eql(issue)
+ end
+ end
+
+ context 'when the gitlab_html_url attribute matches' do
+ it 'finds by gitlab_html_url' do
+ expect(described_class.find_by_gitlab_identifiers(url: issue.gitlab_html_url)).to eql(issue)
+ end
+ end
+
+ context 'when the provided gitlab_id does not match' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(id: issue.gitlab_id + 1)).to be_nil
+ end
+ end
+
+ context 'when the provided gitlab_html_url does not match' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(url: "#{issue.gitlab_html_url}zzzz"))
+ .to be_nil
+ end
+ end
+
+ context 'when neither match' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(id: issue.gitlab_id + 1,
+ url: "#{issue.gitlab_html_url}zzzz"))
+ .to be_nil
+ end
+ end
+
+ context 'when the provided gitlab_html_url does not match but the gitlab_id does' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(id: issue.gitlab_id,
+ url: "#{issue.gitlab_html_url}zzzz"))
+ .to eql issue
+ end
+ end
+
+ context 'when the provided gitlab_html_url does match but the gitlab_id does not' do
+ it 'is nil' do
+ expect(described_class.find_by_gitlab_identifiers(id: issue.gitlab_id + 1,
+ url: issue.gitlab_html_url))
+ .to eql issue
+ end
+ end
+
+ context 'when neither match but initialize is true' do
+ subject(:finder) do
+ described_class.find_by_gitlab_identifiers(id: issue.gitlab_id + 1,
+ url: "#{issue.gitlab_html_url}zzzz",
+ initialize: true)
+ end
+
+ it 'returns an issue' do
+ expect(finder).to be_a(described_class)
+ end
+
+ it 'returns a new record' do
+ expect(finder).to be_new_record
+ end
+
+ it 'has the provided attributes initialized' do
+ expect(finder.attributes.compact)
+ .to eql("gitlab_id" => issue.gitlab_id + 1,
+ "gitlab_html_url" => "#{issue.gitlab_html_url}zzzz")
+ end
+ end
+ end
+end
diff --git a/modules/gitlab_integration/spec/models/gitlab_user_spec.rb b/modules/gitlab_integration/spec/models/gitlab_user_spec.rb
new file mode 100644
index 00000000000..0c47d4d6496
--- /dev/null
+++ b/modules/gitlab_integration/spec/models/gitlab_user_spec.rb
@@ -0,0 +1,44 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 GitlabUser do
+ describe 'Associations' do
+ it { is_expected.to have_many(:gitlab_merge_requests) }
+ end
+
+ describe 'Validations' do
+ it { is_expected.to validate_presence_of :gitlab_id }
+ it { is_expected.to validate_presence_of :gitlab_name }
+ it { is_expected.to validate_presence_of :gitlab_username }
+ it { is_expected.to validate_presence_of :gitlab_email }
+ it { is_expected.to validate_presence_of :gitlab_avatar_url }
+ end
+end