58 lines
1.6 KiB
Ruby
58 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe Fiken::Client do
|
|
subject(:client) { described_class.new(token: "test-token") }
|
|
|
|
describe "#initialize" do
|
|
it "raises without a token" do
|
|
expect { described_class.new }.to raise_error(ArgumentError)
|
|
end
|
|
|
|
it "accepts an access_token alias" do
|
|
expect { described_class.new(access_token: "x") }.not_to raise_error
|
|
end
|
|
end
|
|
|
|
describe "#user" do
|
|
it "GETs /user with a bearer token and wraps the response" do
|
|
stub_fiken(:get, "/user", body: { "name" => "Runar", "email" => "runar@rin.no" })
|
|
|
|
user = client.user
|
|
|
|
expect(user.name).to eq("Runar")
|
|
expect(a_request(:get, "#{API_BASE}/user")
|
|
.with(headers: { "Authorization" => "Bearer test-token" })).to have_been_made
|
|
end
|
|
end
|
|
|
|
describe "#companies" do
|
|
it "lists companies" do
|
|
stub_fiken(:get, "/companies", body: [{ "slug" => "acme", "name" => "Acme AS" }])
|
|
|
|
companies = client.companies.list
|
|
|
|
expect(companies.map(&:slug)).to eq(["acme"])
|
|
end
|
|
|
|
it "finds a single company by slug" do
|
|
stub_fiken(:get, "/companies/acme", body: { "slug" => "acme", "name" => "Acme AS" })
|
|
|
|
expect(client.company("acme").name).to eq("Acme AS")
|
|
end
|
|
end
|
|
|
|
describe "error mapping" do
|
|
it "raises Unauthorized on 401" do
|
|
stub_fiken(:get, "/user", status: 401, body: { "message" => "Bad token" })
|
|
|
|
expect { client.user }.to raise_error(Fiken::Unauthorized, "Bad token")
|
|
end
|
|
|
|
it "raises NotFound on 404" do
|
|
stub_fiken(:get, "/companies/missing", status: 404)
|
|
|
|
expect { client.company("missing") }.to raise_error(Fiken::NotFound)
|
|
end
|
|
end
|
|
end
|