Ask A Question

Notifications

You’re not receiving notifications from this thread.

Testing API Requests Discussion

Discussion for Testing API Requests

I am getting an error "Faraday::Adapter::Test::Stubs::NotFound: no stubbed request for post" and cannot find anything that could be wrong.

secret_test.rb
stub_token = stub_request("oauth2/v2.0/token", method: :post, response: stub_response(fixture: "token/create"))

stub_resource = stub_request("secrets/test", response: stub_response(fixture: "secret/retrieve"))

client = AzureKV::Client.new(adapter: :test, stub_token: stub_token, stub_resource: stub_resource)
secret = client.secret.retrieve(name: "test")
test_helper.rb
def stub_response(fixture:, status: 200, headers: {"Content-Type" => "application/json"})
    [status, headers, File.read("test/fixtures/#{fixture}.json")]
  end

  def stub_request(path, response:, method: :get, body: {})
    Faraday::Adapter::Test::Stubs.new do |stub|
      arguments = [method, path]
      arguments << body.to_json if [:post, :put, :patch].include?(method)
      stub.send(*arguments) { |env| response }
    end
  end
client.rb
def connection
      fetch_token

      @connection ||= Faraday.new(url: vault_base_url) do |conn|
        conn.request :authorization, "Bearer", @token
        conn.request :json
        conn.response :json, content_type: "application/json"
        conn.adapter adapter, @stub_resource
      end
    end

def fetch_token
      base_url = "https://login.microsoftonline.com/#{tenant_id}/"
      token_url = "oauth2/v2.0/token"
      body = {
        client_id: client_id,
        client_secret: client_secret,
        grant_type: "client_credentials",
        scope: scope
      }

@token ||= Faraday.new(url: base_url) do |conn|
        conn.request :url_encoded
        conn.response :json, content_type: "application/json"
        conn.adapter adapter, @stub_token
      end.post(token_url, body).body['access_token']
end
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.