OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/spec/controllers/api/v2/timelines_controller_spec.rb

204 lines
5.6 KiB

#-- copyright
# OpenProject is a project management system.
#
# Copyright (C) 2012-2013 the OpenProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require File.expand_path('../../../../spec_helper', __FILE__)
describe Api::V2::TimelinesController do
# ===========================================================
# Helpers
def self.become_admin
let(:current_user) { FactoryGirl.create(:admin) }
end
def self.become_non_member
let(:current_user) { FactoryGirl.create(:user) }
before do
current_user.memberships.select {|m| m.project_id == project.id}.each(&:destroy)
end
end
def self.become_member_with_all_permissions
let(:current_user) { FactoryGirl.create(:user) }
before do
role = FactoryGirl.create(:role, :permissions => [:view_timelines, :edit_timelines, :delete_timelines])
member = FactoryGirl.build(:member, :user => current_user, :project => project)
member.roles = [role]
member.save!
end
end
def self.become_member_with_view_permissions
let(:current_user) { FactoryGirl.create(:user) }
before do
role = FactoryGirl.create(:role, :permissions => [:view_timelines])
member = FactoryGirl.build(:member, :user => current_user, :project => project)
member.roles = [role]
member.save!
end
end
def self.become_member_with_edit_permissions
let(:current_user) { FactoryGirl.create(:user) }
before do
role = FactoryGirl.create(:role, :permissions => [:edit_timelines])
member = FactoryGirl.build(:member, :user => current_user, :project => project)
member.roles = [role]
member.save!
end
end
def self.become_member_with_delete_permissions
let(:current_user) { FactoryGirl.create(:user) }
before do
role = FactoryGirl.create(:role, :permissions => [:delete_timelines])
member = FactoryGirl.build(:member, :user => current_user, :project => project)
member.roles = [role]
member.save!
end
end
before do
User.stub(:current).and_return current_user
end
shared_examples_for 'all actions related to all timelines within a project' do
describe 'w/o a given project' do
become_admin
it 'renders a 404 Not Found page' do
fetch
response.response_code.should == 404
end
end
describe 'w/ an unknown project' do
become_admin
it 'renders a 404 Not Found page' do
fetch :project_id => '4711'
response.response_code.should == 404
end
end
describe 'w/ a known project' do
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
fetch :project_id => project.identifier
response.response_code.should == 403
end
end
end
end
shared_examples_for 'all actions related to an existing timeline' do
become_admin
describe 'w/o a valid timelines id' do
describe 'w/o a given project' do
it 'renders a 404 Not Found page' do
fetch :id => '4711'
response.response_code.should == 404
end
end
describe 'w/ an unknown project' do
it 'renders a 404 Not Found page' do
fetch :project_id => '4711', :id => '1337'
response.response_code.should == 404
end
end
describe 'w/ a known project' do
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
fetch :project_id => project.id, :id => '1337'
response.response_code.should === 403
end
end
describe 'w/ the current user being a member' do
become_member_with_all_permissions
it 'raises ActiveRecord::RecordNotFound errors' do
lambda do
fetch :project_id => project.id, :id => '1337'
end.should raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
describe 'w/ a valid timelines id' do
let(:project) { FactoryGirl.create(:project, :identifier => 'test_project') }
let(:timeline) { FactoryGirl.create(:timelines, :project_id => project.id, :name => 'b') }
describe 'w/o a given project' do
it 'renders a 404 Not Found page' do
fetch :id => timeline.id
response.response_code.should == 404
end
end
describe 'w/ a different project' do
let(:other_project) { FactoryGirl.create(:project, :identifier => 'other') }
it 'raises ActiveRecord::RecordNotFound errors' do
lambda do
fetch :project_id => other_project.identifier,:id => timeline.id
end.should raise_error(ActiveRecord::RecordNotFound)
end
end
describe 'w/ a proper project' do
describe 'w/o being a member or administrator' do
become_non_member
it 'renders a 403 Forbidden page' do
fetch :project_id => project.id, :id => timeline.id
response.response_code.should == 403
end
end
describe 'w/ the current user being a member' do
become_member_with_all_permissions
it 'assigns the timeline' do
fetch :project_id => project.id, :id => timeline.id
assigns(:timeline).should == timeline
end
end
end
end
end
end