<commit_before>require 'spec_helper'

# This file is part of Mconf-Web, a web application that provides access
# to the Mconf webconferencing system. Copyright (C) 2010-2012 Mconf
#
# This file is licensed under the Affero General Public License version
# 3 or later. See the LICENSE file.

describe AdminsController do
  render_views

  describe "#new_user" do
    context "logged as a superuser" do
      let(:superuser) { FactoryGirl.create(:superuser) }
      before(:each) { sign_in(superuser) }

      context "template and view" do
        before(:each) { get :new_user }
        it { should render_with_layout("application") }
        it { should render_template("admins/new_user") }
      end

      context "template and view via xhr" do
        before(:each) { xhr :get, :new_user }
        it { should_not render_with_layout() }
        it { should render_template("admins/new_user") }
      end

      it "assigns @user" do
        get :new_user
        should assign_to(:user).with(instance_of(User))
      end
    end

    context "logged as a regular user" do
      let(:user) { FactoryGirl.create(:user) }
      before(:each) { 
        sign_in(user)
        get :new_user
      }

      it "raises AccessDenied" do
        bypass_rescue
        expect { get :new_user }.to raise_error(CanCan::AccessDenied)
      end

      it { should set_the_flash.to(I18n.t('admins.access_forbidden')) }
      it { should redirect_to root_path }
    end

    context "a anonymous user" do
      before(:each) { get :new_user }

      it "raises AccessDenied" do
        bypass_rescue
        expect { get :new_user }.to raise_error(CanCan::AccessDenied)
      end

      it { should redirect_to root_path }
    end
  end

  describe "#create_user"  do
    let(:superuser) { FactoryGirl.create(:superuser) }
    before(:each) { sign_in(superuser) }

    describe "creates a new user with valid attributes" do
      let(:user) { FactoryGirl.build(:user) }
      before(:each) {
        expect {
          post :create_user, user: {
            email: user.email, _full_name: "<NAME>", username: "maria-test", 
            password: "<PASSWORD>", password_confirmation: "<PASSWORD>"
          }
        }.to change(User, :count).by(1)
      }

      it { should set_the_flash.to(I18n.t('admins.user.created')) }
      it { should redirect_to manage_users_path }
      it { User.last.confirmed?.should be true }
      it { User.last.approved?.should be true }
      end

    describe "creates a new user with invalid attributes" do
      before(:each) {
        expect {
          test = post :create_user, user: {
            email: "<EMAIL>", _full_name: "<NAME>", username: "maria-test", 
            password: "<PASSWORD>", password_confirmation: "<PASSWORD>"
          }
        }.not_to change(User, :count)
      }

      it { should set_the_flash.to(I18n.t('admins.user.error')) }
      it { should redirect_to manage_users_path }
      end
<commit_msg>More tests to admins controller.<commit_after>require 'spec_helper'

# This file is part of Mconf-Web, a web application that provides access
# to the Mconf webconferencing system. Copyright (C) 2010-2012 Mconf
#
# This file is licensed under the Affero General Public License version
# 3 or later. See the LICENSE file.

describe AdminsController do
  render_views

  describe "#new_user" do
    context "logged as a superuser" do
      let(:superuser) { FactoryGirl.create(:superuser) }
      before(:each) { sign_in(superuser) }

      context "template and view" do
        before(:each) { get :new_user }
        it { should render_with_layout("application") }
        it { should render_template("admins/new_user") }
      end

      context "template and view via xhr" do
        before(:each) { xhr :get, :new_user }
        it { should_not render_with_layout() }
        it { should render_template("admins/new_user") }
      end

      it "assigns @user" do
        get :new_user
        should assign_to(:user).with(instance_of(User))
      end
    end

    context "logged as a regular user" do
      let(:user) { FactoryGirl.create(:user) }
      before(:each) { 
        sign_in(user)
        get :new_user
      }

      it "raises AccessDenied" do
        bypass_rescue
        expect { get :new_user }.to raise_error(CanCan::AccessDenied)
      end

      it { should set_the_flash.to(I18n.t('admins.access_forbidden')) }
      it { should redirect_to root_path }
    end

    context "a anonymous user" do
      before(:each) { get :new_user }

      it "raises AccessDenied" do
        bypass_rescue
        expect { get :new_user }.to raise_error(CanCan::AccessDenied)
      end

      it { should redirect_to root_path }
    end
  end

  describe "#create_user"  do
    describe "as a global admin" do
      let(:superuser) { FactoryGirl.create(:superuser) }
      before(:each) { sign_in(superuser) }

      describe "creates a new user with valid attributes" do
        let(:user) { FactoryGirl.build(:user) }
        before(:each) {
          expect {
            post :create_user, user: {
              email: user.email, _full_name: "<NAME>", username: "maria-test", 
              password: "<PASSWORD>", password_confirmation: "<PASSWORD>"
            }
          }.to change(User, :count).by(1)
        }

        it { should set_the_flash.to(I18n.t('admins.user.created')) }
        it { should redirect_to manage_users_path }
        it { User.last.confirmed?.should be true }
        it { User.last.approved?.should be true }
      end

      describe "creates a new user with invalid attributes" do
        before(:each) {
          expect {
            test = post :create_user, user: {
              email: "<EMAIL>", _full_name: "<NAME>", username: "maria-test", 
              password: "<PASSWORD>", password_confirmation: "<PASSWORD>"
            }
          }.not_to change(User, :count)
        }

        it { should set_the_flash.to(I18n.t('admins.user.error')) }
        it { should redirect_to manage_users_path }
      end
    end

    describe "as a institution admin" do
      let(:institution) { FactoryGirl.create(:institution) }
      let(:institution_admin) { FactoryGirl.create(:user) }
      before(:each) { 
        institution.add_member!(institution_admin, "Admin")
        sign_in(institution_admin)
      }

      describe "creates a new user with valid attributes" do
        let(:user) { FactoryGirl.build(:user) }
        before(:each) {
          expect {
            post :create_user, user: {
              email: user.email, _full_name: "<NAME>", username: "maria-test", 
              password: "<PASSWORD>", password_confirmation: "<PASSWORD>"
            }
          }.to change(User, :count).by(1)
        }

        it { should set_the_flash.to(I18n.t('admins.user.created')) }
        it { should redirect_to manage_users_path }
        it { User.last.confirmed?.should be true }
        it { User.last.approved?.should be true }
        it { User.last.institution.should eql(institution) }
      end

      describe "creates a new user with valid attributes and a institution" do
        let(:other_institution) { FactoryGirl.create(:institution) }
        let(:user) { FactoryGirl.build(:user) }
        before(:each) {
          expect {
            post :create_user, user: {
              email: user.email, _full_name: "<NAME>", username: "maria-test", 
              password: "<PASSWORD>", password_confirmation: "<PASSWORD>", institution_id: other_institution.id
            }
          }.to change(User, :count).by(1)
        }

        it { should set_the_flash.to(I18n.t('admins.user.created')) }
        it { should redirect_to manage_users_path }
        it { User.last.confirmed?.should be true }
        it { User.last.approved?.should be true }
        it { User.last.institution.should_not eql(other_institution) }
        it { User.last.institution.should eql(institution) }
      end

      describe "creates a new user with invalid attributes" do
        before(:each) {
          expect {
            test = post :create_user, user: {
              email: "<EMAIL>", _full_name: "<NAME>", username: "maria-test", 
              password: "<PASSWORD>", password_confirmation: "<PASSWORD>"
            }
          }.not_to change(User, :count)
        }

        it { should set_the_flash.to(I18n.t('admins.user.error')) }
        it { should redirect_to manage_users_path }
      end
    end
{'fraction_non_alphanumeric': 0.11034809377219985, 'fraction_numerical': 0.004735969689793985, 'mean_word_length': 2.3760991207034374, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '7578051', 'n_tokens_mistral': 2619, 'n_tokens_neox': 2504, 'n_words': 660}