Add API datetime/format matcher for serialization specs, reduce factories (#33325)

This commit is contained in:
Matt Jankowski 2024-12-17 08:38:57 -05:00 committed by GitHub
parent 978142ac9e
commit ce5c33c65d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 127 additions and 96 deletions

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::AccountSerializer do
let(:record) { Fabricate :account, user: Fabricate(:user) }
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end
end

View file

@ -11,11 +11,11 @@ RSpec.describe REST::Admin::CohortSerializer do
it 'returns expected values' do
expect(subject)
.to include(
'data' => be_a(Array),
'period' => /2024-01-01/
'data' => be_a(Array).and(
all(include('date' => match_api_datetime_format))
),
'period' => match(/2024-01-01/).and(match_api_datetime_format)
)
expect { DateTime.rfc3339(subject['period']) }.to_not raise_error
subject['data'].each { |datum| expect { DateTime.rfc3339(datum['date']) }.to_not raise_error }
end
end
end

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::DomainAllowSerializer do
context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end
end
end

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::DomainBlockSerializer do
context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end
end
end

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::EmailDomainBlockSerializer do
context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
expect(subject)
.to include(
'created_at' => match_api_datetime_format
)
end
end
end

View file

@ -5,19 +5,15 @@ require 'rails_helper'
RSpec.describe REST::Admin::IpBlockSerializer do
subject { serialized_record_json(record, described_class) }
let(:record) { Fabricate(:ip_block) }
context 'when created_at is populated' do
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
end
end
context 'when expires_at is populated' do
context 'when timestamps are populated' do
let(:record) { Fabricate(:ip_block, expires_at: DateTime.new(2024, 11, 28, 16, 20, 0)) }
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['expires_at']) }.to_not raise_error
expect(subject)
.to include(
'created_at' => match_api_datetime_format,
'expires_at' => match_api_datetime_format
)
end
end
end

View file

@ -9,7 +9,10 @@ RSpec.describe REST::Admin::IpSerializer do
context 'when used_at is populated' do
it 'parses as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['used_at']) }.to_not raise_error
expect(subject)
.to include(
'used_at' => match_api_datetime_format
)
end
end
end

View file

@ -12,7 +12,12 @@ RSpec.describe REST::Admin::MeasureSerializer do
context 'when start_at is populated' do
it 'parses as RFC 3339 datetime' do
subject['data'].each { |datum| expect { DateTime.rfc3339(datum['date']) }.to_not raise_error }
expect(subject)
.to include(
'data' => all(
include('date' => match_api_datetime_format)
)
)
end
end
end

View file

@ -5,23 +5,15 @@ require 'rails_helper'
RSpec.describe REST::Admin::ReportSerializer do
subject { serialized_record_json(report, described_class) }
let(:report) { Fabricate(:report) }
context 'with created_at' do
it 'is serialized as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['created_at']) }.to_not raise_error
end
end
context 'with action_taken_at' do
let(:acting_account) { Fabricate(:account) }
before do
report.resolve!(acting_account)
end
context 'with timestamps' do
let(:report) { Fabricate(:report, action_taken_at: 3.days.ago) }
it 'is serialized as RFC 3339 datetime' do
expect { DateTime.rfc3339(subject['action_taken_at']) }.to_not raise_error
expect(subject)
.to include(
'action_taken_at' => match_api_datetime_format,
'created_at' => match_api_datetime_format
)
end
end
end