require 'helper' describe Bearcat::Client::Reports do before do @client = Bearcat::Client.new(prefix: 'http://canvas.instructure.com', token: 'test_token') end it 'lists the reports availible' do stub_get(@client, '/api/v1/accounts/1/reports').to_return(json_response('report_list.json')) reports = @client.report_list(1) reports.count.should == 38 end it 'starts a report' do stub_post(@client, '/api/v1/accounts/1/reports/last_user_access_csv').to_return(json_response('start_report.json')) status = @client.start_report(1, 'last_user_access_csv') status['report'].should == 'last_user_access_csv' end it 'lists the report history' do stub_get(@client, '/api/v1/accounts/1/reports/last_user_access_csv').to_return(json_response('report_history.json')) history = @client.report_history(1, 'last_user_access_csv') history.count == 2 end it 'gets the status of a report' do stub_get(@client, '/api/v1/accounts/1/reports/last_user_access_csv/72').to_return(json_response('report_status.json')) status = @client.report_status(1, 'last_user_access_csv', 72) status['status'] == 'complete' end it 'deletes a report' do stub_delete(@client, '/api/v1/accounts/1/reports/last_user_access_csv/72').to_return(json_response('report_status.json')) status = @client.delete_report(1, 'last_user_access_csv', 72) status['report'] == 'last_user_access_csv' end context '#download_report' do it 'downloads a report to memory' do report = IO.read(fixture('file.csv')) stub_request(:head, 'https://canvas.instructure.com/files/1/download?download_frd=1&verifier=string').to_return(status: 200, body: report) stub_request(:get, 'https://canvas.instructure.com/files/1/download?download_frd=1&verifier=string').to_return(status: 200, body: report) csv = @client.download_report('https://canvas.instructure.com/files/1/download?download_frd=1&verifier=string') expect(csv.count).to eq 4 expect(csv.first.fields).to eq ['132',nil,'Alg202CR','Algebra 2 Semester 2 Credit Recovery','11',nil,'1',nil,'unpublished',nil,nil,'false'] end it 'downloads a report to disk' do report = IO.read(fixture('file.csv')) tempfile = Tempfile.new('report') stub_request(:head, 'https://canvas.instructure.com/files/1/download?download_frd=1&verifier=string').to_return(status: 200, body: report) stub_request(:get,'https://canvas.instructure.com/files/1/download?download_frd=1&verifier=string').to_return(status: 200, body: report) @client.download_report('https://canvas.instructure.com/files/1/download?download_frd=1&verifier=string', tempfile) csv = CSV.read(tempfile, headers: true) expect(csv.count).to eq 4 expect(csv.first.fields).to eq ['132',nil,'Alg202CR','Algebra 2 Semester 2 Credit Recovery','11',nil,'1',nil,'unpublished',nil,nil,'false'] end it 'follows redirects' do report = IO.read(fixture('file.csv')) tempfile = Tempfile.new('report') stub_request(:head, 'https://canvas.instructure.com/files/1/download?download_frd=1&verifier=string') .to_return(status: 302, body: 'You are being redirected',headers: {'Location' => 'https://cluster1-files.instructure.com/files/1/download?download_frd=1&sf_verifier=verifier=1478139862&user_id=1&verifier=verifier'}) stub_request(:head, 'https://cluster1-files.instructure.com/files/1/download?download_frd=1&sf_verifier=verifier=1478139862&user_id=1&verifier=verifier') .to_return(status: 302, body: 'You are being redirected', headers: {'Location' => 'https://instructure-uploads.s3.amazonaws.com/account_1/attachments/1/assignment_submission.csv'}) stub_request(:head, 'https://instructure-uploads.s3.amazonaws.com/account_1/attachments/1/assignment_submission.csv') .to_return(status: 200) stub_request(:get, 'https://instructure-uploads.s3.amazonaws.com/account_1/attachments/1/assignment_submission.csv') .to_return(status: 200, body: report) @client.download_report('https://canvas.instructure.com/files/1/download?download_frd=1&verifier=string', tempfile) csv = CSV.read(tempfile, headers: true) expect(csv.count).to eq 4 expect(csv.first.fields).to eq ['132',nil,'Alg202CR','Algebra 2 Semester 2 Credit Recovery','11',nil,'1',nil,'unpublished',nil,nil,'false'] end end end