<commit_before>require 'test_helper'

describe DocumentsController do
  describe 'GET #index' do
    context 'List documents' do
      let(:user) { FactoryGirl.create :user }
      let(:document) { FactoryGirl.create :document }

      before do
        user.documents << document
        sign_in user
      end

      it "it list document's name" do
        get :index
        assert_response :success
        assert_template :index
      end
    end
  end

  describe 'POST #create' do
    context 'no user signed in' do
      it 'does not creates a new document' do
        post :create, document: { title: 'title' }
        response.status.must_equal 302
      end
    end
  end

  context 'User signed in' do
    let(:user) { FactoryGirl.create :user }
    let(:document) { FactoryGirl.create :document }

    before do
      sign_in user
    end

    describe 'GET #export' do
      it '' do
        exporter = stub
        exporter.expects(:public_send).with('export_organizations')
        Document.expects(:find).with([document.id.to_s]).returns([document])
        CSVExporterService.expects(:new).with([document], 'http://test.host/').
          returns(exporter)
        get :export, ids: document.id.to_s, class: 'organizations'
      end
    end

    describe 'POST #link' do
      context 'url as an argument' do
        it '' do
          link = mock(call: true)
          LinkService.expects(:new).returns(link)

          post :link, document: { link: 'http://example.org/file.txt' }
          response.status.must_equal 302
        end
      end

      context 'wrong arguments' do
        it 'sends an error message' do
          link = mock(call: false)
          LinkService.expects(:new).returns(link)

          post :link, document: { link: 'bad_link' }
          response.status.must_equal 200
        end
      end
    end

    describe 'POST #flag' do
      it 'flags a document' do
        flagger_service = mock
        FlaggerService.expects(:new).with(user, document).returns(flagger_service)
        flagger_service.expects(:call)
        post :flag, id: document.id

        response.status.must_equal 302
        assert_redirected_to documents_path
      end
    end

    describe 'GET #context' do
      it "Retrieve a JSON with the context" do
        get :context, :format => :json, :id => document.id
        status = JSON.parse(@response.body)

        assert_response :success
        assert_equal document.title, status["title"]
        assert_instance_of Array, status["registers"]
      end
    end

    describe 'GET #show' do
      it "Show one document" do
        get :show, id: document.id
        assert_response :success
        assert_template :show
        assert_not_nil assigns(:document)
      end
    end

    describe 'GET #new' do
      it "Import a new document" do
        get :new
        assert_response :success
        assert_template :new
        assert_select 'h3', "Importar documento"
      end
    end

    describe 'GET #download' do
      it "download documents original file" do
        get :download, :id => document.id

        assert_response :success
        assert_equal "empty content", @response.body
      end
    end

    describe 'GET #comb' do
      it "Show comb page" do
        get :comb, :id => document.id

        assert_response :success
      end<commit_msg>[aphrodite] Fix documents tests<commit_after>require 'test_helper'

describe DocumentsController do
  describe 'GET #index' do
    context 'List documents' do
      let(:user) { FactoryGirl.create :user }
      let(:document) { FactoryGirl.create :document }

      before do
        user.documents << document
        sign_in user
      end

      it "it list document's name" do
        get :index
        assert_response :success
        assert_template :index
      end
    end
  end

  describe 'POST #create' do
    context 'no user signed in' do
      it 'does not creates a new document' do
        post :create, document: { title: 'title' }
        response.status.must_equal 302
      end
    end
  end

  context 'User signed in' do
    let(:user) { FactoryGirl.create :user }
    let(:document) { FactoryGirl.create :document }

    before do
      sign_in user
    end

    describe 'GET #export' do
      it '' do
        exporter = stub
        exporter.expects(:public_send).with('export_organizations')
        Document.expects(:find).with([document.id.to_s]).returns([document])
        CSVExporterService.expects(:new).with([document], 'http://test.host/').
          returns(exporter)
        get :export, ids: document.id.to_s, class: 'organizations'
      end
    end

    describe 'POST #link' do
      context 'url as an argument' do
        it '' do
          link = mock(call: true)
          LinkService.expects(:new).returns(link)

          post :link, document: { link: 'http://example.org/file.txt' }
          response.status.must_equal 302
        end
      end

      context 'wrong arguments' do
        it 'sends an error message' do
          link = mock(call: false)
          LinkService.expects(:new).returns(link)

          post :link, document: { link: 'bad_link' }
          response.status.must_equal 200
        end
      end
    end

    describe 'POST #flag' do
      it 'flags a document' do
        flagger_service = mock
        FlaggerService.expects(:new).with(user, document).returns(flagger_service)
        flagger_service.expects(:call)
        post :flag, id: document.id

        response.status.must_equal 302
        assert_redirected_to documents_path
      end
    end

    describe 'GET #context' do
      it "Retrieve a JSON with the context" do
        get :context, :format => :json, :id => document.id
        status = JSON.parse(@response.body)

        assert_response :success
        assert_equal document.title, status["title"]
        assert_instance_of Array, status["people"]
        assert_instance_of Array, status["dates"]
        assert_instance_of Array, status["organizations"]
        assert_instance_of Array, status["places"]
      end
    end

    describe 'GET #show' do
      it "Show one document" do
        get :show, id: document.id
        assert_response :success
        assert_template :show
        assert_not_nil assigns(:document)
      end
    end

    describe 'GET #new' do
      it "Import a new document" do
        get :new
        assert_response :success
        assert_template :new
        assert_select 'h3', "Importar documento"
      end
    end

    describe 'GET #download' do
      it "download documents original file" do
        get :download, :id => document.id

        assert_response :success
        assert_equal "empty content", @response.body
      end
{'fraction_non_alphanumeric': 0.09054415700267618, 'fraction_numerical': 0.003865596193874517, 'mean_word_length': 2.2002854424357756, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2556301', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1872, 'n_words': 574}