Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#48457] Replace RestClient with Net::HTTP in storages module.

Pavel Balashou0a2cc19e67b8
diff --git a/Gemfile b/Gemfile
index 4a46ee14e25..e4e0b8bb951 100644
--- a/Gemfile
+++ b/Gemfile
@@ -191,8 +191,6 @@ gem 'openproject-token', '~> 3.0.1'
gem 'plaintext', '~> 0.3.2'
-gem 'rest-client', '~> 2.0'
-
gem 'ruby-progressbar', '~> 1.13.0', require: false
gem 'mini_magick', '~> 4.12.0', require: false
diff --git a/Gemfile.lock b/Gemfile.lock
index f543d469b46..1f24a64d52f 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -398,8 +398,6 @@ GEM
disposable (0.6.3)
declarative (>= 0.0.9, < 1.0.0)
representable (>= 3.1.1, < 4)
- domain_name (0.5.20190701)
- unf (>= 0.0.5, < 1.0.0)
doorkeeper (5.6.6)
railties (>= 5)
dotenv (2.8.1)
@@ -532,9 +530,6 @@ GEM
htmlbeautifier (1.4.2)
htmldiff (0.0.1)
htmlentities (4.3.4)
- http-accept (1.7.0)
- http-cookie (1.0.5)
- domain_name (~> 0.5)
http_parser.rb (0.6.0)
httpclient (2.8.3)
i18n (1.14.1)
@@ -638,7 +633,6 @@ GEM
timeout
net-smtp (0.3.3)
net-protocol
- netrc (0.11.0)
nio4r (2.5.9)
nokogiri (1.15.4)
mini_portile2 (~> 2.8.2)
@@ -813,11 +807,6 @@ GEM
responders (3.1.0)
actionpack (>= 5.2)
railties (>= 5.2)
- rest-client (2.1.0)
- http-accept (>= 1.7.0, < 2.0)
- http-cookie (>= 1.0.2, < 2.0)
- mime-types (>= 1.16, < 4.0)
- netrc (~> 0.8)
retriable (3.1.2)
rexml (3.2.6)
rinku (2.0.6)
@@ -1136,7 +1125,6 @@ DEPENDENCIES
rdoc (>= 2.4.2)
request_store (~> 1.5.0)
responders (~> 3.0)
- rest-client (~> 2.0)
retriable (~> 3.1.1)
rinku (~> 2.0.4)
roar (~> 1.2.0)
@@ -1187,4 +1175,4 @@ RUBY VERSION
ruby 3.2.1p31
BUNDLED WITH
- 2.4.7
+ 2.4.6
diff --git a/app/services/oauth_clients/connection_manager.rb b/app/services/oauth_clients/connection_manager.rb
index cee446293d0..8417800ad8b 100644
--- a/app/services/oauth_clients/connection_manager.rb
+++ b/app/services/oauth_clients/connection_manager.rb
@@ -141,28 +141,35 @@ module OAuthClients
# valid authentication, so we use it for testing the validity of the Bearer token.
# curl -H "Authorization: Bearer MY_TOKEN" -X GET 'https://my.nextcloud.org/ocs/v1.php/cloud/user' \
# -H "OCS-APIRequest: true" -H "Accept: application/json"
- RestClient.get(
- File.join(oauth_client.integration.host, AUTHORIZATION_CHECK_PATH),
- {
- 'Authorization' => "Bearer #{oauth_client_token.access_token}",
- 'OCS-APIRequest' => "true",
- 'Accept' => "application/json"
- }
- )
- :connected
- rescue RestClient::Unauthorized, RestClient::Forbidden
- service_result = refresh_token # `refresh_token` already has exception handling
- return :connected if service_result.success?
-
- if service_result.result == 'invalid_request'
- # This can happen if the Authorization Server invalidated all tokens.
- # Then the user would ideally be asked to reauthorize.
- :failed_authorization
- else
- # It could also be that some other error happened, i.e. firewall badly configured.
- # Then the user needs to know that something is technically off. The user could try
- # to reload the page or contact an admin.
- :error
+ util = Storages::Peripherals::StorageInteraction::Nextcloud::Util
+ uri = URI(oauth_client.integration.host).normalize
+ response = util
+ .http(uri)
+ .get(
+ util.join_uri_path(uri, AUTHORIZATION_CHECK_PATH),
+ {
+ 'Authorization' => "Bearer #{oauth_client_token.access_token}",
+ 'OCS-APIRequest' => 'true',
+ 'Accept' => 'application/json'
+ }
+ )
+ case response
+ when Net::HTTPSuccess
+ :connected
+ when Net::HTTPForbidden, Net::HTTPUnauthorized
+ service_result = refresh_token # `refresh_token` already has exception handling
+ if service_result.success?
+ :connected
+ elsif service_result.result == 'invalid_request'
+ # This can happen if the Authorization Server invalidated all tokens.
+ # Then the user would ideally be asked to reauthorize.
+ :failed_authorization
+ else
+ # It could also be that some other error happened, i.e. firewall badly configured.
+ # Then the user needs to know that something is technically off. The user could try
+ # to reload the page or contact an admin.
+ :error
+ end
end
rescue StandardError
:error
diff --git a/docs/api/apiv3/tags/baseline_comparisons.yml b/docs/api/apiv3/tags/baseline_comparisons.yml
index 5e329c6fb87..90576dc821a 100644
--- a/docs/api/apiv3/tags/baseline_comparisons.yml
+++ b/docs/api/apiv3/tags/baseline_comparisons.yml
@@ -268,8 +268,8 @@ description: |-
timestamps = [baseline_timestamp, current_timestamp]
# Retrieve work packages
- url = "https://community.openproject.org/api/v3/work_packages?timestamps=#{timestamps.join(',')}"
- response = JSON.parse(RestClient.get(url), object_class: OpenStruct)
+ url = URI("https://community.openproject.org/api/v3/work_packages?timestamps=#{timestamps.join(',')}")
+ response = JSON.parse(Net::HTTP.get(url), object_class: OpenStruct)
work_packages = response.dig("_embedded", "elements")
# Extract differing baseline attributes
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
index 2004a9cde4a..9ed5804f28b 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/download_link_query.rb
@@ -31,7 +31,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
using Storages::Peripherals::ServiceResultRefinements
def initialize(storage)
- @base_uri = URI(storage.host).normalize
+ @uri = URI(storage.host).normalize
@oauth_client = storage.oauth_client
end
@@ -39,25 +39,25 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
def call(user:, file_link:)
result = Util.token(user:, oauth_client: @oauth_client) do |token|
service_result = begin
- ServiceResult.success(
- result: RestClient.post(
- Util.join_uri_path(@base_uri, '/ocs/v2.php/apps/dav/api/v1/direct'),
- { fileId: file_link.origin_id },
- {
- 'Authorization' => "Bearer #{token.access_token}",
- 'OCS-APIRequest' => 'true',
- 'Accept' => 'application/json'
- }
- )
+ response = Util.http(@uri).post(
+ Util.join_uri_path(@uri.path, '/ocs/v2.php/apps/dav/api/v1/direct'),
+ { fileId: file_link.origin_id },
+ {
+ 'Authorization' => "Bearer #{token.access_token}",
+ 'OCS-APIRequest' => 'true',
+ 'Accept' => 'application/json'
+ }
)
- rescue RestClient::Unauthorized => e
- Util.error(:not_authorized, 'Outbound request not authorized!', e.response)
- rescue RestClient::NotFound => e
- Util.error(:not_found, 'Outbound request destination not found!', e.response)
- rescue RestClient::ExceptionWithResponse => e
- Util.error(:error, 'Outbound request failed!', e.response)
- rescue StandardError
- Util.error(:error, 'Outbound request failed!')
+ case response
+ when Net::HTTPSuccess
+ ServiceResult.success(result: response)
+ when Net::HTTPNotFound
+ Util.error(:not_found, 'Outbound request destination not found!', response)
+ when Net::HTTPUnauthorized
+ Util.error(:not_authorized, 'Outbound request not authorized!', response)
+ else
+ Util.error(:error, 'Outbound request failed!')
+ end
end
service_result.bind do |response|
@@ -65,7 +65,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
if response.body.blank?
Util.error(:not_authorized, 'Outbound request not authorized!')
else
- ServiceResult.success(result: response)
+ ServiceResult.success(result: response.body)
end
end
end
@@ -80,7 +80,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
private
def download_link(token, origin_name)
- Util.join_uri_path(@base_uri, 'index.php/apps/integration_openproject/direct', token, CGI.escape(origin_name))
+ Util.join_uri_path(@uri, 'index.php/apps/integration_openproject/direct', token, CGI.escape(origin_name))
end
def direct_download_token(body:)
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
index 9fe5ac3ddef..9d33ce40015 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_info_query.rb
@@ -54,27 +54,26 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
private
def files_info(file_ids, token)
- ServiceResult.success(
- result: RestClient::Request.execute(
- method: :post,
- url: Util.join_uri_path(@uri, FILES_INFO_PATH),
- payload: { fileIds: file_ids }.to_json,
- headers: {
- 'Authorization' => "Bearer #{token.access_token}",
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json',
- 'OCS-APIRequest' => true
- }
- )
- ).map(&:body)
- rescue RestClient::Unauthorized => e
- Util.error(:not_authorized, 'Outbound request not authorized!', e.response)
- rescue RestClient::NotFound => e
- Util.error(:not_found, 'Outbound request destination not found!', e.response)
- rescue RestClient::ExceptionWithResponse => e
- Util.error(:error, 'Outbound request failed!', e.response)
- rescue StandardError
- Util.error(:error, 'Outbound request failed!')
+ response = Util.http(@uri).post(
+ Util.join_uri_path(@uri.path, FILES_INFO_PATH),
+ { fileIds: file_ids }.to_json,
+ {
+ 'Authorization' => "Bearer #{token.access_token}",
+ 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json',
+ 'OCS-APIRequest' => 'true'
+ }
+ )
+ case response
+ when Net::HTTPSuccess
+ ServiceResult.success(result: response.body)
+ when Net::HTTPNotFound
+ Util.error(:not_found, 'Outbound request destination not found!', response)
+ when Net::HTTPUnauthorized
+ Util.error(:not_authorized, 'Outbound request not authorized!', response)
+ else
+ Util.error(:error, 'Outbound request failed!')
+ end
end
def parse_json
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
index 6d823e8a5a8..747359e41b5 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/upload_link_query.rb
@@ -34,7 +34,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
URI_UPLOAD_BASE_PATH = 'index.php/apps/integration_openproject/direct-upload'.freeze
def initialize(storage)
- @base_uri = URI(storage.host).normalize
+ @uri = URI(storage.host).normalize
@oauth_client = storage.oauth_client
end
@@ -50,7 +50,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
token:
).map do |response|
Storages::UploadLink.new(
- URI.parse(Util.join_uri_path(@base_uri, URI_UPLOAD_BASE_PATH, response.token))
+ URI.parse(Util.join_uri_path(@uri, URI_UPLOAD_BASE_PATH, response.token))
)
end
end
@@ -59,46 +59,33 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
private
- # rubocop:disable Metrics/AbcSize
def outbound_response(method:, relative_path:, payload:, token:)
- response = begin
- ServiceResult.success(
- result: RestClient::Request.execute(
- method:,
- url: Util.join_uri_path(@base_uri, relative_path),
- payload: payload.to_json,
- headers: {
- 'Authorization' => "Bearer #{token.access_token}",
- 'Accept' => 'application/json',
- 'Content-Type' => 'application/json'
- }
- )
- )
- rescue RestClient::Unauthorized => e
- Util.error(:not_authorized, 'Outbound request not authorized!', e.response)
- rescue RestClient::NotFound => e
- Util.error(:not_found, 'Outbound request destination not found!', e.response)
- rescue RestClient::ExceptionWithResponse => e
- Util.error(:error, 'Outbound request failed!', e.response)
- rescue StandardError
- Util.error(:error, 'Outbound request failed!')
- end
-
- # rubocop:disable Style/OpenStructUse
- # rubocop:disable Style/MultilineBlockChain
- response
- .bind do |r|
+ response = Util.http(@uri).post(
+ Util.join_uri_path(@uri, relative_path),
+ payload.to_json,
+ {
+ 'Authorization' => "Bearer #{token.access_token}",
+ 'Accept' => 'application/json',
+ 'Content-Type' => 'application/json'
+ }
+ )
+ case response
+ when Net::HTTPSuccess
# The nextcloud API returns a successful response with empty body if the authorization is missing or expired
- if r.body.blank?
- Util.error(:not_authorized, 'Outbound request not authorized!')
+ if response.body.present?
+ ServiceResult.success(
+ result: JSON.parse(response.body, object_class: OpenStruct)
+ )
else
- ServiceResult.success(result: r)
+ Util.error(:not_authorized, 'Outbound request not authorized!')
end
- end.map { |r| JSON.parse(r.body, object_class: OpenStruct) }
- # rubocop:enable Style/MultilineBlockChain
- # rubocop:enable Style/OpenStructUse Style/MultilineBlockChain
+ when Net::HTTPNotFound
+ Util.error(:not_found, 'Outbound request destination not found!', response)
+ when Net::HTTPUnauthorized
+ Util.error(:not_authorized, 'Outbound request not authorized!', response)
+ else
+ Util.error(:error, 'Outbound request failed!')
+ end
end
-
- # rubocop:enable Metrics/AbcSize
end
end
diff --git a/modules/webhooks/app/workers/project_webhook_job.rb b/modules/webhooks/app/workers/project_webhook_job.rb
index 857db6e384d..73436bc314f 100644
--- a/modules/webhooks/app/workers/project_webhook_job.rb
+++ b/modules/webhooks/app/workers/project_webhook_job.rb
@@ -1,4 +1,3 @@
-require 'rest-client'
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
diff --git a/modules/webhooks/app/workers/represented_webhook_job.rb b/modules/webhooks/app/workers/represented_webhook_job.rb
index 8a71c6b1194..daa7018dd53 100644
--- a/modules/webhooks/app/workers/represented_webhook_job.rb
+++ b/modules/webhooks/app/workers/represented_webhook_job.rb
@@ -1,4 +1,3 @@
-require 'rest-client'
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
@@ -48,11 +47,15 @@ class RepresentedWebhookJob < WebhookJob
end
begin
- response = RestClient.post webhook.url, request_body, headers
- rescue RestClient::RequestTimeout => e
+ response = Faraday.post(
+ webhook.url,
+ request_body,
+ headers
+ )
+ rescue Faraday::TimeoutError => e
response = e.response
exception = e
- rescue RestClient::Exception => e
+ rescue Faraday::Error => e
response = e.response
exception = e
rescue StandardError => e
@@ -66,16 +69,14 @@ class RepresentedWebhookJob < WebhookJob
url: webhook.url,
request_headers: headers,
request_body: body,
- response_code: response.try(:code).to_i,
- response_headers: response.try(:headers),
- response_body: response.try(:to_s) || exception.try(:message)
+ response_code: response&.status,
+ response_headers: response&.headers&.to_h&.transform_keys {|k| k.underscore.to_sym},
+ response_body: response&.body || exception&.message
)
# We want to re-raise timeout exceptions
# but log the request beforehand
- if exception&.is_a?(RestClient::RequestTimeout)
- raise exception
- end
+ raise exception if exception.is_a?(Faraday::TimeoutError)
end
def accepted_in_project?
@@ -84,7 +85,7 @@ class RepresentedWebhookJob < WebhookJob
def request_signature(request_body)
if secret = webhook.secret.presence
- 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, request_body)
+ "sha1=#{OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, request_body)}"
end
end
diff --git a/modules/webhooks/app/workers/time_entry_webhook_job.rb b/modules/webhooks/app/workers/time_entry_webhook_job.rb
index 3565078bfbf..5184c9261af 100644
--- a/modules/webhooks/app/workers/time_entry_webhook_job.rb
+++ b/modules/webhooks/app/workers/time_entry_webhook_job.rb
@@ -1,4 +1,3 @@
-require 'rest-client'
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
diff --git a/modules/webhooks/app/workers/webhook_job.rb b/modules/webhooks/app/workers/webhook_job.rb
index f7556813bd6..f4d8c73c580 100644
--- a/modules/webhooks/app/workers/webhook_job.rb
+++ b/modules/webhooks/app/workers/webhook_job.rb
@@ -32,7 +32,7 @@ class WebhookJob < ApplicationJob
# Retry webhook jobs three times with exponential backoff
# in case of timeouts
retry_on Timeout::Error,
- RestClient::RequestTimeout,
+ Faraday::TimeoutError,
wait: :exponentially_longer,
attempts: 3
diff --git a/modules/webhooks/app/workers/work_package_webhook_job.rb b/modules/webhooks/app/workers/work_package_webhook_job.rb
index 911ecc5e69b..893123f1ecd 100644
--- a/modules/webhooks/app/workers/work_package_webhook_job.rb
+++ b/modules/webhooks/app/workers/work_package_webhook_job.rb
@@ -1,4 +1,3 @@
-require 'rest-client'
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
diff --git a/modules/webhooks/spec/workers/project_webhook_job_spec.rb b/modules/webhooks/spec/workers/project_webhook_job_spec.rb
index 6811d30f576..dd93b7f04d9 100644
--- a/modules/webhooks/spec/workers/project_webhook_job_spec.rb
+++ b/modules/webhooks/spec/workers/project_webhook_job_spec.rb
@@ -36,7 +36,7 @@ RSpec.describe ProjectWebhookJob, type: :job, webmock: true do
shared_examples "a project webhook call" do
let(:event) { "project:created" }
- let(:job) { ProjectWebhookJob.perform_now(webhook.id, project, event) }
+ let(:job) { described_class.perform_now(webhook.id, project, event) }
let(:stubbed_url) { request_url }
diff --git a/script/github_pr_errors b/script/github_pr_errors
index 3bd4793ac76..68c590c0894 100755
--- a/script/github_pr_errors
+++ b/script/github_pr_errors
@@ -9,8 +9,8 @@ require 'json'
require 'optparse'
require 'pathname'
require 'pry'
-require 'rest-client'
require 'yaml'
+require 'faraday'
GITHUB_API_OPENPROJECT_PREFIX = 'https://api.github.com/repos/opf/openproject'.freeze
GITHUB_HTML_OPENPROJECT_PREFIX = 'https://github.com/opf/openproject'.freeze
@@ -130,17 +130,23 @@ def get_http(path)
end
say_verbose("HTTP GET #{url}")
- response = RestClient::Request.new(
- method: :get,
- url:,
- user: ENV.fetch('GITHUB_USERNAME'),
- password: ENV.fetch('GITHUB_TOKEN')
- ).execute
- say_verbose("HTTP Response #{response.code}")
+ conn = Faraday.new do |faraday|
+ faraday.response :raise_error # raise Faraday::Error on status code 4xx or 5xx
+ end
+
+ response = conn.get(
+ url,
+ nil,
+ {
+ 'Authorization' => "Basic #{Base64::strict_encode64("#{ENV.fetch('GITHUB_USERNAME')}:#{ENV.fetch('GITHUB_TOKEN')}")}"
+ }
+ )
+
+ say_verbose("HTTP Response #{response.status}")
- response.to_str
-rescue RestClient::ExceptionWithResponse => e
+ response.body
+rescue Faraday::Error => e
warn error_details(e)
exit(1)
rescue StandardError => e
@@ -148,16 +154,16 @@ rescue StandardError => e
exit 1
end
-def error_details(rest_client_exception_with_response)
- response = rest_client_exception_with_response.response
- error = JSON.parse(response.body)
+def error_details(error)
+ response = error.response
+ response_body = JSON.parse(response[:body])
parts = []
- parts << "Failed to perform API request #{response.request.method.upcase} #{response.request.url}: " \
- "#{rest_client_exception_with_response}"
- parts << " #{error['message']}"
- parts << " See #{error['documentation_url']}"
- parts += rest_client_exception_with_response.backtrace.map { " #{_1}" }
+ parts << "Failed to perform API request #{response[:request][:method].upcase} #{response[:request][:url]}: " \
+ "#{error}"
+ parts << " #{response_body['message']}"
+ parts << " See #{response_body['documentation_url']}"
+ parts += error.backtrace.map { " #{_1}" }
parts.join("\n")
end
diff --git a/spec/requests/oauth/client_credentials_flow_spec.rb b/spec/requests/oauth/client_credentials_flow_spec.rb
index 7314e84003b..3764e4e787a 100644
--- a/spec/requests/oauth/client_credentials_flow_spec.rb
+++ b/spec/requests/oauth/client_credentials_flow_spec.rb
@@ -27,9 +27,7 @@
#++
require 'spec_helper'
-require 'rest-client'
-# rubocop:disable Rails/HttpPositionalArguments
RSpec.describe 'OAuth client credentials flow' do
include Rack::Test::Methods
@@ -71,4 +69,3 @@ RSpec.describe 'OAuth client credentials flow' do
end
end
end
-# rubocop:enable Rails/HttpPositionalArguments
diff --git a/spec/services/oauth_clients/connection_manager_spec.rb b/spec/services/oauth_clients/connection_manager_spec.rb
index f00b1ebf351..0e9297a1c60 100644
--- a/spec/services/oauth_clients/connection_manager_spec.rb
+++ b/spec/services/oauth_clients/connection_manager_spec.rb
@@ -527,36 +527,46 @@ RSpec.describe OAuthClients::ConnectionManager, type: :model do
let(:new_oauth_client_token) { create(:oauth_client_token) }
let(:refresh_service_result) { ServiceResult.success }
- before do
- stub_request(:get, File.join(host, OAuthClients::ConnectionManager::AUTHORIZATION_CHECK_PATH))
- .to_return(status: 401) # 401 unauthorized
- allow(instance).to receive(:refresh_token).and_return(refresh_service_result)
- end
+ shared_examples 'refresh' do |code|
+ before do
+ stub_request(:get, File.join(host, OAuthClients::ConnectionManager::AUTHORIZATION_CHECK_PATH))
+ .to_return(status: code)
+ allow(instance).to receive(:refresh_token).and_return(refresh_service_result)
+ end
- context 'with valid refresh token' do
- it 'refreshes the access token and returns :connected' do
- expect(subject).to eq :connected
- expect(instance).to have_received(:refresh_token)
+ context 'with valid refresh token' do
+ it 'refreshes the access token and returns :connected' do
+ expect(subject).to eq :connected
+ expect(instance).to have_received(:refresh_token)
+ end
end
- end
- context 'with invalid refresh token' do
- let(:refresh_service_result) { ServiceResult.failure(result: 'invalid_request') }
+ context 'with invalid refresh token' do
+ let(:refresh_service_result) { ServiceResult.failure(result: 'invalid_request') }
- it 'refreshes the access token and returns :failed_authorization' do
- expect(subject).to eq :failed_authorization
- expect(instance).to have_received(:refresh_token)
+ it 'refreshes the access token and returns :failed_authorization' do
+ expect(subject).to eq :failed_authorization
+ expect(instance).to have_received(:refresh_token)
+ end
end
- end
- context 'with some other error while refreshing access token' do
- let(:refresh_service_result) { ServiceResult.failure }
+ context 'with some other error while refreshing access token' do
+ let(:refresh_service_result) { ServiceResult.failure }
- it 'returns :error' do
- expect(subject).to eq :error
- expect(instance).to have_received(:refresh_token)
+ it 'returns :error' do
+ expect(subject).to eq :error
+ expect(instance).to have_received(:refresh_token)
+ end
end
end
+
+ context 'when Uanthorized is returned' do
+ it_behaves_like 'refresh', 401
+ end
+
+ context 'when Forbidded is returned' do
+ it_behaves_like 'refresh', 403
+ end
end
end