# typed: false # frozen_string_literal: true module Webmocks module YettoWebmock def yetto_auth_header(payload, signing_secret = "super-secret") "sha256=#{OpenSSL::HMAC.hexdigest(Authable::SHA256_DIGEST, signing_secret, payload.to_json)}" end def assert_requested_post_access_token(plug_installation_id) assert_requested(:post, "#{::YettoService::YETTO_API_VERSION_TLD}/plugs/installations/#{plug_installation_id}/access_tokens") end def stub_post_access_token(plug_installation_id) response = { token: Faker::Alphanumeric.alphanumeric(number: 26).upcase, } stub_request(:post, "#{::YettoService::YETTO_API_VERSION_TLD}/plugs/installations/#{plug_installation_id}/access_tokens") .to_return( body: response.to_json, ) end def assert_requested_get_plug_installation(plug_installation_id) assert_requested_post_access_token(plug_installation_id) assert_requested(:get, "#{::YettoService::YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}") end def stub_get_plug_installation(plug_installation_id, response = nil, status: 200) stub_post_access_token(plug_installation_id) response ||= { installed_on_inbox: { id: inbox_id, organization: { id: organization_id, status: "active", }, }, plug: { id: "plg_#{Faker::Alphanumeric.alphanumeric(number: 26).upcase}", }, settings: { from_email: "new@from.company", }, } stub_request(:get, "#{::YettoService::YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}") .to_return( status: status, headers: { content_type: "application/json; charset=utf-8" }, body: response.to_json, ) end def assert_requested_update_installation(plug_installation_id) assert_requested_post_access_token(plug_installation_id) assert_requested(:patch, "#{::YettoService::YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}") end def stub_update_installation(plug_installation_id, params, response: {}, status: 200) stub_post_access_token(plug_installation_id) stub_request(:patch, "#{::YettoService::YETTO_API_VERSION_TLD}/installations/#{plug_installation_id}") .with( body: params, ) .to_return( status: status, headers: { content_type: "application/json; charset=utf-8" }, body: response.to_json, ) end def assert_requested_create_message(inbox_id) assert_requested_post_access_token(plug_installation_id) assert_requested(:post, "#{::YettoService::YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/messages") end def stub_create_message(inbox_id, payload) stub_post_access_token(plug_installation_id) stub_request(:post, "#{::YettoService::YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/messages") .with( body: payload, ) .to_return( status: 200, headers: { content_type: "application/json; charset=utf-8" }, body: {}.to_json, ) end def assert_requested_create_inbox_switch(inbox_id, plug_installation_id) assert_requested_post_access_token(plug_installation_id) assert_requested(:post, "#{::YettoService::YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/switches") end def stub_create_inbox_switch(inbox_id, plug_installation_id, params) stub_post_access_token(plug_installation_id) payload = { name: "After install", } stub_request(:post, "#{::YettoService::YETTO_API_VERSION_TLD}/inboxes/#{inbox_id}/switches") .with( body: payload, ) .to_return( status: 200, headers: { content_type: "application/json; charset=utf-8" }, body: {}.to_json, ) end end end