<commit_before>require 'spec_helper'

describe SearchController, :search => true do
  before (:each) do

    Sunspot.remove_all!(User, Challenge, Message)
    Sunspot.commit
    Sunspot.index(User.all)
    Sunspot.index(Challenge.all)
    Sunspot.index(Message.all)
    Sunspot.commit

    @self   = FactoryGirl.create(:user, :role => 0, :active => true)

    @student1   = FactoryGirl.create(:user, :role => 0, :active => true, :email => "<EMAIL>", :firstname => "student user foo")
    @student2   = FactoryGirl.create(:user, :role => 0, :active => true, :email => "<EMAIL>", :firstname => "student user bar")
    @student3   = FactoryGirl.create(:user, :role => 0, :active => false, :email => "<EMAIL>", :firstname => "inactive user foo")
    @student4   = FactoryGirl.create(:user, :role => 0, :active => true, :email => "<EMAIL>", :firstname => "dummy")
    @supervisor = FactoryGirl.create(:user, :role => 1, :active => true, :email => "<EMAIL>", :firstname => "supervisor user foo")
    @admin      = FactoryGirl.create(:user, :role => 2, :active => true, :email => "<EMAIL>", :firstname => "admin user foo")
    @challenge1 = FactoryGirl.create(:challenge, :title => "Foo visible", :supervisor => @supervisor, :state => "approved")
    @challenge2 = FactoryGirl.create(:challenge, :title => "Foo hidden", :supervisor => @supervisor, :state => "pending")
    @message1   = FactoryGirl.create(:message, :subject => "msg foo 1", :sender_id => @self.id, :receiver_id => @student4.id)
    @message2   = FactoryGirl.create(:message, :subject => "msg foo 2", :sender_id => @student1.id, :receiver_id => @student4.id)
    @message3   = FactoryGirl.create(:message, :subject => "msg bar 1", :sender_id => @self.id, :receiver_id => @student4.id)
    @message4   = FactoryGirl.create(:message, :subject => "msg foo 3", :sender_id => @student4.id, :receiver_id => @self.id)
    
    Sunspot.commit

    sign_in @self
  end

  describe "does not allow outsiders to view pages" do
    it "returns http redirect for query" do
      sign_out @self
      get 'query'
      response.status.should eq(302)
    end
  end

  describe "Search functionality" do
    it "returns visible challenges matching the query" do
      get 'query', {:q => "Foo"}

      response.body.should include(@challenge1.decorate.title)
      response.body.should_not include(@challenge2.decorate.title)
    end

    it "returns users matching the query" do
      get 'query', {:q => "Foo"}

      response.body.should include(@student1.decorate.firstname)
      response.body.should_not include(@student2.decorate.firstname)
      response.body.should_not include(@student3.decorate.firstname)
      response.body.should include(@supervisor.decorate.firstname)
      response.body.should include(@admin.decorate.firstname)
    end

    it "returns visible messages matching the query" do
      get 'query', {:q => "Foo"}

      # You can't see this one, because sent messages can not be seen (yet)
      response.body.should_not include(@message1.decorate.subject)
      response.body.should_not include(@message2.decorate.subject)
      response.body.should_not include(@message3.decorate.subject)
      response.body.should include(@message4.decorate.subject)
    end
  end

  describe "Selective search" do
    it "allows selective searching for challenges" do
      get 'query', {:q => "Foo", :c => "c"}
      response.body.should include(@challenge1.decorate.title)
      response.body.should_not include(@student1.decorate.firstname)
      response.body.should_not include(@message1.decorate.subject)
    end

    it "allows selective searching for users" do
      get 'query', {:q => "Foo", :c => "u"}
      response.body.should include(@student1.decorate.firstname)
      response.body.should_not include(@challenge1.decorate.title)
      response.body.should_not include(@message1.decorate.subject)
    end

    it "allows selective searching for messages" do
      get 'query', {:q => "Foo", :c => "m"}

      response.body.should include(@message4.decorate.subject)
      response.body.should_not include(@student1.decorate.firstname)
      response.body.should_not include(@challenge1.decorate.title)
    end

    it "allows full search" do
      get 'query', {:q => "Foo"}
      response.body.should include(@challenge1.decorate.title)
      response.body.should include(@student1.decorate.firstname)
      response.body.should include(@message4.decorate.subject)
    end
  end
end<commit_msg>More rspec tests All API tests<commit_after>require 'spec_helper'

describe SearchController, :search => true do
  before (:each) do

    Sunspot.remove_all!(User, Challenge, Message)
    Sunspot.commit
    Sunspot.index(User.all)
    Sunspot.index(Challenge.all)
    Sunspot.index(Message.all)
    Sunspot.commit

    @self   = FactoryGirl.create(:user, :role => 0, :active => true)

    @student1   = FactoryGirl.create(:user, :role => 0, :active => true, :email => "<EMAIL>", :firstname => "student user foo")
    @student2   = FactoryGirl.create(:user, :role => 0, :active => true, :email => "<EMAIL>", :firstname => "student user bar")
    @student3   = FactoryGirl.create(:user, :role => 0, :active => false, :email => "<EMAIL>", :firstname => "inactive user foo")
    @student4   = FactoryGirl.create(:user, :role => 0, :active => true, :email => "<EMAIL>", :firstname => "dummy")
    @supervisor = FactoryGirl.create(:user, :role => 1, :active => true, :email => "<EMAIL>", :firstname => "supervisor user foo")
    @admin      = FactoryGirl.create(:user, :role => 2, :active => true, :email => "<EMAIL>", :firstname => "admin user foo")
    @challenge1 = FactoryGirl.create(:challenge, :title => "Foo visible", :supervisor => @supervisor, :state => "approved")
    @challenge2 = FactoryGirl.create(:challenge, :title => "Foo hidden", :supervisor => @supervisor, :state => "pending")
    @message1   = FactoryGirl.create(:message, :subject => "msg foo 1", :sender_id => @self.id, :receiver_id => @student4.id)
    @message2   = FactoryGirl.create(:message, :subject => "msg foo 2", :sender_id => @student1.id, :receiver_id => @student4.id)
    @message3   = FactoryGirl.create(:message, :subject => "msg bar 1", :sender_id => @self.id, :receiver_id => @student4.id)
    @message4   = FactoryGirl.create(:message, :subject => "msg foo 3", :sender_id => @student4.id, :receiver_id => @self.id)
    
    Sunspot.commit

    sign_in @self
  end

  describe "does not allow outsiders to view pages" do
    it "returns http redirect for query" do
      sign_out @self
      get 'query'
      response.status.should eq(302)
    end
  end

  describe "Search functionality" do
    it "returns visible challenges matching the query" do
      get 'query', {:q => "Foo"}

      response.body.should include(@challenge1.decorate.title)
      response.body.should_not include(@challenge2.decorate.title)
    end

    it "returns users matching the query" do
      get 'query', {:q => "Foo"}

      response.body.should include(@student1.decorate.firstname)
      response.body.should_not include(@student2.decorate.firstname)
      response.body.should_not include(@student3.decorate.firstname)
      response.body.should include(@supervisor.decorate.firstname)
      response.body.should include(@admin.decorate.firstname)
    end

    it "returns visible messages matching the query" do
      get 'query', {:q => "Foo"}

      # You can't see this one, because sent messages can not be seen (yet)
      response.body.should_not include(@message1.decorate.subject)
      response.body.should_not include(@message2.decorate.subject)
      response.body.should_not include(@message3.decorate.subject)
      response.body.should include(@message4.decorate.subject)
    end
  end

  describe "Selective search" do
    it "allows selective searching for challenges" do
      get 'query', {:q => "Foo", :c => "c"}
      response.body.should include(@challenge1.decorate.title)
      response.body.should_not include(@student1.decorate.firstname)
      response.body.should_not include(@message1.decorate.subject)
    end

    it "allows selective searching for users" do
      get 'query', {:q => "Foo", :c => "u"}
      response.body.should include(@student1.decorate.firstname)
      response.body.should_not include(@challenge1.decorate.title)
      response.body.should_not include(@message1.decorate.subject)
    end

    it "allows selective searching for messages" do
      get 'query', {:q => "Foo", :c => "m"}

      response.body.should include(@message4.decorate.subject)
      response.body.should_not include(@student1.decorate.firstname)
      response.body.should_not include(@challenge1.decorate.title)
    end

    it "allows full search" do
      get 'query', {:q => "Foo"}
      response.body.should include(@challenge1.decorate.title)
      response.body.should include(@student1.decorate.firstname)
      response.body.should include(@message4.decorate.subject)
    end
  end

  describe "Partial search" do
    it "allows matching start" do
      get 'query', {:q => "Fo"}
      response.body.should include('Foo')
    end

    it 'does not allow matching half-way a word' do
      get 'query', {:q => "oo"}
      response.body.should_not include('Foo')
    end
  end

  describe "Invalid queries" do
    it "does not allow an empty" do
      get 'query', {:q => ""}
      response.body.should == '[]'
    end

    it "does not allow single-character queries" do
      get 'query', {:q => "F"}
      response.body.should == '[]'
    end
  end
end
{'fraction_non_alphanumeric': 0.14314198761674887, 'fraction_numerical': 0.0104942806170637, 'mean_word_length': 3.5994208494208495, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 129, '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': '4999791', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2861, 'n_words': 747}