cirandas.net

ref: master

plugins/custom_forms/test/unit/ext/role_assingment_test.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'test_helper'

class RoleAssignmentsTest < ActiveSupport::TestCase
  should 'create membership_surveys on membership creation' do
    environment = Environment.default
    environment.enable_plugin(CustomFormsPlugin)
    organization = fast_create(Organization)
    person = create_user('john').person
    f1 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 1', :on_membership => true)
    f2 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 2', :on_membership => true)
    f3 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 3', :on_membership => false)

    assert_difference 'CustomFormsPlugin::MembershipSurvey.count', 2 do
      organization.add_member(person)
    end
  end

  should 'create membership_survey on membership creation with form accessible to members only' do
    environment = Environment.default
    environment.enable_plugin(CustomFormsPlugin)
    organization = fast_create(Organization)
    person = create_user('john').person
    form = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form', :on_membership => true, :access => 'associated')

    assert_difference 'CustomFormsPlugin::MembershipSurvey.count', 1 do
      organization.add_member(person)
    end
  end

  should 'cancel surveys if membership is undone and task is active' do
    environment = Environment.default
    environment.enable_plugin(CustomFormsPlugin)
    organization = fast_create(Organization)
    person = create_user('john').person
    form1 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 1', :on_membership => true)
    organization.add_member(person)

    assert_difference 'CustomFormsPlugin::MembershipSurvey.pending.count', -1 do
      organization.remove_member(person)
    end

    form2 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 2', :for_admission => true)
    organization.add_member(person)

    assert_difference 'CustomFormsPlugin::AdmissionSurvey.pending.count', -1 do
      organization.remove_member(person)
    end

    organization.add_member(person)
    tasks = CustomFormsPlugin::MembershipSurvey.all.last(2)
    tasks.each {|t| t.status = Task::Status::FINISHED }
    tasks.each {|t| t.save! }
    assert_no_difference 'CustomFormsPlugin::MembershipSurvey.finished.count' do
      organization.remove_member(person)
    end
  end

  should 'create admission survey when atempted membership' do
    environment = Environment.default
    environment.enable_plugin(CustomFormsPlugin)
    organization = fast_create(Organization)
    person = create_user('john').person
    f1 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 1', :for_admission => true)
    f2 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 2', :for_admission => true)
    f3 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 3', :for_admission => false)

    assert_difference 'CustomFormsPlugin::AdmissionSurvey.count', 2 do
      organization.add_member(person)
    end
    refute organization.members.include?(person)
  end
end