Skip to content

Commit

Permalink
add more validations around event timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
annvelents committed Dec 18, 2024
1 parent c8ed898 commit 39eb29b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/services/events/create_batch_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ def validate_events
event.external_subscription_id = event_params[:external_subscription_id]
event.properties = event_params[:properties] || {}
event.metadata = metadata || {}
event.timestamp = Time.zone.at(event_params[:timestamp] ? event_params[:timestamp].to_f : timestamp)
event.timestamp = Time.zone.at(event_params[:timestamp] ? Float(event_params[:timestamp]) : timestamp)
event.precise_total_amount_cents = event_params[:precise_total_amount_cents]

CalculateExpressionService.call(organization:, event:).raise_if_error!

result.events.push(event)
result.errors = result.errors.merge({index => event.errors.messages}) unless event.valid?
rescue ArgumentError
result.errors = result.errors.merge({index => {timestamp: ['invalid_format']}})
end
end

Expand Down
34 changes: 34 additions & 0 deletions spec/requests/api/v1/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,40 @@
expect(response).to have_http_status(:ok)
expect(json[:events].first[:external_subscription_id]).to eq(subscription.external_id)
end

context 'with invalid timestamp for one event' do
let(:batch_params) do
[
{
code: metric.code,
transaction_id: SecureRandom.uuid,
external_subscription_id: subscription.external_id,
timestamp: Time.current.to_i,
precise_total_amount_cents: '123.45',
properties: {
foo: 'bar'
}
},
{
code: metric.code,
transaction_id: SecureRandom.uuid,
external_subscription_id: subscription.external_id,
timestamp: Time.current.to_s,
precise_total_amount_cents: '123.45',
properties: {
foo: 'bar'
}
}
]
end

it 'returns an error indicating which event contained which error' do
expect { subject }.not_to change(Event, :count)

expect(response).to have_http_status(:unprocessable_entity)
expect(json[:error_details]).to eq({'1': {timestamp: ["invalid_format"]}})
end
end
end

describe 'GET /api/v1/events' do
Expand Down
26 changes: 26 additions & 0 deletions spec/services/events/validate_creation_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,5 +254,31 @@
end
end
end

context 'when timestamp is in a wrong format' do
let(:params) do
{external_customer_id: customer.external_id, code: billable_metric.code, transaction_id:, timestamp: '2025-01-01'}
end

it 'returns a timestamp_is_not_valid error' do
validate_event

expect(result).not_to be_success
expect(result.error).to be_a(BaseService::ValidationFailure)
expect(result.error.messages.keys).to include(:timestamp)
expect(result.error.messages[:timestamp]).to include('invalid_format')
end
end

context 'when timestamp is valid' do
let(:params) do
{external_customer_id: customer.external_id, code: billable_metric.code, transaction_id:, timestamp: Time.current.to_i + 0.11}
end

it 'does not raise any errors' do
expect(validate_event).to be_nil
expect(result).to be_success
end
end
end
end

0 comments on commit 39eb29b

Please sign in to comment.