<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -13,19 +13,17 @@ class ProjectsController &lt; ApplicationController
         format.xml  { head :found, :location =&gt; project_path(@projects.first, :format =&gt; 'xml') }              
       else
         format.html
-        format.rss  { render_index_rss }
+        format.rss  { index_rss }
         format.xml  { render :xml =&gt; @projects }              
       end    
     end    
   end
   
   def show
-    @project = @projects.find! params[:id]
-  
     respond_to do |format|
-      format.html { redirect_to @project.path_to_first_menu_item }
-      format.rss  { render_show_rss(@project) }
-      format.xml  { render :xml =&gt; @project }
+      format.html { show_html } 
+      format.rss  { find_project!; show_rss(@project) }
+      format.xml  { find_project!; render :xml =&gt; @project }
     end
   end
   
@@ -38,6 +36,10 @@ class ProjectsController &lt; ApplicationController
       end
     end
 
+    def find_project!
+      @project = @projects.find! params[:id]
+    end
+
     def project_has_no_accessible_menu_items?(project)
       project.enabled_menu_items.find do |item|
         path = item.path(self, project)
@@ -51,7 +53,7 @@ class ProjectsController &lt; ApplicationController
       end.nil?
     end
 
-    def render_index_rss
+    def index_rss
       @records = User.current.projects.active.inject([]) do |result, project|                
         find_feedable_records(project).each do |record|
           result &lt;&lt; [record, project]
@@ -68,7 +70,22 @@ class ProjectsController &lt; ApplicationController
       end
     end
 
-    def render_show_rss(project)
+    def show_html
+      @project = @projects.find params[:id]
+      
+      if @project      
+        redirect_to @project.path_to_first_menu_item
+      elsif @projects.any?
+        redirect_to(projects_path)
+      else        
+        # Fail with 404 if the project genuinely doesn't exist
+        Project.active.find params[:id]
+        # Fail authorization otherwise
+        failed_authorization!  
+      end
+    end
+    
+    def show_rss(project)
       render_rss project.name, 
         _('All news for {{project}}', :project =&gt; project.name), 
         project_url(project) do |items|</diff>
      <filename>app/controllers/projects_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -183,8 +183,8 @@ module Retrospectiva
           def failed_authorization!
             project = Project.current ? Project.current.name : 'nil'
             user = User.current ? User.current.name : 'nil'
-            permissions = self.class.require_permissions[action_name]
-            
+            permissions = self.class.authorized_controller? ? self.class.require_permissions[action_name] : nil 
+
             raise Retrospectiva::AccessManager::NoAuthorizationError, 
               &quot;No authorization for #{self.class.name}/#{action_name} - params: #{params.except(:controller, :action).inspect}, user: #{user}, project: #{project}, permissions: #{permissions.inspect}&quot;
           end</diff>
      <filename>lib/retrospectiva/access_manager/secure_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -90,7 +90,7 @@ describe 'real-world behaviour' do
     
   before do
     @user = mock_current_user! :name =&gt; 'Public', :public? =&gt; true, :admin? =&gt; false, :permitted? =&gt; false
-    @project = mock_model Project, 
+    @project = stub_model Project,
       :name =&gt; 'Retro', :short_name =&gt; 'retro',
       :enabled_modules =&gt; ['milestones']
 </diff>
      <filename>spec/controllers/project_area_controller_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -3,7 +3,7 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
 describe ProjectsController do
 
   before do 
-    @user = mock_current_user!
+    @user = mock_current_user! :name =&gt; 'Someone'
   end
   
   describe 'loading active projects' do
@@ -237,10 +237,9 @@ describe ProjectsController do
       controller.stub!(:find_feedable_records).and_return([@changeset, @ticket])
       controller.stub!(:project_has_no_accessible_menu_items?).and_return(false)
       
-      @projects = [mock_model(Project, :to_param =&gt; 'retro', :name =&gt; 'Retro', :path_to_first_menu_item =&gt; '/projects/retro/changesets')]
-      @projects.stub!(:active).and_return(@projects)      
-      @user.should_receive(:projects).and_return(@projects)
-      @projects.stub!(:find!).and_return(@projects.first)      
+      @project  = mock_model Project, :to_param =&gt; 'retro', :name =&gt; 'Retro', :path_to_first_menu_item =&gt; '/projects/retro/changesets', :active? =&gt; true
+      @projects = AssociationProxies::UserProjects.new([@project])
+      @user.stub!(:projects).and_return(@projects)
     end
 
     def do_get(project_name, format = 'html')
@@ -248,20 +247,53 @@ describe ProjectsController do
     end
     
     describe 'for HTML requests' do
-           
+                 
       it 'should redirect to the first available menu item' do
-        @projects.should_receive(:find!).with('retro').and_return(@projects.first)      
         do_get 'retro'
         response.should be_redirect
-        response.should redirect_to(project_changesets_path(@projects.first))        
+        response.should redirect_to(project_changesets_path(@project))        
       end
 
+      describe 'if project is not accessible and other projects are present' do
+        
+        it 'should redirect to the projects overview page' do
+          do_get 'other'
+          response.should be_redirect
+          response.should redirect_to(projects_path)        
+        end
+      
+      end
+
+      describe 'if project is not accessible and no other projects are present' do
+        before do
+          @projects = AssociationProxies::UserProjects.new
+          @user.stub!(:projects).and_return(@projects)         
+        end
+
+        describe 'but project is does actually exist (just the user has no permission to see it)' do
+                    
+          it 'should fail authorization' do
+            Project.should_receive(:find).with('other').and_return(stub_model(Project))
+            lambda { do_get 'other' }.should raise_error(Retrospectiva::AccessManager::NoAuthorizationError)
+          end
+        
+        end
+
+        describe 'and project is genuinely doesn\'t exist' do
+                    
+          it 'should fail with 404' do            
+            lambda { do_get 'other' }.should raise_error(ActiveRecord::RecordNotFound)
+          end
+        
+        end
+                
+      end
     end
 
     describe 'for RSS requests' do
 
       it 'should load the feedable records' do
-        controller.should_receive(:find_feedable_records).with(@projects.first).and_return([@changeset, @ticket])
+        controller.should_receive(:find_feedable_records).with(@project).and_return([@changeset, @ticket])
         do_get  'retro', 'rss'
       end
   </diff>
      <filename>spec/controllers/projects_controller_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -20,7 +20,8 @@ describe Notifications do
         :domain =&gt; mock('Setting::Domain', :name =&gt; 'domain', :value =&gt; 'mydomain.com'),
         :authentication =&gt; mock('Setting::AuthType', :name =&gt; 'authentication', :value =&gt; 'plain'),
         :user_name =&gt; mock('Setting::User', :name =&gt; 'user_name', :value =&gt; 'me'),
-        :password =&gt; mock('Setting::Pass', :name =&gt; 'password', :value =&gt; 'secret')
+        :password =&gt; mock('Setting::Pass', :name =&gt; 'password', :value =&gt; 'secret'),
+        :enable_starttls_auto =&gt; mock('Setting::AutoTLS', :name =&gt; 'enable_starttls_auto', :value =&gt; true)
       }
     end
     </diff>
      <filename>spec/models/notifications_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -9,7 +9,7 @@ module Spec::TypicalMilestonesControllerSetup
       @milestones.stub!(:count)    
       @milestones.stub!(:maximum)    
 
-      @project = mock_model(Project, :name =&gt; 'Retro', :short_name =&gt; 'retro', :milestones =&gt; @milestones)
+      @project = stub_model(Project, :name =&gt; 'Retro', :short_name =&gt; 'retro', :milestones =&gt; @milestones)
       @projects = [@project]    
       @projects.stub!(:active).and_return(@projects)
       @projects.stub!(:find).and_return(@project)</diff>
      <filename>spec/support/helpers/typical_milestones_controller_setup.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>8f5cee4513d7f48a6151daa33363399b30178e6e</id>
    </parent>
  </parents>
  <author>
    <name>Dimitrij Denissenko</name>
    <email>contact@dvisionfactory.com</email>
  </author>
  <url>http://github.com/dim/retrospectiva/commit/55924800f09052516ab8f499faa3fe676d853671</url>
  <id>55924800f09052516ab8f499faa3fe676d853671</id>
  <committed-date>2009-10-23T00:34:25-07:00</committed-date>
  <authored-date>2009-10-23T00:34:25-07:00</authored-date>
  <message>Be more flexible on project selection [#692] / Fixed specs</message>
  <tree>215d648c97883bcc7c5e2282f9a113e736269e3c</tree>
  <committer>
    <name>Dimitrij Denissenko</name>
    <email>contact@dvisionfactory.com</email>
  </committer>
</commit>
