Skip to content

Commit

Permalink
add client test
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Jul 29, 2024
1 parent d19bd8e commit 57ad58a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion spec/domain/ptime/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
expect(fetched_employees).to eq(ptime_employees_data)
end

it 'should not raise PTimeClientError if LAST_PTIME_ERROR is less than 5 minutes ago' do
it 'should not raise PTimeClientError if LAST_PTIME_ERROR is more than 5 minutes ago' do
stub_env_var("LAST_PTIME_ERROR", 6.minutes.ago.to_s)
fetched_employees = Ptime::Client.new.request(:get, "employees", { per_page: 1000 })
expect(fetched_employees).to eq(ptime_employees_data)
end

it 'should raise PTimeClientError page is unreachable' do
stub_env_var("PTIME_BASE_URL", "irgend.oepp.is")
stub_ptime_request(ptime_employees.to_json, "employees?per_page=1000", 404)
expect {
Ptime::Client.new.request(:get, "employees", { per_page: 1000 })
}.to raise_error(CustomExceptions::PTimeClientError)
end

it 'should raise PTimeClientError if LAST_PTIME_ERROR is less than 5 minutes ago' do
stub_env_var("LAST_PTIME_ERROR", 4.minutes.ago.to_s)
expect {
Expand Down
4 changes: 2 additions & 2 deletions spec/support/ptime_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def ptime_employees_data
fixture_data("all_ptime_employees")[:data]
end

def stub_ptime_request(return_body, path =nil)
def stub_ptime_request(return_body, path =nil, status = 200)
path ||= "employees?per_page=1000"
url = "http://#{ENV["PTIME_BASE_URL"]}/api/v1/#{path}"
content_type = "application/vnd.api+json; charset=utf-8"

stub_request(:get, url)
.to_return(body: return_body, headers: { 'content-type': content_type }, status: 200)
.to_return(body: return_body, headers: { 'content-type': content_type }, status: status)
.with(basic_auth: [ENV["PTIME_API_USERNAME"], ENV["PTIME_API_PASSWORD"]])
end

Expand Down

0 comments on commit 57ad58a

Please sign in to comment.