diff --git a/lib/inferno/dsl/resume_test_route.rb b/lib/inferno/dsl/resume_test_route.rb index f92e18c0c..c1b44ffdb 100644 --- a/lib/inferno/dsl/resume_test_route.rb +++ b/lib/inferno/dsl/resume_test_route.rb @@ -1,4 +1,4 @@ -require_relative 'suite_endpoint' +require 'hanami/controller' module Inferno module DSL @@ -6,47 +6,58 @@ module DSL # an incoming request. # @private # @see Inferno::DSL::Runnable#resume_test_route - class ResumeTestRoute < SuiteEndpoint - # The incoming request - # - # @return [Inferno::Entities::Request] - def request - @request ||= Inferno::Entities::Request.from_hanami_request(req) + class ResumeTestRoute < Hanami::Action + include Import[ + requests_repo: 'inferno.repositories.requests', + results_repo: 'inferno.repositories.results', + test_runs_repo: 'inferno.repositories.test_runs', + tests_repo: 'inferno.repositories.tests' + ] + + def self.call(...) + new.call(...) end # @private - def test_run_identifier - @test_run_identifier ||= instance_exec(request, &test_run_identifier_block) + def test_run_identifier_block + self.class.singleton_class.instance_variable_get(:@test_run_identifier_block) end # @private def tags - self.class.singleton_class.instance_variable_get(:@tags) || [] + self.class.singleton_class.instance_variable_get(:@tags) end # @private - def update_result - results_repo.update_result(result.id, new_result) + def result + self.class.singleton_class.instance_variable_get(:@result) end # @private - def make_response - res.redirect_to redirect_route(test_run, test) + def find_test_run(test_run_identifier) + test_runs_repo.find_latest_waiting_by_identifier(test_run_identifier) end # @private - def name - test.config.request_name(test.incoming_request_name) + def find_waiting_result(test_run) + results_repo.find_waiting_result(test_run_id: test_run.id) end # @private - def test_run_identifier_block - self.class.singleton_class.instance_variable_get(:@test_run_identifier_block) + def update_result(waiting_result) + results_repo.update_result(waiting_result.id, result) end # @private - def new_result - self.class.singleton_class.instance_variable_get(:@new_result) + def persist_request(request, test_run, waiting_result, test) + requests_repo.create( + request.to_hash.merge( + test_session_id: test_run.test_session_id, + result_id: waiting_result.id, + name: test.config.request_name(test.incoming_request_name), + tags: + ) + ) end # @private @@ -54,10 +65,38 @@ def redirect_route(test_run, test) "#{Application['base_url']}/test_sessions/#{test_run.test_session_id}##{resume_ui_at_id(test_run, test)}" end + # @private + def find_test(waiting_result) + tests_repo.find(waiting_result.test_id) + end + # @private def resume_ui_at_id(test_run, test) test_run.test_suite_id || test_run.test_group_id || test.parent.id end + + # @private + def handle(req, res) + request = Inferno::Entities::Request.from_hanami_request(req) + + test_run_identifier = instance_exec(request, &test_run_identifier_block) + + test_run = find_test_run(test_run_identifier) + + halt 500, "Unable to find test run with identifier '#{test_run_identifier}'." if test_run.nil? + + test_runs_repo.mark_as_no_longer_waiting(test_run.id) + + waiting_result = find_waiting_result(test_run) + test = find_test(waiting_result) + + update_result(waiting_result) + persist_request(request, test_run, waiting_result, test) + + Jobs.perform(Jobs::ResumeTestRun, test_run.id) + + res.redirect_to redirect_route(test_run, test) + end end end end diff --git a/lib/inferno/dsl/runnable.rb b/lib/inferno/dsl/runnable.rb index dad4d1bde..e74182541 100644 --- a/lib/inferno/dsl/runnable.rb +++ b/lib/inferno/dsl/runnable.rb @@ -343,7 +343,7 @@ def resume_test_route(method, path, tags: [], result: 'pass', &block) route_class = Class.new(ResumeTestRoute) do |klass| klass.singleton_class.instance_variable_set(:@test_run_identifier_block, block) klass.singleton_class.instance_variable_set(:@tags, tags) - klass.singleton_class.instance_variable_set(:@new_result, result) + klass.singleton_class.instance_variable_set(:@result, result) end route(method, path, route_class) diff --git a/lib/inferno/dsl/suite_endpoint.rb b/lib/inferno/dsl/suite_endpoint.rb index ecb2bd632..a08acfc0d 100644 --- a/lib/inferno/dsl/suite_endpoint.rb +++ b/lib/inferno/dsl/suite_endpoint.rb @@ -244,7 +244,7 @@ def persist_request # @private def resume_test_run? - find_result&.result != 'waiting' + find_result&.result != 'wait' end # @private