diff options
Diffstat (limited to 'spec/unit')
-rw-r--r-- | spec/unit/user_spec.rb | 275 | ||||
-rw-r--r-- | spec/unit/user_v0_spec.rb | 275 | ||||
-rw-r--r-- | spec/unit/user_v1_spec.rb | 54 |
3 files changed, 301 insertions, 303 deletions
diff --git a/spec/unit/user_spec.rb b/spec/unit/user_spec.rb index a1806db987..e69de29bb2 100644 --- a/spec/unit/user_spec.rb +++ b/spec/unit/user_spec.rb @@ -1,275 +0,0 @@ -# -# Author:: Steven Danna (steve@chef.io) -# Copyright:: Copyright 2012-2016, Chef Software 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. -# - -# DEPRECATION NOTE -# This code only remains to support users still operating with -# Open Source Chef Server 11 and should be removed once support -# for OSC 11 ends. New development should occur in user_spec.rb. - -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 - expect(@user).to be_a_kind_of(Chef::User) - end - end - - describe "name" do - it "should let you set the name to a string" do - expect(@user.name("ops_master")).to eq("ops_master") - end - - it "should return the current name" do - @user.name "ops_master" - expect(@user.name).to eq("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 - expect { @user.name "Bar" }.to raise_error(ArgumentError) - # slashes - expect { @user.name "foo/bar" }.to raise_error(ArgumentError) - # ? - expect { @user.name "foo?" }.to raise_error(ArgumentError) - # & - expect { @user.name "foo&" }.to raise_error(ArgumentError) - end - - it "should not accept spaces" do - expect { @user.name "ops master" }.to raise_error(ArgumentError) - end - - it "should throw an ArgumentError if you feed it anything but a string" do - expect { @user.name Hash.new }.to raise_error(ArgumentError) - end - end - - describe "admin" do - it "should let you set the admin bit" do - expect(@user.admin(true)).to eq(true) - end - - it "should return the current admin value" do - @user.admin true - expect(@user.admin).to eq(true) - end - - it "should default to false" do - expect(@user.admin).to eq(false) - end - - it "should throw an ArgumentError if you feed it anything but true or false" do - expect { @user.name Hash.new }.to raise_error(ArgumentError) - end - end - - describe "public_key" do - it "should let you set the public key" do - expect(@user.public_key("super public")).to eq("super public") - end - - it "should return the current public key" do - @user.public_key("super public") - expect(@user.public_key).to eq("super public") - end - - it "should throw an ArgumentError if you feed it something lame" do - expect { @user.public_key Hash.new }.to raise_error(ArgumentError) - end - end - - describe "private_key" do - it "should let you set the private key" do - expect(@user.private_key("super private")).to eq("super private") - end - - it "should return the private key" do - @user.private_key("super private") - expect(@user.private_key).to eq("super private") - end - - it "should throw an ArgumentError if you feed it something lame" do - expect { @user.private_key Hash.new }.to 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 - expect(@json).to match(/^\{.+\}$/) - end - - it "includes the name value" do - expect(@json).to include(%q{"name":"black"}) - end - - it "includes the public key value" do - expect(@json).to include(%{"public_key":"crowes"}) - end - - it "includes the 'admin' flag" do - expect(@json).to include(%q{"admin":false}) - end - - it "includes the private key when present" do - @user.private_key("monkeypants") - expect(@user.to_json).to include(%q{"private_key":"monkeypants"}) - end - - it "does not include the private key if not present" do - expect(@json).not_to include("private_key") - end - - it "includes the password if present" do - @user.password "password" - expect(@user.to_json).to include(%q{"password":"password"}) - end - - it "does not include the password if not present" do - expect(@json).not_to include("password") - end - - include_examples "to_json equivalent to Chef::JSONCompat.to_json" do - let(:jsonable) { @user } - 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(Chef::JSONCompat.to_json(user)) - end - - it "should deserialize to a Chef::User object" do - expect(@user).to be_a_kind_of(Chef::User) - end - - it "preserves the name" do - expect(@user.name).to eq("mr_spinks") - end - - it "preserves the public key" do - expect(@user.public_key).to eq("turtles") - end - - it "preserves the admin status" do - expect(@user.admin).to be_truthy - end - - it "includes the private key if present" do - expect(@user.private_key).to eq("pandas") - end - - it "includes the password if present" do - expect(@user.password).to eq("password") - end - - end - - describe "API Interactions" do - before (:each) do - @user = Chef::User.new - @user.name "foobar" - @http_client = double("Chef::ServerAPI mock") - allow(Chef::ServerAPI).to receive(: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" } } ] - allow(Chef::User).to receive(:load).with("admin").and_return(@user) - @osc_inflated_response = { "admin" => @user } - end - - it "lists all clients on an OSC server" do - allow(@http_client).to receive(:get).with("users").and_return(@osc_response) - expect(Chef::User.list).to eq(@osc_response) - end - - it "inflate all clients on an OSC server" do - allow(@http_client).to receive(:get).with("users").and_return(@osc_response) - expect(Chef::User.list(true)).to eq(@osc_inflated_response) - end - - it "lists all clients on an OHC/OPC server" do - allow(@http_client).to receive(:get).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. - expect(Chef::User.list).to eq(@osc_response) - end - - it "inflate all clients on an OHC/OPC server" do - allow(@http_client).to receive(:get).with("users").and_return(@ohc_response) - expect(Chef::User.list(true)).to eq(@osc_inflated_response) - end - end - - describe "create" do - it "creates a new user via the API" do - @user.password "password" - expect(@http_client).to receive(:post).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 - expect(@http_client).to receive(:get).with("users/foobar").and_return({ "name" => "foobar", "admin" => true, "public_key" => "pubkey" }) - user = Chef::User.load("foobar") - expect(user.name).to eq("foobar") - expect(user.admin).to eq(true) - expect(user.public_key).to eq("pubkey") - end - end - - describe "update" do - it "updates an existing user on via the API" do - expect(@http_client).to receive(:put).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 - expect(@http_client).to receive(:delete).with("users/foobar") - @user.destroy - end - end - end -end diff --git a/spec/unit/user_v0_spec.rb b/spec/unit/user_v0_spec.rb new file mode 100644 index 0000000000..1bc3bbb571 --- /dev/null +++ b/spec/unit/user_v0_spec.rb @@ -0,0 +1,275 @@ +# +# Author:: Steven Danna (steve@chef.io) +# Copyright:: Copyright 2012-2016, Chef Software 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. +# + +# DEPRECATION NOTE +# This code only remains to support users still operating with +# Open Source Chef Server 11 and should be removed once support +# for OSC 11 ends. New development should occur in user_spec.rb. + +require "spec_helper" + +require "chef/user/v0" +require "tempfile" + +describe Chef::User::V0 do + before(:each) do + @user = Chef::User::V0.new + end + + describe "initialize" do + it "should be a Chef::User::V0" do + expect(@user).to be_a_kind_of(Chef::User::V0) + end + end + + describe "name" do + it "should let you set the name to a string" do + expect(@user.name("ops_master")).to eq("ops_master") + end + + it "should return the current name" do + @user.name "ops_master" + expect(@user.name).to eq("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 + expect { @user.name "Bar" }.to raise_error(ArgumentError) + # slashes + expect { @user.name "foo/bar" }.to raise_error(ArgumentError) + # ? + expect { @user.name "foo?" }.to raise_error(ArgumentError) + # & + expect { @user.name "foo&" }.to raise_error(ArgumentError) + end + + it "should not accept spaces" do + expect { @user.name "ops master" }.to raise_error(ArgumentError) + end + + it "should throw an ArgumentError if you feed it anything but a string" do + expect { @user.name Hash.new }.to raise_error(ArgumentError) + end + end + + describe "admin" do + it "should let you set the admin bit" do + expect(@user.admin(true)).to eq(true) + end + + it "should return the current admin value" do + @user.admin true + expect(@user.admin).to eq(true) + end + + it "should default to false" do + expect(@user.admin).to eq(false) + end + + it "should throw an ArgumentError if you feed it anything but true or false" do + expect { @user.name Hash.new }.to raise_error(ArgumentError) + end + end + + describe "public_key" do + it "should let you set the public key" do + expect(@user.public_key("super public")).to eq("super public") + end + + it "should return the current public key" do + @user.public_key("super public") + expect(@user.public_key).to eq("super public") + end + + it "should throw an ArgumentError if you feed it something lame" do + expect { @user.public_key Hash.new }.to raise_error(ArgumentError) + end + end + + describe "private_key" do + it "should let you set the private key" do + expect(@user.private_key("super private")).to eq("super private") + end + + it "should return the private key" do + @user.private_key("super private") + expect(@user.private_key).to eq("super private") + end + + it "should throw an ArgumentError if you feed it something lame" do + expect { @user.private_key Hash.new }.to 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 + expect(@json).to match(/^\{.+\}$/) + end + + it "includes the name value" do + expect(@json).to include(%q{"name":"black"}) + end + + it "includes the public key value" do + expect(@json).to include(%{"public_key":"crowes"}) + end + + it "includes the 'admin' flag" do + expect(@json).to include(%q{"admin":false}) + end + + it "includes the private key when present" do + @user.private_key("monkeypants") + expect(@user.to_json).to include(%q{"private_key":"monkeypants"}) + end + + it "does not include the private key if not present" do + expect(@json).not_to include("private_key") + end + + it "includes the password if present" do + @user.password "password" + expect(@user.to_json).to include(%q{"password":"password"}) + end + + it "does not include the password if not present" do + expect(@json).not_to include("password") + end + + include_examples "to_json equivalent to Chef::JSONCompat.to_json" do + let(:jsonable) { @user } + 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::V0.from_json(Chef::JSONCompat.to_json(user)) + end + + it "should deserialize to a Chef::User::V0 object" do + expect(@user).to be_a_kind_of(Chef::User::V0) + end + + it "preserves the name" do + expect(@user.name).to eq("mr_spinks") + end + + it "preserves the public key" do + expect(@user.public_key).to eq("turtles") + end + + it "preserves the admin status" do + expect(@user.admin).to be_truthy + end + + it "includes the private key if present" do + expect(@user.private_key).to eq("pandas") + end + + it "includes the password if present" do + expect(@user.password).to eq("password") + end + + end + + describe "API Interactions" do + before (:each) do + @user = Chef::User::V0.new + @user.name "foobar" + @http_client = double("Chef::ServerAPI mock") + allow(Chef::ServerAPI).to receive(: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" } } ] + allow(Chef::User::V0).to receive(:load).with("admin").and_return(@user) + @osc_inflated_response = { "admin" => @user } + end + + it "lists all clients on an OSC server" do + allow(@http_client).to receive(:get).with("users").and_return(@osc_response) + expect(Chef::User::V0.list).to eq(@osc_response) + end + + it "inflate all clients on an OSC server" do + allow(@http_client).to receive(:get).with("users").and_return(@osc_response) + expect(Chef::User::V0.list(true)).to eq(@osc_inflated_response) + end + + it "lists all clients on an OHC/OPC server" do + allow(@http_client).to receive(:get).with("users").and_return(@ohc_response) + # We expect that Chef::User::V0.list will give a consistent response + # so OHC API responses should be transformed to OSC-style output. + expect(Chef::User::V0.list).to eq(@osc_response) + end + + it "inflate all clients on an OHC/OPC server" do + allow(@http_client).to receive(:get).with("users").and_return(@ohc_response) + expect(Chef::User::V0.list(true)).to eq(@osc_inflated_response) + end + end + + describe "create" do + it "creates a new user via the API" do + @user.password "password" + expect(@http_client).to receive(:post).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 + expect(@http_client).to receive(:get).with("users/foobar").and_return({ "name" => "foobar", "admin" => true, "public_key" => "pubkey" }) + user = Chef::User::V0.load("foobar") + expect(user.name).to eq("foobar") + expect(user.admin).to eq(true) + expect(user.public_key).to eq("pubkey") + end + end + + describe "update" do + it "updates an existing user on via the API" do + expect(@http_client).to receive(:put).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 + expect(@http_client).to receive(:delete).with("users/foobar") + @user.destroy + end + end + end +end diff --git a/spec/unit/user_v1_spec.rb b/spec/unit/user_v1_spec.rb index 16f0d0158b..0d2afe8db2 100644 --- a/spec/unit/user_v1_spec.rb +++ b/spec/unit/user_v1_spec.rb @@ -18,12 +18,12 @@ require "spec_helper" -require "chef/user_v1" +require "chef/user/v1" require "tempfile" -describe Chef::UserV1 do +describe Chef::User::V1 do before(:each) do - @user = Chef::UserV1.new + @user = Chef::User::V1.new end shared_examples_for "string fields with no contraints" do @@ -62,8 +62,8 @@ describe Chef::UserV1 do end describe "initialize" do - it "should be a Chef::UserV1" do - expect(@user).to be_a_kind_of(Chef::UserV1) + it "should be a Chef::User::V1" do + expect(@user).to be_a_kind_of(Chef::User::V1) end end @@ -262,11 +262,11 @@ describe Chef::UserV1 do "private_key" => "pandas", "create_key" => false, } - @user = Chef::UserV1.from_json(Chef::JSONCompat.to_json(user)) + @user = Chef::User::V1.from_json(Chef::JSONCompat.to_json(user)) end - it "should deserialize to a Chef::UserV1 object" do - expect(@user).to be_a_kind_of(Chef::UserV1) + it "should deserialize to a Chef::User::V1 object" do + expect(@user).to be_a_kind_of(Chef::User::V1) end it "preserves the username" do @@ -315,9 +315,8 @@ describe Chef::UserV1 do let(:exception_406) { Net::HTTPServerException.new("406 Not Acceptable", response_406) } before (:each) do - @user = Chef::UserV1.new - allow(@user).to receive(:chef_root_rest_v0).and_return(double("chef rest root v0 object")) - allow(@user).to receive(:chef_root_rest_v1).and_return(double("chef rest root v1 object")) + @user = Chef::User::V1.new + allow(@user).to receive(:chef_rest).and_return(double("chef rest root v1 object")) end describe "update" do @@ -347,7 +346,7 @@ describe Chef::UserV1 do context "when server API V1 is valid on the Chef Server receiving the request" do context "when the user submits valid data" do it "properly updates the user" do - expect(@user.chef_root_rest_v1).to receive(:put).with("users/some_username", payload).and_return({}) + expect(@user.chef_rest).to receive(:put).with("users/some_username", payload).and_return({}) @user.update end end @@ -369,7 +368,7 @@ describe Chef::UserV1 do before do @user.public_key "some_public_key" - allow(@user.chef_root_rest_v1).to receive(:put) + allow(@user.chef_rest).to receive(:put) end context "when the server returns a 400" do @@ -381,7 +380,7 @@ describe Chef::UserV1 do before do allow(response_400).to receive(:body).and_return(response_body_400) - allow(@user.chef_root_rest_v1).to receive(:put).and_raise(exception_400) + allow(@user.chef_rest).to receive(:put).and_raise(exception_400) end it "proceeds with the V0 PUT since it can handle public / private key fields" do @@ -401,7 +400,7 @@ describe Chef::UserV1 do before do allow(response_400).to receive(:body).and_return(response_body_400) - allow(@user.chef_root_rest_v1).to receive(:put).and_raise(exception_400) + allow(@user.chef_rest).to receive(:put).and_raise(exception_400) end it "will not proceed with the V0 PUT since the original bad request was not key related" do @@ -422,13 +421,13 @@ describe Chef::UserV1 do let(:object) { @user } let(:method) { :update } let(:http_verb) { :put } - let(:rest_v1) { @user.chef_root_rest_v1 } + let(:rest_v1) { @user.chef_rest } end context "when the server supports API V0" do before do allow(@user).to receive(:server_client_api_version_intersection).and_return([0]) - allow(@user.chef_root_rest_v1).to receive(:put).and_raise(exception_406) + allow(@user.chef_rest).to receive(:put).and_raise(exception_406) end it "properly updates the user" do @@ -465,15 +464,14 @@ describe Chef::UserV1 do it_should_behave_like "user or client create" do let(:object) { @user } let(:error) { Chef::Exceptions::InvalidUserAttribute } - let(:rest_v0) { @user.chef_root_rest_v0 } - let(:rest_v1) { @user.chef_root_rest_v1 } + let(:rest_v1) { @user.chef_rest } let(:url) { "users" } end context "when handling API V1" do it "creates a new user via the API with a middle_name when it exists" do @user.middle_name "some_middle_name" - expect(@user.chef_root_rest_v1).to receive(:post).with("users", payload.merge({ :middle_name => "some_middle_name" })).and_return({}) + expect(@user.chef_rest).to receive(:post).with("users", payload.merge({ :middle_name => "some_middle_name" })).and_return({}) @user.create end end # when server API V1 is valid on the Chef Server receiving the request @@ -484,14 +482,14 @@ describe Chef::UserV1 do let(:object) { @user } let(:method) { :create } let(:http_verb) { :post } - let(:rest_v1) { @user.chef_root_rest_v1 } + let(:rest_v1) { @user.chef_rest } end end context "when handling API V0" do before do allow(@user).to receive(:server_client_api_version_intersection).and_return([0]) - allow(@user.chef_root_rest_v1).to receive(:post).and_raise(exception_406) + allow(@user.chef_rest).to receive(:post).and_raise(exception_406) end it "creates a new user via the API with a middle_name when it exists" do @@ -536,7 +534,7 @@ describe Chef::UserV1 do describe "API Interactions" do before (:each) do - @user = Chef::UserV1.new + @user = Chef::User::V1.new @user.username "foobar" @http_client = double("Chef::ServerAPI mock") allow(Chef::ServerAPI).to receive(:new).and_return(@http_client) @@ -547,27 +545,27 @@ describe Chef::UserV1 do Chef::Config[:chef_server_url] = "http://www.example.com" @osc_response = { "admin" => "http://www.example.com/users/admin" } @ohc_response = [ { "user" => { "username" => "admin" } } ] - allow(Chef::UserV1).to receive(:load).with("admin").and_return(@user) + allow(Chef::User::V1).to receive(:load).with("admin").and_return(@user) @osc_inflated_response = { "admin" => @user } end it "lists all clients on an OHC/OPC server" do allow(@http_client).to receive(:get).with("users").and_return(@ohc_response) - # We expect that Chef::UserV1.list will give a consistent response + # We expect that Chef::User::V1.list will give a consistent response # so OHC API responses should be transformed to OSC-style output. - expect(Chef::UserV1.list).to eq(@osc_response) + expect(Chef::User::V1.list).to eq(@osc_response) end it "inflate all clients on an OHC/OPC server" do allow(@http_client).to receive(:get).with("users").and_return(@ohc_response) - expect(Chef::UserV1.list(true)).to eq(@osc_inflated_response) + expect(Chef::User::V1.list(true)).to eq(@osc_inflated_response) end end describe "read" do it "loads a named user from the API" do expect(@http_client).to receive(:get).with("users/foobar").and_return({ "username" => "foobar", "admin" => true, "public_key" => "pubkey" }) - user = Chef::UserV1.load("foobar") + user = Chef::User::V1.load("foobar") expect(user.username).to eq("foobar") expect(user.public_key).to eq("pubkey") end |