<commit_before>require File.expand_path '../spec_helper.rb', __FILE__

# These examples have dependencies, and need to be run in order

describe "Collector Application" do

  token = '3<PASSWORD>'
  member_id = '384739284793284293'
  HTTP_X_FORWARDED_FOR = '172.16.31.10'
  dataset = nil
  profile_id = nil
  client_id = nil

  before(:all) do
    dataset = Meda::Dataset.new('test', Meda.configuration)
    dataset.token = token
    dataset.google_analytics = {
      'record' => true,
      'tracking_id' => 'UA-666-1',
      'custom_dimensions' => {}
    }
    Meda.datasets[token] = dataset
  end

  after(:all) do
    Meda.datasets[token] = nil
  end

  describe 'index' do
    it 'says hello world' do
      get '/'
      last_response.should be_ok
    end
  end

  describe 'identify.json' do

    it 'identifies a new user' do
      post_data = {'dataset' => token, 'member_id' => member_id}
      post 'identify.json', post_data.to_json, :content_type => 'application/json'
      expect(last_response).to be_ok
      body = JSON.parse(last_response.body)
      expect(body['id']).to be_present
      profile_id = body['id']
    end

  end

  describe 'profile.json' do

    it 'posts profile info' do
      post_data = {'dataset' => token, 'profile_id' => profile_id, 'state' => 'Maine', 'weight' => '200'}
      post 'profile.json', post_data.to_json, :content_type => 'application/json'
      expect(last_response).to be_ok
    end

  end

  describe 'page.json' do

    it 'fail to post page info' do
      post_data = {
        'dataset' => token, 'profile_id' => profile_id,
        'title' => 'foo', 'hostname' => 'http://www.example.com'
      }
      post 'page.json', post_data.to_json, :content_type => 'application/json'
      app.settings.connection.join_threads

      expect(last_response).to be_bad_request
    end

    it 'posts page info' do
      post_data = {
        'dataset' => token, 'profile_id' => profile_id, 'client_id' => client_id,
        'title' => 'foo', 'hostname' => 'http://www.example.com'
      }
      post 'page.json', post_data.to_json, :content_type => 'application/json'
      app.settings.connection.join_threads

      expect(last_response).to be_ok
      expect(dataset.last_hit).to be_present
      expect(dataset.last_disk_hit).to be_present
      path = dataset.last_disk_hit[:path]
      expect(File.read(path)).to match(dataset.last_disk_hit[:data])
      expect(dataset.last_ga_hit).to be_present
    end

  end

  describe 'track.json' do

    it 'fail to post event info' do
      post_data = {
        'dataset' => token, 'client_id' => client_id,
        'category' => 'foo', 'action' => 'testing', 'label' => 'boop!', 'value' => '1'
      }
      post 'track.json', post_data.to_json, :content_type => 'application/json'
      app.settings.connection.join_threads

      expect(last_response).to be_bad_request
    end

    it 'posts event info' do
      post_data = {
        'dataset' => token, 'profile_id' => profile_id, 'client_id' => client_id,
        'category' => 'foo', 'action' => 'testing', 'label' => 'boop!', 'value' => '1'
      }
      post 'track.json', post_data.to_json, :content_type => 'application/json'
      app.settings.connection.join_threads

      expect(last_response).to be_ok
    end

  end
end
<commit_msg>Writing request specs more explicitly, showing requirement for client_id<commit_after>require File.expand_path '../spec_helper.rb', __FILE__

# These examples have dependencies, and need to be run in order

describe "Collector Application" do

  token = '3423432423423423423423'
  member_id = '384739284793284293'
  HTTP_X_FORWARDED_FOR = '172.16.31.10'
  dataset = nil
  profile_id = nil
  client_id = nil

  before(:all) do
    dataset = Meda::Dataset.new('test', Meda.configuration)
    dataset.token = token
    dataset.google_analytics = {
      'record' => true,
      'tracking_id' => 'UA-666-1',
      'custom_dimensions' => {}
    }
    Meda.datasets[token] = dataset
  end

  after(:all) do
    Meda.datasets[token] = nil
  end

  describe 'index' do
    it 'says hello world' do
      get '/'
      last_response.should be_ok
    end
  end

  describe 'identify.json' do

    it 'identifies a new user' do
      post_data = {'dataset' => token, 'member_id' => member_id}
      post 'identify.json', post_data.to_json, :content_type => 'application/json'
      expect(last_response).to be_ok
      body = JSON.parse(last_response.body)
      expect(body['id']).to be_present
      profile_id = body['id']
    end

  end

  describe 'profile.json' do

    it 'posts profile info' do
      post_data = {'dataset' => token, 'profile_id' => profile_id, 'state' => 'Maine', 'weight' => '200'}
      post 'profile.json', post_data.to_json, :content_type => 'application/json'
      expect(last_response).to be_ok
    end

  end

  describe 'page.json' do

    context 'with missing client_id' do
      it 'responds with bad request' do
        post_data = {
          'dataset' => token, 'profile_id' => profile_id,
          'title' => 'foo', 'hostname' => 'http://www.example.com'
        }
        post 'page.json', post_data.to_json, :content_type => 'application/json'
        app.settings.connection.join_threads

        expect(last_response).to be_bad_request
      end
    end

    context 'with dataset, profile_id, client_id, and page params' do
      it 'records the pageview' do
        post_data = {
          'dataset' => token, 'profile_id' => profile_id, 'client_id' => client_id,
          'title' => 'foo', 'hostname' => 'http://www.example.com'
        }
        post 'page.json', post_data.to_json, :content_type => 'application/json'
        app.settings.connection.join_threads

        expect(last_response).to be_ok
        expect(dataset.last_hit).to be_present
        expect(dataset.last_disk_hit).to be_present
        path = dataset.last_disk_hit[:path]
        expect(File.read(path)).to match(dataset.last_disk_hit[:data])
        expect(dataset.last_ga_hit).to be_present
      end
    end
  end

  describe 'track.json' do

    context 'with missing client_id' do
      it 'responds with bad request' do
        post_data = {
          'dataset' => token, 'client_id' => client_id,
          'category' => 'foo', 'action' => 'testing', 'label' => 'boop!', 'value' => '1'
        }
        post 'track.json', post_data.to_json, :content_type => 'application/json'
        app.settings.connection.join_threads

        expect(last_response).to be_bad_request
      end
    end

    context 'with dataset, profile_id, client_id, and event params' do
      it 'records the event' do
        post_data = {
          'dataset' => token, 'profile_id' => profile_id, 'client_id' => client_id,
          'category' => 'foo', 'action' => 'testing', 'label' => 'boop!', 'value' => '1'
        }
        post 'track.json', post_data.to_json, :content_type => 'application/json'
        app.settings.connection.join_threads

        expect(last_response).to be_ok
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.1311965811965812, 'fraction_numerical': 0.013532763532763533, 'mean_word_length': 2.842911877394636, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 2, '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': '1974574', 'n_tokens_mistral': 2379, 'n_tokens_neox': 2292, 'n_words': 532}