summaryrefslogtreecommitdiff
path: root/spec/unit/win32/error_spec.rb
blob: 9991b149bd8227b1470ffa12d8052a282c1ad60e (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
#
# Author:: Aliasgar Batterywala (aliasgar.batterywala@msystechnologies.com)
# Copyright:: Copyright 2017, 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.
#

require "spec_helper"
if ChefHelpers.windows?
  require "chef/win32/error"
  require "chef/win32/api/error"
end

describe "Chef::Win32::Error", :windows_only do
  describe "self.raise!" do
    context "code is not passed to the raise! method" do
      context "last error received is user_not_found" do
        it "raises UserIDNotFound exception" do
          expect(Chef::ReservedNames::Win32::Error).to receive(:get_last_error).and_return(
            Chef::ReservedNames::Win32::API::Error::ERROR_USER_NOT_FOUND
          )
          expect(Chef::ReservedNames::Win32::Error).to receive_message_chain(:format_message, :strip)
          expect { Chef::ReservedNames::Win32::Error.raise! }.to raise_error Chef::Exceptions::UserIDNotFound
        end
      end

      context "last error received is not user_not_found" do
        it "raises Win32APIError exception with code 2202" do
          expect(Chef::ReservedNames::Win32::Error).to receive(:get_last_error).and_return(
            Chef::ReservedNames::Win32::API::Error::ERROR_BAD_USERNAME
          )
          expect(Chef::ReservedNames::Win32::Error).to receive_message_chain(:format_message, :strip).and_return("Bad Username")
          expect { Chef::ReservedNames::Win32::Error.raise! }.to raise_error(Chef::Exceptions::Win32APIError)
        end
      end
    end

    context "code is passed to the raise! method" do
      context "last error received is user_not_found" do
        it "raises UserIDNotFound exception" do
          expect(Chef::ReservedNames::Win32::Error).to_not receive(:get_last_error)
          expect(Chef::ReservedNames::Win32::Error).to receive_message_chain(:format_message, :strip)
          expect { Chef::ReservedNames::Win32::Error.raise! nil, Chef::ReservedNames::Win32::API::Error::ERROR_USER_NOT_FOUND }.to raise_error Chef::Exceptions::UserIDNotFound
        end
      end

      context "last error received is not user_not_found" do
        it "raises Win32APIError exception" do
          expect(Chef::ReservedNames::Win32::Error).to_not receive(:get_last_error)
          expect(Chef::ReservedNames::Win32::Error).to receive_message_chain(:format_message, :strip).and_return("Bad Username")
          expect { Chef::ReservedNames::Win32::Error.raise! nil, Chef::ReservedNames::Win32::API::Error::ERROR_BAD_USERNAME }.to raise_error Chef::Exceptions::Win32APIError
        end
      end

      context "last error received is logon failure" do
        it "raises a Win32ApiError exception with code 1326" do
          expect { Chef::ReservedNames::Win32::Error.raise! nil, Chef::ReservedNames::Win32::API::Error::ERROR_LOGON_FAILURE }.to raise_error { |error|
            expect(error).to be_a(Chef::Exceptions::Win32APIError)
            # This is not localized but neither is the exception.
            expect(error.to_s).to match(/System Error Code: 1326/)
          }
        end
      end
    end
  end
end