Commit c7774666 authored by Corinna Wiesner's avatar Corinna Wiesner

Use normalized license data in csv export

When exporting a license's usage data to csv, the file included the
stored data. For some licenses, this led to the license key to contain
carriage returns that made the file unreadable with `CSV.read`. This
change will use the normalized data that removes any carriage returns
from the license data.

Changelog: changed
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/78266
EE: true
parent 615752d1
......@@ -391,9 +391,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,7 @@ RSpec.describe HistoricalUserData::CsvService do
end
it 'shows the license key' do
expect(csv[0][1]).to eq(current_license.data)
expect(csv[0][1]).to eq(current_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