From 542135da6707c0055482a4c2f2bf54573a2a4967 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 31 Mar 2010 14:46:25 -0300 Subject: [PATCH] Fixing the Enterprise Registering Process --- app/controllers/public/enterprise_registration_controller.rb | 15 +++++++++++---- app/models/create_enterprise.rb | 49 +++++++++++++++++++++++++++++++++++++------------ app/models/enterprise.rb | 4 +++- app/models/environment.rb | 1 + app/models/region.rb | 4 ++++ app/views/enterprise_registration/basic_information.rhtml | 54 +++++++++++++++++++++++++++--------------------------- app/views/enterprise_registration/select_validator.rhtml | 3 +-- app/views/memberships/index.rhtml | 1 + app/views/profile/enterprises.rhtml | 3 +-- app/views/profile_editor/_organization.rhtml | 5 +---- app/views/shared/_custom_fields.rhtml | 3 +++ app/views/tasks/_task.rhtml | 10 ++++++---- features/location.feature | 4 ++-- features/register_enterprise.feature | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ features/step_definitions/noosfero_steps.rb | 37 ++++++++++++++++++++++++++++++------- test/functional/enterprise_registration_controller_test.rb | 32 ++++++++++++++++++++------------ test/integration/enterprise_registration_test.rb | 2 ++ test/unit/create_enterprise_test.rb | 15 +++++++++++++++ test/unit/region_test.rb | 11 +++++++++++ 19 files changed, 367 insertions(+), 77 deletions(-) create mode 100644 features/register_enterprise.feature diff --git a/app/controllers/public/enterprise_registration_controller.rb b/app/controllers/public/enterprise_registration_controller.rb index 843e625..31b2281 100644 --- a/app/controllers/public/enterprise_registration_controller.rb +++ b/app/controllers/public/enterprise_registration_controller.rb @@ -9,15 +9,20 @@ class EnterpriseRegistrationController < ApplicationController # FIXME: shouldn't this action present some sort of welcome message and point # to the first step explicitly? def index + @validation = environment.organization_approval_method @create_enterprise = CreateEnterprise.new(params[:create_enterprise]) - if params[:create_enterprise] && params[:create_enterprise][:target_id] - @create_enterprise.target = Profile.find(params[:create_enterprise][:target_id]) + if @validation == :region + if params[:create_enterprise] && params[:create_enterprise][:target_id] + @create_enterprise.target = Profile.find(params[:create_enterprise][:target_id]) + end + elsif @validation == :admin + @create_enterprise.target = @create_enterprise.environment end @create_enterprise.requestor = current_user.person the_action = if request.post? if @create_enterprise.valid_before_selecting_target? - if @create_enterprise.valid? + if @create_enterprise.valid? || @validation == :admin :confirmation else :select_validator @@ -38,7 +43,9 @@ class EnterpriseRegistrationController < ApplicationController # # Posts back. def basic_information - @regions = environment.regions.select{|i| i.has_validator?}.map {|item| [item.name, item.id]} + if @validation == :region + @regions = @create_enterprise.available_regions.map {|region| [region.name, region.id]} + end end # present information about validator organizations, and the user one to diff --git a/app/models/create_enterprise.rb b/app/models/create_enterprise.rb index b78bbd5..9f45429 100644 --- a/app/models/create_enterprise.rb +++ b/app/models/create_enterprise.rb @@ -11,7 +11,7 @@ class CreateEnterprise < Task N_('Economic activity') N_('Management information') - DATA_FIELDS = %w[ name identifier address contact_phone contact_person acronym foundation_year legal_form economic_activity management_information region_id reject_explanation ] + DATA_FIELDS = Enterprise.fields + %w[name identifier region_id reject_explanation] serialize :data, Hash attr_protected :data @@ -31,20 +31,30 @@ class CreateEnterprise < Task end # checks for virtual attributes - validates_presence_of :name, :identifier, :address, :contact_phone, :contact_person, :legal_form, :economic_activity, :region_id + validates_presence_of :name, :identifier + + #checks if the validation method is region to validates + validates_presence_of :region_id, :if => lambda { |obj| obj.environment.organization_approval_method == :region } + validates_format_of :foundation_year, :with => /^\d*$/ # checks for actual attributes validates_presence_of :requestor_id, :target_id + # checks for admins required attributes + DATA_FIELDS.each do |attribute| + validates_presence_of attribute, :if => lambda { |obj| obj.environment.required_enterprise_fields.include?(attribute) } + end + # check for explanation when rejecting validates_presence_of :reject_explanation, :if => (lambda { |record| record.status == Task::Status::CANCELLED } ) xss_terminate :only => [ :acronym, :address, :contact_person, :contact_phone, :economic_activity, :legal_form, :management_information, :name ], :on => 'validation' def validate + if self.region && self.target - unless self.region.validators.include?(self.target) + unless self.region.validators.include?(self.target) || self.target_type == "Environment" self.errors.add(:target, '%{fn} is not a validator for the chosen region') end end @@ -58,7 +68,7 @@ class CreateEnterprise < Task if valid? true else - self.errors.size == 1 and self.errors[:target_id] + self.errors.size == 1 && !self.errors[:target_id].nil? end end @@ -81,7 +91,27 @@ class CreateEnterprise < Task end def environment - region ? region.environment : nil + region ? region.environment : self.requestor ? self.requestor.environment : Environment.default + end + + def available_regions + environment.regions.with_validators + end + + def active_fields + environment ? environment.active_enterprise_fields : [] + end + + def required_fields + environment ? environment.required_enterprise_fields : [] + end + + def community? + false + end + + def enterprise? + true end # Rejects the enterprise registration request. @@ -107,21 +137,16 @@ class CreateEnterprise < Task def perform enterprise = Enterprise.new - profile_fields = %w[ name identifier contact_phone address region_id ] - profile_fields.each do |field| + DATA_FIELDS.reject{|field| field == "reject_explanation"}.each do |field| enterprise.send("#{field}=", self.send(field)) end - organization_data = self.data.reject do |key,value| - profile_fields.include?(key.to_s) - end - enterprise.environment = environment enterprise.user = self.requestor.user - enterprise.update_attributes(organization_data) enterprise.save! + enterprise.add_admin(enterprise.user.person) end def description diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index cd27b20..6e20217 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -34,6 +34,8 @@ class Enterprise < Organization organization_website historic_and_current_context activities_short_description + acronym + foundation_year ] def self.fields @@ -44,7 +46,7 @@ class Enterprise < Organization super self.required_fields.each do |field| if self.send(field).blank? - self.errors.add(field, _('%{fn} is mandatory')) + self.errors.add(field, _("%{fn} can't be blank")) end end end diff --git a/app/models/environment.rb b/app/models/environment.rb index ef0466d..8e237ef 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -89,6 +89,7 @@ class Environment < ActiveRecord::Base 'disable_select_city_for_contact' => _('Disable state/city select for contact form'), 'disable_contact_person' => _('Disable contact for people'), 'disable_contact_community' => _('Disable contact for groups/communities'), + 'disable_enterprise_registration' => _('Disable the enterprise registration'), 'join_community_popup' => _('Ask users to join a group/community with a popup'), 'enterprise_activation' => _('Enable activation of enterprises'), diff --git a/app/models/region.rb b/app/models/region.rb index 73f1d0e..03e5ea4 100644 --- a/app/models/region.rb +++ b/app/models/region.rb @@ -14,6 +14,10 @@ class Region < Category def has_validator? validators.count > 0 end + + def self.with_validators + Region.find(:all, :joins => 'INNER JOIN region_validators on (region_validators.region_id = categories.id)') + end end diff --git a/app/views/enterprise_registration/basic_information.rhtml b/app/views/enterprise_registration/basic_information.rhtml index 45f0be8..18d9624 100644 --- a/app/views/enterprise_registration/basic_information.rhtml +++ b/app/views/enterprise_registration/basic_information.rhtml @@ -2,34 +2,34 @@

<%= _('Register enterprise') %>

-

<%= _('How to proceed') %>

-

<%= _('Fill the form and hit the Register button then the enterprise will be submitted for evaluation at the validation entitiy of your choice (within your state), when the enterprise is aproved you will be able to activate its profile') %>

- -<% labelled_form_for(:create_enterprise, @create_enterprise) do |f| %> - <%= f.text_field 'identifier', 'size' => 20 %> - - <%= f.text_field 'name', 'size' => 20 %> - - <%= f.text_field 'address', 'size' => 50 %> - - <%= f.text_field 'contact_phone', 'size' => 20 %> - - <%= f.text_field 'contact_person', 'size' => 20 %> - - <%= f.text_field 'acronym', 'size' => 20 %> - - <%= f.text_field 'foundation_year', 'size' => 20 %> - - <%= f.text_field 'legal_form', 'size' => 20 %> - - <%= f.text_field 'economic_activity', 'size' => 20 %> - - <%= labelled_form_field(_('Management information'), text_editor('create_enterprise', 'management_information')) %> - - <%= labelled_form_field(_('Region'), f.select('region_id', @regions)) %> + +<% if @validation == :region && @regions.empty? %> +
+ <%= _('There are no validators to validate the registration of this new enterprise. Contact your administrator for instructions.') %> +
<% button_bar do %> - <%= submit_button('next', _('Next'), :cancel => {:action => 'index'}) %> + <%= button :back, _('Go back'), { :profile => current_user.person.identifier, :action=>"enterprises", :controller=>"profile" }%> + <% end %> +<% else %> +
+ <%= _('To register a new enterprise, fill in the form and hit the Register button. Then the enterprise will be submitted for evaluation at the validation entitiy of your choice (within your state) and when the enterprise is aproved you will be able to activate its profile.') %> +
+ + <%= required_fields_message %> + + <% labelled_form_for(:create_enterprise, @create_enterprise) do |f| %> + <%= required f.text_field 'identifier'%> + <%= required f.text_field 'name'%> + <%= render :partial => 'shared/custom_fields', :locals => { :f => f, :object_name => :create_enterprise, :profile => @create_enterprise, :only_required => false } %> + <%= required labelled_form_field(_('Region'), f.select('region_id', @regions)) if @validation == :region %> + + <% if @validation == :admin %> + <%= hidden_field_tag 'create_enterprise[target_id]', environment.id %> + <% end %> + + <% button_bar do %> + <%= submit_button('next', _('Next'), :cancel => {:profile => current_user.person.identifier, :action=>"enterprises", :controller=>"profile"}) %> + <% end %> <% end %> - <% end %> diff --git a/app/views/enterprise_registration/select_validator.rhtml b/app/views/enterprise_registration/select_validator.rhtml index 4f02f0b..1233e4b 100644 --- a/app/views/enterprise_registration/select_validator.rhtml +++ b/app/views/enterprise_registration/select_validator.rhtml @@ -9,8 +9,7 @@ <% for validator in @validators %>
- <%= radio_button_tag('create_enterprise[target_id]', validator.id) %> - <%= validator.name %> + <%= labelled_radio_button validator.name, 'create_enterprise[target_id]', validator.id, true %> <%= labelled_form_field(_('Validation Methodology:'), validator.validation_methodology || _("(not informed)")) %> <%= labelled_form_field(_('Restrictions (if any):'), validator.validation_restrictions || _("(not informed)")) %> diff --git a/app/views/memberships/index.rhtml b/app/views/memberships/index.rhtml index 620479e..cc3e85f 100644 --- a/app/views/memberships/index.rhtml +++ b/app/views/memberships/index.rhtml @@ -4,6 +4,7 @@ <% button_bar do %> <%= button(:add, __('Create a new community'), :controller => 'memberships', :action => 'new_community') %> + <%= button :add, __('Register a new enterprise'), :controller => 'enterprise_registration' if !environment.enabled?('disable_enterprise_registration') %> <%= button :back, _('Go back'), :controller => 'profile_editor' %> <% end %> diff --git a/app/views/profile/enterprises.rhtml b/app/views/profile/enterprises.rhtml index 7ba7e1f..f358975 100644 --- a/app/views/profile/enterprises.rhtml +++ b/app/views/profile/enterprises.rhtml @@ -12,8 +12,7 @@ <% button_bar do %> <%= button :back, _('Go back'), { :controller => 'profile' }, :help => _('Back to the page where you come from.') %> - <%= button :add, __('Register a new Enterprise'), - :controller => 'enterprise_registration' if logged_in? %> + <%= button :add, __('Register a new Enterprise'), :controller => 'enterprise_registration' if logged_in? && !environment.enabled?('disable_enterprise_registration') %> <% end %>
diff --git a/app/views/profile_editor/_organization.rhtml b/app/views/profile_editor/_organization.rhtml index 3e4b710..371ac48 100644 --- a/app/views/profile_editor/_organization.rhtml +++ b/app/views/profile_editor/_organization.rhtml @@ -1,6 +1,6 @@

<%= _('General information') %>

- <%= required_fields_message if @profile.required_fields.any? %> + <%= required_fields_message %> <%= required f.text_field(:name) %> @@ -66,9 +66,6 @@ <% end %> - <%= f.text_field(:acronym) %> - <%= f.text_field(:foundation_year) %> - <%= render :partial => 'shared/custom_fields', :locals => { :f => f, :object_name => 'profile_data', :profile => @profile, :only_required => false } %> <%= labelled_check_box(_('Enable "contact us"'), 'profile_data[enable_contact_us]', "1", @profile.enable_contact_us) if @profile.enterprise? %> diff --git a/app/views/shared/_custom_fields.rhtml b/app/views/shared/_custom_fields.rhtml index b045963..c72718a 100644 --- a/app/views/shared/_custom_fields.rhtml +++ b/app/views/shared/_custom_fields.rhtml @@ -12,6 +12,7 @@ <%= optional_field(profile, 'address', labelled_form_field(_('Address (street and number)'), text_field(object_name, :address)), only_required) %> <% if profile.enterprise? %> + <%= optional_field(profile, 'business_name', f.text_field(:business_name), only_required) %> <%= optional_field(profile, 'zip_code', labelled_form_field(_('ZIP code'), text_field(object_name, :zip_code)), only_required) %> <%= optional_field(profile, 'city', f.text_field(:city), only_required) %> <%= optional_field(profile, 'state', f.text_field(:state), only_required) %> @@ -19,4 +20,6 @@ <%= optional_field(profile, 'organization_website', f.text_field(:organization_website), only_required) %> <%= optional_field(profile, 'historic_and_current_context', f.text_area(:historic_and_current_context, :rows => 5), only_required) %> <%= optional_field(profile, 'activities_short_description', f.text_area(:activities_short_description, :rows => 5), only_required) %> + <%= optional_field(profile, 'acronym', f.text_field(:acronym), only_required) %> + <%= optional_field(profile, 'foundation_year', f.text_field(:foundation_year), only_required) %> <% end %> diff --git a/app/views/tasks/_task.rhtml b/app/views/tasks/_task.rhtml index 40dcd88..ed1e777 100644 --- a/app/views/tasks/_task.rhtml +++ b/app/views/tasks/_task.rhtml @@ -4,12 +4,14 @@ <% form_for('task', task, :url => { :action => 'close', :id => task.id}) do |f| %>
- <%= radio_button_tag(:decision, 'finish', true) %> - <%= _('OK') %> + <%= labelled_radio_button _('OK'), :decision, 'finish', true, :onclick => 'if(this.checked) $("rejection-field").style.display="none"' %>
- <%= radio_button_tag(:decision, 'cancel', false) %> - <%= _('Cancel') %> + <%= labelled_radio_button _('Cancel'), :decision, 'cancel', false, :onclick => 'if(this.checked) $("rejection-field").style.display="block"' %> +
+ + <% button_bar do %> diff --git a/features/location.feature b/features/location.feature index 8c34674..4161c6c 100644 --- a/features/location.feature +++ b/features/location.feature @@ -10,7 +10,7 @@ Feature: Location And I am logged in as "zezinho" Scenario: editing my address - Given the following Person fields are enabled + Given the following Person fields are active | address | | country | | state | @@ -29,7 +29,7 @@ Feature: Location | Rua Marechal Floriano, 28 | BR | Bahia | Salvador | 40110010 | Scenario Outline: editing address of collectives - Given the following fields are enabled + Given the following fields are active | address | | country | | state | diff --git a/features/register_enterprise.feature b/features/register_enterprise.feature new file mode 100644 index 0000000..b313096 --- /dev/null +++ b/features/register_enterprise.feature @@ -0,0 +1,191 @@ +Feature: register enterprise + As a noosfero user + I want to register an enterprise + In order to interact in the web with my enterprise + + Background: + Given the following users + | login | name | + | joaosilva | Joao Silva | + + And I am logged in as "joaosilva" + And I am on Joao Silva's control panel + + Scenario: enterprise registration is disabled by admin + Given feature "disable_enterprise_registration" is enabled on environment + When I follow "Manage my groups" + Then I should not see "Register a new enterprise" + + Scenario: approval method is admin + Given organization_approval_method is "admin" on environment + And I follow "Manage my groups" + When I follow "Register a new enterprise" + Then I should not see "Region" + + Scenario: approval method is region + Given organization_approval_method is "region" on environment + And the following enterprise + | name | identifier | owner | + | Validator | validator | joaosilva | + And the following validation info + | validation_methodology | organization_name | + | "Sample methodology" | Validator | + And the following states + | name | validator_name | + | Sample State | Validator | + And I follow "Manage my groups" + When I follow "Register a new enterprise" + Then I should see "Region" + + Scenario: approval method is by region validator but there are no validators + Given organization_approval_method is "region" on environment + And I follow "Manage my groups" + When I follow "Register a new enterprise" + Then I should see "There are no validators to validate the registration of this new enterprise. Contact your administrator for instructions." + + Scenario: some active fields + Given the following enterprise fields are active + | foundation_year | + | contact_person | + | contact_email | + And I follow "Manage my groups" + When I follow "Register a new enterprise" + Then I should see "Foundation year" + Then I should see "Contact person" + Then I should see "Contact email" + + Scenario: some required fields + Given organization_approval_method is "admin" on environment + And I follow "Manage my groups" + And the following states + | name | + | Sample State | + And the following enterprise fields are required + | foundation_year | + | contact_person | + | contact_email | + And I follow "Register a new enterprise" + And I fill in the following: + | Identifier | my-enterprise | + | Name | My Enterprise | + | Foundation year | | + | Contact person | | + | Contact email | | + When I press "Next" + Then I should see "Foundation year can't be blank" + Then I should see "Contact person can't be blank" + Then I should see "Contact email can't be blank" + + Scenario: a user register an enterprise successfully through the admin + validator method and the admin accepts + Given organization_approval_method is "admin" on environment + And I follow "Manage my groups" + And the following states + | name | + | Sample State | + And I follow "Register a new enterprise" + And I fill in the following: + | Identifier | my-enterprise | + | Name | My Enterprise | + And I press "Next" + Then I should see "Enterprise Registration completed" + And I am logged in as admin + And I follow "Control panel" + When I follow "Tasks" + Then I should see /Processing task: Enterprise registration: "My Enterprise"/ + And I choose "Ok" + And I press "Ok" + And I am logged in as "joaosilva" + And I am on Joao Silva's control panel + When I follow "Manage my groups" + Then I should see "My Enterprise" + + Scenario: a user register an enterprise successfully through the admin + validator method and the admin rejects + Given organization_approval_method is "admin" on environment + And I follow "Manage my groups" + And the following states + | name | + | Sample State | + And I follow "Register a new enterprise" + And I fill in the following: + | Identifier | my-enterprise | + | Name | My Enterprise | + And I press "Next" + Then I should see "Enterprise Registration completed" + And I am logged in as admin + And I follow "Control panel" + When I follow "Tasks" + Then I should see /Processing task: Enterprise registration: "My Enterprise"/ + And I choose "Cancel" + And I fill in "Rejection explanation" with "This enterprise has some irregularities." + And I press "Ok" + And I am logged in as "joaosilva" + And I am on Joao Silva's control panel + When I follow "Manage my groups" + Then I should not see "My Enterprise" + + Scenario: a user register an enterprise successfully through the region + validator method and the validator accepts + Given organization_approval_method is "region" on environment + And I follow "Manage my groups" + And the following enterprise + | name | identifier | owner | + | Validator | validator | joaosilva | + And the following validation info + | validation_methodology | organization_name | + | "Sample methodology" | Validator | + And the following states + | name | validator_name | + | Sample State | Validator | + And I follow "Register a new enterprise" + And I fill in the following: + | Identifier | my-enterprise | + | Name | My Enterprise | + And I select "Sample State" from "Region" + And I press "Next" + Then I should see "Validator" + Then I should see "Sample methodology" + When I press "Confirm" + Then I should see "Enterprise Registration completed" + And I am on Validator's control panel + When I follow "Tasks" + Then I should see /Processing task: Enterprise registration: "My Enterprise"/ + And I choose "Ok" + And I press "Ok" + And I am on Joao Silva's control panel + When I follow "Manage my groups" + Then I should see "My Enterprise" + + Scenario: a user register an enterprise successfully through the region + validator method and the validator rejects + Given organization_approval_method is "region" on environment + And I follow "Manage my groups" + And the following enterprise + | name | identifier | owner | + | Validator | validator | joaosilva | + And the following validation info + | validation_methodology | organization_name | + | "Sample methodology" | Validator | + And the following states + | name | validator_name | + | Sample State | Validator | + And I follow "Register a new enterprise" + And I fill in the following: + | Identifier | my-enterprise | + | Name | My Enterprise | + And I select "Sample State" from "Region" + And I press "Next" + Then I should see "Validator" + Then I should see "Sample methodology" + When I press "Confirm" + Then I should see "Enterprise Registration completed" + And I am on Validator's control panel + When I follow "Tasks" + Then I should see /Processing task: Enterprise registration: "My Enterprise"/ + And I choose "Cancel" + And I fill in "Rejection explanation" with "This enterprise has some irregularities." + And I press "Ok" + And I am on Joao Silva's control panel + When I follow "Manage my groups" + Then I should not see "My Enterprise" diff --git a/features/step_definitions/noosfero_steps.rb b/features/step_definitions/noosfero_steps.rb index 9ef648f..391c42f 100644 --- a/features/step_definitions/noosfero_steps.rb +++ b/features/step_definitions/noosfero_steps.rb @@ -65,7 +65,27 @@ Given /^the following products$/ do |table| end end +Given /^the following states$/ do |table| + table.hashes.each do |item| + data = item.dup + if validator = Enterprise.find_by_name(data.delete("validator_name")) + State.create!(data.merge(:environment => Environment.default, :validators => [validator])) + else + r = State.create!(data.merge(:environment => Environment.default)) + end + end +end + +Given /^the following validation info$/ do |table| + table.hashes.each do |item| + data = item.dup + organization = Organization.find_by_name(data.delete("organization_name")) + ValidationInfo.create!(data.merge(:organization => organization)) + end +end + Given /^I am logged in as "(.+)"$/ do |username| + visit('/account/logout') visit('/account/login') fill_in("Username", :with => username) fill_in("Password", :with => '123456') @@ -73,6 +93,7 @@ Given /^I am logged in as "(.+)"$/ do |username| end Given /^I am logged in as admin$/ do + visit('/account/logout') user = User.create!(:login => 'admin_user', :password => '123456', :password_confirmation => '123456', :email => 'admin_user@example.com') e = Environment.default e.add_admin(user.person) @@ -86,15 +107,16 @@ Given /^I am not logged in$/ do visit('/account/logout') end -Given /^feature "(.+)" is enabled on environment$/ do |feature| +Given /^feature "(.+)" is (enabled|disabled) on environment$/ do |feature, status| e = Environment.default - e.enable(feature) + status.chop! + e.send status, feature e.save end -Given /^feature "(.+)" is disabled on environment$/ do |feature| +Given /^organization_approval_method is "(.+)" on environment$/ do |approval_method| e = Environment.default - e.disable(feature) + e.organization_approval_method = approval_method e.save end @@ -122,17 +144,18 @@ Given /^"([^\"]*)" has no articles$/ do |profile| (Profile[profile] || Profile.find_by_name(profile)).articles.delete_all end -Given /^the following (\w+) fields are enabled$/ do |klass, table| +Given /^the following (\w+) fields are (\w+)$/ do |klass, status, table| env = Environment.default fields = table.raw.inject({}) do |hash, line| hash[line.first] = { "active" => 'true' } + hash[line.first].merge!({ "required" => 'true'}) if status == "required" hash end env.send("custom_#{klass.downcase}_fields=", fields) env.save! - if fields.keys != env.send("active_#{klass.downcase}_fields") - raise "Not all fields enabled! Requested: %s; Enabled: %s" % [fields.keys.inspect, env.send("active_#{klass.downcase}_fields").inspect] + if fields.keys != env.send("#{status}_#{klass.downcase}_fields") + raise "Not all fields #{status}! Requested: %s; #{status.camelcase}: %s" % [fields.keys.inspect, env.send("#{status}_#{klass.downcase}_fields").inspect] end end diff --git a/test/functional/enterprise_registration_controller_test.rb b/test/functional/enterprise_registration_controller_test.rb index d03bd0f..f323d34 100644 --- a/test/functional/enterprise_registration_controller_test.rb +++ b/test/functional/enterprise_registration_controller_test.rb @@ -30,23 +30,29 @@ all_fixtures assert_template 'basic_information' end - should 'prompt for basic information' do - get :index - %w[ name identifier address contact_phone contact_person - acronym foundation_year legal_form economic_activity ].each do |item| - assert_tag :tag => 'input', :attributes => { :name => "create_enterprise[#{item}]" } - end - assert_tag :tag => 'textarea', :attributes => { :name => "create_enterprise[management_information]"} - assert_tag :tag => 'select', :attributes => { :name => "create_enterprise[region_id]"} - end - should 'get back to entering basic information if data is invalid' do post :index, :create_enterprise => {} assert_response :success assert_template 'basic_information' end - should 'prompt for selecting validator' do + should 'skip prompt for selection validator if approval method is admin' do + env = Environment.default + env.organization_approval_method = :admin + env.save + region = fast_create(Region) + + data = { :name => 'My new enterprise', :identifier => 'mynew', :region => region } + create_enterprise = CreateEnterprise.new(data) + + post :index, :create_enterprise => data + assert_template 'confirmation' + end + + should 'prompt for selecting validator if approval method is region' do + env = Environment.default + env.organization_approval_method = :region + env.save data = { 'name' => 'My new enterprise', 'identifier' => 'mynew' } create_enterprise = CreateEnterprise.new @@ -135,8 +141,10 @@ all_fixtures assert_sanitized assigns(:create_enterprise).management_information end - should 'load only regions with validator organizations' do + should 'load only regions with validator organizations if approval method is region' do env = Environment.default + env.organization_approval_method = :region + env.save reg1 = env.regions.create!(:name => 'Region with validator') reg1.validators.create!(:name => 'Validator one', :identifier => 'validator-one') diff --git a/test/integration/enterprise_registration_test.rb b/test/integration/enterprise_registration_test.rb index 73e3442..26d0d59 100644 --- a/test/integration/enterprise_registration_test.rb +++ b/test/integration/enterprise_registration_test.rb @@ -7,6 +7,8 @@ class EnterpriseRegistrationTest < ActionController::IntegrationTest should 'be able to create an enterprise registration request' do environment = Environment.default + environment.organization_approval_method = :region + environment.save region1 = environment.regions.build(:name => 'A region') region1.save! region2 = environment.regions.build(:name => 'Other region') diff --git a/test/unit/create_enterprise_test.rb b/test/unit/create_enterprise_test.rb index 777212c..3c8dd0b 100644 --- a/test/unit/create_enterprise_test.rb +++ b/test/unit/create_enterprise_test.rb @@ -219,6 +219,21 @@ class CreateEnterpriseTest < Test::Unit::TestCase assert request.errors.invalid?(:identifier) end + should 'require the same fields as an enterprise does' do + environment = mock + request = CreateEnterprise.new + request.stubs(:environment).returns(environment) + environment.stubs(:organization_approval_method).returns(:region) + + environment.stubs(:required_enterprise_fields).returns([]) + request.valid? + assert_nil request.errors[:contact_person], 'should not require contact_person unless Enterprise requires it' + + environment.stubs(:required_enterprise_fields).returns(['contact_person']) + request.valid? + assert_not_nil request.errors[:contact_person], 'should require contact_person when Enterprise requires it' + end + should 'has permission to validate enterprise' do t = CreateEnterprise.new assert_equal :validate_enterprise, t.permission diff --git a/test/unit/region_test.rb b/test/unit/region_test.rb index 7ec7355..aad03a9 100644 --- a/test/unit/region_test.rb +++ b/test/unit/region_test.rb @@ -53,4 +53,15 @@ class RegionTest < Test::Unit::TestCase assert !region.has_validator? end + should 'list regions with validators' do + bahia = fast_create(Region, :name => 'Bahia') + forum_ecosol_ba = fast_create(Enterprise, :name => 'Forum Baiano de Economia Solidaria', :identifier => 'ecosol-ba') + bahia.validators << forum_ecosol_ba + + sergipe = fast_create(Region, :name => 'Sergipe') + # Sergipe has no validators + + assert_equivalent Region.with_validators, [bahia] + end + end -- libgit2 0.21.2