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

3.6 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"
require "work_package"

RSpec.describe OAuth::ApplicationsController do
  shared_let(:user) { create(:admin) }
  let(:application_stub) { build_stubbed(:oauth_application, id: 1, secret: "foo") }

  before do
    login_as user
  end

  context "not logged as admin" do
    shared_let(:user) { create(:user) }

    it "does not grant access" do
      get :index
      expect(response.response_code).to eq 403

      get :new
      expect(response.response_code).to eq 403

      get :edit, params: { id: 1 }
      expect(response.response_code).to eq 403

      post :create
      expect(response.response_code).to eq 403

      put :update, params: { id: 1 }
      expect(response.response_code).to eq 403

      delete :destroy, params: { id: 1 }
      expect(response.response_code).to eq 403
    end
  end

  describe "#new" do
    it do
      get :new
      expect(response.status).to be 200
      expect(response).to render_template :new
    end
  end

  describe "#edit" do
    before do
      allow(Doorkeeper::Application)
        .to receive(:find)
        .with("1")
        .and_return(application_stub)
    end

    it do
      get :edit, params: { id: 1, application: { name: "foo" } }
      expect(response.status).to be 200
      expect(response).to render_template :edit
    end
  end

  describe "#create" do
    it do
      post :create, params: {
        application: {
          name: "foo",
          redirect_uri: "urn:ietf:wg:oauth:2.0:oob"
        }
      }
      expect(response).to be_redirect
      app = Doorkeeper::Application.last
      expect(app.name).to eq "foo"
    end
  end

  describe "#update" do
    before do
      allow(Doorkeeper::Application)
        .to receive(:find)
        .with("1")
        .and_return(application_stub)
      expect(application_stub).to receive(:attributes=)
      expect(application_stub).to receive(:save).and_return(true)
    end

    it do
      patch :update, params: { id: 1, application: { name: "foo" } }
      expect(response).to redirect_to action: :index
    end
  end

  describe "#destroy" do
    before do
      allow(Doorkeeper::Application)
        .to receive(:find)
        .with("1")
        .and_return(application_stub)
      expect(application_stub).to receive(:destroy).and_return(true)
    end

    it do
      delete :destroy, params: { id: 1 }
      expect(flash[:notice]).to be_present
      expect(response).to redirect_to action: :index
    end
  end
end