summaryrefslogtreecommitdiff
path: root/chef-server-api/spec/spec_helper.rb
blob: c94435ca625566744a75683d5e41ce6790b6a114 (plain)
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
#
# Author:: Tim Hinderliter (<tim@opscode.com>)
# Copyright:: Copyright (c) 2011 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "rubygems"
require "merb-core"
require "rspec"
require "chef/config"
require "tmpdir"
require "fileutils"

# Avoid system-wide directories we may not be able to access
fake_var_chef = Dir.mktmpdir("fake-var-chef")
at_exit { FileUtils.remove_entry_secure(fake_var_chef) }
Chef::Config[:file_cache_path]      = "#{fake_var_chef}/cache"
Chef::Config[:cache_options][:path] = "#{fake_var_chef}/cache/checksums"
Chef::Config[:checksum_path]        = "#{fake_var_chef}/checksums"

Merb.push_path(:spec_helpers, "spec" / "spec_helpers", "**/*.rb")
Merb.push_path(:spec_fixtures, "spec" / "fixtures", "**/*.rb")

$:.unshift(File.expand_path('../../app/', __FILE__))

Merb.start_environment(:testing => true, :adapter => 'runner', :environment => ENV['MERB_ENV'] || 'test')

RSpec.configure do |config|
  config.include(Merb::Test::RouteHelper)
  config.include(Merb::Test::ControllerHelper)
end

def get_json(path, params = {}, env = {}, &block)
  request_json("GET", path, params, env, &block)
end

def post_json(path, post_body, env = {}, &block)
  request_json("POST", path, {}, env) do |controller|
    # Merb FakeRequest allows me no way to pass JSON across the
    # FakeRequest/StringIO boundary, so we hack it here.
    if post_body.is_a?(Hash)
      controller.params.merge!(post_body)
    else
      controller.params['inflated_object'] = post_body
    end
    block.call if block
  end
end

def put_json(path, put_body, env = {}, &block)
  request_json("PUT", path, {}, env) do |controller|
    # Merb FakeRequest allows me no way to pass JSON across the
    # FakeRequest/StringIO boundary, so we hack it here.
    if put_body.is_a?(Hash)
      controller.params.merge!(put_body)
    else
      controller.params['inflated_object'] = put_body
    end
    block.call if block
  end
end

# Make an HTTP call of <method>, assign the accept header to
# application/json, and return the JSON-parsed output.
#
# Side effects:
#  Raw textual output available in @response_raw
#  Controller used available in @controller
def request_json(method, path, params, env, &block)
  @controller = mock_request(path, params, env.merge({'HTTP_ACCEPT' => "application/json", :request_method => method})) do |controller|
    stub_authentication(controller)
    block.call(controller) if block
  end

  @response_raw = @controller.body
  @response_json = Chef::JSONCompat.from_json(@response_raw)
end

def stub_authentication(controller,user=nil)
  username = "tester"

  unless user
    user = Chef::ApiClient.new
    user.name(username)
    user.admin(true)
  end

  # authenticate_every has a side-effect of setting @auth_user
  controller.stub!(:authenticate_every).and_return(true)
  controller.instance_variable_set(:@auth_user, user)
end

def root_url
  # include organization name to run these tests in the platform
  "http://localhost"
end