<commit_before>require "spec_helper"

describe DashboardController do
  before(:each) do
    described_class.any_instance.stub(:set_user_time_zone)
  end

  context "POST authenticate" do
    it "validates user" do
      role = FactoryGirl.create(:miq_user_role, :name => 'test_role')
      group = FactoryGirl.create(:miq_group, :description => 'test_group', :miq_user_role => role)
      user = FactoryGirl.create(:user, :userid => 'wilma', :miq_groups => [group])
      User.stub(:authenticate).and_return(user)
      cfg = {:product => {:vdi => true}}
      controller.stub(:get_vmdb_config).and_return(cfg)
      controller.stub(:start_url_for_user).and_return('some_url')
      post :authenticate, :user_name => user.userid, :user_password => '<PASSWORD>'
      session[:userid].should == user.userid
    end
  end

  context "#validate_user" do
    let(:server) { instance_double("MiqServer", :logon_status => :ready) }

    before do
      MiqServer.stub(:my_server).with(true).and_return(server)
    end

    it "returns flash message when user's group is missing" do
      user = FactoryGirl.create(:user, :userid => 'wilma')
      User.stub(:authenticate).and_return(user)
      controller.send(:validate_user, user)
      assigns(:flash_msg).should include('User\'s Group is missing')
    end

    it "returns flash message when user's role is missing" do
      group = FactoryGirl.create(:miq_group, :description => 'test_group')
      user = FactoryGirl.create(:user, :userid => 'wilma', :miq_groups => [group])
      User.stub(:authenticate).and_return(user)
      controller.send(:validate_user, user)
      session[:group].should eq(group.id)
      assigns(:flash_msg).should include('User\'s Role is missing')
    end

    it "returns url for the user and sets user's group/role id in session" do
      role = FactoryGirl.create(:miq_user_role, :name => 'test_role')
      group = FactoryGirl.create(:miq_group, :description => 'test_group', :miq_user_role => role)
      user = FactoryGirl.create(:user, :userid => 'wilma', :miq_groups => [group])
      User.stub(:authenticate).and_return(user)

      cfg = {:product => {:vdi => true}}
      controller.stub(:get_vmdb_config).and_return(cfg)
      controller.stub(:start_url_for_user).and_return('some_url')
      url = controller.send(:validate_user, user)
      session[:group].should eq(group.id)
      session[:role].should eq(role.id)
      assigns(:flash_msg).should be_nil
      url.should eq('some_url')
    end
  end

  context "Create Dashboard" do
    it "dashboard show" do
      #create dashboard for a group
      ws = FactoryGirl.create(:miq_widget_set, :name => "default",
                              :set_data => {:last_group_db_updated => Time.now.utc,
                              :col1 => [1], :col2 => [], :col3 =>[]})

      ur = FactoryGirl.create(:miq_user_role)
      group = FactoryGirl.create(:miq_group, :miq_user_role => ur, :settings => {:dashboard_order => [ws.id]})<commit_msg>Fix dashboard_controller specs after rebase.<commit_after>require "spec_helper"

describe DashboardController do
  before(:each) do
    described_class.any_instance.stub(:set_user_time_zone)
  end

  context "POST authenticate" do
    it "validates user" do
      role = FactoryGirl.create(:miq_user_role, :name => 'test_role')
      group = FactoryGirl.create(:miq_group, :description => 'test_group', :miq_user_role => role)
      user = FactoryGirl.create(:user, :userid => 'wilma', :miq_groups => [group])
      User.stub(:authenticate).and_return(user)
      cfg = {:product => {:vdi => true}}
      controller.stub(:get_vmdb_config).and_return(cfg)
      controller.stub(:user_is_super_admin?).and_return(true)
      controller.stub(:start_url_for_user).and_return('some_url')
      post :authenticate, :user_name => user.userid, :user_password => '<PASSWORD>'
      session[:userid].should == user.userid
    end
  end

  context "#validate_user" do
    let(:server) { instance_double("MiqServer", :logon_status => :ready) }

    before do
      MiqServer.stub(:my_server).with(true).and_return(server)
    end

    it "returns flash message when user's group is missing" do
      user = FactoryGirl.create(:user, :userid => 'wilma')
      User.stub(:authenticate).and_return(user)
      validation = controller.send(:validate_user, user)
      expect(validation.flash_msg).to include('User\'s Group is missing')
    end

    it "returns flash message when user's role is missing" do
      group = FactoryGirl.create(:miq_group, :description => 'test_group')
      user = FactoryGirl.create(:user, :userid => 'wilma', :miq_groups => [group])
      User.stub(:authenticate).and_return(user)
      validation = controller.send(:validate_user, user)
      expect(validation.flash_msg).to include('User\'s Role is missing')
    end

    it "returns url for the user and sets user's group/role id in session" do
      role = FactoryGirl.create(:miq_user_role, :name => 'test_role')
      group = FactoryGirl.create(:miq_group, :description => 'test_group', :miq_user_role => role)
      user = FactoryGirl.create(:user, :userid => 'wilma', :miq_groups => [group])
      User.stub(:authenticate).and_return(user)

      cfg = {:product => {:vdi => true}}
      controller.stub(:get_vmdb_config).and_return(cfg)
      controller.stub(:start_url_for_user).and_return('some_url')
      controller.stub(:user_is_super_admin?).and_return(true)
      validation = controller.send(:validate_user, user)
      session[:group].should eq(group.id)
      session[:role].should eq(role.id)
      expect(validation.flash_msg).to be_nil
      expect(validation.url).to eq('some_url')
    end
  end

  context "Create Dashboard" do
    it "dashboard show" do
      #create dashboard for a group
      ws = FactoryGirl.create(:miq_widget_set, :name => "default",
                              :set_data => {:last_group_db_updated => Time.now.utc,
                              :col1 => [1], :col2 => [], :col3 =>[]})

      ur = FactoryGirl.create(:miq_user_role)
      group = FactoryGirl.create(:miq_group, :miq_user_role => ur, :settings => {:dashboard_order => [ws.id]})
{'fraction_non_alphanumeric': 0.13887984369912082, 'fraction_numerical': 0.0013025073266037122, 'mean_word_length': 3.6257530120481927, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5825394', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1931, 'n_words': 401}