diff --git a/.env b/.env new file mode 100644 index 00000000..2b53fceb --- /dev/null +++ b/.env @@ -0,0 +1,6 @@ +API_URL=http://localhost:9393 +ONTOLOGIES_LINKED_DATA_PATH= +GOO_PATH= +SPARQL_CLIENT_PATH= +ONTOPORTAL_KB=ontoportal_kb +REDIS_HOST=redis-ut diff --git a/Gemfile b/Gemfile index a3bcd66b..c836033a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,11 +1,11 @@ source 'https://rubygems.org' - -gem 'activesupport', '~> 3.2' +gem 'activesupport', '~> 5' # see https://github.com/ncbo/ontologies_api/issues/69 -gem 'bigdecimal', '1.4.2' +gem 'bigdecimal' +# gem 'faraday', '~> 1.9' gem 'json-schema', '~> 2.0' -gem 'multi_json', '~> 1.0' -gem 'oj', '~> 3.0' +gem 'multi_json' +gem 'oj' gem 'parseconfig' gem 'rack' gem 'rake', '~> 10.0' @@ -43,12 +43,12 @@ gem 'redcarpet' # NCBO gems (can be from a local dev path or from rubygems/git) -gem 'ncbo_annotator', github: 'ncbo/ncbo_annotator', branch: 'master' -gem 'ncbo_cron', github: 'ncbo/ncbo_cron', branch: 'master' -gem 'ncbo_ontology_recommender', github: 'ncbo/ncbo_ontology_recommender', branch: 'master' -gem 'sparql-client', github: 'ncbo/sparql-client', branch: 'master' -gem 'goo', github: 'ncbo/goo', branch: 'master' -gem 'ontologies_linked_data', github: 'ncbo/ontologies_linked_data', branch: 'master' +gem 'ncbo_annotator', github: 'ncbo/ncbo_annotator', branch: 'develop' +gem 'ncbo_cron', github: 'ncbo/ncbo_cron', branch: 'develop' +gem 'ncbo_ontology_recommender', github: 'ncbo/ncbo_ontology_recommender', branch: 'develop' +gem 'sparql-client', github: 'ncbo/sparql-client', branch: 'develop' +gem 'goo', github: 'ncbo/goo', branch: 'develop' +gem 'ontologies_linked_data', github: 'ncbo/ontologies_linked_data', branch: 'develop' group :development do @@ -72,7 +72,9 @@ group :profiling do end group :test do - gem 'minitest', '~> 4.0' + gem 'crack', '0.4.5' + gem 'minitest', '~> 5.0' + gem 'minitest-hooks', "~> 1.5" gem 'minitest-stub_any_instance' gem 'rack-test' gem 'simplecov', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 9c5d6f8d..c15eb4af 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ GIT remote: https://github.com/ncbo/goo.git - revision: 2cec53c2d84179171def81d7dc0a18e462e78fc2 - branch: master + revision: 74a012eebb9433d031eb00df5abbe488cb8b4512 + branch: develop specs: goo (0.0.2) addressable (~> 2.8) @@ -16,8 +16,8 @@ GIT GIT remote: https://github.com/ncbo/ncbo_annotator.git - revision: 63c986880aa88c9384043e6611a682434a14aba7 - branch: master + revision: b2731c98bfef0958fecb440c8648b7755a670895 + branch: develop specs: ncbo_annotator (0.0.1) goo @@ -27,8 +27,8 @@ GIT GIT remote: https://github.com/ncbo/ncbo_cron.git - revision: 15ccdaa2835be9f35f9c3afdcc4ed5bf5d130999 - branch: master + revision: b279f9caa9a6a169bad7350890d7015d56e9e9b2 + branch: develop specs: ncbo_cron (0.0.1) dante @@ -45,8 +45,8 @@ GIT GIT remote: https://github.com/ncbo/ncbo_ontology_recommender.git - revision: 013abea4af3b10910ec661dbb358a4b6cae198a4 - branch: master + revision: 084cfc74a78b1c6334f3b74e228ddbf80d36e30a + branch: develop specs: ncbo_ontology_recommender (0.0.1) goo @@ -56,8 +56,8 @@ GIT GIT remote: https://github.com/ncbo/ontologies_linked_data.git - revision: a9da0982eef16ea8272002d14d6b8851dc07fb0a - branch: master + revision: 812dd78f02b77c9c6d579b0febf1c2a42d513022 + branch: develop specs: ontologies_linked_data (0.0.1) activesupport @@ -76,8 +76,8 @@ GIT GIT remote: https://github.com/ncbo/sparql-client.git - revision: e89c26aa96f184dbe9b52d51e04fb3d9ba998dbc - branch: master + revision: 1657f0dd69fd4b522d3549a6848670175f5e98cc + branch: develop specs: sparql-client (1.0.1) json_pure (>= 1.4) @@ -103,9 +103,11 @@ GIT GEM remote: https://rubygems.org/ specs: - activesupport (3.2.22.5) - i18n (~> 0.6, >= 0.6.4) - multi_json (~> 1.0) + activesupport (5.2.8.1) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) addressable (2.8.7) public_suffix (>= 2.0.2, < 7.0) airbrussh (1.5.3) @@ -116,7 +118,7 @@ GEM bcrypt (3.1.20) bcrypt_pbkdf (1.1.1) bcrypt_pbkdf (1.1.1-arm64-darwin) - bigdecimal (1.4.2) + bigdecimal (3.1.8) builder (3.3.0) capistrano (3.19.1) airbrussh (>= 1.0.0) @@ -133,8 +135,7 @@ GEM coderay (1.1.3) concurrent-ruby (1.3.4) connection_pool (2.4.1) - crack (1.0.0) - bigdecimal + crack (0.4.5) rexml cube-ruby (0.0.3) dante (0.2.0) @@ -174,7 +175,7 @@ GEM google-cloud-core (1.7.1) google-cloud-env (>= 1.0, < 3.a) google-cloud-errors (~> 1.0) - google-cloud-env (2.2.0) + google-cloud-env (2.2.1) faraday (>= 1.0, < 3.a) google-cloud-errors (1.4.0) google-protobuf (3.25.5-aarch64-linux) @@ -186,7 +187,7 @@ GEM grpc (~> 1.41) googleapis-common-protos-types (1.16.0) google-protobuf (>= 3.18, < 5.a) - googleauth (1.11.0) + googleauth (1.11.1) faraday (>= 1.0, < 3.a) google-cloud-env (~> 2.1) jwt (>= 1.4, < 3.0) @@ -209,13 +210,13 @@ GEM http-accept (1.7.0) http-cookie (1.0.7) domain_name (~> 0.5) - i18n (0.9.5) + i18n (1.14.6) concurrent-ruby (~> 1.0) json (2.7.2) json-schema (2.8.1) addressable (>= 2.4) json_pure (2.7.2) - jwt (2.9.1) + jwt (2.9.3) base64 kgio (2.11.4) language_server-protocol (3.17.0.3) @@ -229,11 +230,14 @@ GEM net-pop net-smtp method_source (1.1.0) - mime-types (3.5.2) + mime-types (3.6.0) + logger mime-types-data (~> 3.2015) - mime-types-data (3.2024.0903) + mime-types-data (3.2024.1001) mini_mime (1.1.5) - minitest (4.7.5) + minitest (5.25.1) + minitest-hooks (1.5.2) + minitest (> 5.3) minitest-stub_any_instance (1.0.3) mlanett-redis-lock (0.2.7) redis @@ -254,10 +258,12 @@ GEM net-ssh (>= 5.0.0, < 8.0.0) net-smtp (0.5.0) net-protocol - net-ssh (7.2.3) + net-ssh (7.3.0) netrc (0.11.0) - newrelic_rpm (9.13.0) - oj (3.16.1) + newrelic_rpm (9.14.0) + oj (3.16.6) + bigdecimal (>= 3.0) + ostruct (>= 0.2) omni_logger (0.1.4) logger os (1.1.4) @@ -313,7 +319,7 @@ GEM http-cookie (>= 1.0.2, < 2.0) mime-types (>= 1.16, < 4.0) netrc (~> 0.8) - rexml (3.3.7) + rexml (3.3.8) rsolr (2.6.0) builder (>= 2.1.2) faraday (>= 0.9, < 3, != 2.0.0) @@ -369,10 +375,11 @@ GEM ostruct systemu (2.6.5) temple (0.10.3) + thread_safe (0.3.6) tilt (2.4.0) timeout (0.4.1) - tzinfo (2.0.6) - concurrent-ruby (~> 1.0) + tzinfo (1.2.11) + thread_safe (~> 0.1) unicode-display_width (2.6.0) unicorn (6.1.0) kgio (~> 2.6) @@ -395,27 +402,29 @@ PLATFORMS x86_64-linux DEPENDENCIES - activesupport (~> 3.2) + activesupport (~> 5) bcrypt_pbkdf (>= 1.0, < 2.0) - bigdecimal (= 1.4.2) + bigdecimal capistrano (~> 3) capistrano-bundler capistrano-locally capistrano-rbenv + crack (= 0.4.5) cube-ruby ed25519 (>= 1.2, < 2.0) ffi goo! haml (~> 5.2.2) json-schema (~> 2.0) - minitest (~> 4.0) + minitest (~> 5.0) + minitest-hooks (~> 1.5) minitest-stub_any_instance - multi_json (~> 1.0) + multi_json ncbo_annotator! ncbo_cron! ncbo_ontology_recommender! newrelic_rpm - oj (~> 3.0) + oj ontologies_linked_data! parseconfig rack @@ -447,4 +456,4 @@ DEPENDENCIES webrick BUNDLED WITH - 2.5.19 + 2.5.20 diff --git a/controllers/recommender_v1_controller.rb b/controllers/recommender_v1_controller.rb index 5b0123a9..29991299 100644 --- a/controllers/recommender_v1_controller.rb +++ b/controllers/recommender_v1_controller.rb @@ -1,17 +1,10 @@ class RecommenderController < ApplicationController namespace "/recommender_v1" do - # execute an annotator query + # Mark this route as deprecated get do - text = params["text"] - raise error 400, "A text to be analyzed by the recommender must be supplied using the argument text=" if text.nil? || text.strip.empty? - acronyms = restricted_ontologies_to_acronyms(params) - display_classes = params['display_classes'].eql?('true') # default will be false - recommender = Recommender::Models::NcboRecommender.new - recommendations = recommender.recommend(text, acronyms, display_classes) - reply 200, recommendations + reply 410, { message: "This API endpoint has been deprecated and is no longer available. Please use /recommender or refer to the API documentation for updated routes." } end end end - diff --git a/controllers/users_controller.rb b/controllers/users_controller.rb index fdad6a74..a25c1b5d 100644 --- a/controllers/users_controller.rb +++ b/controllers/users_controller.rb @@ -46,14 +46,24 @@ class UsersController < ApplicationController params["display"] = User.attributes.join(",") # used to serialize everything via the serializer user = LinkedData::Models::User.where(email: email, username: username).include(User.goo_attrs_to_load(includes_param)).first error 404, "User not found" unless user + + user.bring(:resetToken) + user.bring(:resetTokenExpireTime) + user.bring(:passwordHash) + if token.eql?(user.resetToken) error 401, "Invalid password reset token" if user.resetTokenExpireTime.nil? error 401, "The password reset token expired" if user.resetTokenExpireTime < Time.now.to_i user.resetToken = nil user.resetTokenExpireTime = nil - user.save(override_security: true) if user.valid? - user.show_apikey = true - reply user + + if user.valid? + user.save(override_security: true) + user.show_apikey = true + reply user + else + error 422, "Error resetting password" + end else error 401, "Password reset not authorized with this token" end diff --git a/test/controllers/test_annotator_controller.rb b/test/controllers/test_annotator_controller.rb index 53d7bb8a..2e6dada1 100644 --- a/test/controllers/test_annotator_controller.rb +++ b/test/controllers/test_annotator_controller.rb @@ -2,7 +2,7 @@ class TestAnnotatorController < TestCase - def self.before_suite + def before_suite @@redis = Redis.new(:host => Annotator.settings.annotator_redis_host, :port => Annotator.settings.annotator_redis_port) db_size = @@redis.dbsize if db_size > MAX_TEST_REDIS_SIZE @@ -25,7 +25,7 @@ def self.before_suite annotator = Annotator::Models::NcboAnnotator.new annotator.init_redis_for_tests() annotator.create_term_cache_from_ontologies(@@ontologies, false) - mapping_test_set + self.class.mapping_test_set end def test_annotate diff --git a/test/controllers/test_batch_controller.rb b/test/controllers/test_batch_controller.rb index d1e9d144..ca37b156 100644 --- a/test/controllers/test_batch_controller.rb +++ b/test/controllers/test_batch_controller.rb @@ -1,7 +1,8 @@ require_relative '../test_case' class TestBatchController < TestCase - def self.before_suite + def before_suite + LinkedData::SampleData::Ontology.delete_ontologies_and_submissions @@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies(process_submission: true) end @@ -25,7 +26,7 @@ def test_class_batch_one_ontology assert last_response.ok? data = MultiJson.load(last_response.body) classes = data["http://www.w3.org/2002/07/owl#Class"] - assert classes.length == 4 + assert_equal 4, classes.length classes.each do |klass| assert_instance_of String, klass["prefLabel"] assert_instance_of Array, klass["synonym"] @@ -75,11 +76,11 @@ def test_class_batch_multiple assert last_response.ok? data = MultiJson.load(last_response.body) classes = data["http://www.w3.org/2002/07/owl#Class"] - assert classes.length == 6 + assert_equal 6, classes.length classes.each do |klass| assert_instance_of String, klass["prefLabel"] - assert !klass["synonym"] - assert klass["prefLabel"] == class_ids[klass["@id"]] + refute klass["synonym"] + assert_equal klass["prefLabel"], class_ids[klass["@id"]] end end @@ -87,6 +88,7 @@ def test_class_batch_multiple def test_class_all_bro mccl = @@ontologies.select { |y| y.id.to_s.include? "MCCL"}.first assert mccl, "mccl is not found to execute batch test." + # binding.pry classes = LinkedData::Models::Class.in(mccl.latest_submission).include(:prefLabel).page(1,500).read_only.all class_ids = {} classes.each do |klass| @@ -101,9 +103,10 @@ def test_class_all_bro } post "/batch/", call_params assert last_response.ok? +# refute last_response.ok? data = MultiJson.load(last_response.body) classes_response = data["http://www.w3.org/2002/07/owl#Class"] - assert classes_response.length == classes.length + assert_equal classes.length, classes_response.length classes_response.each do |klass| assert_instance_of String, klass["prefLabel"] assert klass["prefLabel"] == class_ids[klass["@id"]] diff --git a/test/controllers/test_classes_controller.rb b/test/controllers/test_classes_controller.rb index 780ded0e..ab8ff63e 100644 --- a/test/controllers/test_classes_controller.rb +++ b/test/controllers/test_classes_controller.rb @@ -2,7 +2,7 @@ class TestClassesController < TestCase - def self.before_suite + def before_suite options = {ont_count: 1, submission_count: 3, submissions_to_process: [1, 2], diff --git a/test/controllers/test_instances_controller.rb b/test/controllers/test_instances_controller.rb index 4e9b8a5c..1735455f 100644 --- a/test/controllers/test_instances_controller.rb +++ b/test/controllers/test_instances_controller.rb @@ -2,7 +2,7 @@ class TestInstancesController < TestCase - def self.before_suite + def before_suite LinkedData::SampleData::Ontology.create_ontologies_and_submissions({ process_submission: true, acronym: 'XCT-TEST-INST', diff --git a/test/controllers/test_mappings_controller.rb b/test/controllers/test_mappings_controller.rb index 44bfa032..d8c6f619 100644 --- a/test/controllers/test_mappings_controller.rb +++ b/test/controllers/test_mappings_controller.rb @@ -2,8 +2,8 @@ class TestMappingsController < TestCase - def self.before_suite - + def before_suite + self.backend_4s_delete ["BRO-TEST-MAP-0","CNO-TEST-MAP-0","FAKE-TEST-MAP-0"].each do |acr| LinkedData::Models::OntologySubmission.where(ontology: [acronym: acr]).to_a.each do |s| s.delete diff --git a/test/controllers/test_metrics_controller.rb b/test/controllers/test_metrics_controller.rb index b27fa198..0d8b547d 100644 --- a/test/controllers/test_metrics_controller.rb +++ b/test/controllers/test_metrics_controller.rb @@ -2,7 +2,7 @@ class TestMetricsController < TestCase - def self.before_suite + def before_suite if OntologySubmission.all.count > 100 puts "this test is going to wipe out all submission and ontologies. probably this is not a test env." return diff --git a/test/controllers/test_notes_controller.rb b/test/controllers/test_notes_controller.rb index d9223669..83cb1045 100644 --- a/test/controllers/test_notes_controller.rb +++ b/test/controllers/test_notes_controller.rb @@ -3,9 +3,9 @@ class TestNotesController < TestCase - def self.before_suite - self.new("before_suite").delete_ontologies_and_submissions - @@ontology, @@cls = self.new("before_suite")._ontology_and_class + def before_suite + self.delete_ontologies_and_submissions + @@ontology, @@cls = self._ontology_and_class @@note_user = "test_note_user" @@user = LinkedData::Models::User.new( @@ -41,7 +41,7 @@ def test_all_notes get '/notes' assert last_response.ok? notes = MultiJson.load(last_response.body) - assert notes.length >= 5 + assert_operator 5, :<=, notes.length end def test_single_note @@ -62,21 +62,22 @@ def test_note_lifecycle relatedOntology: [@@ontology.id.to_s] } post "/notes", MultiJson.dump(note), "CONTENT_TYPE" => "application/json" - assert last_response.status == 201 + assert_equal 201, last_response.status new_note = MultiJson.load(last_response.body) get new_note["@id"] assert last_response.ok? + assert_equal 200, last_response.status note_changes = {body: "New testing body"} patch new_note["@id"], MultiJson.dump(note_changes), "CONTENT_TYPE" => "application/json" - assert last_response.status == 204 + assert_equal 204, last_response.status get new_note["@id"] patched_note = MultiJson.load(last_response.body) assert_equal patched_note["body"], note_changes[:body] delete new_note["@id"] - assert last_response.status == 204 + assert_equal 204, last_response.status end def test_proposal_lifecycle @@ -97,21 +98,23 @@ def test_proposal_lifecycle } post "/notes", MultiJson.dump(note), "CONTENT_TYPE" => "application/json" - assert last_response.status == 201 + assert_equal 201, last_response.status new_note = MultiJson.load(last_response.body) + # assert_equal 'blah', new_note get new_note["@id"] assert last_response.ok? note_changes = {proposal: {label: "New sleed study facility"}} patch new_note["@id"], MultiJson.dump(note_changes), "CONTENT_TYPE" => "application/json" - assert last_response.status == 204 + assert_equal 204, last_response.status get new_note["@id"] patched_note = MultiJson.load(last_response.body) - assert_equal patched_note["label"], note_changes[:label] + refute_nil patched_note['proposal']['label'] + assert_equal patched_note['proposal']['label'], note_changes[:proposal][:label] delete new_note["@id"] - assert last_response.status == 204 + assert_equal 204, last_response.status end def test_notes_for_ontology @@ -120,8 +123,8 @@ def test_notes_for_ontology get ont["links"]["notes"] notes = MultiJson.load(last_response.body) test_note = notes.select {|n| n["subject"].eql?("Test subject 1")} - assert test_note.length == 1 - assert notes.length >= 5 + assert_equal 1, test_note.length + assert_operator 5, :<=, notes.length end def test_notes_for_class @@ -130,7 +133,7 @@ def test_notes_for_class get cls["links"]["notes"] notes = MultiJson.load(last_response.body) test_note = notes.select {|n| n["subject"].eql?("Test subject 1")} - assert test_note.length == 1 - assert notes.length >= 5 + assert_equal 1, test_note.length + assert_operator 5, :<=, notes.length end end diff --git a/test/controllers/test_ontologies_controller.rb b/test/controllers/test_ontologies_controller.rb index a5f242bd..874efe39 100644 --- a/test/controllers/test_ontologies_controller.rb +++ b/test/controllers/test_ontologies_controller.rb @@ -2,11 +2,11 @@ require_relative '../test_case' class TestOntologiesController < TestCase - def self.before_suite - _set_vars - _delete - _create_user - _create_onts + def before_suite + self.class._set_vars + self.class._delete + self.class._create_user + self.class._create_onts end def teardown @@ -264,7 +264,7 @@ def test_on_demand_ontology_pull acronym = ont.acronym sub = ont.submissions.first sub.bring(:pullLocation) if sub.bring?(:pullLocation) - assert_equal(nil, sub.pullLocation, msg="Pull location should be nil at this point in the test") + assert_nil sub.pullLocation, msg = "Pull location should be nil at this point in the test" allowed_user = ont.administeredBy.first allowed_user.bring(:apikey) if allowed_user.bring?(:apikey) diff --git a/test/controllers/test_ontology_analytics_controller.rb b/test/controllers/test_ontology_analytics_controller.rb index b8e36dce..721efea6 100644 --- a/test/controllers/test_ontology_analytics_controller.rb +++ b/test/controllers/test_ontology_analytics_controller.rb @@ -196,7 +196,7 @@ class TestOntologyAnalyticsController < TestCase } } - def self.before_suite + def before_suite @@redis = Redis.new(:host => Annotator.settings.annotator_redis_host, :port => Annotator.settings.annotator_redis_port) db_size = @@redis.dbsize if db_size > MAX_TEST_REDIS_SIZE @@ -212,9 +212,9 @@ def self.before_suite "SNOMEDCT" => "SNOMEDCT Ontology", "TST" => "TST Ontology" } - _delete - _create_user - _create_onts + self.class._delete + self.class._create_user + self.class._create_onts end def teardown diff --git a/test/controllers/test_ontology_submissions_controller.rb b/test/controllers/test_ontology_submissions_controller.rb index ef356fcf..d0f58582 100644 --- a/test/controllers/test_ontology_submissions_controller.rb +++ b/test/controllers/test_ontology_submissions_controller.rb @@ -2,10 +2,11 @@ class TestOntologySubmissionsController < TestCase - def self.before_suite - _set_vars - _create_user - _create_onts + def before_suite + self.backend_4s_delete + self.class._set_vars + self.class._create_user + self.class._create_onts end def self._set_vars diff --git a/test/controllers/test_properties_controller.rb b/test/controllers/test_properties_controller.rb index cbf249b8..d2957956 100644 --- a/test/controllers/test_properties_controller.rb +++ b/test/controllers/test_properties_controller.rb @@ -2,7 +2,7 @@ class TestPropertiesController < TestCase - def self.before_suite + def before_suite count, acronyms, bro = LinkedData::SampleData::Ontology.create_ontologies_and_submissions({ process_submission: true, process_options:{process_rdf: true, extract_metadata: false}, @@ -27,7 +27,7 @@ def self.before_suite @@acronyms = @@ontologies.map { |ont| ont.bring_remaining; ont.acronym } end - def self.after_suite + def after_suite LinkedData::SampleData::Ontology.delete_ontologies_and_submissions end diff --git a/test/controllers/test_properties_search_controller.rb b/test/controllers/test_properties_search_controller.rb index 2589a293..2a8cadd0 100644 --- a/test/controllers/test_properties_search_controller.rb +++ b/test/controllers/test_properties_search_controller.rb @@ -2,7 +2,9 @@ class TestPropertiesSearchController < TestCase - def self.before_suite + def before_suite + self.backend_4s_delete + count, acronyms, bro = LinkedData::SampleData::Ontology.create_ontologies_and_submissions({ process_submission: true, process_options:{process_rdf: true, extract_metadata: false, index_properties: true}, @@ -26,7 +28,7 @@ def self.before_suite @@ontologies = bro.concat(mccl) end - def self.after_suite + def after_suite LinkedData::SampleData::Ontology.delete_ontologies_and_submissions LinkedData::Models::Ontology.indexClear(:property) LinkedData::Models::Ontology.indexCommit(nil, :property) diff --git a/test/controllers/test_provisional_classes_controller.rb b/test/controllers/test_provisional_classes_controller.rb index 7225d772..e54c7805 100644 --- a/test/controllers/test_provisional_classes_controller.rb +++ b/test/controllers/test_provisional_classes_controller.rb @@ -1,9 +1,9 @@ require_relative '../test_case' class TestProvisionalClassesController < TestCase - def self.before_suite - self.new("before_suite").delete_ontologies_and_submissions - @@ontology, classes = self.new("before_suite")._ontology_and_classes + def before_suite + self.delete_ontologies_and_submissions + @@ontology, classes = self._ontology_and_classes @@cls = classes[0] @@cls1 = classes[1] @@ -32,7 +32,7 @@ def self.before_suite end end - def self.after_suite + def after_suite 3.times do |i| @@pcs[i].delete end diff --git a/test/controllers/test_provisional_relations_controller.rb b/test/controllers/test_provisional_relations_controller.rb index 83f2761d..f097dde9 100644 --- a/test/controllers/test_provisional_relations_controller.rb +++ b/test/controllers/test_provisional_relations_controller.rb @@ -1,9 +1,9 @@ require_relative '../test_case' class TestProvisionalRelationsController < TestCase - def self.before_suite - self.new("before_suite").delete_ontologies_and_submissions - @@ontology, classes = self.new("before_suite")._ontology_and_classes + def before_suite + self.delete_ontologies_and_submissions + @@ontology, classes = self._ontology_and_classes @@cls1 = classes[0] @@cls2 = classes[1] @@ -39,7 +39,7 @@ def self.before_suite @@test_rel.save end - def self.after_suite + def after_suite @@test_pc.delete @@test_user.delete end diff --git a/test/controllers/test_recommender_controller.rb b/test/controllers/test_recommender_controller.rb index 58d6d942..c46255fc 100644 --- a/test/controllers/test_recommender_controller.rb +++ b/test/controllers/test_recommender_controller.rb @@ -2,7 +2,7 @@ class TestRecommenderController < TestCase - def self.before_suite + def before_suite @@redis = Redis.new(:host => Annotator.settings.annotator_redis_host, :port => Annotator.settings.annotator_redis_port) db_size = @@redis.dbsize if db_size > MAX_TEST_REDIS_SIZE diff --git a/test/controllers/test_recommender_v1_controller.rb b/test/controllers/test_recommender_v1_controller.rb index 3ac4862d..7ef09cee 100644 --- a/test/controllers/test_recommender_v1_controller.rb +++ b/test/controllers/test_recommender_v1_controller.rb @@ -1,40 +1,10 @@ require_relative '../test_case' - +# recommender_v1 is deprecated as of 2024-10-27 +# TODO: remove completely after 2025-10-27 class TestRecommenderV1Controller < TestCase - - def self.before_suite - LinkedData::SampleData::Ontology.delete_ontologies_and_submissions - @@ontologies = LinkedData::SampleData::Ontology.sample_owl_ontologies(process_submission: true) - @@text = < @@text - } + def test_recommender_v1_deprecation + params = { text: 'recommender v1 is deprecated' } get "/recommender_v1", params - assert last_response.ok? - recommendations = MultiJson.load(last_response.body) - assert_instance_of(Array, recommendations) - assert_equal(3, recommendations.length, msg='Failed to return 3 recommendations') - rec = recommendations.first - assert_instance_of(Hash, rec) - ont_acronyms = @@ontologies.map {|o| o.bring(:acronym); o.acronym } - assert ont_acronyms.include? rec['ontology']['acronym'] - assert rec['annotatedClasses'].length == 0 # no classes requested - assert rec['numTermsMatched'] > 0 - assert rec['numTermsTotal'] > 0 - assert rec['numTermsTotal'] >= rec['numTermsMatched'] - assert recommendations[0]['score'].to_i >= recommendations[1]['score'].to_i - assert recommendations[1]['score'].to_i >= recommendations[2]['score'].to_i + assert_equal 410, last_response.status end - end diff --git a/test/controllers/test_replies_controller.rb b/test/controllers/test_replies_controller.rb index 41b15e2e..19a6ddd0 100644 --- a/test/controllers/test_replies_controller.rb +++ b/test/controllers/test_replies_controller.rb @@ -2,8 +2,8 @@ class TestRepliesController < TestCase - def self.before_suite - ontologies = self.new("before_suite").create_ontologies_and_submissions(ont_count: 1, submission_count: 1, process_submission: false)[2] + def before_suite + ontologies = self.create_ontologies_and_submissions(ont_count: 1, submission_count: 1, process_submission: false)[2] @@ontology = ontologies.first @@reply_user = "test_reply_user" diff --git a/test/controllers/test_search_controller.rb b/test/controllers/test_search_controller.rb index 99134eed..1e98a84a 100644 --- a/test/controllers/test_search_controller.rb +++ b/test/controllers/test_search_controller.rb @@ -2,7 +2,8 @@ class TestSearchController < TestCase - def self.before_suite + def before_suite + self.backend_4s_delete count, acronyms, bro = LinkedData::SampleData::Ontology.create_ontologies_and_submissions({ process_submission: true, acronym: "BROSEARCHTEST", @@ -54,7 +55,7 @@ def self.before_suite @@test_pc_child.save end - def self.after_suite + def after_suite @@test_pc_root.delete @@test_pc_child.delete LinkedData::SampleData::Ontology.delete_ontologies_and_submissions diff --git a/test/controllers/test_slices_controller.rb b/test/controllers/test_slices_controller.rb index 308b9fb7..e1250ba2 100644 --- a/test/controllers/test_slices_controller.rb +++ b/test/controllers/test_slices_controller.rb @@ -2,12 +2,11 @@ class TestSlicesController < TestCase - def self.before_suite + def before_suite ont_count, ont_acronyms, @@onts = LinkedData::SampleData::Ontology.create_ontologies_and_submissions(ont_count: 1, submission_count: 0) - @@slice_acronyms = ["tst-a", "tst-b"].sort - _create_slice(@@slice_acronyms[0], "Test Slice A", @@onts) - _create_slice(@@slice_acronyms[1], "Test Slice B", @@onts) + self.class._create_slice(@@slice_acronyms[0], "Test Slice A", @@onts) + self.class._create_slice(@@slice_acronyms[1], "Test Slice B", @@onts) @@user = User.new({ username: "test-slice", @@ -15,13 +14,13 @@ def self.before_suite password: "12345" }).save @@new_slice_data = { acronym: 'tst-c', name: "Test Slice C", ontologies: ont_acronyms} - enable_security + self.class.enable_security end - def self.after_suite + def after_suite LinkedData::Models::Slice.all.each(&:delete) @@user.delete - reset_security + self.class.reset_security end def setup diff --git a/test/controllers/test_users_controller.rb b/test/controllers/test_users_controller.rb index 53bf520f..1aea137a 100644 --- a/test/controllers/test_users_controller.rb +++ b/test/controllers/test_users_controller.rb @@ -1,7 +1,7 @@ require_relative '../test_case' class TestUsersController < TestCase - def self.before_suite + def before_suite # Create a bunch of test users @@usernames = %w(fred goerge henry ben mark matt charlie) @@ -48,6 +48,21 @@ def test_single_user assert_equal "fred", MultiJson.load(last_response.body)["username"] end + def test_hide_sensitive_data + user = @@users[0] + reset_token = token(36) + user.resetToken = reset_token + user.resetTokenExpireTime = Time.now.to_i - 2.hours.to_i + user.save + + username = user.username + get "/users/#{username}?display=resetToken,resetTokenExpireTime" + assert last_response.ok? + + refute_includes MultiJson.load(last_response.body), 'resetToken', "resetToken should NOT be included in the response" + refute_includes MultiJson.load(last_response.body), 'resetTokenExpireTime', "resetTokenExpireTime should NOT be included in the response" + end + def test_create_new_user user = {email: "#{@@username}@example.org", password: "pass_the_word"} put "/users/#{@@username}", MultiJson.dump(user), "CONTENT_TYPE" => "application/json" @@ -171,6 +186,13 @@ def test_authentication private + def token(len) + chars = ("a".."z").to_a + ("A".."Z").to_a + ("1".."9").to_a + token = "" + 1.upto(len) { |i| token << chars[rand(chars.size-1)] } + token + end + def _delete_user(username) LinkedData::Models::User.find(@@username).first&.delete end diff --git a/test/helpers/test_access_control_helper.rb b/test/helpers/test_access_control_helper.rb index 1e3807d5..19b0ae0f 100644 --- a/test/helpers/test_access_control_helper.rb +++ b/test/helpers/test_access_control_helper.rb @@ -2,8 +2,8 @@ class TestAccessControlHelper < TestCaseHelpers - def self.before_suite - self.new("before_suite").delete_ontologies_and_submissions + def before_suite + self.backend_4s_delete @@usernames = ["user1", "user2", "user3", "admin"] @@usernames.each do |username| @@ -14,7 +14,7 @@ def self.before_suite ) user.save user.bring_remaining - self.class_variable_set(:"@@#{username}", user) + self.class.class_variable_set(:"@@#{username}", user) end @@admin.role = [LinkedData::Models::Users::Role.find(LinkedData::Models::Users::Role::ADMIN).first] @@ -42,8 +42,9 @@ def self.before_suite LinkedData.settings.enable_security = true end - def self.after_suite - LinkedData.settings.enable_security = @@old_security_setting if class_variable_defined?("@@old_security_setting") + def after_suite + self.backend_4s_delete + LinkedData.settings.enable_security = @@old_security_setting unless @@old_security_setting.nil? end def test_filtered_list diff --git a/test/helpers/test_application_helper.rb b/test/helpers/test_application_helper.rb index b90aa30f..2315a677 100644 --- a/test/helpers/test_application_helper.rb +++ b/test/helpers/test_application_helper.rb @@ -3,7 +3,7 @@ class TestApplicationHelper < TestCaseHelpers - def self.before_suite + def before_suite @@ontologies = LinkedData::SampleData::Ontology.create_ontologies_and_submissions[2] end diff --git a/test/helpers/test_http_cache_helper.rb b/test/helpers/test_http_cache_helper.rb index 944198a6..8a88cc55 100644 --- a/test/helpers/test_http_cache_helper.rb +++ b/test/helpers/test_http_cache_helper.rb @@ -2,10 +2,10 @@ class TestHTTPCacheHelper < TestCaseHelpers - def self.before_suite + def before_suite raise Exception, "Redis is unavailable, caching will not function" if LinkedData::HTTPCache.redis.ping.nil? - self.new("before_suite").delete_ontologies_and_submissions - ontologies = self.new("before_suite")._ontologies + self.delete_ontologies_and_submissions + ontologies = self._ontologies @@ontology = ontologies.shift @@ontology_alt = ontologies.shift @@ontology.bring_remaining @@ -39,7 +39,7 @@ def self.before_suite LinkedData.settings.enable_http_cache = true end - def self.after_suite + def after_suite LinkedData.settings.enable_http_cache = @orig_enable_cache LinkedData::HTTPCache.invalidate_all end diff --git a/test/helpers/test_slices_helper.rb b/test/helpers/test_slices_helper.rb index 165a2a7e..e776c155 100644 --- a/test/helpers/test_slices_helper.rb +++ b/test/helpers/test_slices_helper.rb @@ -2,13 +2,14 @@ class TestSlicesHelper < TestCaseHelpers - def self.before_suite - self.new("before_suite").delete_ontologies_and_submissions + def before_suite + self.backend_4s_delete + @@orig_slices_setting = LinkedData.settings.enable_slices LinkedData.settings.enable_slices = true @@onts = LinkedData::SampleData::Ontology.create_ontologies_and_submissions(ont_count: 5, submission_count: 0)[2] @@group_acronym = "test-group" - @@group = _create_group + @@group = self.class._create_group @@onts[0..2].each do |o| o.bring_remaining o.group = [@@group] @@ -30,6 +31,10 @@ def self.before_suite LinkedData::Models::Slice.synchronize_groups_to_slices end + def after_suite + self.backend_4s_delete + end + def test_filtered_list get "http://#{@@group_acronym}.dev/ontologies" assert last_response.ok? diff --git a/test/helpers/test_users_helper.rb b/test/helpers/test_users_helper.rb index 6cd5bd50..9f1614f1 100644 --- a/test/helpers/test_users_helper.rb +++ b/test/helpers/test_users_helper.rb @@ -2,9 +2,10 @@ class TestUsersHelper < TestCaseHelpers - def self.before_suite - @@user = _create_user - @@non_custom_user = _create_user("notcustom") + def before_suite + self.backend_4s_delete + @@user = self.class._create_user + @@non_custom_user = self.class._create_user("notcustom") @@onts = LinkedData::SampleData::Ontology.create_ontologies_and_submissions({ ont_count: 5, @@ -29,7 +30,7 @@ def self.before_suite LinkedData.settings.enable_security = true end - def self.after_suite + def after_suite LinkedData.settings.enable_security = @@old_security_setting end diff --git a/test/middleware/test_rack_attack.rb b/test/middleware/test_rack_attack.rb index 47e30a52..92b4d636 100644 --- a/test/middleware/test_rack_attack.rb +++ b/test/middleware/test_rack_attack.rb @@ -5,8 +5,8 @@ RACK_CONFIG = File.join([settings.root, "config.ru"]) class TestRackAttack < TestCase - - def self.before_suite + + def before_suite # Store app settings @@auth_setting = LinkedData.settings.enable_security @@throttling_setting = LinkedData.settings.enable_throttling @@ -35,7 +35,7 @@ def self.before_suite $stderr = File.open("/dev/null", "w") - @@port1 = self.new('').unused_port + @@port1 = unused_port # Fork the process to create two servers. This isolates the Rack::Attack configuration, which makes other tests fail if included. @@pid1 = fork do @@ -47,7 +47,7 @@ def self.before_suite Signal.trap("HUP") { Process.exit! } end - @@port2 = self.new('').unused_port + @@port2 = unused_port @@pid2 = fork do require_relative '../../config/rack_attack' Rack::Server.start( @@ -61,7 +61,7 @@ def self.before_suite sleep(5) end - def self.after_suite + def after_suite # Restore app settings LinkedData.settings.enable_security = @@auth_setting LinkedData::OntologiesAPI.settings.enable_throttling = @@throttling_setting diff --git a/test/test_case.rb b/test/test_case.rb index 0e91aa69..dba7d327 100644 --- a/test/test_case.rb +++ b/test/test_case.rb @@ -20,9 +20,9 @@ require_relative 'test_log_file' require_relative '../app' -require 'minitest/unit' +require 'minitest/autorun' +require 'minitest/hooks/test' require 'webmock/minitest' -MiniTest::Unit.autorun WebMock.allow_net_connect! require 'rack/test' require 'multi_json' @@ -62,7 +62,9 @@ def safe_redis_hosts?(sh) $stdout.flush end -class AppUnit < MiniTest::Unit +class AppUnit < Minitest::Test + include Minitest::Hooks + def count_pattern(pattern) q = "SELECT (count(DISTINCT ?s) as ?c) WHERE { #{pattern} }" rs = Goo.sparql_query_client.query(q) @@ -90,23 +92,27 @@ def backend_4s_delete end end - def before_suites + def before_suite # code to run before the first test (gets inherited in sub-tests) end - def after_suites + def after_suite # code to run after the last test (gets inherited in sub-tests) end - def _run_suites(suites, type) - begin - before_suites - super(suites, type) - ensure - after_suites - end + def before_all + super + backend_4s_delete + before_suite + end + + def after_all + after_suite + super end + + def _run_suite(suite, type) begin backend_4s_delete @@ -124,12 +130,12 @@ def _run_suite(suite, type) end end -AppUnit.runner = AppUnit.new + # All tests should inherit from this class. # Use 'rake test' from the command line to run tests. # See http://www.sinatrarb.com/testing.html for testing information -class TestCase < MiniTest::Unit::TestCase +class TestCase < AppUnit include Rack::Test::Methods @@ -220,16 +226,12 @@ def self.reset_to_not_admin(user) end def unused_port - max_retries = 5 - retries = 0 - server_port = Random.rand(55000..65535) - while port_in_use?(server_port) - retries += 1 - break if retries >= max_retries - server_port = Random.rand(55000..65535) - end - server_port + server = TCPServer.new('127.0.0.1', 0) + port = server.addr[1] + server.close + port end + private def port_in_use?(port) server = TCPServer.new(port)