2015-10-12 03:37:44 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
RSpec.describe TopicsController, type: :controller do
|
2016-02-08 03:56:46 +00:00
|
|
|
let(:topic) { create(:topic) }
|
2016-02-09 05:42:24 +00:00
|
|
|
let(:valid_attributes) { topic.attributes.except('id') }
|
|
|
|
let(:invalid_attributes) { { permission: :invalid_lol } }
|
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 Topic' do
|
2016-02-09 05:42:24 +00:00
|
|
|
topic.reload # ensure it's created
|
2015-12-16 14:19:58 +00:00
|
|
|
expect do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
topic: valid_attributes
|
|
|
|
}
|
2015-12-16 14:19:58 +00:00
|
|
|
end.to change(Topic, :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 topic as @topic' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
topic: valid_attributes
|
|
|
|
}
|
|
|
|
expect(comparable(Topic.last)).to eq comparable(topic)
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
it 'returns 201 CREATED' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
topic: valid_attributes
|
|
|
|
}
|
2016-02-09 05:42:24 +00:00
|
|
|
expect(response.status).to eq 201
|
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 topic as @topic' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, format: :json, params: {
|
|
|
|
topic: invalid_attributes
|
|
|
|
}
|
|
|
|
expect(Topic.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
|
|
|
|
let(:new_attributes) do
|
2016-02-09 05:42:24 +00:00
|
|
|
{ name: 'Cool Topic with no number',
|
|
|
|
desc: 'This is a cool topic.',
|
|
|
|
link: 'https://cool-topics.com/4',
|
|
|
|
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 topic' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: topic.to_param, topic: new_attributes
|
|
|
|
}
|
2015-10-12 03:37:44 +00:00
|
|
|
topic.reload
|
2016-02-09 05:42:24 +00:00
|
|
|
expect(topic.name).to eq 'Cool Topic with no number'
|
|
|
|
expect(topic.desc).to eq 'This is a cool topic.'
|
|
|
|
expect(topic.link).to eq 'https://cool-topics.com/4'
|
|
|
|
expect(topic.permission).to eq 'public'
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
it 'assigns the requested topic as @topic' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: topic.to_param, topic: valid_attributes
|
|
|
|
}
|
|
|
|
expect(Topic.last).to eq(topic)
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
it 'returns status of no content' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: topic.to_param, topic: valid_attributes
|
|
|
|
}
|
2016-02-09 05:42:24 +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 topic as @topic' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, format: :json, params: {
|
|
|
|
id: topic.to_param, topic: invalid_attributes
|
|
|
|
}
|
|
|
|
expect(Topic.last).to eq topic
|
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 05:42:24 +00:00
|
|
|
let(:owned_topic) { create(:topic, user: controller.current_user) }
|
2015-12-16 14:19:58 +00:00
|
|
|
it 'destroys the requested topic' do
|
2016-02-09 05:42:24 +00:00
|
|
|
owned_topic.reload # ensure it's there
|
2015-12-16 14:19:58 +00:00
|
|
|
expect do
|
2016-09-21 17:22:40 +00:00
|
|
|
delete :destroy, format: :json, params: {
|
|
|
|
id: owned_topic.to_param
|
|
|
|
}
|
2015-12-16 14:19:58 +00:00
|
|
|
end.to change(Topic, :count).by(-1)
|
2016-03-14 03:46:45 +00:00
|
|
|
expect(response.body).to eq ''
|
2016-02-09 05:42:24 +00:00
|
|
|
expect(response.status).to eq 204
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|