<commit_before>require 'helper'

class TestGoogleCalendar < Minitest::Test
  include Google

  context "When connected" do

    setup do
      @client_mock = mock('Faraday::Response')
      @client_mock.stubs(:body).returns(get_mock_body('successful_login.json'))
      @client_mock.stubs(:finish).returns('')
      @client_mock.stubs(:status).returns(200)
      Faraday::Response.stubs(:new).returns(@client_mock)

      @client_id = "671053090364-ntifn8rauvhib9h3vnsegi6dhfglk9ue.apps.googleusercontent.com"
      @client_secret = "<KEY>"
      @refresh_token = "<KEY>"
      @calendar_id = "<EMAIL>"
      @access_token = '<KEY>'

      @calendar = Calendar.new(:client_id => @client_id, :client_secret => @client_secret, :redirect_url => "urn:ietf:wg:oauth:2.0:oob", :refresh_token => @refresh_token, :calendar => @calendar_id)

    end

    context "a calendar" do

      should "generate auth url" do
        assert_equal @calendar.authorize_url.to_s, 'https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=671053090364-ntifn8rauvhib9h3vnsegi6dhfglk9ue.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/calendar'
      end

      should "login with auth code" do
        @client_mock.stubs(:body).returns( get_mock_body("login_with_auth_code_success.json") )
        @calendar.login_with_auth_code('<KEY>')
        assert_equal @calendar.auth_code, nil # the auth_code is discarded after it is used.
        assert_equal @calendar.access_token, @access_token
        assert_equal @calendar.refresh_token, '<PASSWORD>'
      end

      should "login with refresh token" do
        # no refresh_token specified
        cal = Calendar.new(:client_id => @client_id, :client_secret => @client_secret, :redirect_url => "urn:ietf:wg:oauth:2.0:oob", :calendar => @calendar_id)
        @client_mock.stubs(:body).returns( get_mock_body("login_with_refresh_token_success.json") )
        cal.login_with_refresh_token(@refresh_token)
        assert_equal @calendar.access_token, @access_token
      end

      should "catch login with invalid credentials" do
        @client_mock.stubs(:status).returns(403)
        @client_mock.stubs(:body).returns( get_mock_body("403.json") )
        assert_raises(HTTPAuthorizationFailed) do
          Calendar.new(:client_id => 'abadid', :client_secret => 'abadsecret', :redirect_url => "urn:ietf:wg:oauth:2.0:oob", :refresh_token => @refresh_token, :calendar => @calendar_id)
        end
      end

      should "catch login with missing credentials" do
        assert_raises(ArgumentError) do
        @client_mock.stubs(:status).returns(401)
        @client_mock.stubs(:body).returns( get_mock_body("401.json") )
          Calendar.new()
        end
      end

    end # login context

    context "and logged in" do
      setup do
        @calendar = Calendar.new(:client_id => @client_id, :client_secret => @client_secret, :redirect_url => "urn:ietf:wg:oauth:2.0:oob", :refresh_token => @refresh_token, :calendar => @calendar_id)
      end

      should "find all events" do
        @client_mock.stubs(:body).returns( get_mock_body("events.json") )
        assert_equal @calendar.events.length, 3
      end

      should "query events" do
        @client_mock.stubs(:body).returns( get_mock_body("query_events.json") )
        event = @calendar.find_events('Test&gsessionid=12345')
        assert_equal event[0].title, 'Test Event'
      end

      should "find events in range" do
        start_min = Time.new(2011, 2, 1, 11, 1, 1).utc
        start_max = Time.new(2011, 2, 28, 23, 59, 59).utc
        @calendar.expects(:event_lookup).with('?timeMin=2011-02-01T19:01:01Z&timeMax=2011-03-01T07:59:59Z&orderBy=startTime&maxResults=25&singleEvents=true')
        events = @calendar.find_events_in_range(start_min, start_max)
      end

      should "find future events" do
        now = Time.now.utc
        Time.stubs(:now).returns(now)
        formatted_time = now.strftime("%FT%TZ")
        @calendar.expects(:event_lookup).with("?timeMin=#{formatted_time}&orderBy=startTime&maxResults=25&singleEvents=true")
        events = @calendar.find_future_events()
      end

      should "return multiple events in range as array" do<commit_msg>Fix broken test<commit_after>require 'helper'

class TestGoogleCalendar < Minitest::Test
  include Google

  context "When connected" do

    setup do
      @client_mock = mock('Faraday::Response')
      @client_mock.stubs(:body).returns(get_mock_body('successful_login.json'))
      @client_mock.stubs(:finish).returns('')
      @client_mock.stubs(:status).returns(200)
      Faraday::Response.stubs(:new).returns(@client_mock)

      @client_id = "671053090364-ntifn8rauvhib9h3vnsegi6dhfglk9ue.apps.googleusercontent.com"
      @client_secret = "<KEY>"
      @refresh_token = "<KEY>"
      @calendar_id = "<EMAIL>"
      @access_token = '<KEY>'

      @calendar = Calendar.new(:client_id => @client_id, :client_secret => @client_secret, :redirect_url => "urn:ietf:wg:oauth:2.0:oob", :refresh_token => @refresh_token, :calendar => @calendar_id)

    end

    context "a calendar" do

      should "generate auth url" do
        assert_equal @calendar.authorize_url.to_s, 'https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=671053090364-ntifn8rauvhib9h3vnsegi6dhfglk9ue.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/calendar'
      end

      should "login with auth code" do
        @client_mock.stubs(:body).returns( get_mock_body("login_with_auth_code_success.json") )
        @calendar.login_with_auth_code('<KEY>')
        assert_equal @calendar.auth_code, nil # the auth_code is discarded after it is used.
        assert_equal @calendar.access_token, @access_token
        assert_equal @calendar.refresh_token, '<PASSWORD>JU<PASSWORD>zc4fUMX89BMMLeAfKcYteBKRMpQvf4fQFX0'
      end

      should "login with refresh token" do
        # no refresh_token specified
        cal = Calendar.new(:client_id => @client_id, :client_secret => @client_secret, :redirect_url => "urn:ietf:wg:oauth:2.0:oob", :calendar => @calendar_id)
        @client_mock.stubs(:body).returns( get_mock_body("login_with_refresh_token_success.json") )
        cal.login_with_refresh_token(@refresh_token)
        assert_equal @calendar.access_token, @access_token
      end

      should "catch login with invalid credentials" do
        @client_mock.stubs(:status).returns(403)
        @client_mock.stubs(:body).returns( get_mock_body("403.json") )
        assert_raises(HTTPAuthorizationFailed) do
          Calendar.new(:client_id => 'abadid', :client_secret => 'abadsecret', :redirect_url => "urn:ietf:wg:oauth:2.0:oob", :refresh_token => @refresh_token, :calendar => @calendar_id)
        end
      end

      should "catch login with missing credentials" do
        assert_raises(ArgumentError) do
        @client_mock.stubs(:status).returns(401)
        @client_mock.stubs(:body).returns( get_mock_body("401.json") )
          Calendar.new()
        end
      end

    end # login context

    context "and logged in" do
      setup do
        @calendar = Calendar.new(:client_id => @client_id, :client_secret => @client_secret, :redirect_url => "urn:ietf:wg:oauth:2.0:oob", :refresh_token => @refresh_token, :calendar => @calendar_id)
      end

      should "find all events" do
        @client_mock.stubs(:body).returns( get_mock_body("events.json") )
        assert_equal @calendar.events.length, 3
      end

      should "query events" do
        @client_mock.stubs(:body).returns( get_mock_body("query_events.json") )
        event = @calendar.find_events('Test&gsessionid=12345')
        assert_equal event[0].title, 'Test Event'
      end

      should "find events in range" do
        now = Time.now.utc
        Time.stubs(:now).returns(now)
        start_min = now
        start_max = (now + 60*60*24)
        @calendar.expects(:event_lookup).with("?timeMin=#{start_min.strftime("%FT%TZ")}&timeMax=#{start_max.strftime("%FT%TZ")}&orderBy=startTime&maxResults=25&singleEvents=true")
        events = @calendar.find_events_in_range(start_min, start_max)
      end

      should "find future events" do
        now = Time.now.utc
        Time.stubs(:now).returns(now)
        formatted_time = now.strftime("%FT%TZ")
        @calendar.expects(:event_lookup).with("?timeMin=#{formatted_time}&orderBy=startTime&maxResults=25&singleEvents=true")
        events = @calendar.find_future_events()
      end

      should "return multiple events in range as array" do
{'fraction_non_alphanumeric': 0.12801764159702878, 'fraction_numerical': 0.023676880222841225, 'mean_word_length': 3.7320153761669412, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 54, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '3839868', 'n_tokens_mistral': 2967, 'n_tokens_neox': 2793, 'n_words': 473}