summaryrefslogtreecommitdiff
path: root/spec/unit/user_spec.rb
blob: 08bde33d7b3405433b86da875bae9a927cea3577 (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
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
#
# Author:: Steven Danna (steve@opscode.com)
# Copyright:: Copyright (c) 2012 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/user'
require 'tempfile'

describe Chef::User do
  before(:each) do
    @user = Chef::User.new
  end

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

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

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

    # It is not feasible to check all invalid characters.  Here are a few
    # that we probably care about.
    it "should not accept invalid characters" do
      # capital letters
      lambda { @user.name "Bar" }.should raise_error(ArgumentError)
      # slashes
      lambda { @user.name "foo/bar" }.should raise_error(ArgumentError)
      # ?
      lambda { @user.name "foo?" }.should raise_error(ArgumentError)
      # &
      lambda { @user.name "foo&" }.should raise_error(ArgumentError)
    end


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

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

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

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

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

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

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

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

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

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

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

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

  describe "when serializing to JSON" do
    before(:each) do
      @user.name("black")
      @user.public_key("crowes")
      @json = @user.to_json
    end

    it "serializes as a JSON object" do
      @json.should match(/^\{.+\}$/)
    end

    it "includes the name value" do
      @json.should include(%q{"name":"black"})
    end

    it "includes the public key value" do
      @json.should include(%{"public_key":"crowes"})
    end

    it "includes the 'admin' flag" do
      @json.should include(%q{"admin":false})
    end

    it "includes the private key when present" do
      @user.private_key("monkeypants")
      @user.to_json.should include(%q{"private_key":"monkeypants"})
    end

    it "does not include the private key if not present" do
      @json.should_not include("private_key")
    end

    it "includes the password if present" do
      @user.password "password"
      @user.to_json.should include(%q{"password":"password"})
    end

    it "does not include the password if not present" do
      @json.should_not include("password")
    end
  end

  describe "when deserializing from JSON" do
    before(:each) do
      user = { "name" => "mr_spinks",
        "public_key" => "turtles",
        "private_key" => "pandas",
        "password" => "password",
        "admin" => true }
      @user = Chef::User.from_json(user.to_json)
    end

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

    it "preserves the name" do
      @user.name.should == "mr_spinks"
    end

    it "preserves the public key" do
      @user.public_key.should == "turtles"
    end

    it "preserves the admin status" do
      @user.admin.should be_true
    end

    it "includes the private key if present" do
      @user.private_key.should == "pandas"
    end

    it "includes the password if present" do
      @user.password.should == "password"
    end

  end

  describe "API Interactions" do
    before (:each) do
      @user = Chef::User.new
      @user.name "foobar"
      @http_client = double("Chef::REST mock")
      Chef::REST.stub(:new).and_return(@http_client)
    end

    describe "list" do
      before(:each) do
        Chef::Config[:chef_server_url] = "http://www.example.com"
        @osc_response = { "admin" => "http://www.example.com/users/admin"}
        @ohc_response = [ { "user" => { "username" => "admin" }} ]
        Chef::User.stub(:load).with("admin").and_return(@user)
        @osc_inflated_response = { "admin" => @user }
      end

      it "lists all clients on an OSC server" do
        @http_client.stub(:get_rest).with("users").and_return(@osc_response)
        Chef::User.list.should == @osc_response
      end

      it "inflate all clients on an OSC server" do
        @http_client.stub(:get_rest).with("users").and_return(@osc_response)
        Chef::User.list(true).should == @osc_inflated_response
      end

      it "lists all clients on an OHC/OPC server" do
        @http_client.stub(:get_rest).with("users").and_return(@ohc_response)
        # We expect that Chef::User.list will give a consistent response
        # so OHC API responses should be transformed to OSC-style output.
        Chef::User.list.should == @osc_response
      end

      it "inflate all clients on an OHC/OPC server" do
        @http_client.stub(:get_rest).with("users").and_return(@ohc_response)
        Chef::User.list(true).should == @osc_inflated_response
      end
    end

    describe "create" do
      it "creates a new user via the API" do
        @user.password "password"
        @http_client.should_receive(:post_rest).with("users", {:name => "foobar", :admin => false, :password => "password"}).and_return({})
        @user.create
      end
    end

    describe "read" do
      it "loads a named user from the API" do
        @http_client.should_receive(:get_rest).with("users/foobar").and_return({"name" => "foobar", "admin" => true, "public_key" => "pubkey"})
        user = Chef::User.load("foobar")
        user.name.should == "foobar"
        user.admin.should == true
        user.public_key.should == "pubkey"
      end
    end

    describe "update" do
      it "updates an existing user on via the API" do
        @http_client.should_receive(:put_rest).with("users/foobar", {:name => "foobar", :admin => false}).and_return({})
        @user.update
      end
    end

    describe "destroy" do
      it "deletes the specified user via the API" do
        @http_client.should_receive(:delete_rest).with("users/foobar")
        @user.destroy
      end
    end
  end
end