Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 411 lines (342 sloc) 10.651 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
require 'spec/spec_helper'

class Article; end
class Comment; end

describe Sinatra::Hat::Maker do
  attr_reader :model, :maker, :request

  describe "initializing" do
    it "takes a klass" do
      proc {
        new_maker(Article)
      }.should_not raise_error
    end
    
    it "takes options" do
      proc {
        new_maker(Article, :only => :index)
      }.should_not raise_error
    end
    
    it "merges options hash with defaults" do
      maker = new_maker(Article, :only => :index)
      maker.options[:only].should == :index
    end
  end
  
  describe "setup" do
    before(:each) do
      stub.instance_of(Sinatra::Hat::Router).generate(:app)
    end
    
    it "stores reference to app" do
      maker = new_maker
      maker.setup(:app)
      maker.app.should == :app
    end
  end
  
  it "has a klass" do
    new_maker(Article).klass.should == Article
  end
  
  describe "protect" do
    before(:each) do
      @maker = new_maker
    end
    
    it "sets the protected actions" do
      maker.protect :index, :show
      maker.protect.should == [:index, :show]
    end
    
    it "can set the protected actions to :all" do
      maker.protect :all
      maker.protect.should == maker.only
    end
    
    it "can set the credentials" do
      maker.protect :username => "admin", :password => "awesome", :realm => "awesome app"
      maker.credentials.should == { :username => "admin", :password => "awesome", :realm => "awesome app" }
    end
  end
  
  describe "handling actions" do
    before(:each) do
      @request = fake_request
      @maker = new_maker
      stub(Sinatra::Hat::Maker.actions[:index])[:fn].returns proc { |passed_request| [self, passed_request] }
    end
    
    it "takes an action and instance_exec's its event handler" do
      mock(Sinatra::Hat::Maker.actions[:index])[:fn].returns proc { |passed_request| [self, passed_request] }
      maker.handle(:index, request).should == [maker, request]
    end
    
    context "when the action is protected" do
      before(:each) do
        maker.protect :index
      end
      
      context "when the user is not authenticated" do
        it "protects the action" do
          proc {
            maker.handle(:index, request)
          }.should throw_symbol(:halt)
        end
      end
      
      context "when the user is authenticated" do
        before(:each) do
          request.env['REMOTE_USER'] = 'hello'
        end
        
        it "allows the request" do
          proc {
            maker.handle(:index, request)
          }.should_not throw_symbol(:halt)
        end
      end
    end
    
    context "when the action is not part of the :only list" do
      before(:each) do
        maker.only :show
      end
      
      it "returns 404" do
        mock.proxy(request).error(404)
        catch(:halt) { maker.handle(:index, request) }
      end
    end

  end
  
  describe "default options" do
    before(:each) do
      @maker = new_maker(Article)
    end
    
    it "has a default options hash" do
      maker.options.should_not be_nil
    end
    
    describe ":only" do
      it "returns all actions" do
        maker.options[:only].should include(:index, :show, :new, :create, :edit, :update, :destroy)
      end
      
      it "is methodized" do
        maker.only.should == maker.options[:only]
      end
      
      it "has methodized setter" do
        maker.only :index, :show
        maker.only.should have(2).entries
        maker.only.should include(:index)
        maker.only.should include(:show)
      end
    end
    
    describe ":formats" do
      it "is an empty hash" do
        maker.options[:formats].should == { }
      end
      
      it "is methodized" do
        maker.formats.should === maker.options[:formats]
      end
    end
    
    describe ":to_param" do
      it "is :id by default" do
        maker.options[:to_param].should == :id
      end
      
      it "is methodized" do
        maker.to_param.should == :id
      end
      
      it "has methodized setter" do
        maker.to_param :permalink
        maker.to_param.should == :permalink
      end
    end
    
    describe ":parent" do
      it "is nil" do
        maker.options[:parent].should be_nil
      end
      
      it "is methodized" do
        maker.parent.should === maker.options[:parent]
      end
    end
    
    describe ':format' do
      it 'is nil by default' do
        maker.options[:format].should be_nil
        maker.options.should have_key(:format)
      end
      
      it 'is methodized' do
        maker.options[:format] = :json
        maker.format.should == :json
      end
      
      it 'has methodized setter' do
        maker.format :json
        maker.format.should == :json
      end
    end
    
    describe ":protect" do
      it "is empty by default" do
        maker.options[:protect].should be_empty
      end
      
      it "is methodized" do
        maker.protect.should === maker.options[:protect]
      end
      
      it "has methodized setter" do
        maker.protect :index, :show
        maker.protect.should == [:index, :show]
      end
    end
    
    describe ":finder" do
      it "finds all for the model" do
        mock(Article).all
        maker.options[:finder][Article, { }]
      end
    end
    
    describe ":authenticator" do
      it "finds all for the model" do
        mock(Article).all
        maker.options[:finder][Article, { }]
      end
      
      it "has a block setter" do
        fn = proc { |u, p| [u, p] }
        maker.authenticator(&fn)
        maker.authenticator[:user, :pass].should == [:user, :pass]
      end
    end
    
    describe ":credentials" do
      it "has username" do
        maker.options[:credentials][:username].should_not be_nil
      end
      
      it "has password" do
        maker.options[:credentials][:password].should_not be_nil
      end
      
      it "has realm" do
        maker.options[:credentials][:realm].should_not be_nil
      end
    end
    
    describe ":record" do
      it "loads a single record" do
        mock(Article).find_by_id(2)
        maker.options[:record][Article, { :id => 2 }]
      end
    end
  end
  
  describe "finder" do
    context "when no block is provided" do
      it "returns the finder option" do
        maker = new_maker
        maker.finder.should == maker.options[:finder]
      end
    end
    
    context "when a block is provided" do
      it "sets the finder option" do
        maker = new_maker
        block = proc { |model, params| model.find_by_id(params[:id]) }
        maker.finder(&block)
        maker.options[:finder].should == block
      end
    end
  end
  
  describe "record" do
    context "when no block is provided" do
      it "returns the record option" do
        maker = new_maker
        maker.record.should == maker.options[:record]
      end
    end
    
    context "when a block is provided" do
      it "sets the record option" do
        maker = new_maker
        block = proc { |model, params| model.find_by_id(params[:id]) }
        maker.record(&block)
        maker.options[:record].should == block
      end
    end
  end
  
  describe "prefix" do
    context "when used as a method" do
      before(:each) do
        @maker = new_maker(Article)
      end

      it "is a getter if called without argument" do
        maker.prefix.should == maker.options[:prefix]
      end

      it "is a setter if called with an argument" do
        maker.prefix 'super/heroes'
        maker.prefix.should == 'super/heroes'
        maker.prefix.should == maker.options[:prefix]
      end
    end

    context "when specified as an option" do
      it "returns the option value" do
        new_maker(Article, :prefix => "posts").prefix.should == "posts"
      end
    end
    
    context "when it's not specified as an option" do
      it "returns the pluralized, downcased klass name" do
        new_maker(Article).prefix.should == "articles"
      end
      
      it "snakecases" do
        stub(klass = Class.new).name { "AwesomePerson" }
        new_maker(klass).prefix.should == 'awesome_people'
      end
    end
  end
  
  describe "parents" do
    context "when there are none" do
      it "is empty" do
        new_maker.parents.should be_empty
      end
    end
    
    context "when there is one" do
      it "includes the parent" do
        parent = new_maker(Article)
        child = new_maker(Comment, :parent => parent)
        child.parents.should == [parent]
      end
    end
    
    context "when there are many" do
      it "includes all ancestors" do
        grand_parent = new_maker(Article)
        parent = new_maker(Article, :parent => grand_parent)
        child = new_maker(Comment, :parent => parent)
        child.parents.should == [grand_parent, parent]
      end
    end
  end
  
  describe "#model" do
    it "returns an instance of Sinatra::Hat::Model" do
      maker = new_maker
      mock.proxy(Sinatra::Hat::Model.new(maker))
      maker.model
    end
  end
  
  describe "#responder" do
    it "returns an instance of Sinatra::Hat::Responder" do
      maker = new_maker
      mock.proxy(Sinatra::Hat::Responder.new(maker))
      maker.responder
    end
  end
  
  describe "generating routes" do
    it "generates routes for maker instance" do
      maker = new_maker
      router = Sinatra::Hat::Router.new(maker)
      mock.proxy(Sinatra::Hat::Router).new(maker) { router }
      mock(router).generate(maker.app)
      maker.generate_routes!
    end
  end
  
  describe "#after" do
    before(:each) do
      @maker = new_maker
    end
    
    it "takes the name of an action" do
      proc {
        maker.after(:create) { }
      }.should_not raise_error
    end
    
    it "passes a new hash mutator to the block" do
      maker.after(:create) { |arg| arg }.should be_kind_of(Sinatra::Hat::HashMutator)
    end
    
    it "lets you alter the default options" do
      maker.after(:create) do |on|
        on.success { :new_default! }
      end
      maker.responder.defaults[:create][:success][].should == :new_default!
    end
    
    describe "when there are nested resources" do
      before(:each) do
        @child_maker = new_maker(Comment)
        @child_maker.parent = @maker
      end
      
      it "mutates the child's response" do
        @child_maker.after(:create) do |on|
          on.success { :child_response }
        end
        
        @child_maker.responder.defaults[:create][:success][].should == :child_response
        @maker.responder.defaults[:create][:success].should_not === @child_maker.responder.defaults[:create][:success]
      end
    end
  end
end
Something went wrong with that request. Please try again.