diff --git a/app/models/stream_events.rb b/app/models/stream_events.rb index e8c5a28..8ba995c 100644 --- a/app/models/stream_events.rb +++ b/app/models/stream_events.rb @@ -30,12 +30,12 @@ def self.user_group_ids?(event_data) def self.started_at(event_data) started_at = event_data.fetch('metadata')&.fetch('started_at', nil) - Time.parse(started_at) if started_at + Time.parse(started_at) rescue nil if started_at end def self.finished_at(event_data) finished_at = event_data.fetch('metadata')&.fetch('finished_at', nil) - Time.parse(finished_at) if finished_at + Time.parse(finished_at) rescue nil if finished_at end def self.session_time(event_data) diff --git a/spec/models/stream_events_spec.rb b/spec/models/stream_events_spec.rb index 6ff841f..c3fd16b 100644 --- a/spec/models/stream_events_spec.rb +++ b/spec/models/stream_events_spec.rb @@ -51,11 +51,22 @@ expect(StreamEvents.started_at(event_data)).to be_nil end + it 'returns started_at to nil if started_at cannot be parsed' do + # set started_at to some unparseable time string + event_metadata['started_at'] = '-1 OR 2+742-742-1=0+0+0+1 --' + expect(StreamEvents.started_at(event_data)).to be_nil + end + it 'returns finished_at to nil if no finished_at in metadata' do event_metadata.delete('finished_at') expect(StreamEvents.finished_at(event_data)).to be_nil end + it 'returns finished_at to nil if started_at cannot be parsed' do + event_metadata['finished_at'] = '-1 OR 2+742-742-1=0+0+0+1 --' + expect(StreamEvents.finished_at(event_data)).to be_nil + end + context 'user_group_ids?' do it 'returns true if classification done by user that belongs to multiple user groups' do expect(StreamEvents.user_group_ids?(event_data)).to be true