From 5a424e8e3dfdd00202a5dcd07da4dca96e8fc4df Mon Sep 17 00:00:00 2001 From: merefield Date: Tue, 5 Oct 2021 10:07:20 +0100 Subject: [PATCH] API: add mocked request spec --- spec/components/custom_wizard/action_spec.rb | 27 +++++++++++++++++++ spec/fixtures/api/no_authorization.json | 3 +++ spec/fixtures/endpoints/test_endpoint.json | 5 ++++ .../endpoints/test_endpoint_body.json | 3 +++ spec/plugin_helper.rb | 1 + 5 files changed, 39 insertions(+) create mode 100644 spec/fixtures/api/no_authorization.json create mode 100644 spec/fixtures/endpoints/test_endpoint.json create mode 100644 spec/fixtures/endpoints/test_endpoint_body.json diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index e34dd861..f5296a54 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -13,6 +13,9 @@ describe CustomWizard::Action do let(:add_to_group) { get_wizard_fixture("actions/add_to_group") } let(:send_message) { get_wizard_fixture("actions/send_message") } let(:send_message_multi) { get_wizard_fixture("actions/send_message_multi") } + let(:api_test_endpoint) { get_wizard_fixture("endpoints/test_endpoint") } + let(:api_test_endpoint_body) { get_wizard_fixture("endpoints/test_endpoint_body")} + let(:api_test_no_authorization) { get_wizard_fixture("api/no_authorization")} def update_template(template) CustomWizard::Template.save(template, skip_jobs: true) @@ -265,5 +268,29 @@ describe CustomWizard::Action do expect(group.users.first.username).to eq('angus') end + + it '#send_to_api' do + stub_request(:put, "https://myexternalapi.com/update"). + with( + body: "some_body", + headers: { + 'Host'=>'myexternalapi.com' + }). + to_return(status: 200, body: "success", headers: {}) + + new_api = CustomWizard::Api.new("my_api") + CustomWizard::Api.set("my_api", title: "Mocked external api") + CustomWizard::Api::Authorization.set("my_api", api_test_no_authorization) + CustomWizard::Api::Endpoint.new("my_api") + CustomWizard::Api::Endpoint.set("my_api", api_test_endpoint) + endpoint_id = CustomWizard::Api::Endpoint.list("my_api").first.id + + result = CustomWizard::Api::Endpoint.request("my_api", endpoint_id, "some_body") + log_entry = CustomWizard::Api::LogEntry.list("my_api").first + byebug + expect(result).to eq('success') + expect(log_entry.status).to eq('SUCCESS') + end end end + diff --git a/spec/fixtures/api/no_authorization.json b/spec/fixtures/api/no_authorization.json new file mode 100644 index 00000000..b1c6506e --- /dev/null +++ b/spec/fixtures/api/no_authorization.json @@ -0,0 +1,3 @@ +{ + "auth_type": "none" +} \ No newline at end of file diff --git a/spec/fixtures/endpoints/test_endpoint.json b/spec/fixtures/endpoints/test_endpoint.json new file mode 100644 index 00000000..ba979064 --- /dev/null +++ b/spec/fixtures/endpoints/test_endpoint.json @@ -0,0 +1,5 @@ +{ + "method": "PUT", + "url": "https://myexternalapi.com/update", + "success_codes": [200] +} \ No newline at end of file diff --git a/spec/fixtures/endpoints/test_endpoint_body.json b/spec/fixtures/endpoints/test_endpoint_body.json new file mode 100644 index 00000000..837e7c72 --- /dev/null +++ b/spec/fixtures/endpoints/test_endpoint_body.json @@ -0,0 +1,3 @@ +{ + "data": "some_data" +} \ No newline at end of file diff --git a/spec/plugin_helper.rb b/spec/plugin_helper.rb index d47c47c1..e72fe355 100644 --- a/spec/plugin_helper.rb +++ b/spec/plugin_helper.rb @@ -15,6 +15,7 @@ require 'oj' Oj.default_options = Oj.default_options.merge(cache_str: -1) require 'rails_helper' +require 'webmock/rspec' def get_wizard_fixture(path) JSON.parse(