Miroslav Kralik

Joined

60 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Testing API Requests Discussion

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