# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Issues::BuildFromVulnerabilityService do
  describe '#execute' do
    let_it_be(:group)   { create(:group) }
    let_it_be(:project) { create(:project, :public, :repository, namespace: group) }
    let_it_be(:user)    { create(:user) }

    before_all do
      group.add_developer(user)
    end

    before do
      stub_licensed_features(security_dashboard: true)
    end

    it 'builds the issue with the given params' do
      vulnerability = create(:vulnerability, :with_finding, project: project)
      service = described_class.new(project: project, current_user: user, params: { vulnerability: vulnerability })

      issue = service.execute

      expect(issue).not_to be_persisted
      expect(issue).to have_attributes(
        project: project,
        author: user,
        title: "Investigate vulnerability: #{vulnerability.title}",
        description:
          <<~DESC
            Issue created from vulnerability <a href="http://localhost/#{group.name}/#{project.name}/-/security/vulnerabilities/#{vulnerability.id}">#{vulnerability.id}</a>

            ### Description:

            Description of #{vulnerability.title}

            * Severity: #{vulnerability.severity}
            * Confidence: #{vulnerability.confidence}
            * Location: [maven/src/main/java/com/gitlab/security_products/tests/App.java:29](http://localhost/#{project.full_path}/-/blob/master/maven/src/main/java/com/gitlab/security_products/tests/App.java#L29)

            ### Solution:

            #{vulnerability.solution}

            ### Identifiers:

            * [CVE-2018-1234](http://cve.mitre.org/cgi-bin/cvename.cgi?name=2018-1234)

            ### Links:

            * [Cipher does not check for integrity first?](https://crypto.stackexchange.com/questions/31428/pbewithmd5anddes-cipher-does-not-check-for-integrity-first)

            ### Scanner:

            * Name: Find Security Bugs
          DESC
      )
    end

    context 'when a vulnerability has remediations' do
      it 'displays Remediations section with attached diff' do
        vulnerability = create(:vulnerability, :with_remediation, project: project)
        service = described_class.new(project: project, current_user: user, params: { vulnerability: vulnerability })

        issue = service.execute

        expect(issue.description).to match(/Remediations/)
        expect(issue.description).to match(/This is a diff/)
      end
    end
  end
end
