Commit cc7f0482 authored by Felipe Artur's avatar Felipe Artur

Allow to create test cases

Allow to create issues with test case type
parent e8dcdf17
...@@ -73,7 +73,8 @@ class Issue < ApplicationRecord ...@@ -73,7 +73,8 @@ class Issue < ApplicationRecord
enum issue_type: { enum issue_type: {
issue: 0, issue: 0,
incident: 1 incident: 1,
test_case: 2 ## EE-only
} }
alias_attribute :parent_ids, :project_id alias_attribute :parent_ids, :project_id
......
...@@ -8002,6 +8002,11 @@ enum IssueType { ...@@ -8002,6 +8002,11 @@ enum IssueType {
Issue issue type Issue issue type
""" """
ISSUE ISSUE
"""
Test Case issue type
"""
TEST_CASE
} }
""" """
......
...@@ -22086,6 +22086,12 @@ ...@@ -22086,6 +22086,12 @@
"description": "Incident issue type", "description": "Incident issue type",
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
},
{
"name": "TEST_CASE",
"description": "Test Case issue type",
"isDeprecated": false,
"deprecationReason": null
} }
], ],
"possibleTypes": null "possibleTypes": null
# frozen_string_literal: true
module QualityManagement
module TestCases
class CreateService < BaseService
ISSUE_TYPE = 'test_case'
def initialize(project, current_user, title:, description:, label_ids: [])
super(project, current_user)
@title = title
@description = description
@label_ids = label_ids
end
def execute
return error(_('Test cases are not available for this project')) unless can_create_test_cases?
issue = Issues::CreateService.new(
project,
current_user,
issue_type: ISSUE_TYPE,
title: title,
description: description,
label_ids: label_ids
).execute
return error(issue.errors.full_messages.to_sentence, issue) unless issue.valid?
success(issue)
end
private
attr_reader :title, :description, :label_ids
def success(issue)
ServiceResponse.success(payload: { issue: issue })
end
def error(message, issue = nil)
ServiceResponse.error(payload: { issue: issue }, message: message)
end
def can_create_test_cases?
project.feature_available?(:quality_management) && Feature.enabled?(:quality_test_cases, project)
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe QualityManagement::TestCases::CreateService do
let_it_be(:user) { create(:user) }
let_it_be_with_refind(:project) { create(:project, :empty_repo) }
let(:description) { 'test case description' }
let_it_be(:label) { create(:label, project: project) }
let(:service) { described_class.new(project, user, title: title, description: description, label_ids: [label.id]) }
describe '#execute' do
before_all do
project.add_reporter(user)
end
context 'when test has title and description' do
let(:title) { 'test case title' }
let(:new_issue) { Issue.last! }
context 'when quality management license is not available' do
it 'responds with errors' do
expect(service.execute).to be_error
expect(service.execute.message).to eq("Test cases are not available for this project")
end
end
context 'when quality_management license is available' do
before do
stub_licensed_features(quality_management: true)
end
it 'responds with success' do
expect(service.execute).to be_success
end
it 'creates an test case issue' do
expect { service.execute }.to change(Issue, :count).by(1)
end
it 'created issue has correct attributes' do
service.execute
aggregate_failures do
expect(new_issue.title).to eq(title)
expect(new_issue.description).to eq(description)
expect(new_issue.author).to eq(user)
expect(new_issue.issue_type).to eq('test_case')
expect(new_issue.labels.map(&:title)).to eq([label.title])
end
end
context 'when quality_test_cases feature flag is disabled' do
before do
stub_feature_flags(quality_test_cases: false)
end
it 'responds with errors' do
expect(service.execute).to be_error
expect(service.execute.message).to eq("Test cases are not available for this project")
end
end
end
end
context 'when test case has no title' do
let(:title) { '' }
before do
stub_licensed_features(quality_management: true)
end
it 'does not create an issue' do
expect { service.execute }.not_to change(Issue, :count)
end
it 'responds with errors' do
expect(service.execute).to be_error
expect(service.execute.message).to eq("Title can't be blank")
end
it 'result payload contains an Issue object' do
expect(service.execute.payload[:issue]).to be_kind_of(Issue)
end
end
end
end
...@@ -24311,6 +24311,9 @@ msgstr "" ...@@ -24311,6 +24311,9 @@ msgstr ""
msgid "Test Cases" msgid "Test Cases"
msgstr "" msgstr ""
msgid "Test cases are not available for this project"
msgstr ""
msgid "Test coverage parsing" msgid "Test coverage parsing"
msgstr "" msgstr ""
......
...@@ -52,5 +52,9 @@ FactoryBot.define do ...@@ -52,5 +52,9 @@ FactoryBot.define do
factory :incident do factory :incident do
issue_type { :incident } issue_type { :incident }
end end
factory :quality_test_case do
issue_type { :test_case }
end
end end
end end
...@@ -133,6 +133,7 @@ RSpec.describe Issue do ...@@ -133,6 +133,7 @@ RSpec.describe Issue do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:issue) { create(:issue, project: project) } let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:incident) { create(:incident, project: project) } let_it_be(:incident) { create(:incident, project: project) }
let_it_be(:test_case) { create(:quality_test_case, project: project) }
it 'gives issues with the given issue type' do it 'gives issues with the given issue type' do
expect(described_class.with_issue_type('issue')) expect(described_class.with_issue_type('issue'))
...@@ -140,8 +141,8 @@ RSpec.describe Issue do ...@@ -140,8 +141,8 @@ RSpec.describe Issue do
end end
it 'gives issues with the given issue type' do it 'gives issues with the given issue type' do
expect(described_class.with_issue_type(%w(issue incident))) expect(described_class.with_issue_type(%w(issue incident test_case)))
.to contain_exactly(issue, incident) .to contain_exactly(issue, incident, test_case)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment