<commit_before>require 'spec_helper'

describe UsersController do
  describe "#send_mail" do
    let(:action) { get :send_mail, :id => 1 }

    it_blocks_unauthenticated_access

    it "renders get_mail template" do
      sign_in :user, double('user')
      response.should render_template(action: 'get_mail')
    end
  end

  describe "#mail_sent" do
    let(:action) { post :mail_sent, id: 'flov', message_body: 'text' }
    let(:current_user) { double('user1',
                                username: 'flov',
                                to_param: 'flov',
                                email: '<EMAIL>') }
    let(:mail_to_user) { double('user2',
                                'attributes=' => '',
                                username: 'Malte',
                                to_param: 'malte',
                                email: '<EMAIL>') }
    let(:user_mailer) { double('UserMailer') }

    it_blocks_unauthenticated_access

    before do
      sign_in :user, current_user
      User.stub(:find).and_return(mail_to_user)
      user_mailer.stub(:deliver){ true }
    end

    it 'sends out mail' do
      UserMailer.should_receive(:mail_to_user)
        .with(current_user, mail_to_user, 'text')
        .and_return(user_mailer)

      action
    end

    it "redirects to the hitchhikers page" do
      user_mailer.stub(deliver: true)
      UserMailer.stub(:mail_to_user).and_return(user_mailer)

      action

      response.should redirect_to(user_path(mail_to_user))
    end
  end

  describe "DELETE destroy" do
    context "if not logged in" do
      it "redirects to log in page" do
        delete :destroy, id: 1
        response.should redirect_to(user_session_path)
      end
    end

    context "if logged in" do
      before do 
        @user = FactoryGirl.create :user
        sign_in :user, @user
      end

      context "signed in user tries to destroy another user" do
        before do
          @another_user = FactoryGirl.create :user
          delete :destroy, id: @another_user.to_param
        end

        it "should not delete another user" do
          flash[:alert].should == 'You are not allowed to do that!'
        end

        it "should redirect to the profile path" do
          response.should redirect_to(user_path(@user))
        end
      end
    end
  end
end<commit_msg>decoupled controller test from model<commit_after>require 'spec_helper'

describe UsersController do
  describe "#send_mail" do
    let(:action) { get :send_mail, :id => 1 }

    it_blocks_unauthenticated_access

    it "renders get_mail template" do
      sign_in :user, double('user')
      response.should render_template(action: 'get_mail')
    end
  end

  describe "#mail_sent" do
    let(:action) { post :mail_sent, id: 'flov', message_body: 'text' }
    let(:current_user) { double('user1',
                                username: 'flov',
                                to_param: 'flov',
                                email: '<EMAIL>') }
    let(:mail_to_user) { double('user2',
                                'attributes=' => '',
                                username: 'Malte',
                                to_param: 'malte',
                                email: '<EMAIL>') }
    let(:user_mailer) { double('UserMailer') }

    it_blocks_unauthenticated_access

    before do
      sign_in :user, current_user
      User.stub(:find).and_return(mail_to_user)
      user_mailer.stub(:deliver){ true }
    end

    it 'sends out mail' do
      UserMailer.should_receive(:mail_to_user)
        .with(current_user, mail_to_user, 'text')
        .and_return(user_mailer)

      action
    end

    it "redirects to the hitchhikers page" do
      user_mailer.stub(deliver: true)
      UserMailer.stub(:mail_to_user).and_return(user_mailer)

      action

      response.should redirect_to(user_path(mail_to_user))
    end
  end

  describe "DELETE destroy" do
    context "if not logged in" do
      it "redirects to log in page" do
        delete :destroy, id: 1
        response.should redirect_to(user_session_path)
      end
    end

    context "if logged in" do
      let(:current_user) { double('user') }
      let(:another_user) { double('another_user', to_param: 'another_user') }

      before do 
        sign_in :user, current_user
      end

      context "signed in user tries to destroy another user" do
        before do
          User.stub(:find).and_return(another_user)
          delete :destroy, id: another_user.to_param
        end

        it "should not delete another user" do
          flash[:alert].should == 'You are not allowed to do that!'
        end

        it "should redirect to the profile path" do
          response.should redirect_to(user_path(current_user))
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09233340296636725, 'fraction_numerical': 0.0016711928138709003, 'mean_word_length': 2.032298923369221, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '4140686', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1404, 'n_words': 370}