2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2015-10-12 03:37:44 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
RSpec.describe MapsController, type: :controller do
|
2016-02-08 03:56:46 +00:00
|
|
|
let(:map) { create(:map) }
|
|
|
|
let(:valid_attributes) { map.attributes.except(:id) }
|
2016-07-26 00:14:23 +00:00
|
|
|
let(:invalid_attributes) { { permission: :commons } }
|
2016-02-08 03:56:46 +00:00
|
|
|
before :each do
|
2016-09-21 17:22:40 +00:00
|
|
|
sign_in create(:user)
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
describe 'POST #create' do
|
|
|
|
context 'with valid params' do
|
|
|
|
it 'creates a new Map' do
|
|
|
|
expect do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
map: valid_attributes
|
|
|
|
}
|
2015-12-16 14:19:58 +00:00
|
|
|
end.to change(Map, :count).by(1)
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
it 'assigns a newly created map as @map' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
map: valid_attributes
|
|
|
|
}
|
|
|
|
expect(Map.last).to eq map
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
context 'with invalid params' do
|
|
|
|
it 'assigns a newly created but unsaved map as @map' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
map: invalid_attributes
|
|
|
|
}
|
|
|
|
expect(Map.count).to eq 0
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
describe 'PUT #update' do
|
|
|
|
context 'with valid params' do
|
2016-07-26 00:14:23 +00:00
|
|
|
let(:new_attributes) { { name: 'Uncool map', permission: :private } }
|
2015-10-12 03:37:44 +00:00
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
it 'updates the requested map' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: map.to_param, map: new_attributes
|
|
|
|
}
|
|
|
|
map.reload
|
|
|
|
expect(map.name).to eq 'Uncool map'
|
|
|
|
expect(map.permission).to eq 'private'
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
it 'assigns the requested map as @map' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: map.to_param, map: valid_attributes
|
|
|
|
}
|
|
|
|
expect(Map.last).to eq map
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
context 'with invalid params' do
|
|
|
|
it 'assigns the map as @map' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: map.to_param, map: invalid_attributes
|
|
|
|
}
|
|
|
|
expect(Map.last).to eq map
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
describe 'DELETE #destroy' do
|
2016-02-09 03:33:43 +00:00
|
|
|
let(:unowned_map) { create(:map) }
|
|
|
|
let(:owned_map) { create(:map, user: controller.current_user) }
|
|
|
|
|
|
|
|
it 'prevents deletion by non-owners' do
|
|
|
|
unowned_map.reload
|
2015-12-16 14:19:58 +00:00
|
|
|
expect do
|
2016-09-21 17:22:40 +00:00
|
|
|
delete :destroy, format: :json, params: {
|
|
|
|
id: unowned_map.to_param
|
|
|
|
}
|
2016-02-09 03:33:43 +00:00
|
|
|
end.to change(Map, :count).by(0)
|
2016-10-08 05:42:25 +00:00
|
|
|
expect(response.headers['Location']).to eq(request.base_url + root_path)
|
|
|
|
expect(response.status).to eq 302
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2016-02-09 03:33:43 +00:00
|
|
|
it 'deletes owned map' do
|
|
|
|
owned_map.reload # ensure it's in the database
|
|
|
|
expect do
|
2016-09-21 17:22:40 +00:00
|
|
|
delete :destroy, format: :json, params: {
|
|
|
|
id: owned_map.to_param
|
|
|
|
}
|
2016-02-09 03:33:43 +00:00
|
|
|
end.to change(Map, :count).by(-1)
|
2016-03-14 03:46:45 +00:00
|
|
|
expect(response.body).to eq ''
|
|
|
|
expect(response.status).to eq 204
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|