From 2b120ef8860ec764b9dcd1c98d9caf6acb53dc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 3 Apr 2013 01:17:17 +0200 Subject: [PATCH] add UploadsController specs --- app/controllers/uploads_controller.rb | 3 + spec/controllers/uploads_controller_spec.rb | 70 ++++++++++++++++++++ spec/fixtures/images/logo-dev.png | Bin 0 -> 3266 bytes spec/fixtures/images/logo.png | Bin 0 -> 2290 bytes 4 files changed, 73 insertions(+) create mode 100644 spec/controllers/uploads_controller_spec.rb create mode 100644 spec/fixtures/images/logo-dev.png create mode 100644 spec/fixtures/images/logo.png diff --git a/app/controllers/uploads_controller.rb b/app/controllers/uploads_controller.rb index 1e71187bedd..e3554723690 100644 --- a/app/controllers/uploads_controller.rb +++ b/app/controllers/uploads_controller.rb @@ -1,5 +1,8 @@ class UploadsController < ApplicationController + before_filter :ensure_logged_in + def create + requires_parameter(:topic_id) file = params[:file] || params[:files].first upload = Upload.create_for(current_user, file, params[:topic_id]) render_serialized(upload, UploadSerializer, root: false) diff --git a/spec/controllers/uploads_controller_spec.rb b/spec/controllers/uploads_controller_spec.rb new file mode 100644 index 00000000000..6a1da3fc210 --- /dev/null +++ b/spec/controllers/uploads_controller_spec.rb @@ -0,0 +1,70 @@ +require 'spec_helper' + +describe UploadsController do + + it 'requires you to be logged in' do + -> { xhr :post, :create }.should raise_error(Discourse::NotLoggedIn) + end + + context 'logged in' do + + before do + @user = log_in :user + end + + context '.create' do + + context 'missing params' do + it 'raises an error without the topic_id param' do + -> { xhr :post, :create }.should raise_error(Discourse::InvalidParameters) + end + end + + context 'correct params' do + + let(:logo) do + ActionDispatch::Http::UploadedFile.new({ + filename: 'logo.png', + content_type: 'image/png', + tempfile: File.new("#{Rails.root}/spec/fixtures/images/logo.png") + }) + end + + let(:logo_dev) do + ActionDispatch::Http::UploadedFile.new({ + filename: 'logo-dev.png', + content_type: 'image/png', + tempfile: File.new("#{Rails.root}/spec/fixtures/images/logo-dev.png") + }) + end + + let(:files) { [ logo_dev, logo ] } + + context 'with a file' do + it 'is succesful' do + xhr :post, :create, topic_id: 1234, file: logo + response.should be_success + end + end + + context 'with some files' do + + it 'is succesful' do + xhr :post, :create, topic_id: 1234, files: files + response.should be_success + end + + it 'takes the first file' do + xhr :post, :create, topic_id: 1234, files: files + response.body.should match /logo-dev.png/ + end + + end + + end + + end + + end + +end diff --git a/spec/fixtures/images/logo-dev.png b/spec/fixtures/images/logo-dev.png new file mode 100644 index 0000000000000000000000000000000000000000..1b309fc375521a473c1cea08fdd6ed809c834d3b GIT binary patch literal 3266 zcmb7G*E<^wA5A17b||H0Y*n>qjV5M{+Qh8btB4WQOd<$svAo=R{g@Ug5v8U3_ zD?`o9*h=?G!v7!W>gpO98JU`z+Su5*xVZTE_yh(91_uX6L_{PcB;@Dk*Vor~c6M%V zZeFE6c<|uYuU`ZL;ra9D=H}-9{{9jY5{in7;o;$tk&$t6aq8;owzjs($;nPmPTJbq zuCA^^LPEa2zFS*cUS3{SR#q!3E18*@F)=ZsqM~wga$;g)dU|?>hK7!gjuZ+-N=gcY z!Gwl}wzs#Nn3!~Tci+8x_w(n^V`F2Jlaq6Ea{~hdSS&U=I=ZE$rKzbYDk>@@B*eqR z1Bb(*(dhE>@~Wz;nwlD8W8;SpAKKa3IXF1z=;(BHbp-?j#Ky)-N=oAKcuh^snVFfO zprECtC248tw6ru86_uQvoJXI&+FsQM3$-w@1i;`j2r~>8pHWob^?6}Wh>Ms40MLsX z=pd~k?r+bYP^x8M-Aj0KO15$yII7+uYH#&;{%_2sSZnUeVTn(meBVeFaO?xoSWxzD30&}1UgKs{}ORC5eF3yJh4zh5MMTKDQ zr<<%Lgra&_4KYa`+1*S-(gfZ&$J}YWrd0=CajGH>+Vzd^#tX60_KBP0!hG4m`1;1V zA2*fWT*s#BrU(s~F@QJ1nEP(unXs6qO9t9?H~k0Kj*4)q?GRG_w&mq7t;p3GEMl<=ojcPkIL|7};4W7)Z@ph6dgnz~k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z2juo`!R0U2)|FvN07lO?KLWzsilN*R`4DW3R;kumiB(9+Xt!JtVW;`>dUb(GMON_> zkE8-E6{*blDZE);u*qinx;bNxd-?#9&Fx&Rv|f!HxV+?w-P(gOp&tOv<-y>ZDkdI@ z2lRS_L+-UValQ6wc&vjXW9uIqFxhgV^C4IF()2WypmP~QW?n!x;((5~31#v3rzITZ zOO={8S5%(XB5sW~P*m&+&`h^=bmx zkCM6Dn^|GhKyv(8XFJ3()_NPXJX^~i`RnrC*b4T#yNU|=c;>fvtAYoGHeK+Qg4FtO ztn)$$$ta9^v*GQ^4FP!ZZUm=6&zEc5tCI`vjeFm|UwBQeH}NHX()d9SJB=Y3PIKjA zjRyV#E5{>TGRZOomK?V9L7@&z*{%8Q+~* z`tsz1bVD-}jX(o>?OcaF{Lx-nuxlYFoz|E#LuiYGiB9kQ*)Ep9)q93pDa@WBcPcGj z`!;#p0ePXXWj{uW+w+@}8@@(U64RMn_u^QGs<+@`=4ko*$3IPf&kQ(vn$V01N<8zb zs%%z_+3Xu}^uoyI{U+k#;`*&jrxE8CL6t=~U+397BbV^m+0jp*UZ$nZWo7wHr85tY zi|&<3rW3kRYz)G6woMwxhYjnGL>*mYeSg`tV>VqBD9!P_3^%a6e`KDz^pQsA z`hl$v1gMqTd?H=0H+XrXCQ@FhTFp6;Hatd1)@1FSYRP4j;=iwYdYb-u?%&y`3NlQS zUj2WECVn17zx(^_XKmB~GQCdm6Kgf8&sw!mVs&(Zcel;vM)%^M6&&A0iqG7kj3nc7 z0VLskW-B}^#H(er@D2TCesa)kd>^g-ALpp>ETRkp$z{5$PvFhB6bPCKo(k9!^nyjk z*9Bj4Llf*!y y2#ZoXpTBZE0eM`doI9_*vYCfiJ-7jnzS3XN@>U;T>rlU1(*Ofq6P-FO4Do*&>+`4p literal 0 HcmV?d00001 diff --git a/spec/fixtures/images/logo.png b/spec/fixtures/images/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..c8d7600f7ab60abfc7c00f2f9a336777cb0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