<commit_before>require 'rack/cerberus'

RSpec.describe Rack::Cerberus do
  
  let(:secret_app) {
    lambda {|env| 
      [200, {'Content-Type'=>'text/plain'}, env['rack.session'].inspect] 
    }
  }

  let(:cerberus_app) {
    Rack::Cerberus.new(secret_app, cerberus_options) do |login,pass| 
      [login,pass]==['<EMAIL>','<PASSWORD>']
    end
  }

  let(:app) {
    Rack::URLMap.new({
      mount_path => Rack::Session::Cookie.new(cerberus_app, {secret: '42'})
    })
  }

  let(:cerberus_options) { {} }
  let(:mount_path) { '/' }
  
  before :each do
    clear_cookies
  end

  context 'No session is set' do
    let(:app) { cerberus_app }
    it 'Raises' do
      expect{ get('/') }.to raise_error(Rack::Cerberus::NoSessionError)
    end
  end
  
  context 'Not logged in' do
    it 'Stops requests' do
      get '/'
      expect(last_response.status).to eq 401
      body = last_response.body
      expect(body.class).to eq String
      expect(body).to match(/name="cerberus_login" value=""/)
      expect(body).to match(/name="cerberus_pass" value=""/)
    end
  end
  
  describe 'Logging in' do

    context 'Login details are incorrect' do
      it 'Stops requests' do
        post('/', {'cerberus_login' => 'fake_login', 'cerberus_pass' => '<PASSWORD>'})
        expect(last_response.status).to eq 401
        expect(last_response.body).to match(/Wrong login or password/)
      end
      it 'Keeps what was entered in the fields' do
        post('/', {'cerberus_login' => 'fake_login', 'cerberus_pass' => '<PASSWORD>'})
        expect(last_response.body).to match(/name="cerberus_login" value="fake_login"/)
        expect(last_response.body).to match(/name="cerberus_pass" value="fake_<PASSWORD>"/)
      end
      it 'Escapes HTML on submitted info' do
        expect(Rack::Utils).to receive(:escape_html).with('<script>bad</script>').twice
        post('/', {'cerberus_login' => '<script>bad</script>', 'cerberus_pass' => '<script>bad</script>'})
      end
    end

    context 'Login details are correct' do
      let(:secret_app) {
        lambda {|env| 
          [200, {'Content-Type'=>'text/plain'}, env['REQUEST_METHOD']] 
        }
      }
      it 'Gives access' do
        get('/', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
        expect(last_response.status).to eq 200
      end
      it 'Calls the final page with the original method' do
        get('/')
        expect(last_response.body).to include('name="_method" value="GET"')
        post('/', {
          'cerberus_login' => '<EMAIL>', 
          'cerberus_pass' => '<PASSWORD>',
          '_method' => 'GET'
        })
        expect(last_response.body).to eq 'GET'
      end
    end

  end
  
  describe 'Already logged in' do
  
    it 'Uses session for persistent login' do
      get('/', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
      get('/')
      expect(last_response.status).to eq 200
      expect(last_response.body).to include('"cerberus_user"=>"<EMAIL>"}')
    end

  end
  
  describe 'Logout' do

    let(:mount_path) { '/admin' }

    it 'Happens via /logout path' do
      get('/admin/', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
      expect(last_response.status).to eq 200
      get('/admin/logout')
      expect(last_response.status).to eq 401
    end

    it 'Never redirects to the logout path' do
      get('/admin/logout', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
      expect(last_response.status).to eq 302
      expect(last_response['Location']).to eq '/admin'
    end

  end
  
  describe 'Options' do

    it 'Does not link CSS by default' do
      get('/')
      expect(last_response.body).not_to match(/<link/)
    end

    context 'CSS option is used' do
      let(:cerberus_options) { {:css_location=>'/main.css'} }
      it 'Links the CSS file' do
        get('/')
        expect(last_response.body).to match(/<link/)
      end
    end

    context 'Session key is different' do
      let(:cerberus_options) { {session_key: 'different_user'} }
      it 'Uses the session key of the options' do
        get('/', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
        get('/')
        expect(last_response.status).to eq 200
        expect(last_response.body).to include('"different_user"=>"<EMAIL>"}')
      end
    end

  end
  
end
<commit_msg>Use `include` instead of `match` in the tests when possible<commit_after>require 'rack/cerberus'

RSpec.describe Rack::Cerberus do
  
  let(:secret_app) {
    lambda {|env| 
      [200, {'Content-Type'=>'text/plain'}, env['rack.session'].inspect] 
    }
  }

  let(:cerberus_app) {
    Rack::Cerberus.new(secret_app, cerberus_options) do |login,pass| 
      [login,pass]==['<EMAIL>','bros']
    end
  }

  let(:app) {
    Rack::URLMap.new({
      mount_path => Rack::Session::Cookie.new(cerberus_app, {secret: '42'})
    })
  }

  let(:cerberus_options) { {} }
  let(:mount_path) { '/' }
  
  before :each do
    clear_cookies
  end

  context 'No session is set' do
    let(:app) { cerberus_app }
    it 'Raises' do
      expect{ get('/') }.to raise_error(Rack::Cerberus::NoSessionError)
    end
  end
  
  context 'Not logged in' do
    it 'Stops requests' do
      get '/'
      expect(last_response.status).to eq 401
      body = last_response.body
      expect(body.class).to eq String
      expect(body).to include('name="cerberus_login" value=""')
      expect(body).to include('name="cerberus_pass" value=""')
    end
  end
  
  describe 'Logging in' do

    context 'Login details are incorrect' do
      it 'Stops requests' do
        post('/', {'cerberus_login' => 'fake_login', 'cerberus_pass' => '<PASSWORD>'})
        expect(last_response.status).to eq 401
        expect(last_response.body).to include('Wrong login or password')
      end
      it 'Keeps what was entered in the fields' do
        post('/', {'cerberus_login' => 'fake_login', 'cerberus_pass' => '<PASSWORD>'})
        expect(last_response.body).to include('name="cerberus_login" value="fake_login"')
        expect(last_response.body).to include('name="cerberus_pass" value="<PASSWORD>"')
      end
      it 'Escapes HTML on submitted info' do
        expect(Rack::Utils).to receive(:escape_html).with('<script>bad</script>').twice
        post('/', {'cerberus_login' => '<script>bad</script>', 'cerberus_pass' => '<script>bad</script>'})
      end
    end

    context 'Login details are correct' do
      let(:secret_app) {
        lambda {|env| 
          [200, {'Content-Type'=>'text/plain'}, env['REQUEST_METHOD']] 
        }
      }
      it 'Gives access' do
        get('/', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
        expect(last_response.status).to eq 200
      end
      it 'Calls the final page with the original method' do
        get('/')
        expect(last_response.body).to include('name="_method" value="GET"')
        post('/', {
          'cerberus_login' => '<EMAIL>', 
          'cerberus_pass' => '<PASSWORD>',
          '_method' => 'GET'
        })
        expect(last_response.body).to eq 'GET'
      end
    end

  end
  
  describe 'Already logged in' do
  
    it 'Uses session for persistent login' do
      get('/', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
      get('/')
      expect(last_response.status).to eq 200
      expect(last_response.body).to include('"cerberus_user"=>"<EMAIL>"}')
    end

  end
  
  describe 'Logout' do

    let(:mount_path) { '/admin' }

    it 'Happens via /logout path' do
      get('/admin/', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
      expect(last_response.status).to eq 200
      get('/admin/logout')
      expect(last_response.status).to eq 401
    end

    it 'Never redirects to the logout path' do
      get('/admin/logout', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
      expect(last_response.status).to eq 302
      expect(last_response['Location']).to eq '/admin'
    end

  end
  
  describe 'Options' do

    it 'Does not link CSS by default' do
      get('/')
      expect(last_response.body).not_to match(/<link/)
    end

    context 'CSS option is used' do
      let(:cerberus_options) { {:css_location=>'/main.css'} }
      it 'Links the CSS file' do
        get('/')
        expect(last_response.body).to match(/<link/)
      end
    end

    context 'Session key is different' do
      let(:cerberus_options) { {session_key: 'different_user'} }
      it 'Uses the session key of the options' do
        get('/', {'cerberus_login' => '<EMAIL>', 'cerberus_pass' => '<PASSWORD>'})
        get('/')
        expect(last_response.status).to eq 200
        expect(last_response.body).to include('"different_user"=>"<EMAIL>"}')
      end
    end

  end
  
end


{'fraction_non_alphanumeric': 0.16129032258064516, 'fraction_numerical': 0.007269422989550204, 'mean_word_length': 2.8100389441800084, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 102, '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': '1652730', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2931, 'n_words': 665}