|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +require "spec_helper" |
| 32 | +require_module_spec_helper |
| 33 | + |
| 34 | +module Storages |
| 35 | + module Peripherals |
| 36 | + module ConnectionValidators |
| 37 | + module OneDrive |
| 38 | + RSpec.describe AuthenticationValidator, :webmock do |
| 39 | + subject(:validator) { described_class.new(storage) } |
| 40 | + |
| 41 | + context "when using OAuth2" do |
| 42 | + let(:user) { create(:user) } |
| 43 | + let(:storage) { create(:sharepoint_dev_drive_storage, oauth_client_token_user: user) } |
| 44 | + |
| 45 | + before { User.current = user } |
| 46 | + |
| 47 | + it "passes when the user has a token and the request works", vcr: "one_drive/user_query_success" do |
| 48 | + expect(validator.call).to be_success |
| 49 | + end |
| 50 | + |
| 51 | + it "returns a warning when there's no token for the current user" do |
| 52 | + User.current = create(:user) |
| 53 | + result = validator.call |
| 54 | + |
| 55 | + expect(result[:existing_token]).to be_a_warning |
| 56 | + expect(result[:existing_token].message).to eq(I18n.t(i18n_key(:oauth_token_missing))) |
| 57 | + expect(result[:user_bound_request]).to be_skipped |
| 58 | + end |
| 59 | + |
| 60 | + it "returns a failure if the remote call failed" do |
| 61 | + Registry.stub("one_drive.queries.user", ->(_) { ServiceResult.failure(result: :unauthorized) }) |
| 62 | + |
| 63 | + result = validator.call |
| 64 | + expect(result[:user_bound_request]).to be_a_failure |
| 65 | + expect(result[:user_bound_request].message).to eq(I18n.t(i18n_key(:oauth_request_unauthorized))) |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + private |
| 70 | + |
| 71 | + def i18n_key(key) = "storages.health.connection_validation.#{key}" |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | +end |
0 commit comments