cirandas.net

ref: master

test/unit/create_community_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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
require_relative "../test_helper"

class CreateCommunityTest < ActiveSupport::TestCase

  def setup
    @person = create_user('testing').person
  end
  attr_reader :person

  should 'provide needed data' do
    task = CreateCommunity.new

    Community.fields + %w[ name closed ].each do |field|
      assert task.respond_to?(field)
      assert task.respond_to?("#{field}=")
    end
  end

  should 'require a requestor' do
    task = CreateCommunity.new(:name => 'community test', :target => Environment.default)
    task.valid?

    assert task.errors[:requestor_id.to_s].present?
    task.requestor = person
    task.valid?
    refute task.errors[:requestor_id.to_s].present?
  end

  should 'actually create a community when finishing the task and associate the task requestor as its admin' do

    task = CreateCommunity.create!({
      :name => 'My new community',
      :requestor => person,
      :target => Environment.default,
    })

    assert_difference 'Community.count' do
      task.finish
    end

    assert_equal person, Community['my-new-community'].admins.first
  end

  should 'override message methods from Task' do
    specific = CreateCommunity.new
    %w[ task_created_message task_finished_message task_cancelled_message ].each do |method|
      assert_nothing_raised NotImplementedError do
        specific.send(method)
      end
    end
  end

  should 'provide a message to be sent to the target' do
    assert_not_nil CreateCommunity.new(:name => 'test comm', :requestor => person).target_notification_message
  end

  should 'report as approved when approved' do
    request = CreateCommunity.new
    request.stubs(:status).returns(Task::Status::FINISHED)
    assert request.approved?
  end

  should 'report as rejected when rejected' do
    request = CreateCommunity.new
    request.stubs(:status).returns(Task::Status::CANCELLED)
    assert request.rejected?
  end

  should 'create a community with image when finishing the task' do

    task = CreateCommunity.create!({
      :name => 'My new community',
      :requestor => person,
      :target => Environment.default,
      :image_builder => {:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}
    })

    assert_equal 'rails.png', task.image.filename
    assert_difference 'Community.count' do
      task.finish
    end

    assert_equal 'rails.png', Community['my-new-community'].image.filename
  end

  should 'have target notification message' do
    task = CreateCommunity.new(:name => 'community test', :target => Environment.default, :requestor => person)

    assert_match(/requested to create community.*to approve or reject/, task.target_notification_message)
  end

  should 'have target notification description' do
    task = CreateCommunity.new(:name => 'community test', :target => Environment.default, :requestor => person)

    assert_match(/#{task.requestor.name} wants to create community #{task.subject}/, task.target_notification_description)
  end

  should 'deliver target notification message' do
    task = CreateCommunity.new(:name => 'community test', :target => Environment.default, :requestor => person)

    email = TaskMailer.target_notification(task, task.target_notification_message).deliver
    assert_match(/#{task.requestor.name} wants to create community #{task.subject}/, email.subject)
  end

end