Skip to content

Commit

Permalink
removes rails validation on verification; rely on database unique con…
Browse files Browse the repository at this point in the history
…straint

adds request spec for invalid request - duplicate verification
  • Loading branch information
andrew-1234 committed Feb 7, 2025
1 parent 42ba4bd commit a8c0762
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 34 deletions.
3 changes: 0 additions & 3 deletions app/models/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class Verification < ApplicationRecord
belongs_to :creator, class_name: 'User', inverse_of: :created_verifications
belongs_to :updater, class_name: 'User', inverse_of: :updated_verifications, optional: true

# A user can only have one verification per audio event and tag
validates :creator_id, uniqueness: { scope: [:audio_event_id, :tag_id] }

# Defines the possible values for confirmation
CONFIRMATION_TRUE = 'true'
CONFIRMATION_FALSE = 'false'
Expand Down
26 changes: 13 additions & 13 deletions spec/api/verifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@
let(:audio_recording_id) { audio_recording.id }
let(:audio_event_id) { audio_event.id }

path '/audio_recordings/{audio_recording_id}/audio_events/{audio_event_id}/verifications' do
path '/audio_recordings/{audio_recording_id}/audio_events/{audio_event_id}/verifications/filter' do
with_route_parameter(:audio_recording_id) { audio_recording_id }
with_route_parameter(:audio_event_id) { audio_event_id }

get('list verifications') do
post('filter verification') do
response(200, 'successful') do
schema_for_many
run_test! do
Expand All @@ -133,32 +133,32 @@
end
end

path '/audio_recordings/{audio_recording_id}/audio_events/{audio_event_id}/verifications/{id}' do
path '/audio_recordings/{audio_recording_id}/audio_events/{audio_event_id}/verifications' do
with_route_parameter(:audio_recording_id) { audio_recording_id }
with_route_parameter(:audio_event_id) { audio_event_id }

with_id_route_parameter
let(:id) { verification.id }

get('show verification') do
get('list verifications') do
response(200, 'successful') do
schema_for_single
schema_for_many
run_test! do
expect_id_matches(verification)
expect_at_least_one_item
end
end
end
end

path '/audio_recordings/{audio_recording_id}/audio_events/{audio_event_id}/verifications/filter' do
path '/audio_recordings/{audio_recording_id}/audio_events/{audio_event_id}/verifications/{id}' do
with_route_parameter(:audio_recording_id) { audio_recording_id }
with_route_parameter(:audio_event_id) { audio_event_id }

post('filter verification') do
with_id_route_parameter
let(:id) { verification.id }

get('show verification') do
response(200, 'successful') do
schema_for_many
schema_for_single
run_test! do
expect_at_least_one_item
expect_id_matches(verification)
end
end
end
Expand Down
15 changes: 0 additions & 15 deletions spec/models/verification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,8 @@
end
end

it 'does not allow for a duplicate creator_id, audio_event_id, and tag_id combination' do
v = create(:verification)
expect(build(:verification, creator_id: v.creator_id, audio_event_id: v.audio_event_id,
tag_id: v.tag_id)).not_to be_valid
end

it 'allows a user to have multiple verifications for different audio events and tags' do
v = create(:verification)
expect(build(:verification, creator_id: v.creator_id)).to be_valid
end

it 'allows nil for updater_id' do
expect(build(:verification, updater_id: nil)).to be_valid
end

it 'allows updater_id to be set' do
v = create(:verification)
expect(build(:verification, updater_id: v.creator_id)).to be_valid
end
end
23 changes: 20 additions & 3 deletions spec/permissions/verifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
end

the_users :admin, :reader, :writer, :another_writer, :owner,
can_do: Set[:show, :index],
can_do: Set[:index, :show, :filter],
fails_with: :not_found

the_user :anonymous, can_do: Set[:index], fails_with: [:not_found, :unauthorized]
the_user :anonymous, can_do: Set[:index, :filter], fails_with: [:not_found, :unauthorized]

the_user :invalid, can_do: nothing, fails_with: [:not_found, :unauthorized]

the_user :no_access, can_do: Set[:index], fails_with: [:not_found, :forbidden]
the_user :no_access, can_do: Set[:index, :filter], fails_with: [:not_found, :forbidden]

the_user :harvester, can_do: nothing, fails_with: [:not_found, :forbidden]
end
Expand All @@ -66,6 +66,23 @@
# creating a new tag to satisfy uniqueness constraint
{ audio_event_id: audio_event.id, tag_id: create(:tag).id }
}
send_update_body do
[{
'verification' => {
confirmed: 'false'
}
}, :json]
end

send_create_body do
[{
'verification' => {
confirmed: 'true',
audio_event_id: audio_event.id,
tag_id: create(:tag).id
}
}, :json]
end

let(:another_writer) do
create(:user, user_name: 'another_writer', skip_creation_email: true).tap do |user|
Expand Down
20 changes: 20 additions & 0 deletions spec/requests/verifications_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

describe 'Verifications' do
render_error_responses
create_entire_hierarchy

before do
Expand Down Expand Up @@ -48,4 +49,23 @@
expect(response).to have_http_status(:ok)
expect_number_of_items(3)
end

describe 'invalid requests' do
let(:payload) {
{
verification: {
tag_id: tag.id,
audio_event_id: audio_event.id,
confirmed: 'true'
}
}
}

it 'cannot create a duplicate verification' do
post '/verifications', params: payload, **api_with_body_headers(writer_token)

expect(response).to have_http_status(409)
expect_error(:conflict, 'The item must be unique.', nil)
end
end
end

0 comments on commit a8c0762

Please sign in to comment.