cirandas.net

ref: master

test/unit/kind_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
# encoding: UTF-8
require_relative "../test_helper"

class KindTest < ActiveSupport::TestCase
  should 'not be moderated by default' do
    refute Kind.new.moderated
  end

  should 'require an environment' do
    kind = Kind.new
    kind.valid?
    assert kind.errors[:environment].present?
  end

  should 'require a name' do
    kind = Kind.new
    kind.valid?
    assert kind.errors[:name].present?
  end

  should 'create a kind' do
    assert_nothing_raised do
      Kind.create!(:name => 'Regular', :type => 'Profile', :environment => Environment.default)
    end
  end

  should 'not have same name with the same type in the same environment' do
    Kind.create!(:name => 'Regular', :type => 'Profile', :environment => Environment.default)
    kind = Kind.new(:name => 'Regular', :type => 'Profile', :environment => Environment.default)
    kind.valid?
    assert kind.errors[:name].present?
  end

  should 'have profiles' do
    kind = fast_create(Kind, :name=> 'Regular', :type => 'Profile', :environment_id => Environment.default.id)
    p1 = fast_create(Profile)
    p2 = fast_create(Profile)
    p3 = fast_create(Profile)
    kind.profiles << p1
    kind.profiles << p2

    assert_equal 2, kind.profiles.size
    assert_includes kind.profiles, p1
    assert_includes kind.profiles, p2
    assert_not_includes kind.profiles, p3
  end

  should 'add a profile' do
    kind = fast_create(Kind, :name=> 'Regular', :type => 'Profile', :environment_id => Environment.default.id)
    profile = fast_create(Profile)
    assert_not_includes kind.profiles, profile

    kind.add_profile(profile)
    assert_includes kind.profiles, profile
  end

  should 'not add a profile twice' do
    kind = fast_create(Kind, :name=> 'Regular', :type => 'Profile', :environment_id => Environment.default.id)
    profile = fast_create(Profile)
    assert_not_includes kind.profiles, profile

    kind.add_profile(profile)
    kind.add_profile(profile)
    assert_includes kind.profiles, profile
    assert_equal 1, kind.profiles.size
  end

  should 'create ApproveKind task on moderated kinds' do
    kind = fast_create(Kind, :name=> 'Regular', :type => 'Profile', :environment_id => Environment.default.id, :moderated => true)
    profile = fast_create(Profile)
    assert_difference 'ApproveKind.count', 1 do
      kind.add_profile(profile)
    end
  end

  should 'not duplicate ApproveKind task' do
    kind = fast_create(Kind, :name=> 'Regular', :type => 'Profile', :environment_id => Environment.default.id, :moderated => true)
    profile = fast_create(Profile)
    assert_difference 'ApproveKind.count', 1 do
      kind.add_profile(profile)
      kind.add_profile(profile)
    end
  end

  should 'remove a profile' do
    kind = fast_create(Kind, :name=> 'Regular', :type => 'Profile', :environment_id => Environment.default.id)
    profile = fast_create(Profile)
    kind.profiles << profile
    assert_includes kind.profiles, profile

    kind.remove_profile(profile)
    assert_not_includes kind.profiles, profile
  end
end