Commit 53eec435 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'use_normalized_license_data_in_csv_export' into 'master'

Use normalized license data in csv export

See merge request gitlab-org/gitlab!78266
parents 143512cb dc19c25f
......@@ -394,9 +394,11 @@ class License < ApplicationRecord
self.data = file.read
end
def md5
normalized_data = self.data.gsub("\r\n", "\n").gsub(/\n+$/, '') + "\n"
def normalized_data
data.gsub("\r\n", "\n").gsub(/\n+$/, '') + "\n"
end
def md5
Digest::MD5.hexdigest(normalized_data)
end
......
......@@ -33,7 +33,7 @@ module HistoricalUserData
def header_csv
CSV.generate do |csv|
csv << ['License Key', license.data]
csv << ['License Key', license.normalized_data]
csv << ['Email', license.licensee_email]
csv << ['License Start Date', license.starts_at&.to_s(:csv)]
csv << ['License End Date', license.expires_at&.to_s(:csv)]
......
......@@ -807,6 +807,26 @@ RSpec.describe License do
end
end
describe '#normalized_data' do
it 'replaces carriage returns' do
other_license = build(:license, data: license.data.gsub("\n", "\r\n"))
expect(other_license.normalized_data).not_to include("\r\n")
end
it 'adds a trailing newline' do
other_license = build(:license, data: license.data.chomp)
expect(other_license.normalized_data).to end_with("\n")
end
it 'replaces multiple trailing newlines with a single trailing newline' do
other_license = build(:license, data: "#{license.data}\n\n\n")
expect(other_license.normalized_data).to end_with(/\n{1}$/)
end
end
describe "#md5" do
it "returns the same MD5 for licenses with carriage returns and those without" do
other_license = build(:license, data: license.data.gsub("\n", "\r\n"))
......
......@@ -32,7 +32,12 @@ RSpec.describe HistoricalUserData::CsvService do
end
it 'shows the license key' do
expect(csv[0][1]).to eq(current_license.data)
license = create(:license, data: current_license.data.gsub("\n", "\r\n"))
allow(License).to receive(:current).and_return(license)
expect(license.data).not_to eq(license.normalized_data)
expect(csv[0][1]).to eq(license.normalized_data)
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