fesplugas-typus 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. data/.gitignore +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +80 -0
  4. data/Rakefile +61 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/admin/master_controller.rb +324 -0
  7. data/app/controllers/typus_controller.rb +127 -0
  8. data/app/helpers/admin/form_helper.rb +351 -0
  9. data/app/helpers/admin/master_helper.rb +99 -0
  10. data/app/helpers/admin/public_helper.rb +24 -0
  11. data/app/helpers/admin/sidebar_helper.rb +259 -0
  12. data/app/helpers/admin/table_helper.rb +227 -0
  13. data/app/helpers/typus_helper.rb +169 -0
  14. data/app/models/typus_mailer.rb +14 -0
  15. data/app/models/typus_user.rb +5 -0
  16. data/app/views/admin/dashboard/_sidebar.html.erb +9 -0
  17. data/app/views/admin/resources/edit.html.erb +29 -0
  18. data/app/views/admin/resources/index.html.erb +28 -0
  19. data/app/views/admin/resources/new.html.erb +27 -0
  20. data/app/views/admin/resources/show.html.erb +21 -0
  21. data/app/views/admin/shared/_footer.html.erb +1 -0
  22. data/app/views/admin/shared/_pagination.html.erb +28 -0
  23. data/app/views/layouts/admin.html.erb +72 -0
  24. data/app/views/layouts/typus.html.erb +29 -0
  25. data/app/views/typus/dashboard.html.erb +13 -0
  26. data/app/views/typus/recover_password.html.erb +7 -0
  27. data/app/views/typus/reset_password.html.erb +13 -0
  28. data/app/views/typus/sign_in.html.erb +9 -0
  29. data/app/views/typus/sign_up.html.erb +7 -0
  30. data/app/views/typus_mailer/reset_password_link.erb +11 -0
  31. data/config/locales/es.yml +106 -0
  32. data/config/locales/pt-BR.yml +108 -0
  33. data/config/locales/typus_hacks.yml +14 -0
  34. data/config/routes.rb +14 -0
  35. data/generators/typus/templates/config/initializers/typus.rb +27 -0
  36. data/generators/typus/templates/config/typus/application.yml +45 -0
  37. data/generators/typus/templates/config/typus/application_roles.yml +23 -0
  38. data/generators/typus/templates/config/typus/typus.yml +14 -0
  39. data/generators/typus/templates/config/typus/typus_roles.yml +2 -0
  40. data/generators/typus/templates/db/create_typus_users.rb +21 -0
  41. data/generators/typus/templates/public/images/admin/arrow_down.gif +0 -0
  42. data/generators/typus/templates/public/images/admin/arrow_up.gif +0 -0
  43. data/generators/typus/templates/public/images/admin/spinner.gif +0 -0
  44. data/generators/typus/templates/public/images/admin/status_false.gif +0 -0
  45. data/generators/typus/templates/public/images/admin/status_true.gif +0 -0
  46. data/generators/typus/templates/public/images/admin/trash.gif +0 -0
  47. data/generators/typus/templates/public/javascripts/admin/application.js +14 -0
  48. data/generators/typus/templates/public/stylesheets/admin/reset.css +68 -0
  49. data/generators/typus/templates/public/stylesheets/admin/screen.css +709 -0
  50. data/generators/typus/typus_generator.rb +141 -0
  51. data/generators/typus_update_schema_to_01/templates/config/typus.yml +14 -0
  52. data/generators/typus_update_schema_to_01/templates/migration.rb +11 -0
  53. data/generators/typus_update_schema_to_01/typus_update_schema_to_01_generator.rb +19 -0
  54. data/init.rb +19 -0
  55. data/lib/typus/active_record.rb +298 -0
  56. data/lib/typus/authentication.rb +155 -0
  57. data/lib/typus/configuration.rb +92 -0
  58. data/lib/typus/format.rb +56 -0
  59. data/lib/typus/generator.rb +173 -0
  60. data/lib/typus/hash.rb +10 -0
  61. data/lib/typus/locale.rb +17 -0
  62. data/lib/typus/object.rb +22 -0
  63. data/lib/typus/quick_edit.rb +33 -0
  64. data/lib/typus/reloader.rb +17 -0
  65. data/lib/typus/string.rb +11 -0
  66. data/lib/typus/user.rb +137 -0
  67. data/lib/typus.rb +133 -0
  68. data/lib/vendor/active_record.rb +15 -0
  69. data/lib/vendor/paginator.rb +143 -0
  70. data/tasks/typus_tasks.rake +26 -0
  71. data/test/config/broken/application.yml +68 -0
  72. data/test/config/broken/application_roles.yml +20 -0
  73. data/test/config/broken/empty.yml +0 -0
  74. data/test/config/broken/empty_roles.yml +0 -0
  75. data/test/config/broken/undefined.yml +3 -0
  76. data/test/config/broken/undefined_roles.yml +6 -0
  77. data/test/config/default/typus.yml +14 -0
  78. data/test/config/default/typus_roles.yml +2 -0
  79. data/test/config/empty/empty_01.yml +0 -0
  80. data/test/config/empty/empty_01_roles.yml +0 -0
  81. data/test/config/empty/empty_02.yml +0 -0
  82. data/test/config/empty/empty_02_roles.yml +0 -0
  83. data/test/config/locales/es.yml +10 -0
  84. data/test/config/ordered/001_roles.yml +2 -0
  85. data/test/config/ordered/002_roles.yml +2 -0
  86. data/test/config/unordered/app_one_roles.yml +2 -0
  87. data/test/config/unordered/app_two_roles.yml +2 -0
  88. data/test/config/working/application.yml +67 -0
  89. data/test/config/working/application_roles.yml +22 -0
  90. data/test/config/working/typus.yml +14 -0
  91. data/test/config/working/typus_roles.yml +2 -0
  92. data/test/fixtures/app/controllers/admin/assets_controller.rb +2 -0
  93. data/test/fixtures/app/controllers/admin/categories_controller.rb +2 -0
  94. data/test/fixtures/app/controllers/admin/comments_controller.rb +2 -0
  95. data/test/fixtures/app/controllers/admin/pages_controller.rb +2 -0
  96. data/test/fixtures/app/controllers/admin/posts_controller.rb +2 -0
  97. data/test/fixtures/app/controllers/admin/status_controller.rb +6 -0
  98. data/test/fixtures/app/controllers/admin/typus_users_controller.rb +2 -0
  99. data/test/fixtures/app/controllers/admin/watch_dog_controller.rb +6 -0
  100. data/test/fixtures/app/views/admin/comments/_edit_bottom.html.erb +1 -0
  101. data/test/fixtures/app/views/admin/comments/_edit_sidebar.html.erb +1 -0
  102. data/test/fixtures/app/views/admin/comments/_edit_top.html.erb +1 -0
  103. data/test/fixtures/app/views/admin/comments/_index_bottom.html.erb +1 -0
  104. data/test/fixtures/app/views/admin/comments/_index_sidebar.html.erb +1 -0
  105. data/test/fixtures/app/views/admin/comments/_index_top.html.erb +1 -0
  106. data/test/fixtures/app/views/admin/comments/_new_bottom.html.erb +1 -0
  107. data/test/fixtures/app/views/admin/comments/_new_sidebar.html.erb +1 -0
  108. data/test/fixtures/app/views/admin/comments/_new_top.html.erb +1 -0
  109. data/test/fixtures/app/views/admin/comments/_show_bottom.html.erb +1 -0
  110. data/test/fixtures/app/views/admin/comments/_show_sidebar.html.erb +1 -0
  111. data/test/fixtures/app/views/admin/comments/_show_top.html.erb +1 -0
  112. data/test/fixtures/app/views/admin/dashboard/_bottom.html.erb +1 -0
  113. data/test/fixtures/app/views/admin/dashboard/_sidebar.html.erb +1 -0
  114. data/test/fixtures/app/views/admin/dashboard/_top.html.erb +1 -0
  115. data/test/fixtures/app/views/admin/shared/_footer.html.erb +1 -0
  116. data/test/fixtures/app/views/admin/status/index.html.erb +1 -0
  117. data/test/fixtures/app/views/admin/templates/_datepicker.html.erb +1 -0
  118. data/test/fixtures/assets.yml +11 -0
  119. data/test/fixtures/categories.yml +14 -0
  120. data/test/fixtures/comments.yml +27 -0
  121. data/test/fixtures/pages.yml +41 -0
  122. data/test/fixtures/posts.yml +37 -0
  123. data/test/fixtures/typus_users.yml +54 -0
  124. data/test/functional/admin/assets_controller_test.rb +57 -0
  125. data/test/functional/admin/categories_controller_test.rb +106 -0
  126. data/test/functional/admin/comments_controller_test.rb +121 -0
  127. data/test/functional/admin/master_controller_test.rb +5 -0
  128. data/test/functional/admin/posts_controller_test.rb +278 -0
  129. data/test/functional/admin/status_controller_test.rb +43 -0
  130. data/test/functional/admin/typus_users_controller_test.rb +239 -0
  131. data/test/functional/typus_controller_test.rb +315 -0
  132. data/test/helper.rb +51 -0
  133. data/test/helpers/admin/form_helper_test.rb +316 -0
  134. data/test/helpers/admin/master_helper_test.rb +65 -0
  135. data/test/helpers/admin/public_helper_test.rb +22 -0
  136. data/test/helpers/admin/sidebar_helper_test.rb +351 -0
  137. data/test/helpers/admin/table_helper_test.rb +255 -0
  138. data/test/helpers/typus_helper_test.rb +106 -0
  139. data/test/lib/active_record_test.rb +372 -0
  140. data/test/lib/configuration_test.rb +91 -0
  141. data/test/lib/hash_test.rb +11 -0
  142. data/test/lib/routes_test.rb +82 -0
  143. data/test/lib/string_test.rb +25 -0
  144. data/test/lib/typus_test.rb +105 -0
  145. data/test/models.rb +51 -0
  146. data/test/schema.rb +64 -0
  147. data/test/unit/typus_mailer_test.rb +33 -0
  148. data/test/unit/typus_test.rb +17 -0
  149. data/test/unit/typus_user_roles_test.rb +90 -0
  150. data/test/unit/typus_user_test.rb +177 -0
  151. data/test/vendor/active_record_test.rb +18 -0
  152. data/test/vendor/paginator_test.rb +138 -0
  153. data/typus.gemspec +225 -0
  154. metadata +241 -0
@@ -0,0 +1,105 @@
1
+ require 'test/helper'
2
+
3
+ class TypusTest < ActiveSupport::TestCase
4
+
5
+ def test_should_return_locales
6
+ initializer = "#{Rails.root}/config/initializers/typus.rb"
7
+ return if File.exists?(initializer)
8
+ assert Typus.respond_to?(:locales)
9
+ assert Typus.locales.kind_of?(Array)
10
+ assert_equal [["English", :en]], Typus.locales
11
+ end
12
+
13
+ def test_should_return_default_locale
14
+ assert Typus.respond_to?(:default_locale)
15
+ assert Typus.default_locale.kind_of?(Symbol)
16
+ assert_equal :en, Typus.default_locale
17
+ end
18
+
19
+ def test_should_return_applications_and_should_be_sorted
20
+ assert Typus.respond_to?(:applications)
21
+ assert Typus.applications.kind_of?(Array)
22
+ assert_equal %w( Blog Site Typus ), Typus.applications
23
+ end
24
+
25
+ def test_should_return_modules_of_an_application
26
+ assert Typus.respond_to?(:application)
27
+ assert_equal %w( Comment Post ), Typus.application('Blog')
28
+ end
29
+
30
+ def test_should_return_modules_of_a_module
31
+ assert Typus.respond_to?(:module)
32
+ assert_equal %w( Category ), Typus.module(Post)
33
+ end
34
+
35
+ def test_should_verify_parent_exists
36
+ assert Typus.respond_to?(:parent)
37
+ end
38
+
39
+ =begin
40
+
41
+ # FIXME
42
+
43
+ def test_should_verify_parent_for_module
44
+ assert Typus.parent(TypusUser.name, 'module').kind_of?(String)
45
+ assert_equal 'Typus', Typus.parent(TypusUser.name, 'module')
46
+ end
47
+
48
+ =end
49
+
50
+ def test_should_verify_parent_for_application
51
+ assert Typus.parent(TypusUser, 'application').kind_of?(String)
52
+ assert_equal 'Typus', Typus.parent(TypusUser, 'application')
53
+ end
54
+
55
+ def test_should_verify_parent_for_nothing
56
+ assert Typus.parent(TypusUser, 'nothing').kind_of?(String)
57
+ assert_equal '', Typus.parent(TypusUser, 'nothing')
58
+ end
59
+
60
+ def test_should_return_models_and_should_be_sorted
61
+ assert Typus.respond_to?(:models)
62
+ assert Typus.models.kind_of?(Array)
63
+ assert_equal %w( Asset Category Comment CustomUser Page Post TypusUser ), Typus.models
64
+ end
65
+
66
+ def test_should_verify_resources_class_method
67
+ assert Typus.respond_to?(:resources)
68
+ models = Typus.models
69
+ assert_equal %w( Git Order Status WatchDog ), Typus.resources(models)
70
+ end
71
+
72
+ def test_should_return_description_of_module
73
+ assert Typus.respond_to?(:module_description)
74
+ assert_equal 'System Users Administration', Typus.module_description('TypusUser')
75
+ end
76
+
77
+ def test_should_verify_enable_exists
78
+ assert Typus.respond_to?(:enable)
79
+ end
80
+
81
+ def test_should_verify_enable_exists
82
+ assert Typus.respond_to?(:generator)
83
+ end
84
+
85
+ def test_should_return_user_class
86
+ assert_equal TypusUser, Typus.user_class
87
+ end
88
+
89
+ def test_should_return_overwritted_user_class
90
+ options = { :user_class_name => 'CustomUser' }
91
+ Typus::Configuration.stubs(:options).returns(options)
92
+ assert_equal CustomUser, Typus.user_class
93
+ end
94
+
95
+ def test_should_return_user_fk
96
+ assert_equal 'typus_user_id', Typus.user_fk
97
+ end
98
+
99
+ def test_should_return_overwritted_user_fk
100
+ options = { :user_fk => 'my_user_fk' }
101
+ Typus::Configuration.stubs(:options).returns(options)
102
+ assert_equal 'my_user_fk', Typus.user_fk
103
+ end
104
+
105
+ end
data/test/models.rb ADDED
@@ -0,0 +1,51 @@
1
+ class Asset < ActiveRecord::Base
2
+
3
+ belongs_to :resource, :polymorphic => true
4
+
5
+ end
6
+
7
+ class Category < ActiveRecord::Base
8
+
9
+ acts_as_list if defined?(ActiveRecord::Acts::List)
10
+
11
+ validates_presence_of :name
12
+ has_and_belongs_to_many :posts
13
+
14
+ def self.typus
15
+ end
16
+
17
+ end
18
+
19
+ class Comment < ActiveRecord::Base
20
+
21
+ validates_presence_of :name, :email, :body
22
+ belongs_to :post
23
+
24
+ end
25
+
26
+ class CustomUser < ActiveRecord::Base
27
+ end
28
+
29
+ class Page < ActiveRecord::Base
30
+
31
+ acts_as_tree if defined?(ActiveRecord::Acts::Tree)
32
+
33
+ end
34
+
35
+ class Post < ActiveRecord::Base
36
+
37
+ validates_presence_of :title, :body
38
+ has_and_belongs_to_many :categories
39
+ has_many :comments
40
+ has_many :assets, :as => :resource, :dependent => :destroy
41
+ belongs_to :favorite_comment, :class_name => 'Comment'
42
+
43
+ def self.status
44
+ %w( true false pending published unpublished )
45
+ end
46
+
47
+ def self.typus
48
+ 'plugin'
49
+ end
50
+
51
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,64 @@
1
+ ActiveRecord::Migration.verbose = false
2
+
3
+ ActiveRecord::Schema.define do
4
+
5
+ create_table :assets, :force => true do |t|
6
+ t.string :caption
7
+ t.string :resource_type
8
+ t.integer :resource_id
9
+ t.integer :position
10
+ end
11
+
12
+ create_table :categories, :force => true do |t|
13
+ t.string :name
14
+ t.string :permalink
15
+ t.text :description
16
+ t.integer :position
17
+ end
18
+
19
+ create_table :comments, :force => true do |t|
20
+ t.string :email, :name
21
+ t.text :body
22
+ t.integer :post_id
23
+ end
24
+
25
+ add_index :comments, :post_id
26
+
27
+ create_table :pages, :force => true do |t|
28
+ t.string :title
29
+ t.text :body
30
+ t.boolean :is_published
31
+ t.integer :parent_id
32
+ end
33
+
34
+ create_table :posts, :force => true do |t|
35
+ t.string :title
36
+ t.text :body
37
+ t.boolean :status
38
+ t.integer :favorite_comment_id
39
+ t.timestamps
40
+ t.datetime :published_at
41
+ t.integer :typus_user_id
42
+ end
43
+
44
+ create_table :typus_users, :force => true do |t|
45
+ t.string :first_name, :default => "", :null => false
46
+ t.string :last_name, :default => "", :null => false
47
+ t.string :role, :null => false
48
+ t.string :email, :null => false
49
+ t.boolean :status, :default => false
50
+ t.string :token, :null => false
51
+ t.string :salt, :null => false
52
+ t.string :crypted_password, :null => false
53
+ t.timestamps
54
+ end
55
+
56
+ create_table :categories_posts, :force => true, :id => false do |t|
57
+ t.column :category_id, :integer
58
+ t.column :post_id, :integer
59
+ end
60
+
61
+ add_index :categories_posts, :category_id
62
+ add_index :categories_posts, :post_id
63
+
64
+ end
@@ -0,0 +1,33 @@
1
+ require 'test/helper'
2
+
3
+ class TypusMailerTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @user = typus_users(:admin)
7
+ ActionMailer::Base.default_url_options[:host] = 'test.host'
8
+ @response = TypusMailer.deliver_reset_password_link(@user)
9
+ end
10
+
11
+ def test_should_verify_email_from_is_defined_by_typus_options
12
+ assert_equal [ Typus::Configuration.options[:email] ], @response.from
13
+ end
14
+
15
+ def test_should_verify_email_to_is_typus_user_email
16
+ assert_equal [ @user.email ], @response.to
17
+ end
18
+
19
+ def test_should_verify_email_subject
20
+ expected = "[#{Typus::Configuration.options[:app_name]}] Reset password"
21
+ assert_equal expected, @response.subject
22
+ end
23
+
24
+ def test_should_verify_email_contains_reset_password_link_with_token
25
+ expected = "http://test.host/admin/reset_password?token=1A2B3C4D5E6F"
26
+ assert_match expected, @response.body
27
+ end
28
+
29
+ def test_should_check_email_contains_signature
30
+ assert_match /--\n#{Typus::Configuration.options[:app_name]}/, @response.body
31
+ end
32
+
33
+ end
@@ -0,0 +1,17 @@
1
+ require 'test/helper'
2
+
3
+ class TypusTest < ActiveSupport::TestCase
4
+
5
+ def test_should_verify_models
6
+ models = [ Category, Comment, Post, TypusUser ]
7
+ models.each { |m| assert m.superclass.equal?(ActiveRecord::Base) }
8
+ end
9
+
10
+ def test_should_verify_fixtures_are_loaded
11
+ assert_equal 3, Category.count
12
+ assert_equal 4, Comment.count
13
+ assert_equal 4, Post.count
14
+ assert_equal 5, TypusUser.count
15
+ end
16
+
17
+ end
@@ -0,0 +1,90 @@
1
+ require 'test/helper'
2
+
3
+ class TypusUserRolesTest < ActiveSupport::TestCase
4
+
5
+ def test_should_get_list_of_roles
6
+ roles = %w( admin designer editor )
7
+ assert_equal roles, Typus::Configuration.roles.map(&:first).sort
8
+ end
9
+
10
+ def test_admin_role_settings
11
+
12
+ typus_user = typus_users(:admin)
13
+ assert_equal 'admin', typus_user.role
14
+
15
+ models = %w( Asset Category Comment Git Page Post Status TypusUser WatchDog )
16
+ assert_equal models, typus_user.resources.map(&:first).sort
17
+
18
+ # Order exists on the roles, but, as we compact the hash, the
19
+ # resource is removed.
20
+ assert !typus_user.resources.map(&:first).include?('Order')
21
+
22
+ resources = %w( Git Status WatchDog )
23
+ models.delete_if { |m| resources.include?(m) }
24
+
25
+ %w( create read update destroy ).each do |action|
26
+ models.each { |model| assert typus_user.can_perform?(model, action) }
27
+ end
28
+
29
+ # Order resource doesn't have an index action, so we current user
30
+ # cannot perform the action.
31
+ assert !typus_user.can_perform?('Order', 'index')
32
+
33
+ # Status resource has an index action, but not a show one.
34
+ # We add the { :special => true } option to by-pass the action
35
+ # renaming performed in the TypusUser#can_perform? method.
36
+ assert typus_user.can_perform?('Status', 'index', { :special => true })
37
+ assert !typus_user.can_perform?('Status', 'show', { :special => true })
38
+
39
+ end
40
+
41
+ def test_editor_role_settings
42
+
43
+ typus_user = typus_users(:editor)
44
+ assert_equal 'editor', typus_user.role
45
+
46
+ %w( Category Comment Git Post TypusUser ).each do |model|
47
+ assert typus_user.resources.map(&:first).include?(model)
48
+ end
49
+
50
+ # Category: create, read, update
51
+ %w( create read update ).each { |action| assert typus_user.can_perform?(Category, action) }
52
+ %w( delete ).each { |action| assert !typus_user.can_perform?(Category, action) }
53
+
54
+ # Post: create, read, update
55
+ %w( create read update ).each { |action| assert typus_user.can_perform?(Post, action) }
56
+ %w( delete ).each { |action| assert !typus_user.can_perform?(Post, action) }
57
+
58
+ # Comment: read, update, delete
59
+ %w( read update delete ).each { |action| assert typus_user.can_perform?(Comment, action) }
60
+ %w( create ).each { |action| assert !typus_user.can_perform?(Comment, action) }
61
+
62
+ # TypusUser: read, update
63
+ %w( read update ).each { |action| assert typus_user.can_perform?(TypusUser, action) }
64
+ %w( create delete ).each { |action| assert !typus_user.can_perform?(TypusUser, action) }
65
+
66
+ end
67
+
68
+ def test_designer_role_settings
69
+
70
+ typus_user = typus_users(:designer)
71
+ assert_equal 'designer', typus_user.role
72
+
73
+ models = %w( Category Comment Post )
74
+ assert_equal models, typus_user.resources.map(&:first).sort
75
+
76
+ # Category: read, update
77
+ %w( read update ).each { |action| assert typus_user.can_perform?(Category, action) }
78
+ %w( create delete ).each { |action| assert !typus_user.can_perform?(Category, action) }
79
+
80
+ # Comment: read
81
+ %w( read ).each { |action| assert typus_user.can_perform?(Comment, action) }
82
+ %w( create update delete ).each { |action| assert !typus_user.can_perform?(Comment, action) }
83
+
84
+ # Post: read
85
+ %w( read ).each { |action| assert typus_user.can_perform?(Post, action) }
86
+ %w( create update delete ).each { |action| assert !typus_user.can_perform?(Post, action) }
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,177 @@
1
+ require 'test/helper'
2
+
3
+ class TypusUserTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @data = { :first_name => '',
7
+ :last_name => '',
8
+ :email => 'test@example.com',
9
+ :password => '12345678',
10
+ :password_confirmation => '12345678',
11
+ :role => Typus::Configuration.options[:root] }
12
+ @typus_user = TypusUser.new(@data)
13
+ end
14
+
15
+ def test_should_verify_typus_user_attributes
16
+ %w( first_name last_name email role salt crypted_password ).each do |attribute|
17
+ assert TypusUser.instance_methods.include?(attribute)
18
+ end
19
+ end
20
+
21
+ def test_should_verify_email_format
22
+ @typus_user.email = 'admin'
23
+ assert @typus_user.invalid?
24
+ assert @typus_user.errors.invalid?(:email)
25
+ end
26
+
27
+ def test_should_verify_email_is_not_valid
28
+ email = <<-END
29
+ this_is_chelm@example.com
30
+ <script>location.href="http://spammersite.com"</script>
31
+ END
32
+ @typus_user.email = email
33
+ assert @typus_user.invalid?
34
+ assert @typus_user.errors.invalid?(:email)
35
+ end
36
+
37
+ def test_should_verify_emails_are_downcase
38
+ email = 'TEST@EXAMPLE.COM'
39
+ @typus_user.email = email
40
+ assert @typus_user.invalid?
41
+ assert @typus_user.errors.invalid?(:email)
42
+ end
43
+
44
+ def test_should_verify_some_valid_emails_schemas
45
+ emails = [ 'test+filter@example.com',
46
+ 'test.filter@example.com',
47
+ 'test@example.co.uk',
48
+ 'test@example.es' ]
49
+ emails.each do |email|
50
+ @typus_user.email = email
51
+ assert @typus_user.valid?
52
+ end
53
+ end
54
+
55
+ def test_should_verify_invalid_emails_are_detected
56
+ emails = [ 'test@example', 'test@example.c', 'testexample.com' ]
57
+ emails.each do |email|
58
+ @typus_user.email = email
59
+ assert @typus_user.invalid?
60
+ assert @typus_user.errors.invalid?(:email)
61
+ end
62
+ end
63
+
64
+ def test_should_verify_email_is_unique
65
+ @typus_user.save
66
+ @another_typus_user = TypusUser.new(@data)
67
+ assert @another_typus_user.invalid?
68
+ assert @another_typus_user.errors.invalid?(:email)
69
+ end
70
+
71
+ def test_should_verify_length_of_password_when_under_within
72
+ @typus_user.password = '1234'
73
+ @typus_user.password_confirmation = '1234'
74
+ assert @typus_user.invalid?
75
+ assert @typus_user.errors.invalid?(:password)
76
+ end
77
+
78
+ def test_should_verify_length_of_password_when_its_within_on_lower_limit
79
+ @typus_user.password = '=' * 8
80
+ @typus_user.password_confirmation = '=' * 8
81
+ assert @typus_user.valid?
82
+ end
83
+
84
+ def test_should_verify_length_of_password_when_its_within_on_upper_limit
85
+ @typus_user.password = '=' * 40
86
+ @typus_user.password_confirmation = '=' * 40
87
+ assert @typus_user.valid?
88
+ end
89
+
90
+ def test_should_verify_length_of_password_when_its_over_within
91
+ @typus_user.password = '=' * 50
92
+ @typus_user.password_confirmation = '=' * 50
93
+ assert @typus_user.invalid?
94
+ assert @typus_user.errors.invalid?(:password)
95
+ end
96
+
97
+ def test_should_verify_confirmation_of_password
98
+
99
+ @typus_user.password = '12345678'
100
+ @typus_user.password_confirmation = '87654321'
101
+ assert @typus_user.invalid?
102
+ assert @typus_user.errors.invalid?(:password)
103
+
104
+ =begin
105
+
106
+ # FIXME
107
+
108
+ @typus_user.password = '12345678'
109
+ @typus_user.password_confirmation = nil
110
+ assert @typus_user.invalid?
111
+ assert @typus_user.errors.invalid?(:password)
112
+
113
+ =end
114
+
115
+ @typus_user.password = '12345678'
116
+ @typus_user.password_confirmation = ''
117
+ assert @typus_user.invalid?
118
+ assert @typus_user.errors.invalid?(:password)
119
+
120
+ end
121
+
122
+ def test_should_verify_role
123
+ @typus_user.role = ''
124
+ assert @typus_user.invalid?
125
+ assert @typus_user.errors.invalid?(:role)
126
+ assert_equal "can't be blank", @typus_user.errors[:role]
127
+ end
128
+
129
+ def test_should_return_full_name
130
+ assert_equal "#{@typus_user.email} (#{@typus_user.role})", @typus_user.full_name(:display_role => true)
131
+ assert_equal "#{@typus_user.email}", @typus_user.full_name
132
+ end
133
+
134
+ def test_should_return_full_name_with_role
135
+ @typus_user.first_name = 'John'
136
+ @typus_user.last_name = 'Smith'
137
+ assert_equal "John Smith (#{@typus_user.role})", @typus_user.full_name(:display_role => true)
138
+ assert_equal 'John Smith', @typus_user.full_name
139
+ end
140
+
141
+ def test_should_return_verify_is_root
142
+ assert @typus_user.is_root?
143
+ editor = typus_users(:editor)
144
+ assert !editor.is_root?
145
+ end
146
+
147
+ def test_should_verify_authenticated
148
+ @typus_user.save
149
+ assert @typus_user.authenticated?('12345678')
150
+ assert !@typus_user.authenticated?('87654321')
151
+ end
152
+
153
+ def test_should_verify_typus_user_can_be_created
154
+ assert_difference 'TypusUser.count' do
155
+ TypusUser.create(@data)
156
+ end
157
+ end
158
+
159
+ def test_should_verify_salt_on_user_never_changes
160
+
161
+ @typus_user.save
162
+ salt = @typus_user.salt
163
+ crypted_password = @typus_user.crypted_password
164
+
165
+ @typus_user.update_attributes :password => '11111111', :password_confirmation => '11111111'
166
+ assert_equal salt, @typus_user.salt
167
+ assert_not_equal crypted_password, @typus_user.crypted_password
168
+
169
+ end
170
+
171
+ def test_should_verify_generate
172
+ assert TypusUser.respond_to?(:generate)
173
+ assert TypusUser.generate('demo@example.com', 'XXXXXXXX').valid?
174
+ assert TypusUser.generate('demo@example.com', 'XXXX').invalid?
175
+ end
176
+
177
+ end
@@ -0,0 +1,18 @@
1
+ require 'test/helper'
2
+
3
+ class ActiveRecordTest < ActiveSupport::TestCase
4
+
5
+ def test_should_verify_to_dom
6
+
7
+ assert_equal "typus_user_new", TypusUser.new.to_dom
8
+
9
+ typus_user = typus_users(:admin)
10
+ assert_equal "typus_user_#{typus_user.id}", typus_user.to_dom
11
+
12
+ assert_equal "prefix_typus_user_#{typus_user.id}", typus_user.to_dom(:prefix => 'prefix')
13
+ assert_equal "typus_user_#{typus_user.id}_suffix", typus_user.to_dom(:suffix => 'suffix')
14
+ assert_equal "prefix_typus_user_#{typus_user.id}_suffix", typus_user.to_dom(:prefix => 'prefix', :suffix => 'suffix')
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,138 @@
1
+ require 'test/helper'
2
+
3
+ # require 'paginator'
4
+
5
+ class PaginatorTest < ActiveSupport::TestCase
6
+
7
+ PER_PAGE = 10
8
+
9
+ def setup
10
+ @data = (0..43).to_a
11
+ @pager = Paginator.new(@data.size, PER_PAGE) do |offset,per_page|
12
+ @data[offset,per_page]
13
+ end
14
+ end
15
+
16
+ def test_initializing_paginator_raises_exception
17
+ assert_raises(Paginator::MissingSelectError) do
18
+ @pager = Paginator.new(@data.size, PER_PAGE)
19
+ end
20
+ end
21
+
22
+ def test_can_get_last_page_from_page_object
23
+ assert_equal @pager.last, @pager.page(2).last
24
+ end
25
+
26
+ def test_can_get_first_page_from_page_object
27
+ assert_equal @pager.first, @pager.page(2).first
28
+ end
29
+
30
+ def test_does_not_exceed_per_page
31
+ @pager.each do |page|
32
+ assert page.items.size <= PER_PAGE
33
+ end
34
+ end
35
+
36
+ def test_only_last_page_has_less_items
37
+ @pager.each do |page|
38
+ if page != @pager.last
39
+ assert page.items.size <= PER_PAGE
40
+ else
41
+ assert page.items.size < PER_PAGE
42
+ end
43
+ end
44
+ end
45
+
46
+ def test_returns_correct_first_page
47
+ assert_equal @pager.page(1).number, @pager.first.number
48
+ end
49
+
50
+ def test_returns_correct_last_page
51
+ assert_equal @pager.page(5).number, @pager.last.number
52
+ end
53
+
54
+ def test_last_page_has_no_next_page
55
+ assert !@pager.last.next?
56
+ assert !@pager.last.next
57
+ end
58
+
59
+ def test_first_page_has_no_prev_page
60
+ assert !@pager.first.prev?
61
+ assert !@pager.first.prev
62
+ end
63
+
64
+ def test_page_enumerable
65
+ @pager.each do |page|
66
+ assert page
67
+ page.each do |item|
68
+ assert item
69
+ end
70
+ page.each_with_index do |item, index|
71
+ assert_equal page.items[index], item
72
+ end
73
+ assert_equal page.items, page.inject([]) {|list, item| list << item }
74
+ end
75
+ end
76
+
77
+ def test_each_with_index
78
+ page_offset = 0
79
+ @pager.each_with_index do |page, page_index|
80
+ assert page
81
+ assert_equal page_offset, page_index
82
+ item_offset = 0
83
+ page.each_with_index do |item, item_index|
84
+ assert item
85
+ assert_equal item_offset, item_index
86
+ item_offset += 1
87
+ end
88
+ page_offset += 1
89
+ end
90
+ end
91
+
92
+ def test_number_of_pages
93
+ assert_equal 5, @pager.number_of_pages
94
+ end
95
+
96
+ def test_passing_block_to_initializer_with_arity_of_two_yields_per_page
97
+ pager = Paginator.new(20,2) do |offset,per_page|
98
+ assert_equal 2, per_page
99
+ end
100
+ pager.page(1).items
101
+ end
102
+
103
+ def test_passing_block_to_initializer_with_arity_of_one_does_not_yield_per_page
104
+ pager = Paginator.new(20,2) do |offset|
105
+ assert_equal 0, offset
106
+ end
107
+ pager.page(1).items
108
+ end
109
+
110
+ def test_page_object_knows_first_and_last_item_numbers
111
+ items = (1..11).to_a
112
+ pager = Paginator.new(items.size,3) do |offset, per_page|
113
+ items[offset, per_page]
114
+ end
115
+ page = pager.page(1)
116
+ assert_equal 1, page.first_item_number
117
+ assert_equal 3, page.last_item_number
118
+ page = pager.page(2)
119
+ assert_equal 4, page.first_item_number
120
+ assert_equal 6, page.last_item_number
121
+ page = pager.page(3)
122
+ assert_equal 7, page.first_item_number
123
+ assert_equal 9, page.last_item_number
124
+ page = pager.page(4)
125
+ assert_equal 10, page.first_item_number
126
+ assert_equal 11, page.last_item_number
127
+ end
128
+
129
+ end
130
+
131
+ class PaginatorTestWithMathN < PaginatorTest
132
+
133
+ def setup
134
+ require 'mathn'
135
+ super
136
+ end
137
+
138
+ end