Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
j_platform / spec / controllers / j_platform / tickets_controller_spec.rb
Size: Mime:
require 'spec_helper'

describe JPlatform::TicketsController, :type => :controller do
  include_context "controller setup"
  let(:ticket_attr) { { project_ticket_list_id: 1, social_message_id: 1, status_cd: 1, assignee_id: 1, priority: 1 } }

  before(:each) do
    Rails.cache.clear
    stub_get("/api/v1/users/1.json").to_return(body: fixture("user1.json"), headers: headers)
    stub_get("/api/v1/clients/.json").to_return(body: fixture("clients.json"), headers: headers)
    stub_get("/api/v1/projects/1.json?include_social_accounts=true").to_return(body: fixture("project_social_account.json"), headers: headers)
    stub_get("/api/v1/projects/1.json?include_social_accounts=true&include_client=true&include_client_memberships=true&include_project_ticket_lists=true").to_return(body: fixture("project_with_members_and_tickets.json"), headers: headers)
    stub_get("/api/v1/projects/1.json?include_client=true&include_social_accounts=true").to_return(body: fixture("project_with_members_and_tickets.json"), headers: headers)
    stub_get("/api/v1/tickets.json?created_start=2014-09-02&project_id=1").to_return(body: fixture("tickets_with_project_ticket_lists.json"), headers: headers)
    stub_get("/api/v1/tickets.json?&project_id=1").to_return(body: fixture("tickets_with_project_ticket_lists.json"), headers: headers)
    stub_get("/api/v1/project_ticket_lists.json?&project_id=1").to_return(body: fixture("project_ticket_lists.json"), headers: headers)
  end

  describe "GET 'index'" do
    it "gets the current project" do
      Timecop.freeze Date.new(2014,9,9) do
        get :index
        expect(a_get("/api/v1/projects/1.json?include_client=true&include_client_memberships=true&include_project_ticket_lists=true&include_social_accounts=true")).to have_been_made
        expect(a_get("/api/v1/users/.json?")).not_to have_been_made
      end
    end

    it "does not call the users endpoint" do
      Timecop.freeze Date.new(2014,9,9) do
        get :index
        expect(a_get("/api/v1/users/.json?")).not_to have_been_made
      end
    end

    it "renders the index template" do
      Timecop.freeze Date.new(2014,9,9) do
        get :index
        expect(response).to render_template :index
      end
    end

    context "without setting show_all=true" do
      it "renders list of tickets belonging to the project in the last 7 days" do
        Timecop.freeze Date.new(2014,9,9) do
          get :index
          expect(a_get("/api/v1/tickets.json?created_start=2014-09-02&project_id=1")).to have_been_made
          expect(assigns(:tickets).count).to eq(5)
        end
      end
    end

    context "when setting show_all=true" do
      it "renders list of all tickets belonging to the project" do
        Timecop.freeze Date.new(2014,9,9) do
          get :index, show_all: 'true'
          expect(a_get("/api/v1/tickets.json?project_id=1")).to have_been_made
          expect(assigns(:tickets).count).to eq(5)
        end
      end
    end

    context 'when organization has no project' do
      it 'redirects member to user profile page' do
        stub_get("/api/v1/users/me.json").to_return(body: fixture("member_without_project.json"), headers: headers)
        session[:user_id] = 2

        get :index, use_route: 'jp'
        expect(response).to redirect_to me_path
      end
      it 'redirects super admin to profile page' do
        stub_get("/api/v1/users/me.json").to_return(body: fixture("admin_without_project.json"), headers: headers)
        session[:user_id] = 2

        get :index, use_route: 'jp'
        expect(response).to redirect_to me_path
      end
    end
  end

  describe "GET #new'" do
    it "render new page to create new ticket" do
      xhr :get, :new, use_route: :new_ticket_path
      expect(a_get("/api/v1/projects/1.json?include_social_accounts=true&include_client=true&include_client_memberships=true&include_project_ticket_lists=true")).to have_been_made
      expect(assigns(:project_members)).not_to be_nil
      expect(assigns(:project_ticket_lists)).not_to be_nil
      expect(assigns(:priorities)).not_to be_nil
      expect(response).to render_template :new_edit
    end
  end

  describe "POST #create" do
    context "Create new ticket" do
      before(:each) do
        stub_post("/api/v1/tickets.json").to_return(body: fixture("ticket.json"), headers: headers)
      end
      context "with proper inputs" do
        it "creates a new ticket" do
          post :create, use_route: :tickets_path, ticket: ticket_attr.merge(social_message_origin_ids: ['a0b1'])
          expect(a_post("/api/v1/tickets.json")).to have_been_made
          expect(assigns(:ticket).project_ticket_list_id).to eq(1)
        end

        context "creates multiple new tickets" do
          it 'when passing social_message_origin_ids as an array' do
            tickets_attr = { project_ticket_list_id: 1, social_account_id: 1, status: :new, status_cd: 1, assignee_id: 1, priority: 1, social_message_origin_ids: ['one','two'] }

            post :create, use_route: :tickets_path, ticket: tickets_attr
            expect(a_post("/api/v1/tickets.json").with(body: '{"ticket":{"project_ticket_list_id":"1","assignee_id":"1","priority":"1","social_account_id":"1","social_message_origin_ids":["one","two"]}}')).to have_been_made
          end

          it 'when passing social_message_origin_ids as a string' do
            tickets_attr = { project_ticket_list_id: 1, social_account_id: 1, status: :new, status_cd: 1, assignee_id: 1, priority: 1, social_message_origin_ids: 'three,four' }

            post :create, use_route: :tickets_path, ticket: tickets_attr
            expect(a_post("/api/v1/tickets.json").with(body: '{"ticket":{"project_ticket_list_id":"1","assignee_id":"1","priority":"1","social_message_origin_ids":["three","four"],"social_account_id":"1"}}')).to have_been_made
          end
        end
      end

      context "with invaild inputs" do
        it "renders new client page, and does not create the ticket" do
          post :create, use_route: :tickets_path, ticket: { project_ticket_list_id: nil }
          expect(assigns(:project_members)).not_to be_nil
          expect(assigns(:project_ticket_lists)).not_to be_nil
          expect(assigns(:priorities)).not_to be_nil
          expect(response).to render_template :new_edit
          expect(response.status).to eq(422)
        end
      end
    end
  end

  describe "GET 'show" do
    before(:each) do
      stub_get("/api/v1/tickets/1.json?include_external_conversation=true&include_feed_items=true&project_id=1").to_return(body: fixture("ticket.json"), headers: headers)
      stub_get("/api/v1/projects/1.json").to_return(body: fixture("project.json"), headers: headers)
      stub_get("/api/v1/ticket/resolutions.json?client_id=").to_return(body: fixture("ticket_statuses.json"), headers: headers)
    end
    it "renders show page for ticket" do
      get :show, id: 1, use_route: 'jp'
      expect(a_get("/api/v1/tickets/1.json?include_external_conversation=true&include_feed_items=true&project_id=1")).to have_been_made
      expect(a_get("/api/v1/feed_items/.json")).not_to have_been_made
      expect(a_get("/api/v1/ticket/resolutions.json?client_id=")).to have_been_made
      expect(assigns(:post).is_a? JPlatform::Project::Post).to be_truthy
      expect(response).to render_template :show
    end
  end

  describe "GET 'edit'" do
    before(:each) do
      stub_get("/api/v1/tickets/1.json?project_id=1").to_return(body: fixture("ticket.json"), headers: headers)
    end
    it "renders edit page for ticket update" do
      xhr :get, :edit, id: 1, use_route: 'jp'
      expect(a_get("/api/v1/tickets/1.json?project_id=1")).to have_been_made
      expect(a_get("/api/v1/projects/1.json?include_social_accounts=true&include_client=true&include_client_memberships=true&include_project_ticket_lists=true")).to have_been_made
      expect(assigns(:ticket).project_ticket_list_id).to eq(1)
      expect(assigns(:project_members)).not_to be_nil
      expect(assigns(:project_ticket_lists)).not_to be_nil
      expect(assigns(:ticket_statuses)).not_to be_nil
      expect(assigns(:priorities)).not_to be_nil
      expect(response).to render_template :new_edit
    end
  end

  describe "PUT #update" do
    ticket_params = nil
    before(:each)do
      stub_get("/api/v1/tickets/.json?project_id=1").to_return(body: fixture("ticket.json"), headers: headers)
      stub_get("/api/v1/tickets/.json?include_external_conversation=true&include_feed_items=true&project_id=1").to_return(body: fixture("ticket.json"), headers: headers)
      stub_put("/api/v1/tickets/1.json").to_return(body: fixture("ticket.json"), headers: headers)
      ticket_params = { project_ticket_list_id: 1, social_message_id: 1, status: :new, assignee_id: 1, due_on: "03-29-2013", priority: 1 }
    end

    context "with draft_message" do
      it "saves draft message" do
        put :update, use_route: :ticket_path, ticket: { draft_message: "this is a test draft message" }
        expect(a_put("/api/v1/tickets/1.json")).to have_been_made
        expect(assigns(:ticket).draft_message).to eq("this is a test draft message")
      end
      it "removes draft_message" do
        stub_put("/api/v1/tickets/1.json").to_return(body: fixture("ticket2.json"), headers: headers)
        put :update, use_route: :ticket_path, ticket: { draft_message: nil }
        expect(a_put("/api/v1/tickets/1.json")).to have_been_made
        expect(assigns(:ticket).draft_message).to be_nil
      end
    end

    context "with valid attributes" do
      it "updates ticket's detail" do
        put :update, use_route: 'jp', ticket: ticket_params
        expect(a_put("/api/v1/tickets/1.json")).to have_been_made
        expect(assigns(:post).is_a? JPlatform::Project::Post).to be_truthy
        expect(response).to render_template(partial: '_view')
      end
    end

    context "with invalid attributes" do
      before(:each) do
        stub_get("/api/v1/ticket/resolutions.json?client_id=").to_return(body: fixture("ticket_standard_statuses.json"), headers: headers)
        ticket_params[:project_ticket_list_id] = nil
      end
      it "does not update ticket detail" do
        put :update, use_route: 'jp', ticket: ticket_params
        expect(a_put("/api/v1/tickets/1.json")).not_to have_been_made
        expect(response.status).to eq(422)
      end
    end
  end
end