summaryrefslogtreecommitdiff
path: root/spec/unit/api_client_spec.rb
blob: 3c4cccf5033808867914595f54ef548577ee4b00 (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
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
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Copyright:: Copyright (c) 2008 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 'spec_helper'

require 'chef/api_client'
require 'tempfile'

describe Chef::ApiClient do
  before(:each) do
    @client = Chef::ApiClient.new
  end

  describe "initialize" do
    it "should be a Chef::ApiClient" do
      @client.should be_a_kind_of(Chef::ApiClient)
    end
  end

  describe "name" do
    it "should let you set the name to a string" do
      @client.name("ops_master").should == "ops_master"
    end

    it "should return the current name" do
      @client.name "ops_master"
      @client.name.should == "ops_master"
    end

    it "should not accept spaces" do
      lambda { @client.name "ops master" }.should raise_error(ArgumentError)
    end

    it "should throw an ArgumentError if you feed it anything but a string" do
      lambda { @client.name Hash.new }.should raise_error(ArgumentError)
    end
  end

  describe "admin" do
    it "should let you set the admin bit" do
      @client.admin(true).should == true
    end

    it "should return the current admin value" do
      @client.admin true
      @client.admin.should == true
    end

    it "should default to false" do
      @client.admin.should == false
    end

    it "should throw an ArgumentError if you feed it anything but true or false" do
      lambda { @client.name Hash.new }.should raise_error(ArgumentError)
    end
  end

  describe "public_key" do
    it "should let you set the public key" do
      @client.public_key("super public").should == "super public"
    end

    it "should return the current public key" do
      @client.public_key("super public")
      @client.public_key.should == "super public"
    end

    it "should throw an ArgumentError if you feed it something lame" do
      lambda { @client.public_key Hash.new }.should raise_error(ArgumentError)
    end
  end

  describe "private_key" do
    it "should let you set the private key" do
      @client.private_key("super private").should == "super private"
    end

    it "should return the private key" do
      @client.private_key("super private")
      @client.private_key.should == "super private"
    end

    it "should throw an ArgumentError if you feed it something lame" do
      lambda { @client.private_key Hash.new }.should raise_error(ArgumentError)
    end
  end

  describe "serialize" do
    before(:each) do
      @client.name("black")
      @client.public_key("crowes")
      @client.private_key("monkeypants")
      @serial = @client.to_json
    end

    it "should serialize to a json hash" do
      @client.to_json.should match(/^\{.+\}$/)
    end

    %w{
      name
      public_key
    }.each do |t|
      it "should include '#{t}'" do
        @serial.should =~ /"#{t}":"#{@client.send(t.to_sym)}"/
      end
    end

    it "should include 'admin'" do
      @serial.should =~ /"admin":false/
    end

    it "should not include the private key" do
      @serial.should_not =~ /"private_key":/
    end
  end

  describe "deserialize" do
    before(:each) do
      @client.name("black")
      @client.public_key("crowes")
      @client.private_key("monkeypants")
      @client.admin(true)
      @deserial = Chef::JSONCompat.from_json(@client.to_json)
    end

    it "should deserialize to a Chef::ApiClient object" do
      @deserial.should be_a_kind_of(Chef::ApiClient)
    end

    %w{
      name
      public_key
      admin
    }.each do |t|
      it "should match '#{t}'" do
        @deserial.send(t.to_sym).should == @client.send(t.to_sym)
      end
    end

    it "should not include the private key" do
      @deserial.private_key.should == nil
    end

  end

  describe "when requesting a new key" do
    before do
      @http_client = mock("Chef::REST mock")
      Chef::REST.stub!(:new).and_return(@http_client)
    end

    context "and the client does not exist on the server" do
      before do
        @a_404_response = Net::HTTPNotFound.new("404 not found and such", nil, nil)
        @a_404_exception = Net::HTTPServerException.new("404 not found exception", @a_404_response)

        @http_client.should_receive(:get).with("clients/lost-my-key").and_raise(@a_404_exception)
      end

      it "raises a 404 error" do
        lambda { Chef::ApiClient.reregister("lost-my-key") }.should raise_error(Net::HTTPServerException)
      end
    end

    context "and the client exists" do
      before do
        @api_client_without_key = Chef::ApiClient.new
        @api_client_without_key.name("lost-my-key")
        @http_client.should_receive(:get).with("clients/lost-my-key").and_return(@api_client_without_key)
      end


      context "and the client exists on a Chef 11-like server" do
        before do
          @api_client_with_key = Chef::ApiClient.new
          @api_client_with_key.name("lost-my-key")
          @api_client_with_key.private_key("the new private key")
          @http_client.should_receive(:put).
            with("clients/lost-my-key", :name => "lost-my-key", :admin => false, :private_key => true).
            and_return(@api_client_with_key)
        end

        it "returns an ApiClient with a private key" do
          response = Chef::ApiClient.reregister("lost-my-key")
          # no sane == method for ApiClient :'(
          response.should == @api_client_without_key
          response.private_key.should == "the new private key"
          response.name.should == "lost-my-key"
          response.admin.should be_false
        end
      end

      context "and the client exists on a Chef 10-like server" do
        before do
          @api_client_with_key = {"name" => "lost-my-key", "private_key" => "the new private key"}
          @http_client.should_receive(:put).
            with("clients/lost-my-key", :name => "lost-my-key", :admin => false, :private_key => true).
            and_return(@api_client_with_key)
        end

        it "returns an ApiClient with a private key" do
          response = Chef::ApiClient.reregister("lost-my-key")
          # no sane == method for ApiClient :'(
          response.should == @api_client_without_key
          response.private_key.should == "the new private key"
          response.name.should == "lost-my-key"
          response.admin.should be_false
        end
      end

    end
  end
end