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

describe OmniAuth::Strategies::Crowd, :type=>:strategy do
  include OmniAuth::Test::StrategyTestCase
  @session_based = false
  def strategy
    @crowd_server_url ||= 'https://crowd.example.org'
    @application_name ||= 'bogus_app'
    @application_password ||= '<PASSWORD>'
    [OmniAuth::Strategies::Crowd, {:crowd_server_url => @crowd_server_url, 
                                    :application_name => @application_name,
                                    :application_password => <PASSWORD>,
                                    :session_based => @session_based}]
  end

  describe 'GET /auth/crowd' do
    before do
      get '/auth/crowd'
    end

    it 'should show the login form' do
      last_response.should be_ok
    end
  end

  describe 'POST /auth/crowd' do
    before do
      post '/auth/crowd', :username=>'foo', :password=>'<PASSWORD>'
    end

    it 'should redirect to callback' do
      last_response.should be_redirect
      last_response.headers['Location'].should == 'http://example.org/auth/crowd/callback'
    end
  end

  describe 'GET /auth/crowd/callback without any credentials' do
    before do
      get '/auth/crowd/callback'
    end
    it 'should fail' do
      last_response.should be_redirect
      last_response.headers['Location'].should =~ /no_credentials/
    end
  end

  describe 'GET /auth/crowd/callback with credentials can be successful' do
    context "when using authentication endpoint" do
      before do
        stub_request(:post, "https://bogus_app:bogus_app_password@crowd.example.org/rest/usermanagement/latest/authentication?username=foo").
        to_return(:body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'success.xml')))
        stub_request(:get, "https://bogus_app:bogus_app_password@crowd.example.org/rest/usermanagement/latest/user/group/direct?username=foo").
        to_return(:body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'groups.xml')))
        get '/auth/crowd/callback', nil, 'rack.session'=>{'omniauth.crowd'=> {"username"=>"foo", "password"=>"ba"}}
      end
      it 'should call through to the master app' do
        last_response.body.should == 'true'
      end
      it 'should have an auth hash' do
        auth = last_request.env['omniauth.auth']
        auth.should be_kind_of(Hash)
      end
      it 'should have good data' do
        auth = last_request.env['omniauth.auth']['provider'].should == :crowd
        auth = last_request.env['omniauth.auth']['uid'].should == 'foo'
        auth = last_request.env['omniauth.auth']['user_info'].should be_kind_of(Hash)
        auth = last_request.env['omniauth.auth']['user_info']['groups'].sort.should == ["Developers", "jira-users"].sort
      end
    end

    context "when using session endpoint" do
      before do
        @session_based = true
        stub_request(:post, "https://bogus_app:bogus_app_password@crowd.example.org/rest/usermanagement/latest/authentication?username=foo").
        to_return(:body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'success.xml')))
        stub_request(:post, "https://bogus_app:bogus_app_password@crowd.example.org/rest/usermanagement/latest/session").
        to_return(:body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'session.xml')))
        stub_request(:get, "https://bogus_app:bogus_app_password@crowd.example.org/rest/usermanagement/latest/user/group/direct?username=foo").
        to_return(:body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'groups.xml')))
        get '/auth/crowd/callback', nil, 'rack.session'=>{'omniauth.crowd'=> {"username"=>"foo", "password"=>"ba"}}
      end

      after do
        @session_based = false
      end

      it 'should call through to the master app' do
        last_response.body.should == 'true'
      end
      
      it 'should have an auth hash' do
        auth = last_request.env['omniauth.auth']
        auth.should be_kind_of(Hash)
      end
      
      it 'should have good data' do
        auth = last_request.env['omniauth.auth']['provider'].should == :crowd
        auth = last_request.env['omniauth.auth']['uid'].should == 'foo'
        auth = last_request.env['omniauth.auth']['user_info'].should be_kind_of(Hash)
        auth = last_request.env['omniauth.auth']['user_info']['sso_token'].should == '<PASSWORD>'
        auth = last_request.env['omniauth.auth']['user_info']['groups'].sort.should == ["Developers", "jira-users"].sort
      end
    end<commit_msg>Spec was mock session with 200 status code - should be 201<commit_after>require File.dirname(__FILE__) + '/../../spec_helper'

describe OmniAuth::Strategies::Crowd, :type=>:strategy do
  include OmniAuth::Test::StrategyTestCase
  @session_based = false
  def strategy
    @crowd_server_url ||= 'https://crowd.example.org'
    @application_name ||= 'bogus_app'
    @application_password ||= '<PASSWORD>'
    [OmniAuth::Strategies::Crowd, {:crowd_server_url => @crowd_server_url, 
                                    :application_name => @application_name,
                                    :application_password => @application_password,
                                    :session_based => @session_based}]
  end

  describe 'GET /auth/crowd' do
    before do
      get '/auth/crowd'
    end

    it 'should show the login form' do
      last_response.should be_ok
    end
  end

  describe 'POST /auth/crowd' do
    before do
      post '/auth/crowd', :username=>'foo', :password=>'<PASSWORD>'
    end

    it 'should redirect to callback' do
      last_response.should be_redirect
      last_response.headers['Location'].should == 'http://example.org/auth/crowd/callback'
    end
  end

  describe 'GET /auth/crowd/callback without any credentials' do
    before do
      get '/auth/crowd/callback'
    end
    it 'should fail' do
      last_response.should be_redirect
      last_response.headers['Location'].should =~ /no_credentials/
    end
  end

  describe 'GET /auth/crowd/callback with credentials can be successful' do
    context "when using authentication endpoint" do
      before do
        stub_request(:post, "https://bogus_app:bogus_app_password@crowd.example.org/rest/usermanagement/latest/authentication?username=foo").
        to_return(:body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'success.xml')))
        stub_request(:get, "https://bogus_app:bogus_app_password@crowd.example.org/rest/usermanagement/latest/user/group/direct?username=foo").
        to_return(:body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'groups.xml')))
        get '/auth/crowd/callback', nil, 'rack.session'=>{'omniauth.crowd'=> {"username"=>"foo", "password"=>"ba"}}
      end
      it 'should call through to the master app' do
        last_response.body.should == 'true'
      end
      it 'should have an auth hash' do
        auth = last_request.env['omniauth.auth']
        auth.should be_kind_of(Hash)
      end
      it 'should have good data' do
        auth = last_request.env['omniauth.auth']['provider'].should == :crowd
        auth = last_request.env['omniauth.auth']['uid'].should == 'foo'
        auth = last_request.env['omniauth.auth']['user_info'].should be_kind_of(Hash)
        auth = last_request.env['omniauth.auth']['user_info']['groups'].sort.should == ["Developers", "jira-users"].sort
      end
    end

    context "when using session endpoint" do
      before do
        @session_based = true
        stub_request(:post, "https://bogus_app:bogus_app_password@crowd.example.org/rest/usermanagement/latest/authentication?username=foo").
        to_return(:body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'success.xml')))
        stub_request(:post, "https://bogus_app:bo<EMAIL>/rest/usermanagement/latest/session").
        to_return(:status => 201, :body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'session.xml')))
        stub_request(:get, "https://bogus_app:bogus_app_password@crowd.example.org/rest/usermanagement/latest/user/group/direct?username=foo").
        to_return(:body => File.read(File.join(File.dirname(__FILE__), '..', '..', 'fixtures', 'groups.xml')))
        get '/auth/crowd/callback', nil, 'rack.session'=>{'omniauth.crowd'=> {"username"=>"foo", "password"=>"ba"}}
      end

      after do
        @session_based = false
      end

      it 'should call through to the master app' do
        last_response.body.should == 'true'
      end
      
      it 'should have an auth hash' do
        auth = last_request.env['omniauth.auth']
        auth.should be_kind_of(Hash)
      end
      
      it 'should have good data' do
        auth = last_request.env['omniauth.auth']['provider'].should == :crowd
        auth = last_request.env['omniauth.auth']['uid'].should == 'foo'
        auth = last_request.env['omniauth.auth']['user_info'].should be_kind_of(Hash)
        auth = last_request.env['omniauth.auth']['user_info']['sso_token'].should == '<PASSWORD>'
        auth = last_request.env['omniauth.auth']['user_info']['groups'].sort.should == ["Developers", "jira-users"].sort
      end
    end
{'fraction_non_alphanumeric': 0.15367195082497573, 'fraction_numerical': 0.0009705596894208993, 'mean_word_length': 3.1181172291296626, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 52, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 9, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6068413', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2867, 'n_words': 537}