cirandas.net

ref: master

test/functional/licenses_controller_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
require_relative '../test_helper'

class LicensesControllerTest < ActionController::TestCase

  def setup
    @controller = LicensesController.new

    @environment = Environment.default
    login_as(create_admin_user(@environment))
  end

  attr_accessor :environment

  should 'list environment licenses' do
    l1 = License.create!(:name => 'GPLv3', :environment => environment)
    l2 = License.create!(:name => 'AGPL', :environment => environment)

    get :index

    assert_includes assigns(:licenses), l1
    assert_includes assigns(:licenses), l2
  end

  should 'not list licenses from other environments' do
    other_env = fast_create(Environment)
    l1 = License.create!(:name => 'GPLv3', :environment => environment)
    l2 = License.create!(:name => 'AGPL', :environment => other_env)
    @controller.stubs(:environment).returns(environment)

    get :index

    assert_includes assigns(:licenses), l1
    assert_not_includes assigns(:licenses), l2
  end

  should 'create a new license' do
    assert_difference 'License.count', 1 do
      post :create, :license => {:name => 'GPLv3'}
    end
  end

  should 'edit a license' do
    license = License.create!(:name => 'GPLv2', :environment => environment)
    post :edit, :license_id => license.id, :license => {:name => 'GPLv3'}
    assert_equal 'GPLv3', License.last.name
  end

  should 'remove a license' do
    license = License.create!(:name => 'GPLv3', :environment => environment)
    post :remove, :license_id => license.id
    assert_raise(ActiveRecord::RecordNotFound) {License.find(license.id)}
  end

  should 'remove a license only with post method' do
    license = License.create!(:name => 'GPLv3', :environment => environment)
    get :remove, :license_id => license.id
    assert_nothing_raised ActiveRecord::RecordNotFound do
      License.find(license.id)
    end
    assert_redirected_to :action => 'index'
  end

end