spec/bearcat/client/reports_spec.rb in bearcat-1.3.2 vs spec/bearcat/client/reports_spec.rb in bearcat-1.3.3

- old
+ new

@@ -33,6 +33,45 @@ 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 -end \ No newline at end of file + 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