Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

9.0 KB
Raw Blame
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Organisations::TicketsController, type: :controller do
  before do
    # Set up routes for this controller
    routes.draw do
      root 'static#index'
      scope path: '/org/:organisation_id', module: :organisations, as: :organisation do
        resources :projects do
          resources :tickets do
            member do
              post :create_summary
            end
            resources :posts
          end
        end
      end
    end
  end

  # Include Rails route helpers
  include Rails.application.routes.url_helpers

  # Test constants and class methods - these don't require authentication
  describe 'class attributes and constants' do
    it 'has setup_sortable with Ticket model' do
      # Verify that sortable columns are set up correctly
      expect(Organisations::TicketsController.sortable_model_class).to eq(Ticket)
    end

    it 'has sortable columns' do
      expect(Organisations::TicketsController.sortable_columns_list).to include('id')
      expect(Organisations::TicketsController.sortable_columns_list).to include('title')
      expect(Organisations::TicketsController.sortable_columns_list).to include('created_at')
      expect(Organisations::TicketsController.sortable_columns_list).to include('updated_at')
    end

    it 'has related sortable columns for joins' do
      related_columns = Organisations::TicketsController.related_sortable_columns
      expect(related_columns).to include('status')
      expect(related_columns).to include('priority')
    end

    it 'handles pagination via URL params and user DB preference' do
      expect(Organisations::TicketsController.instance_methods).to include(:items_per_page)
    end

    it 'has searchable columns defined' do
      expect(controller.class).to respond_to(:searchable_columns)
    end
  end

  # Test authentication without hitting controller actions
  describe 'authentication' do
    before do
      @request.env['devise.mapping'] = Devise.mappings[:user]

      # Mock the BaseController behavior to prevent it from trying to access current_user.organisations
      allow(controller).to receive(:set_organisation).and_return(true)
      allow(controller).to receive(:set_current_membership).and_return(true)
      allow(controller).to receive(:current_user).and_return(nil)
      allow(controller).to receive(:user_signed_in?).and_return(false)
      allow(controller).to receive(:authenticate_user!).and_call_original
    end

    it 'requires authentication' do
      get :index, params: { organisation_id: '123', project_id: '456' }
      expect(response).to be_redirect
    end
  end

  # Test controller private methods with minimal auth mocking
  describe 'private methods' do
    let(:organisation) { create(:organisation) }
    let(:project) do
      ActsAsTenant.with_tenant(organisation) do
        create(:project, organisation: organisation)
      end
    end
    let(:user) { create(:user) }
    let(:ticket) do
      ActsAsTenant.with_tenant(organisation) do
        create(:ticket, project: project)
      end
    end

    before do
      # Set up the mapping for Devise
      @request.env['devise.mapping'] = Devise.mappings[:user]

      # Set tenant
      ActsAsTenant.current_tenant = organisation

      # Mock authentication
      allow(controller).to receive(:current_user).and_return(user)
      allow(controller).to receive(:user_signed_in?).and_return(true)
      allow(controller).to receive(:current_organisation).and_return(organisation)
      allow(controller).to receive(:authenticate_user!).and_return(true)
      # authorize_membership! is defined in the BaseController, not mocked
      allow(controller).to receive(:set_organisation).and_return(true)
      allow(controller).to receive(:set_current_membership).and_return(true)

      # Set organisation instance variables
      controller.instance_variable_set(:@organisation, organisation)
      controller.instance_variable_set(:@current_organisation, organisation)

      # Mock Pundit authorization
      allow(controller).to receive(:authorize).and_return(true)
      allow(controller).to receive(:policy).and_return(double(read?: true, write?: true, destroy?: true, create_summary?: true))

      # Mock project
      allow(controller).to receive(:set_project).and_return(project)
      controller.instance_variable_set(:@project, project)
      allow(controller).to receive(:params).and_return(ActionController::Parameters.new(
                                                         organisation_id: organisation.id,
                                                         project_id: project.id
                                                       ))
    end

    describe '#sort_params' do
      it 'returns sort params hash' do
        # Mock params with sort attributes
        allow(controller).to receive(:params).and_return(ActionController::Parameters.new(
                                                           sort: {
                                                             column: 'title',
                                                             direction: 'asc'
                                                           }
                                                         ))

        # Call the method
        result = controller.sort_params

        # Verify the result includes the sort params
        expect(result[:sort]).to be_present
      end
    end

    describe '#set_ticket' do
      it 'finds the ticket by id' do
        # Mock params with id
        allow(controller).to receive(:params).and_return(ActionController::Parameters.new(
                                                           id: ticket.id,
                                                           organisation_id: organisation.id,
                                                           project_id: project.id
                                                         ))

        # Call the method
        controller.send(:set_ticket)

        # Verify that ticket is assigned
        expect(controller.instance_variable_get(:@ticket)).to eq(ticket)
      end

      it 'redirects if ticket not found' do
        # Mock params with invalid id
        allow(controller).to receive(:params).and_return(ActionController::Parameters.new(
                                                           id: 'invalid-id',
                                                           organisation_id: organisation.id,
                                                           project_id: project.id
                                                         ))

        # Mock redirect response
        allow(controller).to receive(:redirect_to)

        # Call the method - expect ActiveRecord::RecordNotFound to be rescued
        expect { controller.send(:set_ticket) }.not_to raise_error
      end
    end

    describe '#set_tickets' do
      it 'assigns tickets for the project' do
        # Create some tickets
        tickets = []
        ActsAsTenant.with_tenant(organisation) do
          tickets << create(:ticket, project: project)
          tickets << create(:ticket, project: project)
        end

        # Call the method
        controller.send(:set_tickets)

        # Verify that tickets are assigned
        expect(controller.instance_variable_get(:@tickets)).not_to be_nil
      end
    end

    describe '#ticket_params' do
      it 'permits the correct parameters' do
        # Mock params with ticket attributes
        allow(controller).to receive(:params).and_return(ActionController::Parameters.new(
                                                           ticket: {
                                                             title: 'Test Ticket',
                                                             content: 'Test content',
                                                             assignee_ids: [1, 2, 3],
                                                             custom_attributes: { status: 'open', priority: 'high' }
                                                           }
                                                         ))

        # Call the method
        result = controller.send(:ticket_params)

        # Verify permitted parameters
        expect(result.to_h).to include(
          'title' => 'Test Ticket',
          'content' => 'Test content',
          'assignee_ids' => [1, 2, 3]
        )
        expect(result.to_h['custom_attributes']).to include(
          'status' => 'open',
          'priority' => 'high'
        )
      end
    end

    describe '#search_params' do
      it 'permits the search parameter' do
        # Mock params with search parameter
        allow(controller).to receive(:params).and_return(ActionController::Parameters.new(
                                                           search: 'test query'
                                                         ))

        # Call the method
        result = controller.send(:search_params)

        # Verify permitted search parameter
        expect(result[:search]).to eq('test query')
      end
    end

    # Authorization tests removed - these methods have complex redirect logic that is hard to test in isolation
  end
end