Class: CheckAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_check_auth/check_auth.rb,
lib/rspec_check_auth/check_auth/output.rb,
lib/rspec_check_auth/check_auth/request.rb

Defined Under Namespace

Classes: Output, Request

Constant Summary collapse

@@block =

Handles checking

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCheckAuth

Returns a new instance of CheckAuth.



3
4
5
# File 'lib/rspec_check_auth/check_auth.rb', line 3

def initialize
  @store = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



82
83
84
# File 'lib/rspec_check_auth/check_auth.rb', line 82

def method_missing method, *args
  add(method, *args)
end

Class Method Details

.checking_block(format = nil, &block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/rspec_check_auth/check_auth.rb', line 16

def self.checking_block format=nil, &block
  if block
    @@block = block
  else
    raise "CheckAuth#checking_block needs defining" unless @@block
    @@block.call(format)
  end
end

.for(&block) ⇒ Object

The main method



8
9
10
11
12
# File 'lib/rspec_check_auth/check_auth.rb', line 8

def self.for &block
  instance = self.new
  block.call(instance)
  instance.output
end

Instance Method Details

#add(action, params = {}) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/rspec_check_auth/check_auth.rb', line 25

def add action, params={}
  params[:format] = [:html, :xml] unless params.has_key?(:format)
  params[:format].arrayize.each do |format|
    @store[format] ||= []
    r = Request.new(action, format, params.except(:format))
    @store[format] << r
  end
end

#outputObject



76
77
78
79
80
# File 'lib/rspec_check_auth/check_auth.rb', line 76

def output
  @store.map do |format, requests|
    Output.generate_steps_for format, requests
  end.join("\n\n")
end

#resource_actions(params = {}) ⇒ Object

Tests all the standard methods a resources route has. Any params given (other than :except) are passed to each action as params, and actions that require an id (edit, show, update) get passed “some_id”

Accepts:

:only => [:action, :names]
:except => [:action, :names]

Either param can be a single action, or an array of actions. :only takes precedence over :except.



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
# File 'lib/rspec_check_auth/check_auth.rb', line 45

def resource_actions params={}
  except = params.delete(:except).arrayize.compact
  only = params.delete(:only).arrayize.compact

  params[:format] = params.has_key?(:format) ? params[:format].arrayize : [:html,:xml]

  # Logic to see if we should add an action
  # It should either be in :only, or :except isn't empty and it's not in :except
  should_add = lambda do |action|
    # Only isn't empty and our action is contained within
    if !only.empty?
      break only.include?(action)
    end
    # Except isn't empty and our action isn't contained therein
    if !except.empty?
      break !except.include?(action)
    end
    # Just add it
    true
  end

  # Add each action, if we should
  add :index, params                             if should_add[:index]
  add :new, params                               if should_add[:new]
  add :create, params                            if should_add[:create]
  add :edit, {:id => "some_id"}.merge(params)    if should_add[:edit]
  add :show, {:id => "some_id"}.merge(params)    if should_add[:show]
  add :update, {:id => "some_id"}.merge(params)  if should_add[:update]
  add :destroy, {:id => "some_id"}.merge(params) if should_add[:destroy]
end