<commit_before>require 'spec_helper'

describe Api::LogsController do
  include Devise::Test::ControllerHelpers
  let(:user) { FactoryBot.create(:user) }
  let!(:logs) { FactoryBot.create_list(:log, 5, device: user.device) }

  describe '#index' do
    it 'lists last x logs' do
      sign_in user
      get :index
      expect(response.status).to eq(200)
      expect(json.first[:id]).to eq(logs.first.id)
      expect(json.first[:created_at]).to eq(logs.first.created_at.to_i)
      expect(json.last[:meta][:type]).to eq(logs.last.type)
    end
  end

  describe "#create" do
    it 'creates one log (legacy format)' do
      sign_in user
      before_count = Log.count
      post :create,
           body: { meta: { x: 1, y: 2, z: 3, type: "info" },
                   channels: ["toast"],
                   message: "Hello, world!"
                 }.to_json,
           params: {format: :json}
      expect(response.status).to eq(200)
      expect(Log.count).to be > before_count
      expect(Log.last.message).to eq("Hello, world!")
      expect(Log.last.device).to eq(user.device)
    end

    it 'creates one log' do
      sign_in user
      before_count = Log.count
      body = {
        channels: [ ],
        major_version: 6,
        message: "HELLO",
        minor_version: 4,
        type: "success",
        verbosity: 1,
        x: 0,
        y: 0,
        z: 0,
      }
      post :create, body: body.to_json, params: {format: :json}
      expect(response.status).to eq(200)
      expect(Log.count).to be > before_count
      expect(json[:created_at]).to be_kind_of(Integer)
      body.keys.map do |key|
        actual   = json[key]
        expected = body[key]

        expect(actual).to eq(expected)
      end
    end

    it 'creates one log with only required fields' do
      sign_in user
      before_count = Log.count
      body = { message: "HELLO", type: "info" }
      post :create, body: body.to_json, params: {format: :json}
      expect(response.status).to eq(200)
      expect(Log.count).to be > before_count
      expect(json[:message]).to eq("HELLO")
    end

    it 'disallows blacklisted (sensitive) words in logs' do
      Log.destroy_all
      stub = { meta: { x: 1, y: 2, z: 3, type: "info" },
               channels: ["toast"],
               message: "my password is <PASSWORD>!" }
      sign_in user
      post :create, body: stub.to_json, params: {format: :json}
      expect(json[:log]).to include(Logs::Create::BAD_WORDS)
      expect(response.status).to eq(422)
      expect(Log.count).to eq(0)
    end

    it 'Runs compaction when the logs pile up' do
      LogDispatch.destroy_all
      Log.destroy_all
      100.times { Log.create!(device: user.device) }
      sign_in user
      user.device.update_attributes!(max_log_count: 15)
      get :index, params: {format: :json}
      expect(response.status).to eq(200)
      expect(json.length).to eq(user.device.max_log_count)
    end

    it 'deletes ALL logs' do
      sign_in user
      before = user.device.logs.count
      delete :destroy, params: { id: "all" }
      expect(response.status).to eq(200)
      expect(user.device.reload.logs.count).to be < before
      expect(user.device.logs.count).to eq(0)
    end

    it 'delivers emails for logs marked as `email`' do
      sign_in user
      empty_mail_bag
      before_count = LogDispatch.count
      body         = { meta: { x: 1, y: 2, z: 3, type: "info" },
                       channels: ["email"],
                       message: "Heyoooo" }.to_json
      run_jobs_now do
        post :create, body: body, params: {format: :json}
        after_count = LogDispatch.count
        expect(response.status).to eq(200)
        expect(last_email).to be
        expect(last_email.body.to_s).to include("Heyoooo")
        expect(last_email.to).to include(user.email)
        expect(before_count).to be < after_count
        expect(LogDispatch.where(sent_at: nil).count).to eq(0)
      end
    end

    it 'delivers emails for logs marked as `fatal_email`' do
      message = "KABOOOOMM - SYSTEM ERROR!"
      sign_in user
      empty_mail_bag
      body         = { meta: { x: 1, y: 2, z: 3, type: "info" },
                       channels: ["fatal_email"],
                       message: message }.to_json
      run_jobs_now do
        post :create, body: body, params: {format: :json}
        expect(response.status).to eq(200)
        expect(last_email).to be
        expect(last_email.body.to_s).to include(message)
        expect(last_email.to).to include(user.email)
      end
    end
  end

  describe "#search" do
    EXAMPLES = [
      [1, "success"],
      [1, "busy"],
      [1, "warn"],
      [1, "error"],<commit_msg>[WIP] Works, but tests need updates.<commit_after>require 'spec_helper'

describe Api::LogsController do
  include Devise::Test::ControllerHelpers
  let(:user) { FactoryBot.create(:user) }
  let!(:logs) { FactoryBot.create_list(:log, 5, device: user.device) }

  describe '#index' do
    it 'lists last x logs' do
      sign_in user
      get :index
      expect(response.status).to eq(200)
      expect(json.first[:id]).to eq(logs.first.id)
      expect(json.first[:created_at]).to eq(logs.first.created_at.to_i)
      expect(json.last[:meta][:type]).to eq(logs.last.type)
    end
  end

  describe "#create" do
    it 'creates one log (legacy format)' do
      sign_in user
      before_count = Log.count
      post :create,
           body: { meta: { x: 1, y: 2, z: 3, type: "info" },
                   channels: ["toast"],
                   message: "Hello, world!"
                 }.to_json,
           params: {format: :json}
      expect(response.status).to eq(200)
      expect(Log.count).to be > before_count
      expect(Log.last.message).to eq("Hello, world!")
      expect(Log.last.device).to eq(user.device)
    end

    it 'creates one log' do
      sign_in user
      before_count = Log.count
      body = {
        channels: [ ],
        major_version: 6,
        message: "HELLO",
        minor_version: 4,
        type: "success",
        verbosity: 1,
        x: 0,
        y: 0,
        z: 0,
      }
      post :create, body: body.to_json, params: {format: :json}
      expect(response.status).to eq(200)
      expect(Log.count).to be > before_count
      expect(json[:created_at]).to be_kind_of(Integer)
      body.keys.map do |key|
        actual   = json[key]
        expected = body[key]

        expect(actual).to eq(expected)
      end
    end

    it 'creates one log with only required fields' do
      sign_in user
      before_count = Log.count
      body = { message: "HELLO", type: "info" }
      post :create, body: body.to_json, params: {format: :json}
      expect(response.status).to eq(200)
      expect(Log.count).to be > before_count
      expect(json[:message]).to eq("HELLO")
    end

    it 'disallows blacklisted (sensitive) words in logs' do
      Log.destroy_all
      stub = { meta: { x: 1, y: 2, z: 3, type: "info" },
               channels: ["toast"],
               message: "my password is <PASSWORD>!" }
      sign_in user
      post :create, body: stub.to_json, params: {format: :json}
      expect(json[:log]).to include(Logs::Create::BAD_WORDS)
      expect(response.status).to eq(422)
      expect(Log.count).to eq(0)
    end

    it 'Runs compaction when the logs pile up' do
      LogDispatch.destroy_all
      Log.destroy_all
      100.times { Log.create!(device: user.device) }
      sign_in user
      user.device.update_attributes!(max_log_count: 15)
      get :index, params: {format: :json}
      expect(response.status).to eq(200)
      expect(json.length).to eq(user.device.max_log_count)
    end

    it 'deletes ALL logs' do
      sign_in user
      before = user.device.logs.count
      delete :destroy, params: { id: "all" }
      expect(response.status).to eq(200)
      expect(user.device.reload.logs.count).to be < before
      expect(user.device.logs.count).to eq(0)
    end

    it 'delivers emails for logs marked as `email`' do
      old_value = LogDispatch::WAIT_PERIOD
      const_reassign(LogDispatch, :WAIT_PERIOD, 0)
      sign_in user
      empty_mail_bag
      before_count = LogDispatch.count
      body         = { meta: { x: 1, y: 2, z: 3, type: "info" },
                       channels: ["email"],
                       message: "Heyoooo" }.to_json
      post :create, body: body, params: {format: :json}
      binding.pry
      after_count = LogDispatch.count
      expect(response.status).to eq(200)
      expect(last_email).to be
      expect(last_email.body.to_s).to include("Heyoooo")
      expect(last_email.to).to include(user.email)
      expect(before_count).to be < after_count
      expect(LogDispatch.where(sent_at: nil).count).to eq(0)
      const_reassign(LogDispatch, :WAIT_PERIOD, old_value)
    end

    it 'delivers emails for logs marked as `fatal_email`' do
      message = "KABOOOOMM - SYSTEM ERROR!"
      sign_in user
      empty_mail_bag
      body         = { meta: { x: 1, y: 2, z: 3, type: "info" },
                       channels: ["fatal_email"],
                       message: message }.to_json
      run_jobs_now do
        post :create, body: body, params: {format: :json}
        expect(response.status).to eq(200)
        expect(last_email).to be
        expect(last_email.body.to_s).to include(message)
        expect(last_email.to).to include(user.email)
      end
    end
  end

  describe "#search" do
    EXAMPLES = [
      [1, "success"],
      [1, "busy"],
      [1, "warn"],
      [1, "error"],
{'fraction_non_alphanumeric': 0.12733767598234924, 'fraction_numerical': 0.012292498424038664, 'mean_word_length': 2.2499146466370776, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '6424201', 'n_tokens_mistral': 3195, 'n_tokens_neox': 3065, 'n_words': 768}