Skip to content

Commit

Permalink
Add spec for gitea api wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
krauselukas authored and eduardoj committed Sep 27, 2022
1 parent 7ef4c03 commit f6a4bee
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/api/spec/lib/gitea_api/v1/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'rails_helper'

RSpec.describe GiteaAPI::V1::Client do
let(:client) { described_class.new(api_endpoint: 'https://gitea.opensuse.org', token: '12345') }
let(:faraday) { instance_double(Faraday::Connection) }

describe '#create_commit_status' do
context 'when it is successful' do
let(:url) { 'repos/krauselukas/hello_world/statuses/abc123cdf' }

before do
allow(Faraday::Connection).to receive(:new).and_return(faraday)
allow(faraday).to receive(:post).and_return(Faraday::Response)
allow(Faraday::Response).to receive(:status).and_return(200)
allow(Faraday::Response).to receive(:body).and_return(true)

client.create_commit_status(owner: 'krauselukas', repo: 'hello_world', sha: 'abc123cdf', state: 'succeeded')
end

it 'sends a post request to the correct api endpoint' do
expect(faraday).to have_received(:post).with(url, { context: nil, description: nil, state: 'succeeded', target_url: nil })
end
end

context 'when something goes wrong' do
before do
allow(Faraday::Connection).to receive(:new).and_return(faraday)
allow(faraday).to receive(:post).and_return(Faraday::Response)
allow(Faraday::Response).to receive(:status).and_return(400)
allow(Faraday::Response).to receive(:body).and_return({ 'message' => 'upppsss something went wrong' })
end

it 'sends a post request and returns the correct exception class' do
expect { client.create_commit_status(owner: 'krauselukas', repo: 'hello_world', sha: 'abc123cdf', state: 'succeeded') }.to raise_error(GiteaAPI::V1::Client::BadRequestError)
end
end
end
end

0 comments on commit f6a4bee

Please sign in to comment.