diff --git a/app/models/synapse.rb b/app/models/synapse.rb index 0b40da02..ea5889cc 100644 --- a/app/models/synapse.rb +++ b/app/models/synapse.rb @@ -1,5 +1,4 @@ class Synapse < ActiveRecord::Base - belongs_to :user belongs_to :topic1, :class_name => "Topic", :foreign_key => "node1_id" @@ -13,15 +12,17 @@ class Synapse < ActiveRecord::Base validates :permission, presence: true validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) } + validates :category, inclusion: { in: ['from-to', 'both'], allow_nil: true } + # :nocov: def user_name - self.user.name + user.name end # :nocov: # :nocov: def user_image - self.user.image.url + user.image.url end # :nocov: diff --git a/spec/controllers/synapses_controller_spec.rb b/spec/controllers/synapses_controller_spec.rb index 9b7471ab..478ee6ed 100644 --- a/spec/controllers/synapses_controller_spec.rb +++ b/spec/controllers/synapses_controller_spec.rb @@ -2,15 +2,15 @@ require 'rails_helper' RSpec.describe SynapsesController, type: :controller do let(:synapse) { create(:synapse) } - let(:valid_attributes) { synapse.attributes.except(:id) } - let(:invalid_attributes) { { permission: :commons } } + let(:valid_attributes) { synapse.attributes.except('id') } + let(:invalid_attributes) { { permission: :invalid_lol } } before :each do sign_in end describe 'GET #show' do it 'assigns the requested synapse as @synapse' do - get :show, { id: synapse.to_param } + get :show, { id: synapse.to_param, format: :json } expect(assigns(:synapse)).to eq(synapse) end end @@ -18,27 +18,28 @@ RSpec.describe SynapsesController, type: :controller do describe 'POST #create' do context 'with valid params' do it 'creates a new Synapse' do + synapse.reload # ensure it's present expect do - post :create, { synapse: valid_attributes } + post :create, { synapse: valid_attributes, format: :json } end.to change(Synapse, :count).by(1) end it 'assigns a newly created synapse as @synapse' do - post :create, { synapse: valid_attributes } + post :create, { synapse: valid_attributes, format: :json } expect(assigns(:synapse)).to be_a(Synapse) expect(assigns(:synapse)).to be_persisted end - it 'redirects to the created synapse' do - post :create, { synapse: valid_attributes } - expect(response).to redirect_to(Synapse.last) + it 'returns 201 CREATED' do + post :create, { synapse: valid_attributes, format: :json } + expect(response.status).to eq 201 end end context 'with invalid params' do - it 'assigns a newly created but unsaved synapse as @synapse' do - post :create, { synapse: invalid_attributes } - expect(assigns(:synapse)).to be_a_new(Synapse) + it 'returns 422 UNPROCESSABLE ENTITY' do + post :create, { synapse: invalid_attributes, format: :json } + expect(response.status).to eq 422 end end end @@ -46,48 +47,49 @@ RSpec.describe SynapsesController, type: :controller do describe 'PUT #update' do context 'with valid params' do let(:new_attributes) do - skip('Add a hash of attributes valid for your model') + { desc: 'My new description', + category: 'both', + permission: :public } end it 'updates the requested synapse' do put :update, - { id: synapse.to_param, synapse: new_attributes } + { id: synapse.to_param, synapse: new_attributes, format: :json } synapse.reload - skip('Add assertions for updated state') + expect(synapse.desc).to eq 'My new description' + expect(synapse.category).to eq 'both' + expect(synapse.permission).to eq 'public' end - it 'assigns the requested synapse as @synapse' do + it 'returns 204 NO CONTENT' do put :update, - { id: synapse.to_param, synapse: valid_attributes } - expect(assigns(:synapse)).to eq(synapse) - end - - it 'redirects to the synapse' do - put :update, - { id: synapse.to_param, synapse: valid_attributes } - expect(response).to redirect_to(synapse) + { id: synapse.to_param, synapse: valid_attributes, format: :json } + expect(response.status).to eq 204 end end context 'with invalid params' do it 'assigns the synapse as @synapse' do put :update, - { id: synapse.to_param, synapse: invalid_attributes } + { id: synapse.to_param, synapse: invalid_attributes, format: :json } expect(assigns(:synapse)).to eq(synapse) end end end describe 'DELETE #destroy' do + let(:synapse) { create(:synapse, user: controller.current_user) } + it 'destroys the requested synapse' do + synapse.reload # ensure it's present expect do - delete :destroy, { id: synapse.to_param } + delete :destroy, { id: synapse.to_param, format: :json } end.to change(Synapse, :count).by(-1) end - it 'redirects to the synapses list' do - delete :destroy, { id: synapse.to_param } - expect(response).to redirect_to(synapses_url) + it 'returns 204 NO CONTENT' do + delete :destroy, { id: synapse.to_param, format: :json } + expect(response.status).to eq 204 end end end diff --git a/spec/factories/synapses.rb b/spec/factories/synapses.rb index b83a0073..4454a7a4 100644 --- a/spec/factories/synapses.rb +++ b/spec/factories/synapses.rb @@ -1,9 +1,10 @@ FactoryGirl.define do factory :synapse do sequence(:desc) { |n| "Cool synapse ##{n}" } - category :to + category :'from-to' permission :commons association :topic1, factory: :topic association :topic2, factory: :topic + user end end diff --git a/spec/models/synapse_spec.rb b/spec/models/synapse_spec.rb index a1069805..dcf85358 100644 --- a/spec/models/synapse_spec.rb +++ b/spec/models/synapse_spec.rb @@ -8,6 +8,7 @@ RSpec.describe Synapse, type: :model do it { is_expected.to have_many :mappings } it { is_expected.to validate_presence_of :permission } it { is_expected.to validate_inclusion_of(:permission).in_array Perm::ISSIONS.map(&:to_s) } + it { is_expected.to validate_inclusion_of(:category).in_array ['from-to', 'both'] } it { is_expected.to validate_length_of(:desc).is_at_least(0) } # TODO don't allow nil context 'permissions' do