diff --git a/app/models/ability.rb b/app/models/ability.rb index ff1628a1..dba29fa7 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -83,6 +83,7 @@ class Ability conference.registration_open? && !conference.registration_limit_exceeded? || conference.program.speakers.confirmed.include?(user) end + can :index, Organization can :index, Ticket can :manage, TicketPurchase, user_id: user.id can [:new, :create], Payment, user_id: user.id diff --git a/app/models/organization.rb b/app/models/organization.rb index 15362d7a..60a83fef 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -1,5 +1,5 @@ class Organization < ActiveRecord::Base - has_many :conferences + has_many :conferences, dependent: :destroy validates :name, presence: true diff --git a/app/views/admin/organizations/index.html.haml b/app/views/admin/organizations/index.html.haml index f0a8c3c7..03bf11f9 100644 --- a/app/views/admin/organizations/index.html.haml +++ b/app/views/admin/organizations/index.html.haml @@ -3,7 +3,7 @@ .page-header %h1 Organizations .btn-group.pull-right - = link_to 'Add Organization', new_admin_organization_path, class: 'btn btn-success pull-right' + = link_to 'Create Organization', new_admin_organization_path, class: 'btn btn-success pull-right' %p.text-muted Manage organizations in OSEM .row diff --git a/config/routes.rb b/config/routes.rb index e2cfe7f7..b10d5d9c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -103,7 +103,7 @@ Osem::Application.routes.draw do get '/revision_history/:id/revert_object' => 'versions#revert_object', as: 'revision_history_revert_object' get '/revision_history/:id/revert_attribute' => 'versions#revert_attribute', as: 'revision_history_revert_attribute' end - resources :organizations + resources :organizations, only: [:index] resources :conferences, only: [:index, :show] do resource :program, only: [] do resources :proposals, except: :destroy do diff --git a/db/migrate/20170529215453_create_organizations.rb b/db/migrate/20170529215453_create_organizations.rb index 11caf736..0fa5450b 100644 --- a/db/migrate/20170529215453_create_organizations.rb +++ b/db/migrate/20170529215453_create_organizations.rb @@ -1,7 +1,7 @@ class CreateOrganizations < ActiveRecord::Migration def change create_table :organizations do |t| - t.string :name + t.string :name, null: false t.text :description t.string :picture end diff --git a/db/migrate/20170531094819_move_conferences_to_organizations.rb b/db/migrate/20170531094819_move_conferences_to_organizations.rb index 80c2ebd6..12bc7580 100644 --- a/db/migrate/20170531094819_move_conferences_to_organizations.rb +++ b/db/migrate/20170531094819_move_conferences_to_organizations.rb @@ -8,14 +8,15 @@ class MoveConferencesToOrganizations < ActiveRecord::Migration end def change - add_reference :conferences, :organization, index: true - add_foreign_key :conferences, :organizations, dependent: :delete + add_reference :conferences, :organization, index: true, foreign_key: true TempConference.reset_column_information - organization = TempOrganization.create(name: 'organization', description: 'Default organization to migrate old conferences to the new version of OSEM') - TempConference.all.each do |conference| - conference.organization_id = organization.id - conference.save! + if TempConference.count != 0 + organization = TempOrganization.create(name: 'organization', description: 'Default organization') + TempConference.all.each do |conference| + conference.organization_id = organization.id + conference.save! + end end end end diff --git a/db/schema.rb b/db/schema.rb index 7db5faa0..9772066a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -270,7 +270,7 @@ ActiveRecord::Schema.define(version: 20170531094819) do end create_table "organizations", force: :cascade do |t| - t.string "name" + t.string "name", null: false t.text "description" t.string "picture" end diff --git a/spec/controllers/admin/organizations_controller_spec.rb b/spec/controllers/admin/organizations_controller_spec.rb index 4635dfdf..43f7266f 100644 --- a/spec/controllers/admin/organizations_controller_spec.rb +++ b/spec/controllers/admin/organizations_controller_spec.rb @@ -2,13 +2,170 @@ require 'spec_helper' describe Admin::OrganizationsController do let(:admin) { create(:admin) } + let(:organization) { create(:organization) } + let(:user) { create(:user) } - describe 'GET #index' do + context 'logged in as user with no role' do before :each do - sign_in admin - get :index + sign_in user end - it { expect(response).to render_template('index') } + describe 'GET #new' do + before :each do + get :new + end + + it 'redirects to root' do + expect(flash[:alert]).to eq('You are not authorized to access this area!') + expect(response).to redirect_to(root_path) + end + end + + describe 'GET #index' do + before :each do + get :index + end + + it 'redirects to root' do + expect(flash[:alert]).to eq('You are not authorized to access this area!') + expect(response).to redirect_to(root_path) + end + end + + describe 'POST #create' do + it 'does not create new organization' do + expected = expect do + post :create, organization: attributes_for(:organization) + end + expected.to_not change { Organization.count } + end + + it 'redirects to root' do + post :create, organization: attributes_for(:organization) + + expect(flash[:alert]).to eq('You are not authorized to access this area!') + expect(response).to redirect_to(root_path) + end + end + + describe 'PATCH #update' do + it 'does not update and redirects to root' do + old_name = organization.name + patch :update, id: organization.id, organization: attributes_for(:organization, name: 'new name') + + organization.reload + expect(organization.name).to eq(old_name) + expect(flash[:alert]).to eq('You are not authorized to access this area!') + expect(response).to redirect_to(root_path) + end + end + + describe 'DELETE #destroy' do + context 'for a valid organization' do + it 'does not destroy a resource' do + expected = expect do + delete :destroy, id: organization.id + end + expected.to_not change { Organization.count } + end + + it 'redirects to root' do + delete :destroy, id: organization.id + + expect(flash[:alert]).to eq('You are not authorized to access this area!') + expect(response).to redirect_to(root_path) + end + end + end + end + + context 'logged in as admin' do + before :each do + sign_in admin + end + + describe 'GET #new' do + before do + get :new + end + it { expect(response).to render_template('new') } + end + + describe 'GET #index' do + before do + get :index + end + it { expect(response).to render_template('index') } + end + + describe 'POST #create' do + context 'with valid attributes' do + it 'creates new organization' do + expected = expect do + post :create, organization: attributes_for(:organization) + end + expected.to change { Organization.count }.by(1) + end + + it 'redirects to index' do + post :create, organization: attributes_for(:organization) + + expect(flash[:notice]).to eq('Organization successfully created') + expect(response).to redirect_to(admin_organizations_path) + end + end + + context 'with invalid attributes' do + it 'does not create new organization' do + expected = expect do + post :create, organization: attributes_for(:organization, name: '') + end + expected.to_not change { Organization.count } + end + + it 'redirects to new' do + post :create, organization: attributes_for(:organization, name: '') + + expect(flash[:error]).to eq("Name can't be blank") + expect(response).to redirect_to(new_admin_organization_path) + end + end + end + + describe 'PATCH #update' do + it 'saves and redirects to index when the attributes are valid' do + patch :update, id: organization.id, organization: attributes_for(:organization, name: 'changed name') + + organization.reload + expect(organization.name).to eq('changed name') + expect(flash[:notice]).to eq('Organization successfully updated') + expect(response).to redirect_to(admin_organizations_path) + end + + it 'redirects to edit when attributes are invalid' do + patch :update, id: organization.id, organization: attributes_for(:organization, name: '') + + expect(flash[:error]).to eq("Name can't be blank") + expect(response).to redirect_to(edit_admin_organization_path(organization)) + end + end + + describe 'DELETE #destroy' do + context 'for a valid organization' do + it 'should successfully destroy a resource' do + expected = expect do + delete :destroy, id: organization.id + end + expected.to change { Organization.count }.by(-1) + end + + it 'redirects to index' do + delete :destroy, id: organization.id + + expect(flash[:notice]).to eq('Organization successfully destroyed') + expect(response).to redirect_to(admin_organizations_path) + end + end + end end end diff --git a/spec/controllers/organizations_controller_spec.rb b/spec/controllers/organizations_controller_spec.rb index 94007630..2916fdea 100644 --- a/spec/controllers/organizations_controller_spec.rb +++ b/spec/controllers/organizations_controller_spec.rb @@ -2,103 +2,14 @@ require 'spec_helper' describe OrganizationsController do let!(:organization) { create(:organization) } - let!(:admin) { create(:admin, is_admin: true) } - - describe 'GET #new' do - before :each do - sign_in admin - get :new - end - - it { expect(response).to render_template('new') } - end + let!(:user) { create(:user) } describe 'GET #index' do before :each do - sign_in admin + sign_in user get :index end it { expect(response).to render_template('index') } end - - describe 'POST #create' do - before :each do - sign_in admin - end - context 'with valid attributes' do - it 'creates new organization' do - expected = expect do - post :create, organization: attributes_for(:organization) - end - expected.to change { Organization.count }.by(1) - end - - it 'redirects to index' do - post :create, organization: attributes_for(:organization) - - expect(flash[:notice]).to eq('Organization successfully created') - expect(response).to redirect_to(organizations_path) - end - end - - context 'with invalid attributes' do - it 'does not create new organization' do - expected = expect do - post :create, organization: attributes_for(:organization, name: '') - end - expected.to_not change { Organization.count } - end - - it 'redirects to new' do - post :create, organization: attributes_for(:organization, name: '') - - expect(flash[:error]).to eq("Name can't be blank") - expect(response).to redirect_to(new_organization_path) - end - end - end - - describe 'PATCH #update' do - before :each do - sign_in admin - end - - it 'saves and redirects to index when the attributes are valid' do - patch :update, id: organization.id, organization: attributes_for(:organization, name: 'changed name') - - expect(organization.name).to eq('changed name') - expect(flash).to eq('Organization successfully updated') - expect(response).to redirect_to(organizations_path) - end - - it 'redirects to edit when attributes are invalid' do - patch :update, id: organization.id, organization: attributes_for(:organization, name: '') - - expect(flash[:error]).to eq("Name can't be blank") - expect(response).to redirect_to(edit_organization_path(organization)) - end - end - - describe 'DELETE #destroy' do - before :each do - sign_in admin - end - - context 'for a valid organization' do - it 'should successfully destroy a resource' do - expected = expect do - delete :destroy, id: organization.id - end - expected.to change { Organization.count }.by(-1) - end - - it 'redirects to index' do - delete :destroy, id: organization.id - - expect(flash[:notice]).to eq('Organization successfully destroyed') - expect(response).to redirect_to(organizations_path) - end - end - end end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 748205d5..ca84f532 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -4,10 +4,6 @@ describe Organization do let(:organization) { create(:organization) } describe 'validation' do - it 'has a valid factory' do - expect(build(:organization)).to be_valid - end - it 'is not valid without a name' do should validate_presence_of(:name) end