<commit_before>require File.dirname(__FILE__) + '/../spec_helper'

describe UsersController do

  route_matches("/account/settings", :get, :controller => "users", :action => "edit", :sub_action => 'settings')
  route_matches("/account/profile", :get, :controller => "users", :action => "edit", :sub_action => 'profile')
  route_matches("/account/profile", :get, :controller => "users", :action => "edit", :sub_action => 'profile')
  route_matches("/account/notifications", :get, :controller => "users", :action => "edit", :sub_action => 'notifications')
      
  it 'allows signup' do
    lambda do
      do_create
      response.should redirect_to(root_path)
      flash[:success].should_not be_blank
    end.should change(User, :count).by(1)
  end
  
  it 'requires email on signup' do
    lambda do
      do_create(:email => nil)
      assigns[:user].errors.on(:email).should_not be_nil
    end.should_not change(User, :count)
  end

  describe "on POST to create with bad params" do
    before do
      post :create, :user => {}
    end

    it "should render the new template" do
      response.should render_template('users/new.haml')
    end

    it "should have errors on the user" do
      assigns[:user].should_not be_nil
    end

    it "should render the new user view" do
      response.should render_template('users/new')
    end
  end
  
  describe "#show" do
    integrate_views
    
    before do
      @first_project = make_a_typical_project
      @first_user = @user
      @another_first_user = Factory(:confirmed_user, :first_name => 'Frank', :last_name => 'Sinatra')
      @first_project.add_user(@another_first_user)

      @second_project = make_a_typical_project
      @second_user = @user
    end
    
    it "should not show unknown users" do
      login_as @first_user
      get :show, :id => @second_user.id
      response.should_not render_template('users/show')
      response.status.should == '302 Found'
    end
    
    it "should show known users" do
      login_as @first_user
      get :show, :id => @first_project.user.id
      response.should render_template('users/show')
    end
    
    it "should show the selected user in the title and not the logged in user" do
      login_as @first_user
      get :show, :id => @another_first_user.id
      response.should have_tag 'title', /#{@another_first_user.name}/
      response.should_not have_tag 'title', /#{@first_user.name}/
    end
  end

    
  def do_create(options = {})
    post :create, :user => { :email       => '<EMAIL>',
                             :login       => 'testing',
                             :first_name  => 'Andrew',
                             :last_name   => 'Wiggin',
                             :password    => '<PASSWORD>',
                             :password_confirmation => '<PASSWORD>'}.merge(options)<commit_msg>add a test to make sure that users#show shows projects in common with a user and not those that you are part of but they are not<commit_after>require File.dirname(__FILE__) + '/../spec_helper'

describe UsersController do

  route_matches("/account/settings", :get, :controller => "users", :action => "edit", :sub_action => 'settings')
  route_matches("/account/profile", :get, :controller => "users", :action => "edit", :sub_action => 'profile')
  route_matches("/account/profile", :get, :controller => "users", :action => "edit", :sub_action => 'profile')
  route_matches("/account/notifications", :get, :controller => "users", :action => "edit", :sub_action => 'notifications')
      
  it 'allows signup' do
    lambda do
      do_create
      response.should redirect_to(root_path)
      flash[:success].should_not be_blank
    end.should change(User, :count).by(1)
  end
  
  it 'requires email on signup' do
    lambda do
      do_create(:email => nil)
      assigns[:user].errors.on(:email).should_not be_nil
    end.should_not change(User, :count)
  end

  describe "on POST to create with bad params" do
    before do
      post :create, :user => {}
    end

    it "should render the new template" do
      response.should render_template('users/new.haml')
    end

    it "should have errors on the user" do
      assigns[:user].should_not be_nil
    end

    it "should render the new user view" do
      response.should render_template('users/new')
    end
  end
  
  describe "#show" do
    integrate_views
    
    before do
      @first_project = make_a_typical_project
      @first_user = @user
      @another_first_user = Factory(:confirmed_user, :first_name => 'Frank', :last_name => 'Sinatra')
      @first_project.add_user(@another_first_user)

      @second_project = make_a_typical_project
      @second_user = @user
    end
    
    it "should not show unknown users" do
      login_as @first_user
      get :show, :id => @second_user.id
      response.should_not render_template('users/show')
      response.status.should == '302 Found'
    end
    
    it "should show known users" do
      login_as @first_user
      get :show, :id => @first_project.user.id
      response.should render_template('users/show')
    end
    
    it "should show the selected user in the title and not the logged in user" do
      login_as @first_user
      get :show, :id => @another_first_user.id
      response.should have_tag 'title', /#{@another_first_user.name}/
      response.should_not have_tag 'title', /#{@first_user.name}/
    end
    
    it "should show projects that you share with this user and not projects that you don't" do
      login_as @first_user
      get :show, :id => @another_first_user.id
      response.should have_tag '.project_link a', @first_project.name
      response.should_not have_tag '.project_link a', @second_project.name
    end
  end

    
  def do_create(options = {})
    post :create, :user => { :email       => '<EMAIL>',
                             :login       => 'testing',
                             :first_name  => 'Andrew',
                             :last_name   => 'Wiggin',
                             :password    => '<PASSWORD>',
                             :password_confirmation => '<PASSWORD>'}.merge(options)
{'fraction_non_alphanumeric': 0.11818478615736207, 'fraction_numerical': 0.001305909239307868, 'mean_word_length': 2.660095579450418, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 62, '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': '1548378', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1818, 'n_words': 503}