-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1644, Validate date filters for /api/rest/customer/v1/origination-ac…
…tive-calls This update introduces a validation check ensuring that the "from_time" value within statistical ranges. This enhancement prevents incorrect configurations, ensuring the ranges are valid and improving data integrity.
- Loading branch information
1 parent
ec0d8b7
commit 1bf12c4
Showing
4 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module DateUtilities | ||
module_function | ||
|
||
def safe_datetime_parse(date_string) | ||
DateTime.parse(date_string) | ||
rescue ArgumentError, TypeError | ||
nil | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
spec/requests/api/rest/customer/v1/origination_active_calls_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Api::Rest::Customer::V1::OriginationActiveCallsController do | ||
include_context :json_api_customer_v1_helpers, type: :accounts | ||
|
||
let(:auth_headers) { { 'Authorization' => json_api_auth_token } } | ||
|
||
describe 'GET /api/rest/customer/v1/origination-active-calls' do | ||
subject { get '/api/rest/customer/v1/origination-active-calls', params: query, headers: auth_headers } | ||
|
||
shared_examples :responds_400 do |error_message| | ||
it 'responds with correct error data' do | ||
expect(CaptureError).not_to receive(:capture) | ||
subject | ||
|
||
expect(response.status).to eq 400 | ||
expect(response_json).to eq error: error_message | ||
end | ||
end | ||
|
||
shared_examples :responds_success do | ||
it 'responds with correct data' do | ||
expect(CaptureError).not_to receive(:capture) | ||
subject | ||
|
||
expect(response.status).to eq 200 | ||
expect(response_json).to eq active_calls_response_body | ||
end | ||
end | ||
|
||
let!(:account) { create(:account, contractor: customer) } | ||
let(:clickhouse_query) do | ||
<<-SQL.squish | ||
SELECT | ||
toUnixTimestamp(snapshot_timestamp) as t, | ||
toUInt32(count(*)) AS calls | ||
FROM active_calls | ||
WHERE | ||
customer_acc_id = {account_id: UInt32} AND | ||
snapshot_timestamp >= {from_time: DateTime} | ||
GROUP BY t | ||
ORDER BY t | ||
WITH FILL | ||
FROM toUnixTimestamp(toStartOfMinute({from_time: DateTime})) | ||
TO toUnixTimestamp(now()) | ||
STEP 60 | ||
FORMAT JSONColumnsWithMetadata | ||
SQL | ||
end | ||
let(:clickhouse_params) do | ||
{ | ||
param_account_id: account.id, | ||
param_from_time: from_time | ||
} | ||
end | ||
let!(:stub_clickhouse_query) do | ||
stub_request(:post, ClickHouse.config.url) | ||
.with( | ||
basic_auth: [ClickHouse.config.username, ClickHouse.config.password], | ||
query: { | ||
database: ClickHouse.config.database, | ||
**clickhouse_params, | ||
query: clickhouse_query, | ||
send_progress_in_http_headers: 1 | ||
} | ||
).to_return( | ||
status: active_calls_response_status, | ||
body: active_calls_response_body.to_json | ||
) | ||
end | ||
let(:active_calls_response_status) { 200 } | ||
let(:active_calls_response_body) { [{ call_id: 'abc123', duration: 123 }] } | ||
let(:from_time) { 1.day.ago.iso8601 } | ||
let(:query) do | ||
{ | ||
'account-id': account.uuid, | ||
'from-time': from_time | ||
} | ||
end | ||
|
||
it_behaves_like :responds_success | ||
|
||
context 'without params' do | ||
let(:query) { nil } | ||
let(:stub_clickhouse_query) { nil } | ||
|
||
include_examples :responds_400, 'missing required param(s) account-id, from-time' | ||
end | ||
|
||
context 'with from-time in the future', freeze_time: Time.parse('2024-12-01 00:00:00 UTC') do | ||
let(:query) { super().merge 'from-time': '2024-12-25T05:00:00Z', 'to-time': '2024-12-26T05:00:00Z' } | ||
|
||
it 'should NOT perform POST request to ClickHouse server and then return empty collection' do | ||
expect(CaptureError).not_to receive(:capture) | ||
subject | ||
|
||
expect(stub_clickhouse_query).not_to have_been_requested | ||
expect(response_json).to eq([]) | ||
end | ||
end | ||
|
||
context 'when the "from-time" is invalid Date (invalid)' do | ||
let(:query) { super().merge 'from-time': 'invalid', 'to-time': '2024-12-26T05:00:00Z' } | ||
|
||
include_examples :responds_400, 'invalid value from-time' | ||
end | ||
|
||
context 'when the "to-time" is invalid Date (invalid)', freeze_time: Time.parse('2024-12-05 00:00:00 UTC') do | ||
let(:query) { super().merge 'from-time': '2024-12-01T05:00:00Z', 'to-time': 'invalid' } | ||
|
||
include_examples :responds_400, 'invalid value to-time' | ||
end | ||
end | ||
end |