Remove controllers spec already tested elsewhere
This commit is contained in:
parent
5e1757aff9
commit
896fe661bd
@ -1,45 +0,0 @@
|
||||
describe API::ApplicationController do
|
||||
include UserControllerHelpers
|
||||
|
||||
before { sign_in }
|
||||
|
||||
context 'CORS: Cross-Origin Ressource Sharing' do
|
||||
before { request.headers['Origin'] = 'http://origin.example/' }
|
||||
|
||||
context 'preflight' do
|
||||
controller(API::ApplicationController) do
|
||||
alias_method :index, :cor_preflight
|
||||
end
|
||||
|
||||
def options(action)
|
||||
process action, 'OPTIONS'
|
||||
end
|
||||
|
||||
it 'sets Access-Control-Allow-Methods header' do
|
||||
options :index
|
||||
expect(response.headers['Access-Control-Allow-Methods'])
|
||||
.to eq 'GET, POST, PUT, DELETE'
|
||||
end
|
||||
|
||||
it 'sets Access-Control-Allow-Methods header' do
|
||||
options :index
|
||||
expect(response.headers['Access-Control-Allow-Headers'])
|
||||
.to eq 'Content-Type, Content-Length, X-Requested-With'
|
||||
end
|
||||
end
|
||||
|
||||
describe 'before filter' do
|
||||
controller(API::ApplicationController) do
|
||||
def index
|
||||
head :ok
|
||||
end
|
||||
end
|
||||
|
||||
it 'sets Access-Control-Allow-Origin header' do
|
||||
get :index
|
||||
expect(response.headers['Access-Control-Allow-Origin'])
|
||||
.to eq request.headers['Origin']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,50 +0,0 @@
|
||||
describe API::PlaylistsController do
|
||||
include UserControllerHelpers
|
||||
|
||||
before { sign_in }
|
||||
|
||||
describe 'GET index' do
|
||||
render_views
|
||||
|
||||
before do
|
||||
FactoryGirl.create(:playlist, name: 'Playlist 1')
|
||||
FactoryGirl.create(:playlist, name: 'Playlist 2')
|
||||
end
|
||||
|
||||
def do_get
|
||||
get :index, format: :json
|
||||
JSON response.body
|
||||
end
|
||||
|
||||
it 'lists all playlists' do
|
||||
expect(do_get.size).to eq 2
|
||||
end
|
||||
|
||||
it 'lists playlists with their id' do
|
||||
do_get.each { |t| expect(t.keys).to include 'id' }
|
||||
end
|
||||
|
||||
it 'lists playlists with their name' do
|
||||
do_get.each { |t| expect(t.keys).to include 'name' }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST create' do
|
||||
def do_create
|
||||
post :create,
|
||||
format: :json,
|
||||
playlist: FactoryGirl.attributes_for(:playlist)
|
||||
end
|
||||
|
||||
it 'creates a new playlist for the current user' do
|
||||
expect {
|
||||
do_create
|
||||
}.to change(controller.current_user.playlists, :count).by(1)
|
||||
end
|
||||
|
||||
it 'assigns the playlist' do
|
||||
do_create
|
||||
expect(assigns[:playlist]).to be_a Playlist
|
||||
end
|
||||
end
|
||||
end
|
@ -1,45 +0,0 @@
|
||||
describe API::SessionsController do
|
||||
describe 'POST create' do
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
|
||||
def do_create
|
||||
post :create, format: :json, session: {
|
||||
email: user.email,
|
||||
password: user.password
|
||||
}
|
||||
end
|
||||
|
||||
context 'with valid credentials' do
|
||||
before { do_create }
|
||||
|
||||
it 'signs the user in' do
|
||||
expect(controller.current_user).to eq user
|
||||
end
|
||||
|
||||
it 'assigns the user' do
|
||||
expect(assigns[:user]).to eq user
|
||||
end
|
||||
end
|
||||
|
||||
[:email, :password].each do |attr|
|
||||
context "with invalid credentials (#{attr})" do
|
||||
before do
|
||||
allow(user).to receive(attr).and_return(user.send(attr) + '_INVALID')
|
||||
do_create
|
||||
end
|
||||
|
||||
it 'returns a not found response' do
|
||||
expect(response).to be_not_found
|
||||
end
|
||||
|
||||
it 'returns an empty body' do
|
||||
expect(response.body).to be_empty
|
||||
end
|
||||
|
||||
it 'assigns no user' do
|
||||
expect(assigns[:user]).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,35 +0,0 @@
|
||||
describe API::TracksController do
|
||||
include UserControllerHelpers
|
||||
|
||||
before { sign_in }
|
||||
|
||||
describe 'GET index' do
|
||||
render_views
|
||||
|
||||
before do
|
||||
FactoryGirl.create(:track_with_sound)
|
||||
FactoryGirl.create(:track_with_sound)
|
||||
end
|
||||
|
||||
def do_get
|
||||
get :index, format: :json
|
||||
JSON response.body
|
||||
end
|
||||
|
||||
it 'lists all tracks' do
|
||||
expect(do_get.size).to eq 2
|
||||
end
|
||||
|
||||
it 'lists tracks with their id' do
|
||||
do_get.each { |p| expect(p).to include 'id' }
|
||||
end
|
||||
|
||||
it 'lists tracks with their name' do
|
||||
do_get.each { |p| expect(p).to include 'name' }
|
||||
end
|
||||
|
||||
it 'lists tracks with sound URL' do
|
||||
do_get.each { |p| expect(p).to include 'sound_url' }
|
||||
end
|
||||
end
|
||||
end
|
@ -1,96 +0,0 @@
|
||||
describe PlaylistsController do
|
||||
include UserControllerHelpers
|
||||
|
||||
before { sign_in }
|
||||
|
||||
describe 'GET index' do
|
||||
it 'assigns all playlists as @playlists' do
|
||||
playlist = FactoryGirl.create(:playlist)
|
||||
get :index
|
||||
expect(assigns[:playlists]).to eq [playlist]
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET new' do
|
||||
it 'assigns a new playlist as @playlist' do
|
||||
get :new
|
||||
expect(assigns[:playlist]).to be_a_new Playlist
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET edit' do
|
||||
it 'assigns the requested playlist as @playlist' do
|
||||
playlist = FactoryGirl.create(:playlist)
|
||||
get :edit, id: playlist.id.to_s
|
||||
expect(assigns[:playlist]).to eq playlist
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST create' do
|
||||
def do_create
|
||||
post :create, playlist: FactoryGirl.attributes_for(:playlist)
|
||||
end
|
||||
|
||||
context 'whith valid params' do
|
||||
it 'creates a new playlist for the current user' do
|
||||
expect {
|
||||
do_create
|
||||
}.to change(controller.current_user.playlists, :count).by(1)
|
||||
end
|
||||
|
||||
it 'redirects to the playlists index' do
|
||||
do_create
|
||||
expect(response).to redirect_to action: 'index'
|
||||
end
|
||||
end
|
||||
|
||||
context 'whith invalid params' do
|
||||
before { allow_any_instance_of(Playlist).to receive(:save) { false } }
|
||||
|
||||
it 'assigns the playlist as @playlist' do
|
||||
do_create
|
||||
expect(assigns[:playlist]).to be_a_new Playlist
|
||||
end
|
||||
|
||||
it 'renders the new template' do
|
||||
do_create
|
||||
expect(response).to render_template 'new'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT update' do
|
||||
let(:playlist) { FactoryGirl.create(:playlist) }
|
||||
|
||||
def do_update
|
||||
put :update, id: playlist.id.to_s, playlist: { name: 'Rock' }
|
||||
end
|
||||
|
||||
context 'whith valid params' do
|
||||
it 'updates the playlist' do
|
||||
expect_any_instance_of(Playlist)
|
||||
.to receive(:update_attributes).with('name' => 'Rock')
|
||||
do_update
|
||||
end
|
||||
|
||||
it 'redirects to the playlists index' do
|
||||
do_update
|
||||
expect(response).to redirect_to(action: 'index')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with invalid params' do
|
||||
before { allow_any_instance_of(Playlist).to receive(:save) { false } }
|
||||
|
||||
it 'assigns the requested playlist as @playlist' do
|
||||
do_update
|
||||
expect(assigns[:playlist]).to eq playlist
|
||||
end
|
||||
|
||||
it 'renders the edit template' do
|
||||
do_update
|
||||
expect(response).to render_template 'edit'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,46 +0,0 @@
|
||||
describe SessionsController do
|
||||
describe 'GET new' do
|
||||
it 'responds successfully' do
|
||||
expect(response).to be_success
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST create' do
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
|
||||
def do_create
|
||||
post :create, session: {
|
||||
email: user.email,
|
||||
password: user.password
|
||||
}
|
||||
end
|
||||
|
||||
context 'with valid credentials' do
|
||||
it 'signs the user in' do
|
||||
do_create
|
||||
expect(controller.current_user).to eq user
|
||||
end
|
||||
|
||||
it 'redirects to the home page' do
|
||||
do_create
|
||||
expect(response).to redirect_to :root
|
||||
end
|
||||
end
|
||||
|
||||
[:email, :password].each do |attr|
|
||||
context "with invalid credentials (#{attr})" do
|
||||
before do
|
||||
allow(user).to receive(attr).and_return(user.send(attr) + '_INVALID')
|
||||
end
|
||||
|
||||
it 'renders the new template' do
|
||||
do_create
|
||||
expect(response).to render_template 'new'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE destroy' do
|
||||
end
|
||||
end
|
@ -1,23 +0,0 @@
|
||||
describe SoundsController do
|
||||
include UserControllerHelpers
|
||||
|
||||
before { sign_in }
|
||||
|
||||
describe 'GET show' do
|
||||
let(:sound) { FactoryGirl.create(:sound) }
|
||||
|
||||
def do_show
|
||||
get :show, id: sound.id
|
||||
end
|
||||
|
||||
it 'sets the sound file content as the response body' do
|
||||
do_show
|
||||
expect(response.body).to eq File.read(sound.path, encoding: 'BINARY')
|
||||
end
|
||||
|
||||
it 'sets the sound mime-type as the response content-type' do
|
||||
do_show
|
||||
expect(response.content_type).to eq sound.mime_type
|
||||
end
|
||||
end
|
||||
end
|
@ -1,58 +0,0 @@
|
||||
describe TracksController do
|
||||
include UserControllerHelpers
|
||||
|
||||
before { sign_in }
|
||||
|
||||
describe 'GET show' do
|
||||
it 'assigns the requested track as @track' do
|
||||
track = FactoryGirl.create(:track)
|
||||
get :show, id: track.id.to_s
|
||||
expect(assigns[:track]).to eq track
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET new' do
|
||||
it 'assigns a new track as @track' do
|
||||
get :new
|
||||
expect(assigns[:track]).to be_a_new Track
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST create new' do
|
||||
def do_create
|
||||
post :create, track: FactoryGirl.attributes_for(:track).merge(
|
||||
file: fixture_file_upload(
|
||||
"#{Rails.root}/spec/fixtures/test.mp3",
|
||||
'audio/mpeg'
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
context 'whith valid params' do
|
||||
it 'creates a new track' do
|
||||
expect {
|
||||
do_create
|
||||
}.to change(Track, :count).by(1)
|
||||
end
|
||||
|
||||
it 'redirects to the track page' do
|
||||
do_create
|
||||
expect(response).to redirect_to Track.last
|
||||
end
|
||||
end
|
||||
|
||||
context 'whith invalid params' do
|
||||
before { allow_any_instance_of(Track).to receive(:save) { false } }
|
||||
|
||||
it 'assigns the track as @track' do
|
||||
do_create
|
||||
expect(assigns[:track]).to be_a_new Track
|
||||
end
|
||||
|
||||
it 'renders the new template' do
|
||||
do_create
|
||||
expect(response).to render_template 'new'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,48 +0,0 @@
|
||||
describe UsersController do
|
||||
let(:attributes) { FactoryGirl.attributes_for(:user) }
|
||||
|
||||
describe 'GET new' do
|
||||
it 'assigns a new user as @user' do
|
||||
get :new
|
||||
expect(assigns[:user]).to be_a_new User
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST create' do
|
||||
def do_post(params = {})
|
||||
post :create, user: attributes.merge(params)
|
||||
end
|
||||
|
||||
context 'whith valid params' do
|
||||
it 'creates a new user' do
|
||||
expect {
|
||||
do_post
|
||||
}.to change(User, :count).by(1)
|
||||
end
|
||||
|
||||
it 'signs the user in' do
|
||||
do_post
|
||||
expect(controller.current_user).not_to be_nil
|
||||
end
|
||||
|
||||
it 'redirects to the home page' do
|
||||
do_post
|
||||
expect(response).to redirect_to :root
|
||||
end
|
||||
end
|
||||
|
||||
context 'whith invalid params' do
|
||||
before { allow_any_instance_of(User).to receive(:save) { false } }
|
||||
|
||||
it 'assigns the user as @user' do
|
||||
do_post
|
||||
expect(assigns[:user]).to be_a_new User
|
||||
end
|
||||
|
||||
it 'renders the new template' do
|
||||
do_post
|
||||
expect(response).to render_template 'new'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
16
spec/features/sounds/crud_spec.rb
Normal file
16
spec/features/sounds/crud_spec.rb
Normal file
@ -0,0 +1,16 @@
|
||||
feature 'Sounds CRUD' do
|
||||
include AcceptanceHelpers
|
||||
|
||||
background { sign_in }
|
||||
|
||||
scenario 'shows sound' do
|
||||
track = create_track file: true
|
||||
visit tracks_path
|
||||
click_link track[:name]
|
||||
|
||||
visit find('audio')[:src]
|
||||
|
||||
expect(response_headers['Content-Type']).to eq 'audio/mpeg'
|
||||
expect(page.body).to eq File.read(track[:file].path, mode: 'rb')
|
||||
end
|
||||
end
|
@ -1,5 +1,7 @@
|
||||
describe 'API sign in' do
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
include AcceptanceHelpers
|
||||
|
||||
let(:user) { create :user }
|
||||
|
||||
def do_create
|
||||
post api_sessions_path, format: :json, session: {
|
||||
@ -11,17 +13,20 @@ describe 'API sign in' do
|
||||
it 'signs the user in with valid credentials' do
|
||||
do_create
|
||||
|
||||
expect(response).to be_success
|
||||
expect(JSON response.body).to include 'id'
|
||||
expect(json).to eq({
|
||||
id: user.id
|
||||
})
|
||||
end
|
||||
|
||||
[:email, :password].each do |attr|
|
||||
it "rejects authentication with invalid credentials (#{attr})" do
|
||||
allow(user).to receive(attr).and_return(user.send(attr) + '_INVALID')
|
||||
do_create
|
||||
context "with invalid #{attr}" do
|
||||
it 'rejects authentication' do
|
||||
allow(user).to receive(attr).and_return(user.send(attr) + '_INVALID')
|
||||
do_create
|
||||
|
||||
expect(response).to be_not_found
|
||||
expect(response.body).to be_empty
|
||||
expect(response).to be_not_found
|
||||
expect(response.body).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -3,16 +3,27 @@ describe 'API playlists' do
|
||||
|
||||
before { api_sign_in }
|
||||
|
||||
it 'creates playlist' do
|
||||
playlist = FactoryGirl.attributes_for :playlist
|
||||
it 'lists playlists' do
|
||||
playlist = create :playlist
|
||||
get api_playlists_path, format: json
|
||||
|
||||
post_via_redirect api_playlists_path,
|
||||
expect(json).to match [
|
||||
a_hash_including(
|
||||
id: an_instance_of(Fixnum),
|
||||
name: playlist[:name]
|
||||
)
|
||||
]
|
||||
end
|
||||
|
||||
it 'creates playlist' do
|
||||
playlist = attributes_for :playlist
|
||||
post api_playlists_path,
|
||||
format: :json,
|
||||
playlist: playlist
|
||||
|
||||
json = JSON response.body
|
||||
|
||||
expect(json['id']).to be_a Fixnum
|
||||
expect(json['name']).to eq playlist[:name]
|
||||
expect(json).to match(
|
||||
id: an_instance_of(Fixnum),
|
||||
name: playlist[:name]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
@ -4,21 +4,20 @@ describe 'API tracks' do
|
||||
before { api_sign_in }
|
||||
|
||||
it 'lists tracks' do
|
||||
track_1 = FactoryGirl.create(:track_with_sound, name: 'Track 1')
|
||||
track_2 = FactoryGirl.create(:track, name: 'Track 2')
|
||||
|
||||
track_1 = create :track_with_sound, name: 'Track 1'
|
||||
track_2 = create :track, name: 'Track 2'
|
||||
get api_tracks_path, format: :json
|
||||
|
||||
expect(response.body).to eq [
|
||||
expect(json).to eq [
|
||||
{
|
||||
id: track_1.id,
|
||||
name: 'Track 1',
|
||||
sound_url: api_sound_url(track_1.sound)
|
||||
id: track_1.id,
|
||||
name: 'Track 1',
|
||||
sound_url: api_sound_url(track_1.sound)
|
||||
},
|
||||
{
|
||||
id: track_2.id,
|
||||
name: 'Track 2'
|
||||
id: track_2.id,
|
||||
name: 'Track 2'
|
||||
}
|
||||
].to_json
|
||||
]
|
||||
end
|
||||
end
|
||||
|
@ -25,12 +25,18 @@ module AcceptanceHelpers
|
||||
playlist
|
||||
end
|
||||
|
||||
def create_track
|
||||
track = attributes_for :track
|
||||
def create_track file: false
|
||||
track = attributes_for(file ? :track_with_sound : :track)
|
||||
visit tracks_path
|
||||
click_link 'Create track'
|
||||
fill_in 'Name', with: track[:name]
|
||||
attach_file 'File', track[:file].path if file
|
||||
click_button 'Upload'
|
||||
track
|
||||
end
|
||||
|
||||
def json
|
||||
expect(response).to be_success
|
||||
JSON.parse(response.body, symbolize_names: true)
|
||||
end
|
||||
end
|
||||
|
@ -1,5 +0,0 @@
|
||||
module UserControllerHelpers
|
||||
def sign_in
|
||||
controller.current_user = FactoryGirl.create(:user)
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user