2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2015-10-12 03:37:44 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
RSpec.describe SynapsesController, type: :controller do
|
2017-02-12 17:53:04 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:synapse) { create(:synapse, user: user, updated_by: user) }
|
2016-02-09 06:25:39 +00:00
|
|
|
let(:valid_attributes) { synapse.attributes.except('id') }
|
2016-07-26 00:14:23 +00:00
|
|
|
let(:invalid_attributes) { { permission: :invalid_lol } }
|
2016-02-08 03:56:46 +00:00
|
|
|
before :each do
|
2017-02-12 17:53:04 +00:00
|
|
|
sign_in(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 Synapse' do
|
2016-02-09 06:25:39 +00:00
|
|
|
synapse.reload # ensure it's present
|
2015-12-16 14:19:58 +00:00
|
|
|
expect do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
synapse: valid_attributes
|
|
|
|
}
|
2015-12-16 14:19:58 +00:00
|
|
|
end.to change(Synapse, :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 synapse as @synapse' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
synapse: valid_attributes
|
|
|
|
}
|
|
|
|
expect(comparable(Synapse.last)).to eq comparable(synapse)
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2016-02-09 06:25:39 +00:00
|
|
|
it 'returns 201 CREATED' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
synapse: valid_attributes
|
|
|
|
}
|
2016-02-09 06:25:39 +00:00
|
|
|
expect(response.status).to eq 201
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-28 03:03:59 +00:00
|
|
|
context 'with private topics' do
|
|
|
|
it 'redirects to /' do
|
|
|
|
post :create, format: :json, params: {
|
|
|
|
synapse: valid_attributes.merge(topic1_id: create(:topic, permission: 'private'), topic2_id: create(:topic, permission: 'private'))
|
|
|
|
}
|
|
|
|
expect(response.status).to eq 302
|
|
|
|
expect(response).to redirect_to('/')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
context 'with invalid params' do
|
2016-02-09 06:25:39 +00:00
|
|
|
it 'returns 422 UNPROCESSABLE ENTITY' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
synapse: invalid_attributes
|
|
|
|
}
|
2016-02-09 06:25:39 +00:00
|
|
|
expect(response.status).to eq 422
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
2016-10-28 03:03:59 +00:00
|
|
|
it 'does not create a synapse' do
|
2016-12-13 03:28:10 +00:00
|
|
|
expect do
|
2016-10-28 03:03:59 +00:00
|
|
|
post :create, format: :json, params: { synapse: invalid_attributes }
|
2016-12-13 03:28:10 +00:00
|
|
|
end.to change {
|
2016-10-28 03:03:59 +00:00
|
|
|
Synapse.count
|
|
|
|
}.by 0
|
|
|
|
end
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
describe 'PUT #update' do
|
|
|
|
context 'with valid params' do
|
|
|
|
let(:new_attributes) do
|
2016-02-09 06:25:39 +00:00
|
|
|
{ desc: 'My new description',
|
|
|
|
category: 'both',
|
|
|
|
permission: :public }
|
2015-12-16 14:19:58 +00:00
|
|
|
end
|
2015-10-12 03:37:44 +00:00
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
it 'updates the requested synapse' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: synapse.to_param, synapse: new_attributes
|
|
|
|
}
|
2015-10-12 03:37:44 +00:00
|
|
|
synapse.reload
|
2016-02-09 06:25:39 +00:00
|
|
|
expect(synapse.desc).to eq 'My new description'
|
|
|
|
expect(synapse.category).to eq 'both'
|
|
|
|
expect(synapse.permission).to eq 'public'
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
it 'returns 204 NO CONTENT' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: synapse.to_param, synapse: valid_attributes
|
|
|
|
}
|
2016-02-09 06:25:39 +00:00
|
|
|
expect(response.status).to eq 204
|
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 synapse as @synapse' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: synapse.to_param, synapse: invalid_attributes
|
|
|
|
}
|
|
|
|
expect(Synapse.last).to eq synapse
|
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 06:25:39 +00:00
|
|
|
let(:synapse) { create(:synapse, user: controller.current_user) }
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
it 'destroys the requested synapse' do
|
2016-02-09 06:25:39 +00:00
|
|
|
synapse.reload # ensure it's present
|
2015-12-16 14:19:58 +00:00
|
|
|
expect do
|
2016-09-21 17:22:40 +00:00
|
|
|
delete :destroy, format: :json, params: {
|
|
|
|
id: synapse.to_param
|
|
|
|
}
|
2015-12-16 14:19:58 +00:00
|
|
|
end.to change(Synapse, :count).by(-1)
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2016-02-09 06:25:39 +00:00
|
|
|
it 'returns 204 NO CONTENT' do
|
2016-09-21 17:22:40 +00:00
|
|
|
delete :destroy, format: :json, params: {
|
|
|
|
id: synapse.to_param
|
|
|
|
}
|
2016-02-09 06:25:39 +00:00
|
|
|
expect(response.status).to eq 204
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|