Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix missing test assertions #2133

Merged
merged 10 commits into from
Feb 3, 2025
12 changes: 7 additions & 5 deletions test/api_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def api_get_test(template, res)
get member_url(res), as: :json, headers: { 'Authorization' => read_access_auth }
assert_response :success

validate_json response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response"
assert_nothing_raised do
validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response")
end

expected = template
actual = JSON.parse(response.body)
Expand All @@ -48,15 +50,15 @@ def api_get_test(template, res)

def api_post_test(template)
expected = template
validate_json template.to_json, "#/components/schemas/#{singular_name.camelize(:lower)}Post"
assert_nothing_raised { validate_json(template.to_json, "#/components/schemas/#{singular_name.camelize(:lower)}Post") }

# debug note: responds with redirect 302 if not really logged in.. could happen if database resets and has no users
assert_difference(-> { model.count }, 1) do
post collection_url, params: template, as: :json, headers: { 'Authorization' => write_access_auth }
assert_response :success
end

validate_json response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response"
assert_nothing_raised { validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response") }

actual = JSON.parse(response.body)

Expand All @@ -80,14 +82,14 @@ def api_patch_test(resource, template)
assert_response :success
expected = JSON.parse(response.body)

validate_json template.to_json, "#/components/schemas/#{singular_name.camelize(:lower)}Patch"
assert_nothing_raised { validate_json(template.to_json, "#/components/schemas/#{singular_name.camelize(:lower)}Patch") }

assert_no_difference(-> { model.count }) do
patch member_url(resource), params: template, as: :json, headers: { 'Authorization' => write_access_auth }
assert_response :success
end

validate_json response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response"
assert_nothing_raised { validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response") }

actual = JSON.parse(response.body)

Expand Down
1 change: 1 addition & 0 deletions test/functional/assays_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ def check_fixtures_for_authorization_of_sops_and_datafiles_links
access_type: Policy::NO_ACCESS,
permissions: [FactoryBot.create(:permission, contributor: proj, access_type: Policy::EDITING)]))
get :edit, params: { id: assay.id }
assert_response :success
end

test "should create sharing permissions 'with your project and with all SysMO members'" do
Expand Down
18 changes: 18 additions & 0 deletions test/functional/policies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,36 @@ def setup
test 'additional permissions and privilege text for preview permission' do
# no additional text
post :preview_permissions, params: { policy_attributes: { access_type: Policy::NO_ACCESS }, resource_name: 'assay' }
assert_response :success
assert_select 'p.private', text: "This #{I18n.t('assay')} is hidden from public view."

# with additional text for permissions
project = FactoryBot.create(:project)
post :preview_permissions, params: { policy_attributes: projects_policy(Policy::VISIBLE, [project.id], Policy::ACCESSIBLE), resource_name: 'data_file', project_ids: project.id }
assert_response :success
assert_select 'h3', text: "Public Visibility"
assert_select 'p.public', text: "All visitors can view summary only."
assert_select 'h3', text: "Additionally..."
assert_select 'p', text: "The following can view summary and get contents"

# with additional text for privileged people
asset_manager = FactoryBot.create(:asset_housekeeper)
post :preview_permissions, params: { policy_attributes: projects_policy(Policy::NO_ACCESS, [asset_manager.projects.first], Policy::ACCESSIBLE), resource_name: 'data_file', project_ids: asset_manager.projects.first.id }
assert_response :success
assert_select 'h3', text: "Public Visibility"
assert_select 'p.private', text: "This #{I18n.t('data_file')} is hidden from public view."
assert_select 'h3', text: "Additionally..."
assert_select 'p', text: "The following can view summary and get contents"

# with additional text for both permissions and privileged people
asset_manager = FactoryBot.create(:asset_housekeeper)
post :preview_permissions, params: { policy_attributes: projects_policy(Policy::VISIBLE, [asset_manager.projects.first], Policy::ACCESSIBLE), resource_name: 'data_file', project_ids: asset_manager.projects.first.id }
assert_response :success
assert_select 'h3', text: "Public Visibility"
assert_select 'p.public', text: "All visitors can view summary only."
assert_select 'h3', text: "Additionally..."
assert_select 'p', text: "The following can view summary and get contents"

end

test 'should display download permissions as view for non-downloadable resource in permission preview' do
Expand Down
1 change: 1 addition & 0 deletions test/functional/studies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ def test_should_show_investigation_tab
access_type: Policy::NO_ACCESS,
permissions: [FactoryBot.create(:permission, contributor: proj, access_type: Policy::EDITING)]))
get :edit, params: { id: study.id }
assert_response :success
end

test 'should show the contributor avatar' do
Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/assay_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def setup
assert_no_difference('Assay.count') do
delete member_url(a), headers: { 'Authorization' => write_access_auth }
assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions test/integration/api/data_file_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def setup
}

assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
blob = df.content_blob.reload
assert_nil blob.md5sum
assert blob.no_content?
Expand All @@ -91,7 +91,7 @@ def setup
}

assert_response :bad_request
validate_json response.body, '#/components/schemas/badRequestResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/badRequestResponse') }
blob = df.content_blob.reload
assert_equal original_md5, blob.md5sum
assert blob.file_size > 0
Expand All @@ -113,7 +113,7 @@ def setup
assert_no_difference(-> { model.count }) do
post collection_url, params: to_post, headers: { 'Authorization' => write_access_auth }
assert_response :unprocessable_entity
validate_json response.body, '#/components/schemas/unprocessableEntityResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
end

h = JSON.parse(response.body)
Expand All @@ -137,7 +137,7 @@ def setup
assert_no_difference(-> { model.count }) do
post collection_url, params: to_post, as: :json, headers: { 'Authorization' => write_access_auth }
assert_response :unprocessable_entity
validate_json response.body, '#/components/schemas/unprocessableEntityResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/investigation_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def setup
assert_no_difference('Investigation.count') do
delete member_url(inv), headers: { 'Authorization' => write_access_auth }
assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/integration/api/model_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def setup
}

assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
blob = pdf_blob.reload
assert_nil blob.md5sum
assert blob.no_content?
Expand All @@ -97,7 +97,7 @@ def setup
}

assert_response :bad_request
validate_json response.body, '#/components/schemas/badRequestResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/badRequestResponse') }
blob = pdf_blob.reload
assert_equal original_md5, blob.md5sum
assert blob.file_size > 0
Expand All @@ -122,7 +122,7 @@ def setup
assert_no_difference(-> { model.count }) do
post collection_url, params: to_post, headers: { 'Authorization' => write_access_auth }
assert_response :unprocessable_entity
validate_json response.body, '#/components/schemas/unprocessableEntityResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
end

h = JSON.parse(response.body)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/api/presentation_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setup
}

assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
blob = pres.content_blob.reload
assert_nil blob.md5sum
assert blob.no_content?
Expand All @@ -70,7 +70,7 @@ def setup
}

assert_response :bad_request
validate_json response.body, '#/components/schemas/badRequestResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/badRequestResponse') }
blob = pres.content_blob.reload
assert_equal original_md5, blob.md5sum
assert blob.file_size > 0
Expand Down
4 changes: 2 additions & 2 deletions test/integration/api/programme_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setup
assert_no_difference('Programme.count', -1) do
delete member_url(prog), headers: { 'Authorization' => write_access_auth }
assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end

#no projects ==> can delete
Expand All @@ -62,6 +62,6 @@ def setup

get member_url(prog), headers: { 'Authorization' => read_access_auth }
assert_response :not_found
validate_json response.body, '#/components/schemas/notFoundResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/notFoundResponse') }
end
end
12 changes: 6 additions & 6 deletions test/integration/api/read_api_test_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def index_response_fragment
end

test 'can get index' do
skip if skip_index_test?
skip('skipping index test') if skip_index_test?
FactoryBot.create("min_#{singular_name}".to_sym)
FactoryBot.create("max_#{singular_name}".to_sym)
get collection_url, as: :json
Expand All @@ -53,7 +53,7 @@ def index_response_fragment
assert_response :not_implemented
else
perform_jsonapi_checks
validate_json response.body, index_response_fragment
assert_nothing_raised { validate_json(response.body, index_response_fragment) }
end
end

Expand All @@ -64,17 +64,17 @@ def index_response_fragment
user_login(FactoryBot.create(:person))
get member_url(res), headers: { 'Authorization' => read_access_auth }
assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end

test 'getting resource with non-existent ID should throw error' do
get member_url(MissingItem.new(model)), headers: { 'Authorization' => read_access_auth }
assert_response :not_found
validate_json response.body, '#/components/schemas/notFoundResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/notFoundResponse') }
end

test 'write show example' do
skip unless write_examples?
skip('skipping writing examples') unless write_examples?

res = FactoryBot.create("max_#{singular_name}".to_sym)
user_login(res.contributor) if res.respond_to?(:contributor)
Expand All @@ -85,7 +85,7 @@ def index_response_fragment
end

test 'write index example' do
skip unless write_examples? && !skip_index_test?
skip('skipping writing index example') unless write_examples? && !skip_index_test?

model.delete_all unless model == Person
FactoryBot.create("min_#{singular_name}".to_sym)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/sop_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def setup

parsed_policy = JSON.parse(@response.body)['data']['attributes']['policy']

validate_json parsed_policy.to_json, "#/components/schemas/policy"
assert_nothing_raised { validate_json(parsed_policy.to_json, "#/components/schemas/policy") }

to_patch = {
data: {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/study_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def setup
assert_no_difference('Study.count') do
delete member_url(study), headers: { 'Authorization' => write_access_auth }
assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/integration/api/workflow_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def setup
post '/workflows.json', params: template, as: :json, headers: { 'Authorization' => write_access_auth }
assert_response :success

validate_json response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response"
assert_nothing_raised { validate_json(response.body, "#/components/schemas/#{singular_name.camelize(:lower)}Response") }
res = JSON.parse(response.body)
tools = res['data']['attributes']['tools']
assert_equal 3, tools.length
Expand Down
18 changes: 9 additions & 9 deletions test/integration/api/write_api_test_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def ignored_attributes
get member_url(res)

assert_response :not_found
validate_json response.body, '#/components/schemas/notFoundResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/notFoundResponse') }
end

test 'unauthorized user cannot update resource' do
Expand All @@ -48,7 +48,7 @@ def ignored_attributes
patch member_url(res), params: body, headers: { 'Authorization' => write_access_auth }

assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end

test 'unauthorized user cannot delete resource' do
Expand All @@ -58,7 +58,7 @@ def ignored_attributes
delete member_url(res), headers: { 'Authorization' => write_access_auth }

assert_response :forbidden
validate_json response.body, '#/components/schemas/forbiddenResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/forbiddenResponse') }
end
end

Expand All @@ -70,7 +70,7 @@ def ignored_attributes
post collection_url, params: body, headers: { 'Authorization' => write_access_auth }, as: :json

assert_response :unprocessable_entity
validate_json response.body, '#/components/schemas/unprocessableEntityResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match 'A POST request is not allowed to specify an id', response.body
end
end
Expand All @@ -82,7 +82,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
post collection_url, params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
validate_json response.body, '#/components/schemas/unprocessableEntityResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "The specified data:type does not match the URL's object (#{body['data']['type']} vs. #{plural_name})", response.body
end
end
Expand All @@ -94,7 +94,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
post collection_url, params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
validate_json response.body, '#/components/schemas/unprocessableEntityResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "A POST/PUT request must specify a data:type", response.body
end
end
Expand All @@ -105,7 +105,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
put member_url(resource), params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
validate_json response.body, '#/components/schemas/unprocessableEntityResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "id specified by the PUT request does not match object-id in the JSON input", response.body
end
end
Expand All @@ -117,7 +117,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
put member_url(resource), params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
validate_json response.body, '#/components/schemas/unprocessableEntityResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "The specified data:type does not match the URL's object (#{body['data']['type']} vs. #{plural_name})", response.body
end
end
Expand All @@ -129,7 +129,7 @@ def ignored_attributes
assert_no_difference(-> { model.count }) do
put member_url(resource), params: body, headers: { 'Authorization' => write_access_auth }, as: :json
assert_response :unprocessable_entity
validate_json response.body, '#/components/schemas/unprocessableEntityResponse'
assert_nothing_raised { validate_json(response.body, '#/components/schemas/unprocessableEntityResponse') }
assert_match "A POST/PUT request must specify a data:type", response.body
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/rdf_test_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module RdfTestCases
logout
get :show, params: { id: private_rdf_test_object, format: 'rdf' }
assert_response :forbidden
else
assert true
end
end

Expand Down
4 changes: 3 additions & 1 deletion test/unit/api_examples_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class ApiExamplesTest < ActiveSupport::TestCase
fragment = fragment.sub('PatchResponse', 'Response')
fragment = fragment.sub(/[a-zA-Z]+sResponse|peopleResponse/, 'indexResponse') unless fragment.end_with?('sampleAttributeTypesResponse')
define_method("test_#{item.sub('.', '_')}") do
validate_json(example.to_json, fragment)
assert_nothing_raised do
validate_json(example.to_json, fragment)
end
end
end
end
Loading