require 'rails_helper'

RSpec.describe Dunlop::SourceFilesController do
  routes { Dunlop::Engine.routes }
  before { sign_in user}
  describe 'authorized' do
    let(:user) { create :user, role_names: ['manage-model-source-file/my-source'] }
    describe '#download' do
      it "downloads the model and logs it" do
        model = create 'source_file/my_source'
        get :download, params: {id: model.id}
        response.body.should eq <<-CSV.strip_heredoc
          my_date,my_var,My count,my_num,my_truth
          2015-05-09,abc D,19,4.8,true
          2015-11-09,def G,19,7.8,false
        CSV

        download = Dunlop::FileDownload.last
        download.user.should eq user
        download.file.should eq model
      end
    end

  end

  describe 'unauthorized' do
    let(:user) { create :user, role_names: [] }
    describe '#download' do
      it "403" do
        model = create 'source_file/my_source'
        get :download, params: {id: model.id}
        response.status.should eq 403
      end
    end
  end
end
