summaryrefslogtreecommitdiff
path: root/spec/unit/knife/org_create_spec.rb
blob: f45ade2df709977b7d5c013eb1f94fa9d2d79c5f (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
#
# Copyright:: Copyright 2014-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.
#

require "knife_spec_helper"
require "chef/org"

describe Chef::Knife::OrgCreate do
  before :each do
    Chef::Knife::OrgCreate.load_deps
    @knife = Chef::Knife::OrgCreate.new
    @org = double("Chef::Org")
    allow(Chef::Org).to receive(:new).and_return(@org)
    @key = "You don't come into cooking to get rich - Ramsay"
    allow(@org).to receive(:private_key).and_return(@key)
    @org_name = "ss"
    @org_full_name = "secretsauce"
  end

  let(:org_args) do
    {
      name: @org_name,
      full_name: @org_full_name,
    }
  end

  describe "with no org_name and org_fullname" do
    it "fails with an informative message" do
      expect(@knife.ui).to receive(:fatal).with("You must specify an ORG_NAME and an ORG_FULL_NAME")
      expect(@knife).to receive(:show_usage)
      expect { @knife.run }.to raise_error(SystemExit)
    end
  end

  describe "with org_name and org_fullname" do
    before :each do
      @knife.name_args << @org_name << @org_full_name
    end

    it "creates an org" do
      expect(@org).to receive(:create).and_return(@org)
      expect(@org).to receive(:full_name).with("secretsauce")
      expect(@knife.ui).to receive(:msg).with(@key)
      @knife.run
    end

    context "with --assocation-user" do
      before :each do
        @knife.config[:association_user] = "ramsay"
      end

      it "creates an org, associates a user, and adds it to the admins group" do
        expect(@org).to receive(:full_name).with("secretsauce")
        expect(@org).to receive(:create).and_return(@org)
        expect(@org).to receive(:associate_user).with("ramsay")
        expect(@org).to receive(:add_user_to_group).with("admins", "ramsay")
        expect(@org).to receive(:add_user_to_group).with("billing-admins", "ramsay")
        expect(@knife.ui).to receive(:msg).with(@key)
        @knife.run
      end
    end
  end
end