<commit_before>require 'spec_helper'

describe MessagesController do
  render_views

  let(:user) { FactoryGirl.create(:user) }
  let(:attr) { { :content => "i like turtles" } }

  describe "access control" do

    it "should deny access to 'create' for non-signed in users" do
      post :create, :message => attr
      response.should redirect_to(signin_path)
    end

    let(:message) { user.messages.create(attr) }
    it "should deny access to 'update' for non-signed in users" do
      post :update, message_id: message.id, format: :jd, remote: true
      response.should redirect_to(signin_path)
    end
  end
  
  describe "POST 'create'" do

    before(:each) do
      test_sign_in(user)
    end
    
    describe "failure" do

      let(:attr) { { :content => "i like turtles" } }
      it "should not create a message" do
        lambda do
          post :create, :message => @attr, format: :js
        end.should_not change(Message, :count)
      end
    end

    describe "success" do

      it "should create a message" do
        lambda do
          post :create, :message => attr, format: :js
        end.should change(Message, :count).by(1)
      end

      let(:cusn) { FactoryGirl.create(:desk, name: "CUS North", abrev: "CUSN", job_type: "td") }
      let(:cuss) { FactoryGirl.create(:desk, name: "CUS South", abrev: "CUSS", job_type: "td") }
      let(:recip_user) { FactoryGirl.create(:user) }
      before do
        recip_user.authenticate_desk(cuss.abrev => 1)
        user.authenticate_desk(cusn.abrev => 1)
        FactoryGirl.create(:recipient, user: user, desk_id: cuss.id)
      end

      it "adds the message sender's desk to the recipient list of all of the message's recipient users" do
        post :create, :message => attr, :format => :js
        recip_user.recipients.size.should == 1
        recip_user.recipients[0].desk_id.should == cusn.id
      end
    end
  end

  describe "POST 'update'" do

    let(:sender) { FactoryGirl.create(:user) }
    let(:message) { sender.messages.create!(attr) }
    let(:cusn) { FactoryGirl.create(:desk, name: "<NAME>", abrev: "CUSN", job_type: "td") }
    before(:each) do
      user.authenticate_desk(cusn.abrev => 1)
      test_sign_in(user)
    end

    it "is successful" do
      post :update, id: message.id, format: :jd, remote: true
      response.should be_success
    end

    it "marks the message read by the current user" do
      post :update, id: message.id, format: :jd, remote: true
      message.reload
      message.read_by.should == [{ user: user.id.to_s, desks: user.desk_names_str }]
    end
  end
end
<commit_msg>update the messages controller spec<commit_after>require 'spec_helper'

describe MessagesController do
  render_views

  let(:user) { FactoryGirl.create(:user) }
  let(:attr) { { :content => "i like turtles" } }

  describe "access control" do

    it "should deny access to 'create' for non-signed in users" do
      post :create, :message => attr
      response.should redirect_to(signin_path)
    end

    let(:message) { user.messages.create(attr) }
    it "should deny access to 'update' for non-signed in users" do
      post :update, message_id: message.id, format: :jd, remote: true
      response.should redirect_to(signin_path)
    end
  end
  
  describe "POST 'create'" do

    before(:each) do
      test_sign_in(user)
    end
    
    describe "failure" do

      let(:attr) { { :content => "i like turtles" } }
      it "should not create a message" do
        lambda do
          post :create, :message => @attr, format: :js
        end.should_not change(Message, :count)
      end
    end

    describe "success" do

      it "should create a message" do
        lambda do
          post :create, :message => attr, format: :js
        end.should change(Message, :count).by(1)
      end

      let(:cusn) { FactoryGirl.create(:desk, name: "CUS North", abrev: "CUSN", job_type: "td") }
      let(:cuss) { FactoryGirl.create(:desk, name: "CUS South", abrev: "CUSS", job_type: "td") }
      let(:recip_user) { FactoryGirl.create(:user) }
      before do
        recip_user.authenticate_desk(cuss.abrev => 1)
        user.authenticate_desk(cusn.abrev => 1)
        FactoryGirl.create(:recipient, user: user, desk_id: cuss.id)
      end

      it "adds the message sender's desk to the recipient list of all of the message's recipient users" do
        post :create, :message => attr, :format => :js
        recip_user.recipients.size.should == 1
        recip_user.recipients[0].desk_id.should == cusn.id
      end
    end
  end

  describe "POST 'update'" do

    let(:sender) { FactoryGirl.create(:user) }
    let(:message) { sender.messages.create!(attr) }
    let(:cusn) { FactoryGirl.create(:desk, name: "CUS North", abrev: "CUSN", job_type: "td") }
    before(:each) do
      user.authenticate_desk(cusn.abrev => 1)
      test_sign_in(user)
    end

    it "is successful" do
      post :update, id: message.id, format: :jd, remote: true
      response.should be_success
    end

    it "marks the message read by the current user" do
      post :update, id: message.id, format: :jd, remote: true
      message.reload
      message.read_by.should == { user.user_name => user.desk_names_str }
    end
  end
end

{'fraction_non_alphanumeric': 0.12094620373903091, 'fraction_numerical': 0.002289202594429607, 'mean_word_length': 2.788294797687861, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '404077', 'n_tokens_mistral': 1759, 'n_tokens_neox': 1678, 'n_words': 491}