# Copyright: Copyright (c) 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 "pedant/rspec/knife_util"
require "pedant/rspec/user_util"
require "chef-utils/dist"

describe "knife", :validation, :knife do
  context "opc" do
    context "org" do
      context "create" do
        include Pedant::RSpec::KnifeUtil
        include Pedant::RSpec::UserUtil

        let(:org_name) { "org-#{rand(1 << 32)}" }
        let(:superuser_rb) { "/etc/#{::ChefUtils::Dist::Org::LEGACY_CONF_DIR}/pivotal.rb" }
        let(:requestor) { superuser }
        let(:admin_requestor) { superuser }

        after(:each) { knife "org delete #{org_name} -c #{superuser_rb} --yes" }

        context "without associations" do
          let(:command) { "knife org create #{org_name} #{org_name} -c #{superuser_rb} --disable-editing" }
          it "should succeed" do
            should have_outcome status: 0, stdout: /-----BEGIN (RSA )?PRIVATE KEY-----/
          end
        end

        context "with associations" do
          let(:username) { "user-#{rand(1 << 32)}" }
          let(:command) { "knife org create #{org_name} #{org_name} --association_user #{username} -c #{superuser_rb} --disable-editing" }

          before(:each) { knife "user create #{username} --email #{username}@foo.bar --password 'badger badger' --first-name #{username} --last-name #{username} -c #{superuser_rb} --disable-editing" }
          after(:each) { knife "user delete #{username} -c #{superuser_rb} --disable-editing" }

          it "should succeed" do
            should have_outcome status: 0, stdout: /-----BEGIN (RSA )?PRIVATE KEY-----/
          end
        end

      end
    end
  end
end
