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 MetacodesController, type: :controller do
|
2016-02-08 03:56:46 +00:00
|
|
|
let(:metacode) { create(:metacode) }
|
2016-02-09 05:30:49 +00:00
|
|
|
let(:valid_attributes) { metacode.attributes.except('id') }
|
2015-12-16 13:32:50 +00:00
|
|
|
before :each do
|
2016-02-09 05:30:49 +00:00
|
|
|
sign_in create(:user, admin: true)
|
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
|
|
|
describe 'GET #index' do
|
|
|
|
it 'assigns all metacodes as @metacodes' do
|
2016-02-09 05:30:49 +00:00
|
|
|
metacode.reload # ensure it's created
|
2016-09-21 17:22:40 +00:00
|
|
|
get :index
|
2016-02-09 05:30:49 +00:00
|
|
|
expect(Metacode.all.to_a).to eq([metacode])
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-16 14:19:58 +00:00
|
|
|
describe 'POST #create' do
|
|
|
|
context 'with valid params' do
|
|
|
|
it 'creates a new Metacode' do
|
2016-02-09 05:30:49 +00:00
|
|
|
metacode.reload # ensure it's present to start
|
2015-12-16 14:19:58 +00:00
|
|
|
expect do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, params: {
|
|
|
|
metacode: valid_attributes
|
|
|
|
}
|
2015-12-16 14:19:58 +00:00
|
|
|
end.to change(Metacode, :count).by(1)
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2016-02-09 05:30:49 +00:00
|
|
|
it 'has the correct attributes' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, params: {
|
|
|
|
metacode: valid_attributes
|
|
|
|
}
|
|
|
|
expect(comparable(Metacode.last)).to eq(comparable(metacode))
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2016-02-09 05:30:49 +00:00
|
|
|
it 'redirects to the metacode index' do
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, params: {
|
|
|
|
metacode: valid_attributes
|
|
|
|
}
|
2016-02-09 05:30:49 +00:00
|
|
|
expect(response).to redirect_to(metacodes_url)
|
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-26 06:20:14 +00:00
|
|
|
{ manual_icon: 'https://newimages.ca/cool-image.jpg',
|
|
|
|
aws_icon: nil,
|
2016-02-09 05:30:49 +00:00
|
|
|
color: '#ffffff',
|
|
|
|
name: 'Cognition' }
|
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 metacode' do
|
2016-09-21 17:22:40 +00:00
|
|
|
put :update, params: {
|
|
|
|
id: metacode.to_param, metacode: new_attributes
|
|
|
|
}
|
2015-10-12 03:37:44 +00:00
|
|
|
metacode.reload
|
2016-02-09 05:30:49 +00:00
|
|
|
expect(metacode.icon).to eq 'https://newimages.ca/cool-image.jpg'
|
|
|
|
expect(metacode.color).to eq '#ffffff'
|
|
|
|
expect(metacode.name).to eq 'Cognition'
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
2016-02-09 05:30:49 +00:00
|
|
|
end
|
|
|
|
end
|
2015-10-12 03:37:44 +00:00
|
|
|
|
2016-02-09 05:30:49 +00:00
|
|
|
context 'not admin' do
|
|
|
|
it 'denies access to create' do
|
|
|
|
sign_in create(:user, admin: false)
|
2016-09-21 17:22:40 +00:00
|
|
|
post :create, params: {
|
|
|
|
metacode: valid_attributes
|
|
|
|
}
|
2016-02-09 05:30:49 +00:00
|
|
|
expect(response).to redirect_to root_url
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
|
2016-02-09 05:30:49 +00:00
|
|
|
it 'denies access to update' do
|
|
|
|
sign_in create(:user, admin: false)
|
2016-09-21 17:22:40 +00:00
|
|
|
post :update, params: {
|
|
|
|
id: metacode.to_param, metacode: valid_attributes
|
|
|
|
}
|
2016-02-09 05:30:49 +00:00
|
|
|
expect(response).to redirect_to root_url
|
2015-10-12 03:37:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|