Skip to content

Commit

Permalink
Added tests for identifier and status filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
nwalker2398 committed Feb 16, 2024
1 parent 8cf5c1a commit d296564
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/controllers/api/v1/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def index
statuses = ['pending', 'processing', 'failure', 'ready']

per_page = 50
status = statuses.include?(index_params[:status].downcase) ? statuses.index(index_params[:status].downcase) : index_params[:status]
status = status.is_a?(String) ? status.downcase : status
page = Integer(index_params[:page])
param_status = index_params[:status].is_a?(String) ? index_params[:status].downcase : index_params[:status]
status = statuses.include?(param_status) ? statuses.index(param_status) : param_status
page = index_params[:page] ? Integer(index_params[:page]) : 1
identifier = index_params[:identifier]

resources = Resource
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/components/resources/ResourceList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default function ResourceList() {
</tr>
</thead>
<tbody className=''>
{filteredResources.slice((pageState.pageNumber - 1) * 50, pageState.pageNumber * 50).map((resource) =>
{filteredResources.map((resource) =>
<tr key={resource.identifier}>
<td>{resource.identifier}</td>
<td>{resource.source_uri}</td>
Expand Down
62 changes: 62 additions & 0 deletions spec/requests/api/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,78 @@
{
'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
'featured_region' => resource.featured_region,
'height' => resource.height,
'id' => resources.index(resource) + 1,
'identifier' => resource.identifier,
'pcdm_type' => 'Image',
'source_uri' => resource.source_uri,
'status' => 'pending',
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}
end
expect(JSON.parse(response.body)).to eq(expected_response_json)
end

it 'properly filters resources by identifier' do
resources = identifiers.map do |identifier|
FactoryBot.create(:resource, identifier: identifier)
end
get_with_auth "/api/v1/resources?identifier=#{identifiers[1]}"
# puts "/api/v1/resources/identifier=#{identifiers[1]}"
expect(response).to have_http_status(:success)
resource = resources[1]
expected_response_json =
[{ 'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
'featured_region' => resource.featured_region,
'height' => resource.height,
'id' => resources.index(resource) + 1,
'identifier' => resource.identifier,
'pcdm_type' => 'Image',
'source_uri' => resource.source_uri,
'status' => 'pending',
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}]
expect(JSON.parse(response.body)).to eq(expected_response_json)
end

it 'properly filters resources by status' do
resources = [
FactoryBot.create(:resource, identifier: identifiers[0]),
FactoryBot.create(:resource, :ready, identifier: identifiers[1])
]
get_with_auth "/api/v1/resources?status=ready"
# puts "/api/v1/resources/identifier=#{identifiers[1]}"
expect(response).to have_http_status(:success)
resource = resources[1]
expected_response_json =
[{ 'accessed_at' => nil,
'created_at' => resource.created_at.to_time.iso8601(3),
'error_message' => nil,
'featured_region' => resource.featured_region,
'height' => resource.height,
'id' => resources.index(resource) + 1,
'identifier' => resource.identifier,
'pcdm_type' => 'Image',
'source_uri' => resource.source_uri,
'status' => 'ready',
'updated_at' => resource.updated_at.to_time.iso8601(3),
'width' => resource.width
}]
expect(JSON.parse(response.body)).to eq(expected_response_json)
end

# it 'does not accept unexpected paramaters', focus=true do
# # puts response
# # expect(response).to have_http_status(:success)
# expect_any_instance_of(ResourcesController).to recieve(:index).with({ identifier: 'test1', status: 'any', page: '1' }.with_indifferent_access)
# get_with_auth '/api/v1/resources?identifier=test1&status=any&page=1&secret=5'
# end
end
end
end

0 comments on commit d296564

Please sign in to comment.