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 ...@@ -394,9 +394,11 @@ class License < ApplicationRecord
self.data = file.read self.data = file.read
end end
def md5 def normalized_data
normalized_data = self.data.gsub("\r\n", "\n").gsub(/\n+$/, '') + "\n" data.gsub("\r\n", "\n").gsub(/\n+$/, '') + "\n"
end
def md5
Digest::MD5.hexdigest(normalized_data) Digest::MD5.hexdigest(normalized_data)
end end
......
...@@ -33,7 +33,7 @@ module HistoricalUserData ...@@ -33,7 +33,7 @@ module HistoricalUserData
def header_csv def header_csv
CSV.generate do |csv| CSV.generate do |csv|
csv << ['License Key', license.data] csv << ['License Key', license.normalized_data]
csv << ['Email', license.licensee_email] csv << ['Email', license.licensee_email]
csv << ['License Start Date', license.starts_at&.to_s(:csv)] csv << ['License Start Date', license.starts_at&.to_s(:csv)]
csv << ['License End Date', license.expires_at&.to_s(:csv)] csv << ['License End Date', license.expires_at&.to_s(:csv)]
......
...@@ -807,6 +807,26 @@ RSpec.describe License do ...@@ -807,6 +807,26 @@ RSpec.describe License do
end end
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 describe "#md5" do
it "returns the same MD5 for licenses with carriage returns and those without" 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")) other_license = build(:license, data: license.data.gsub("\n", "\r\n"))
......
...@@ -32,7 +32,12 @@ RSpec.describe HistoricalUserData::CsvService do ...@@ -32,7 +32,12 @@ RSpec.describe HistoricalUserData::CsvService do
end end
it 'shows the license key' do 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
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