# 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}/plug/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}/plug/installations/#{plug_installation_id}/access_tokens") .to_return( body: response.to_json, ) end def assert_requested_get_inbox_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_inbox_plug_installation(organization_id, inbox_id, plug_installation_id, response = {}, valid: true, status: 200, expires_at: 8.hours.from_now) stub_post_access_token(plug_installation_id) github_installations = JSON.parse(file_fixture_path("plug_installation_settings", valid ? "valid.json" : "invalid.json").read).deep_symbolize_keys response = { installed_on_inbox: { id: inbox_id, organization: { id: organization_id, status: "active", }, }, plug: { id: "plg_#{Faker::Alphanumeric.alphanumeric(number: 26).upcase}", }, settings: github_installations, credentials: { access_token: Faker::Alphanumeric.alphanumeric(number: 26).upcase, refresh_access_token: "refresher", expires_at: expires_at.iso8601, }, }.merge(response) 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(plug_installation_id, message_id) assert_requested_post_access_token(plug_installation_id) assert_requested(:post, "#{::YettoService::YETTO_API_VERSION_TLD}/messages/#{message_id}/replies") end def stub_create_message(plug_installation_id, message_id, payload) stub_post_access_token(plug_installation_id) stub_request(:post, "#{::YettoService::YETTO_API_VERSION_TLD}/messages/#{message_id}/replies") # .with( # body: payload, # ) # .to_return( # status: 200, # headers: { content_type: "application/json; charset=utf-8" }, # body: {}.to_json, # ) end def assert_requested_update_message_metadata(plug_installation_id, message_id) assert_requested_post_access_token(plug_installation_id) assert_requested(:patch, "#{::YettoService::YETTO_API_VERSION_TLD}/messages/#{message_id}") end def stub_update_message_metadata(plug_installation_id, message_id, payload) stub_post_access_token(plug_installation_id) stub_request(:patch, "#{::YettoService::YETTO_API_VERSION_TLD}/messages/#{message_id}") .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, payload) stub_post_access_token(plug_installation_id) 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