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 / posts_controller_spec.rb
Size: Mime:
require 'spec_helper'

describe JPlatform::PostsController, :type => :controller do
  include_context "controller setup"
  let(:post_attr) {{messages: [
      { message: 'Another Model S fire. http://www.teslamotorsclub.com/showthread.php/23581-Ugh-Another-Model-S-fire-2013-11-06', social_account_id: 4 },
      { message: 'Another Model S fire. http://www.teslamotorsclub.com/showthread.php/23581-Ugh-Another-Model-S-fire-2013-11-06', social_account_id: 5 }
  ]}}

  describe "POST #create" do
    context "Create new post" do
      before(:each) do
        stub_post("/api/v1/posts.json?project_id=4").with(renders_ticket_path: true).to_return(body: fixture("post.json"), headers: {content_type: "application/json; charset=utf-8"})
        stub_post("/api/v1/posts.json?project_id=2").with(renders_ticket_path: true).to_return(status: 422, headers: {content_type: "application/json; charset=utf-8"})
      end
      context "with proper inputs" do
        it "creates a post with proper data using a list of social account ids" do
          post :create, use_route: :project_project_posts_path, project_post: post_attr, project_id: 4, renders_ticket_path: true
          expect(a_post("/api/v1/posts.json?project_id=4")).to have_been_made
          expect(assigns(:post).social_messages.first.message).to eq('Another Model S fire. http://t.co/SoRFv1wxyc')
          expect(response).to render_template "j_platform/ticket/external_conversations/_external_conversation"
        end
        it "creates a post with proper data using a social account id" do
          post_attr[:social_account_ids] = nil
          post_attr[:social_account_id] = 1
          post :create, use_route: :project_project_posts_path, project_post: post_attr, project_id: 4, renders_ticket_path: true
          expect(a_post("/api/v1/posts.json?project_id=4")).to have_been_made
          expect(assigns(:post).social_messages.first.message).to eq('Another Model S fire. http://t.co/SoRFv1wxyc')
          expect(response).to render_template "j_platform/ticket/external_conversations/_external_conversation"
        end
      end

      context "with invaild inputs" do
        before(:each) do
          # Dirty the send data:
          post_attr[:social_account_ids] = nil
          post_attr[:social_account_id] = nil
        end
        context "with :renders_ticket_path set true (e.g. on Queue page)" do
          it "fails to post with in proper data" do
            skip "Zhaohan bug, Possibly missing proper fixture"
            # post :create, use_route: :project_project_posts_path, project_post: post_attr, project_id: 2, renders_ticket_path: false
            # expect(a_post("/api/v1/posts.json?project_id=2")).to have_been_made
            # expect(JSON.parse(response.body)['status']).to eq('failure')
          end
        end
        context "elsewhere" do
          it "fails to post with in proper data" do
            skip "Zhaohan bug, Possibly missing proper fixture"
            # post :create, use_route: :project_project_posts_path, project_post: post_attr, project_id: 2, renders_ticket_path: true
            # expect(a_post("/api/v1/posts.json?project_id=2")).to have_been_made
            # expect(JSON.parse(response.body)['status']).to eq('failure')
          end
        end
      end

      context "scheduling with invaild datetime" do
        it "render a datetime format error" do
          post_time = "24:70 AM"
          post :create, use_route: :project_project_posts_path, project_post: post_attr, post_time: post_time, project_id: 4, renders_ticket_path: true
          expect(a_post("/api/v1/posts.json?project_id=4")).to_not have_been_made
          expect(JSON.parse(response.body)['status']).to eq('failure')
          expect(JSON.parse(response.body)['message'].first).to include(I18n.t("schedule.date_time_format_error"))
        end

        it "render a datetime format error" do
          post_time = "2013-08-06TasdfasdfZ"
          post :create, use_route: :project_project_posts_path, project_post: post_attr, post_time: post_time, project_id: 4, renders_ticket_path: true
          expect(a_post("/api/v1/posts.json?project_id=4")).to_not have_been_made
          expect(JSON.parse(response.body)['status']).to eq('failure')
          expect(JSON.parse(response.body)['message'].first).to include(I18n.t("schedule.date_time_format_error"))
        end
      end

      context "with valid inputs, but server failed to post" do
        it "returns social_messages and errors" do
          stub_post("/api/v1/posts.json?project_id=3").with(renders_ticket_path: true).to_return(body: fixture("post_with_errors.json"), headers: {content_type: "application/json; charset=utf-8"})
          post :create, use_route: :project_project_posts_path, project_post: post_attr, project_id: 3
          expect(a_post("/api/v1/posts.json?project_id=3")).to have_been_made
          expect(assigns(:post).social_messages.first.post_errors).to eq('OAuthException :: (#341) Feed action request limit reached')
          expect(assigns(:post).social_messages.second.message).to eq('this is a test message')
        end
      end

      context "as scheduled item" do
        it "creates a post with proper data using a list of social account ids" do
          post_time = 1.hour.from_now.to_s
          post :create, use_route: :project_project_posts_path, project_post: post_attr, post_time: post_time, project_id: 4
          expect(a_post("/api/v1/posts.json?project_id=4")).to have_been_made
          expect(assigns(:post).messages.first.message).to eq('Another Model S fire. http://www.teslamotorsclub.com/showthread.php/23581-Ugh-Another-Model-S-fire-2013-11-06')
          expect(assigns(:post).scheduled_time).to eq(Time.zone.parse(post_time).utc.to_s)
          json_response = JSON.parse response.body
          expect(json_response["status"]).to eq("success")   # no social messages actually posted yet! (as long it's not status: failure we're good)
        end
      end
    end
  end
end