Sha256: c953afaf04f3fec3a42f4cdeba372048eb6af6e7ee3a75fdf6a5d98289cf3051

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require "spec_helper"

describe Decidim::Admin::CreateParticipatoryProcessAdmin do
  let(:my_process) { create :participatory_process }
  let!(:email) { "my_email@example.org" }
  let!(:role) { "admin" }
  let!(:name) { "Weird Guy" }
  let!(:user) { create :user, email: "my_email@example.org", organization: my_process.organization }
  let!(:current_user) { create :user, email: "some_email@example.org", organization: my_process.organization }
  let(:form) do
    double(
      invalid?: invalid,
      email: email,
      role: role,
      name: name
    )
  end
  let(:invalid) { false }

  subject { described_class.new(form, current_user, my_process) }

  context "when to form is not valid" do
    let(:invalid) { true }

    it "is not valid" do
      expect { subject.call }.to broadcast(:invalid)
    end
  end

  context "when there is no user with the given email" do
    let(:email) { "does_not_exist@example.com" }

    it "creates a new user with said email" do
      subject.call
      expect(Decidim::User.last.email).to eq(email)
    end
  end

  context "when a user and a role already exist" do
    before do
      create(
        :participatory_process_user_role,
        user: user,
        role: :admin,
        participatory_process: my_process
      )
    end

    it "is not valid" do
      form_errors = double
      expect(form_errors).to receive(:add).with(:email, :taken)
      expect(form).to receive(:errors).and_return(form_errors)

      expect { subject.call }.to broadcast(:invalid)
    end
  end

  context "when everything is ok" do
    it "creates the user role" do
      subject.call
      roles = Decidim::Admin::ParticipatoryProcessUserRole.where(user: user)

      expect(roles.count).to eq 1
      expect(roles.first.role).to eq "admin"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
decidim-0.3.2 decidim-admin/spec/commands/create_participatory_process_admin_spec.rb
decidim-0.3.1 decidim-admin/spec/commands/create_participatory_process_admin_spec.rb
decidim-0.3.0 decidim-admin/spec/commands/create_participatory_process_admin_spec.rb
decidim-0.2.0 decidim-admin/spec/commands/create_participatory_process_admin_spec.rb