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

16.9 KB
Raw Blame
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

require "spec_helper"

# Concern is included into AccountController and depends on methods available there
RSpec.describe OmniAuthLoginController, :skip_2fa_stage do
  let!(:auth_provider) { create(:oidc_provider_google, slug: "google") }
  let(:omniauth_strategy) { double("Google Strategy", name: "google") } # rubocop:disable RSpec/VerifiedDoubles
  let(:omniauth_hash) do
    OmniAuth::AuthHash.new(
      provider: "google",
      strategy: omniauth_strategy,
      uid: "123545",
      info: { name: "foo",
              email: "[email protected]",
              first_name: "foo",
              last_name: "bar" }
    )
  end

  before do
    request.env["omniauth.auth"] = omniauth_hash
    request.env["omniauth.strategy"] = omniauth_strategy
  end

  after do
    User.current = nil
  end

  describe "GET #omniauth_login", with_settings: { self_registration: Setting::SelfRegistration.automatic } do
    context "with on-the-fly registration" do
      context "when providing all required fields" do
        before do
          request.env["omniauth.origin"] = "https://example.net/some_back_url"
          post :callback, params: { provider: :google }
        end

        it "registers the user on-the-fly" do
          user = User.find_by_login("[email protected]")
          expect(user).to be_an_instance_of(User)
          expect(user.ldap_auth_source_id).to be_nil
          expect(user.current_password).to be_nil
          expect(user.identity_url).to eql("google:123545")
          expect(user.login).to eql("[email protected]")
          expect(user.firstname).to eql("foo")
          expect(user.lastname).to eql("bar")
          expect(user.mail).to eql("[email protected]")
        end

        it "redirects to the first login page with a back_url" do
          expect(response).to redirect_to(home_url(first_time_user: true))
        end
      end

      describe "strategy uid mapping override" do
        let(:omniauth_hash) do
          OmniAuth::AuthHash.new(
            provider: "google",
            strategy: omniauth_strategy,
            uid: "foo",
            info: {
              uid: "internal",
              email: "[email protected]",
              first_name: "what",
              last_name: "theheck"
            }
          )
        end

        it "takes the uid from the mapped attributes" do
          post :callback, params: { provider: :google }

          user = User.find_by_login("[email protected]")
          expect(user).to be_an_instance_of(User)
          expect(user.identity_url).to eq "google:internal"
        end
      end

      describe "strategy attribute mapping override" do
        let(:omniauth_hash) do
          OmniAuth::AuthHash.new(
            provider: "google",
            strategy: omniauth_strategy,
            uid: "foo",
            info: { email: "[email protected]",
                    first_name: "what",
                    last_name: "theheck" },
            extra: { raw_info: {
              real_uid: "[email protected]",
              first_name: "foo",
              last_name: "bar"
            } }
          )
        end

        describe "available" do
          it "merges the strategy mapping" do
            allow(omniauth_strategy).to receive(:omniauth_hash_to_user_attributes) do |auth|
              raw_info = auth[:extra][:raw_info]
              {
                login: raw_info[:real_uid],
                firstname: raw_info[:first_name],
                lastname: raw_info[:last_name]
              }
            end

            post :callback, params: { provider: :google }

            user = User.find_by_login("[email protected]")
            expect(user).to be_an_instance_of(User)
            expect(user.firstname).to eql("foo")
            expect(user.lastname).to eql("bar")

            expect(omniauth_strategy).to have_received(:omniauth_hash_to_user_attributes)
          end
        end

        describe "unavailable" do
          it "keeps the default mapping" do
            post :callback, params: { provider: :google }

            user = User.find_by_login("[email protected]")
            expect(user).to be_an_instance_of(User)
            expect(user.firstname).to eql("what")
            expect(user.lastname).to eql("theheck")
          end
        end
      end

      context "when not providing all required fields" do
        let(:omniauth_hash) do
          OmniAuth::AuthHash.new(
            provider: "google",
            uid: "123545",
            info: { name: "foo", email: "[email protected]" }
            # first_name and last_name not set
          )
        end

        it "renders user form" do
          post :callback, params: { provider: :google }
          expect(response).to render_template :register
          expect(assigns(:user).mail).to eql("[email protected]")
        end
      end

      context "with self-registration disabled",
              with_settings: { self_registration: Setting::SelfRegistration.disabled } do
        let(:omniauth_hash) do
          OmniAuth::AuthHash.new(
            provider: "google",
            uid: "123",
            info: { name: "foo",
                    email: "[email protected]",
                    first_name: "foo",
                    last_name: "bar" }
          )
        end

        before do
          request.env["omniauth.origin"] = "https://example.net/some_back_url"

          post :callback, params: { provider: :google }
        end

        it "shows a notice about the activated account", :aggregate_failures do
          expect(flash[:notice]).to eq(I18n.t("notice_account_registered_and_logged_in"))
          user = User.find_by!(firstname: "foo")

          expect(user.firstname).to eq "foo"
          expect(user.lastname).to eq "bar"
          expect(user.mail).to eq "[email protected]"
          expect(user).to be_active
        end
      end
    end

    describe "login" do
      let(:omniauth_hash) do
        OmniAuth::AuthHash.new(
          provider: "google",
          uid: "123545",
          info: { name: "foo",
                  last_name: "bar",
                  email: "[email protected]" }
        )
      end

      let!(:user) do
        create(:user, force_password_change: false, identity_url: "google:123545")
      end

      context "with an active account" do
        it "signs in the user after successful external authentication" do
          post :callback, params: { provider: :google }

          expect(response).to redirect_to home_path
        end

        it "logs a successful login" do
          post_at = Time.now.utc
          post :callback, params: { provider: :google }

          user.reload
          expect(user.last_login_on.utc.to_i).to be >= post_at.utc.to_i
        end

        context "with a back_url present" do
          before do
            request.env["omniauth.origin"] = "http://test.host/projects"
            post :callback, params: { provider: :google }
          end

          it "redirects to that url", :aggregate_failures do
            expect(response).to redirect_to "/projects"
          end
        end

        context "with a back_url from a login path" do
          before do
            request.env["omniauth.origin"] = "http://test.host/login"
            post :callback, params: { provider: :google }
          end

          it "redirects to the default one" do
            expect(response).to redirect_to home_path
          end
        end

        context "with a partially blank auth_hash" do
          let(:omniauth_hash) do
            OmniAuth::AuthHash.new(
              provider: "google",
              uid: "123545",
              info: { name: "foo",
                      first_name: "",
                      last_name: "newLastname",
                      email: "[email protected]" }
            )
          end

          it "signs in the user after successful external authentication" do
            expect { post :callback, params: { provider: :google } }
                .not_to change { user.reload.firstname }

            expect(response).to redirect_to home_path

            expect(user.lastname).to eq "newLastname"
          end
        end

        describe "authorization" do
          let(:config) do
            Struct.new(:google_name, :global_email).new "foo", "[email protected]"
          end

          before do
            allow(OpenProject::OmniAuth::Authorization).to receive(:after_login!)

            OpenProject::OmniAuth::Authorization.callbacks.clear

            # Let's set up a couple of authorization callbacks to see if the mechanism
            # works as intended.

            OpenProject::OmniAuth::Authorization.authorize_user provider: :google do |dec, auth|
              if auth.info.name == config.google_name
                dec.approve
              else
                dec.reject "Go away #{auth.info.name}!"
              end
            end

            OpenProject::OmniAuth::Authorization.authorize_user do |dec, auth|
              if auth.info.email == config.global_email
                dec.approve
              else
                dec.reject "I only want to see #{config[:global_email]} here."
              end
            end

            # ineffective callback
            OpenProject::OmniAuth::Authorization.authorize_user provider: :foobar do |dec, _|
              dec.reject "Though shalt not pass!"
            end

            # free for all callback
            OpenProject::OmniAuth::Authorization.authorize_user do |dec, _|
              dec.approve
            end
          end

          after do
            OpenProject::OmniAuth::Authorization.callbacks.clear
          end

          it "logs in the user" do
            post :callback, params: { provider: :google }

            expect(response).to redirect_to home_path
            expect(OpenProject::OmniAuth::Authorization)
              .to have_received(:after_login!).with(user, omniauth_hash, any_args)
          end

          context "with wrong email address" do
            before do
              config.global_email = "[email protected]"
            end

            it "is rejected against google" do
              post :callback, params: { provider: :google }

              expect(response).to redirect_to signin_path
              expect(flash[:error]).to eq "I only want to see [email protected] here."

              expect(OpenProject::OmniAuth::Authorization).not_to have_received(:after_login!).with(user, any_args)
            end

            it "is rejected against any other provider too" do
              omniauth_hash.provider = "any other"
              post :callback, params: { provider: :google }

              expect(response).to redirect_to signin_path
              expect(flash[:error]).to eq "I only want to see [email protected] here."

              expect(OpenProject::OmniAuth::Authorization).not_to have_received(:after_login!).with(user, any_args)
            end
          end

          context "with the wrong name" do
            render_views

            before do
              config.google_name = "hans"
            end

            it "is rejected against google" do
              post :callback, params: { provider: :google }

              expect(response).to redirect_to signin_path
              expect(flash[:error]).to eq "Go away foo!"

              expect(OpenProject::OmniAuth::Authorization).not_to have_received(:after_login!).with(user, any_args)
            end

            context "with other provider availabe" do
              let(:other_provider_slug) { "some other" }

              before do
                create(:oidc_provider, slug: other_provider_slug)
              end

              it "is approved against other provider" do
                omniauth_hash.provider = other_provider_slug

                post :callback, params: { provider: :google }

                expect(response).to redirect_to home_url(first_time_user: true)

                # The authorization is successful which results in the registration
                # of a new user in this case because we changed the provider
                # and there isn't a user with that identity URL yet.
                new_user = UserAuthProviderLink.with_identity_url("some other:123545").first.principal
                expect(OpenProject::OmniAuth::Authorization)
                  .to have_received(:after_login!).with(new_user, any_args)
              end

              # ... and to confirm that, here's what happens when the authorization fails
              it "is rejected against any other provider with the wrong email" do
                omniauth_hash.provider = "yet another"
                config.global_email = "[email protected]"

                post :callback, params: { provider: :google }

                expect(response).to redirect_to signin_path
                expect(flash[:error]).to eq "I only want to see [email protected] here."
                expect(OpenProject::OmniAuth::Authorization).not_to have_received(:after_login!).with(user, any_args)
              end
            end
          end
        end
      end

      context "with a registered and not activated accout",
              with_settings: { self_registration: Setting::SelfRegistration.by_email } do
        before do
          user.registered!

          post :callback, params: { provider: :google }
        end

        it "shows a notice about the activated account", :aggregate_failures do
          expect(flash[:notice]).to eq(I18n.t("notice_account_registered_and_logged_in"))
          expect(user.reload).to be_active
        end
      end

      context "with an invited user and self registration disabled",
              with_settings: { self_registration: Setting::SelfRegistration.disabled } do
        before do
          user.invited!

          post :callback, params: { provider: :google }
        end

        it "shows a notice about the activated account", :aggregate_failures do
          expect(flash[:notice]).to eq(I18n.t("notice_account_registered_and_logged_in"))
          expect(user.reload).to be_active
        end
      end

      context "with a locked account",
              with_settings: { brute_force_block_after_failed_logins: 0 } do
        before do
          user.locked!

          post :callback, params: { provider: :google }
        end

        it "shows an error indicating a failed login", :aggregate_failures do
          expect(flash[:error]).to eql(I18n.t(:notice_account_invalid_credentials))
          expect(response).to redirect_to signin_path
        end
      end
    end

    describe "with an invalid auth_hash" do
      let(:omniauth_hash) do
        OmniAuth::AuthHash.new(
          provider: "google",
          # id is deliberately missing here to make the auth_hash invalid
          info: { name: "foo",
                  email: "[email protected]" }
        )
      end

      before do
        post :callback, params: { provider: :google }
      end

      it "responds with an error" do
        expect(flash[:error]).to include "The authentication information returned from the identity provider was invalid."
        expect(response).to redirect_to signin_path
      end

      it "does not sign in the user" do
        expect(controller.send(:current_user)).not_to be_logged
      end

      it "does not set registration information in the session" do
        expect(session[:auth_source_registration]).to be_nil
      end
    end

    describe "Error occurs during authentication" do
      it "display the error" do
        request.env["omniauth.error"] = "invalid_credentials"
        post :failure

        expect(response).to redirect_to signin_path
        expect(flash[:error]).to include "invalid_credentials"
      end

      it "logs a warn message" do
        allow(Rails.logger).to receive(:warn)
        request.env["omniauth.error"] = "invalid_credentials"
        post :failure

        expect(Rails.logger).to have_received(:warn).with(/invalid_credentials/)
      end
    end
  end
end