languages_controller_spec.rb revision 7ca38d75d045fade73b81bff6fdf34f4755d75fb
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Boschrequire 'spec_helper'
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainendescribe LanguagesController do
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen let!(:user) { create :user }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen let!(:language) { create :language, user: user }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen context 'on GET to show' do
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen context 'not signed in' do
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen before { get :show, id: language.to_param }
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it { should respond_with :success }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it { should render_template :show }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it 'does not set the flash' do
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen expect(flash).to be_empty
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen end
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen end
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen context 'signed in as Language-Owner' do
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen before do
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen sign_in user
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen get :show, id: language.to_param
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen end
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it { should respond_with :success }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it { should render_template :show }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it 'does not set the flash' do
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen expect(flash).to be_empty
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen end
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen end
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen end
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen context 'in GET to index' do
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen before { get :index }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it { should respond_with :success }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it { should render_template :index }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it 'does not set the flash' do
204ee6ed414f5e4eeb6f6c10763b55daf56f11acJosef 'Jeff' Sipek expect(flash).to be_empty
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen end
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen end
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen context 'on POST to create' do
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen let!(:language2) { build(:language) }
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen before do
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen sign_in user
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen post :create, language: {
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen name: language2.name,
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen iri: language2.iri
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen }
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen end
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen it 'create the record' do
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen expect(Language.find_by_name(language2.name).name).to eq(language2.name)
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen end
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen it { should respond_with :redirect }
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen it 'sets the flash' do
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen expect(flash[:notice]).to match(/created/i)
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen end
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen end
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen context 'on POST to update' do
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen let!(:oldname) { language.name }
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen context 'signed in' do
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen before do
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen sign_in user
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen post :update, id: language.id, language: {
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen name: "test3"
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen }
b162e16ade1e0d9bfae62e366caf57a3132f5963Timo Sirainen end
96e1ed172ae59cae6bc6e6ddd24d22a158e23dfeTimo Sirainen
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen it 'not leave the record' do
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen expect(Language.find_by_name(oldname)).to be_falsy
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen end
b2ff0468c298dd2f525fff5977f1f24fba3a9f3bTimo Sirainen
it 'change the record' do
expect(Language.find_by_name("test3")).to be_truthy
end
it { should respond_with :redirect }
it 'sets the flash' do
expect(flash[:notice]).to match(/successfully updated/i)
end
end
context 'not signed in' do
before do
post :update, id: language.id, language: {
name: "test3"
}
end
it 'leave the record' do
expect(Language.find_by_name(oldname)).to be_truthy
end
it 'not change the record' do
expect(Language.find_by_name("test3")).to be_falsy
end
it { should respond_with :redirect }
it 'sets the flash' do
expect(flash[:alert]).not_to match(/successfully updated/i)
end
end
context 'not permitted' do
let!(:user2) { create :user }
let!(:oldname) { language.name }
before do
sign_in user2
post :update, id: language.id, language: {
name: 'test3'
}
end
it 'leave the record' do
expect(Language.find_by_name(oldname)).to be_truthy
end
it 'not change the record' do
expect(Language.find_by_name('test3')).to be_falsy
end
it { should respond_with :redirect }
it 'sets the flash' do
expect(flash[:alert]).not_to match(/successfully updated/i)
end
end
end
context 'on POST to DELETE' do
context 'signed in' do
before do
sign_in user
delete :destroy, id: language.id
end
it 'not leave the record' do
expect(Language.find_by_name(language.name)).to be_falsy
end
it { should respond_with :redirect }
it 'sets the flash' do
expect(flash[:notice]).to match(/successfully destroyed/i)
end
end
context 'not signed in' do
before { delete :destroy, id: language.id }
it 'leave the record' do
expect(Language.find_by_name(language.name)).to be_truthy
end
it { should respond_with :redirect }
it 'sets the flash' do
expect(flash[:alert]).not_to match(/successfully destroyed/i)
end
end
context 'not permitted' do
let!(:user2) { create :user }
before do
sign_in user2
delete :destroy, id: language.id
end
it 'leave the record' do
expect(Language.find_by_name(language.name)).to be_truthy
end
it { should respond_with :redirect }
it 'sets the flash' do
expect(flash[:alert]).not_to match(/successfully destroyed/i)
end
end
end
end