loading
Generated 2021-03-31T08:01:41-05:00

All Files ( 63.56% covered at 0.65 hits/line )

15 files in total.
236 relevant lines, 150 lines covered and 86 lines missed. ( 63.56% )
File % covered Lines Relevant Lines Lines covered Lines missed Avg. Hits / Line
app/controllers/admin/users_controller.rb 35.29 % 61 34 12 22 0.35
app/helpers/admin/users_helper.rb 33.33 % 8 6 2 4 0.33
app/models/layout.rb 100.00 % 14 8 8 0 1.00
spec/controllers/application_controller_spec.rb 42.50 % 60 40 17 23 0.50
spec/controllers/users_controller_spec.rb 100.00 % 6 3 3 0 1.00
spec/factories/layout.rb 100.00 % 17 4 4 0 1.00
spec/factories/page.rb 94.12 % 63 34 32 2 0.94
spec/factories/page_part.rb 100.00 % 8 4 4 0 1.00
spec/factories/user.rb 100.00 % 35 25 25 0 1.00
spec/features/config_spec.rb 57.14 % 34 7 4 3 0.57
spec/features/layouts_spec.rb 58.33 % 49 12 7 5 0.58
spec/features/pages_spec.rb 63.64 % 55 11 7 4 0.64
spec/models/layout_spec.rb 36.84 % 29 19 7 12 0.37
spec/rails_helper.rb 70.83 % 73 24 17 7 0.71
spec/support/custom_actions.rb 20.00 % 10 5 1 4 0.20

app/controllers/admin/users_controller.rb

35.29% lines covered

34 relevant lines. 12 lines covered and 22 lines missed.
    
  1. 1 class Admin::UsersController < Admin::ResourceController
  2. 1 paginate_models
  3. 1 only_allow_access_to :index, :show, :new, :create, :edit, :update, :remove, :destroy,
  4. when: :admin,
  5. denied_url: { controller: 'pages', action: 'index' },
  6. denied_message: 'You must have administrative privileges to perform this action.'
  7. 1 before_action :ensure_deletable, only: %i[remove destroy]
  8. 1 def show
  9. redirect_to edit_admin_user_path(params[:id])
  10. end
  11. 1 def create
  12. user = User.new(user_params)
  13. if user.save
  14. flash[:notice] = 'User was created.'
  15. redirect_to admin_users_path
  16. else
  17. flash[:error] = 'There was an error saving the user. Please try again.'
  18. render :new
  19. end
  20. end
  21. 1 def update
  22. user_params = params[model_symbol].permit!
  23. if user_params && user_params['admin'] == false && model == current_user
  24. user_params.delete('admin')
  25. announce_cannot_remove_self_from_admin_role
  26. end
  27. model.skip_password_validation = true unless user_params[:password_confirmation].present?
  28. if model.update(user_params)
  29. response_for :update
  30. else
  31. flash[:error] = 'There was an error saving the user. Please try again.'
  32. render :edit
  33. end
  34. end
  35. 1 def ensure_deletable
  36. if current_user.id.to_s == params[:id].to_s
  37. announce_cannot_delete_self
  38. redirect_to admin_users_path
  39. end
  40. end
  41. 1 private
  42. 1 def user_params
  43. params.require(:user).permit(:first_name, :last_name, :admin, :designer,
  44. :password, :password_confirmation, :email, :site_id, :notes)
  45. end
  46. 1 def announce_cannot_delete_self
  47. flash[:error] = t('users_controller.cannot_delete_self')
  48. end
  49. 1 def announce_cannot_remove_self_from_admin_role
  50. flash[:error] = 'You cannot remove yourself from the admin role.'
  51. end
  52. end

app/helpers/admin/users_helper.rb

33.33% lines covered

6 relevant lines. 2 lines covered and 4 lines missed.
    
  1. 1 module Admin::UsersHelper
  2. 1 def roles(user)
  3. roles = []
  4. roles << I18n.t('admin') if user.admin?
  5. roles << I18n.t('designer') if user.designer?
  6. roles.join(', ')
  7. end
  8. end

app/models/layout.rb

100.0% lines covered

8 relevant lines. 8 lines covered and 0 lines missed.
    
  1. 1 class Layout < ActiveRecord::Base
  2. # Default Order
  3. 1 default_scope { order('name') }
  4. # Associations
  5. 1 has_many :pages
  6. 1 belongs_to :created_by, class_name: 'User'
  7. 1 belongs_to :updated_by, class_name: 'User'
  8. # Validations
  9. 1 validates_presence_of :name
  10. 1 validates_uniqueness_of :name
  11. 1 validates_length_of :name, maximum: 100
  12. end

spec/controllers/application_controller_spec.rb

42.5% lines covered

40 relevant lines. 17 lines covered and 23 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 describe ApplicationController, :type => :controller do
  3. 1 routes { TrustyCms::Engine.routes }
  4. 1 it 'should initialize the javascript and stylesheets arrays' do
  5. controller.send :set_javascripts_and_stylesheets
  6. expect(controller.send(:instance_variable_get, :@javascripts)).not_to be_nil
  7. expect(controller.send(:instance_variable_get, :@javascripts)).to be_instance_of(Array)
  8. expect(controller.send(:instance_variable_get, :@stylesheets)).not_to be_nil
  9. expect(controller.send(:instance_variable_get, :@stylesheets)).to be_instance_of(Array)
  10. end
  11. 1 describe 'self.template_name' do
  12. 1 it "should return 'index' when the controller action_name is 'index'" do
  13. allow(controller).to receive(:action_name).and_return('index')
  14. expect(controller.template_name).to eq('index')
  15. end
  16. 1 ['new', 'create'].each do |action|
  17. 2 it "should return 'new' when the action_name is #{action}" do
  18. allow(controller).to receive(:action_name).and_return(action)
  19. expect(controller.template_name).to eq('new')
  20. end
  21. end
  22. 1 ['edit', 'update'].each do |action|
  23. 2 it "should return 'edit' when the action_name is #{action}" do
  24. allow(controller).to receive(:action_name).and_return(action)
  25. expect(controller.template_name).to eq('edit')
  26. end
  27. end
  28. 1 ['remove', 'destroy'].each do |action|
  29. 2 it "should return 'remove' when the action_name is #{action}" do
  30. allow(controller).to receive(:action_name).and_return(action)
  31. expect(controller.template_name).to eq('remove')
  32. end
  33. end
  34. 1 it "should return 'show' when the action_name is show" do
  35. allow(controller).to receive(:action_name).and_return('show')
  36. expect(controller.template_name).to eq('show')
  37. end
  38. 1 it "should return the action_name when the action_name is a non-standard name" do
  39. allow(controller).to receive(:action_name).and_return('other')
  40. expect(controller.template_name).to eq('other')
  41. end
  42. end
  43. 1 describe "set_timezone" do
  44. 1 it "should use TrustyCms::Config['local.timezone']" do
  45. TrustyCms::Config['local.timezone'] = 'UTC'
  46. controller.send(:set_timezone)
  47. expect(Time.zone.name).to eq('UTC')
  48. end
  49. 1 it "should default to config.time_zone" do
  50. TrustyCms::Config.initialize_cache # to clear out setting from previous tests
  51. controller.send(:set_timezone)
  52. expect(Time.zone.name).to eq('UTC')
  53. end
  54. end
  55. end

spec/controllers/users_controller_spec.rb

100.0% lines covered

3 relevant lines. 3 lines covered and 0 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 describe Admin::UsersController, :type => :controller do
  3. 1 routes { TrustyCms::Engine.routes }
  4. end

spec/factories/layout.rb

100.0% lines covered

4 relevant lines. 4 lines covered and 0 lines missed.
    
  1. 1 FactoryBot.define do
  2. 1 factory :layout do
  3. 1 name { 'Main Layout' }
  4. 1 content { <<-CONTENT }
  5. <html>
  6. <head>
  7. <title><r:title /></title>
  8. </head>
  9. <body>
  10. <r:content />
  11. </body>
  12. </html>
  13. CONTENT
  14. end
  15. end

spec/factories/page.rb

94.12% lines covered

34 relevant lines. 32 lines covered and 2 lines missed.
    
  1. 1 FactoryBot.define do
  2. 1 factory :page do
  3. 1 title { 'Page' }
  4. 1 breadcrumb { title }
  5. 1 slug { title.slugify }
  6. 1 trait :with_parts do
  7. 1 page_parts { [FactoryBot.create(:page_part, name: 'body')] }
  8. end
  9. 1 trait :with_children do
  10. 1 children { [FactoryBot.create(:page, :with_parts)] }
  11. end
  12. 1 factory :page_with_layout do
  13. 1 layout
  14. end
  15. 1 factory :page_with_page_parts do
  16. 1 page_parts
  17. end
  18. 1 factory :file_not_found_page, class: FileNotFoundPage do
  19. end
  20. 1 factory :parent do
  21. end
  22. 1 factory :published_page do
  23. 1 status_id { Status[:published].id }
  24. 1 factory :article do
  25. 1 title { generate(:article_title)}
  26. 1 slug { generate(:article_slug)}
  27. end
  28. 1 factory :page_with_body_page_part do
  29. 1 after(:create) { |page| page.parts.create(name: 'body', content: "#{page.title} body.") }
  30. end
  31. 1 factory :page_with_body_and_sidebar_parts do
  32. 1 after(:create) { |page| page.parts.create(name: 'body', content: "#{page.title} body.") }
  33. 1 after(:create) { |page| page.parts.create(name: 'sidebar', content: "#{page.title} sidebar.") }
  34. end
  35. end
  36. 1 factory :home do |home|
  37. 1 title { 'Home' }
  38. 1 slug { '/' }
  39. 1 status_id { Status[:published].id }
  40. 1 parent_id { nil }
  41. end
  42. end
  43. 1 sequence :article_slug do |n|
  44. "article#{('-' + n.to_s) unless n == 1 }"
  45. end
  46. 1 sequence :article_title do |n|
  47. "Article#{(' ' + n.to_s) unless n == 1 }"
  48. end
  49. end

spec/factories/page_part.rb

100.0% lines covered

4 relevant lines. 4 lines covered and 0 lines missed.
    
  1. 1 FactoryBot.define do
  2. 1 factory :page_part do
  3. 1 name { 'unnamed' }
  4. 1 content { name }
  5. end
  6. end

spec/factories/user.rb

100.0% lines covered

25 relevant lines. 25 lines covered and 0 lines missed.
    
  1. 1 FactoryBot.define do
  2. 1 factory :user do
  3. 1 name { 'User' }
  4. 1 email { 'email@test.com' }
  5. 1 login { 'user' }
  6. 1 password { 'password' }
  7. 1 password_confirmation { password }
  8. 1 factory :admin do
  9. 1 name { 'Admin' }
  10. 1 login { 'admin' }
  11. 1 email { 'admin@example.com' }
  12. 1 admin { true }
  13. end
  14. 1 factory :existing do
  15. 1 name { 'Existing' }
  16. 1 login { 'existing' }
  17. 1 email { 'existing@example.com' }
  18. end
  19. 1 factory :designer do
  20. 1 name { 'Designer' }
  21. 1 login { 'designer' }
  22. 1 email { '' }
  23. 1 designer { true }
  24. end
  25. 1 factory :non_admin do
  26. 1 name { 'Non Admin' }
  27. 1 login { 'non_admin' }
  28. 1 admin { false }
  29. end
  30. end
  31. end

spec/features/config_spec.rb

57.14% lines covered

7 relevant lines. 4 lines covered and 3 lines missed.
    
  1. 1 require 'rails_helper'
  2. 1 describe 'Configuration (Settings)' do
  3. 1 fixtures :users
  4. 1 before(:each) do
  5. @admin = users(:captain_janeway)
  6. login_as(@admin, :scope => :user)
  7. click_link 'Settings'
  8. end
  9. # it 'has personal and site preferences' do
  10. # expect(page).to have_content 'Personal Preferences'
  11. # expect(page).to have_content 'Configuration'
  12. # end
  13. # it 'lets you edit your personal preferences' do
  14. # click_button 'Edit Preferences'
  15. # fill_in 'Name', with: 'Captain Kathryn Janeway'
  16. # click_button 'Save Changes'
  17. # expect(page).to have_content 'Name Captain Kathryn Janeway'
  18. # end
  19. # it 'lets you edit the site preferences' do
  20. # click_button 'Edit Configuration'
  21. # fill_in 'Site Title', with: 'My Special Site'
  22. # click_button 'Save Changes'
  23. # within '#site_title' do
  24. # expect(page).to have_content 'My Special Site'
  25. # end
  26. # end
  27. end

spec/features/layouts_spec.rb

58.33% lines covered

12 relevant lines. 7 lines covered and 5 lines missed.
    
  1. 1 require 'rails_helper'
  2. 1 describe 'Layouts (Design)' do
  3. 1 fixtures :users
  4. 1 before(:each) do
  5. @admin = users(:captain_janeway)
  6. login_as(@admin, :scope => :user)
  7. click_link 'Design'
  8. end
  9. 1 context 'without any layouts' do
  10. # it 'says it has no layouts' do
  11. # expect(page).to have_content 'No Layouts'
  12. # end
  13. # it 'lets you add a layout' do
  14. # click_link 'New Layout'
  15. # fill_in 'Name', with: 'Petunias'
  16. # fill_in 'Body', with: 'Wisteria'
  17. # click_button 'Create Layout'
  18. # expect(page).to have_content 'Petunias'
  19. # end
  20. end
  21. 1 context 'with a layout' do
  22. 1 before(:each) do
  23. Layout.create!(name: 'Petunias', content: 'Wisteria')
  24. visit '/admin/layouts'
  25. end
  26. # it 'lets you edit the layout' do
  27. # click_link 'Petunias'
  28. # expect(page).to have_content 'Edit Layout'
  29. # expect(page).to have_field 'Name', with: 'Petunias'
  30. # expect(page).to have_field 'Body', with: 'Wisteria'
  31. # expect(page).to have_button 'Save Changes'
  32. # expect(page).to have_content 'Last Updated by Kathryn Janeway'
  33. # end
  34. # it 'lets you remove the layout' do
  35. # click_link 'Remove'
  36. # expect(page).to have_content 'Are you sure you want to permanently remove the following layout?'
  37. # click_button 'Delete Layout'
  38. # expect(page).to have_content 'No Layouts'
  39. # expect(page).to have_link 'New Layout'
  40. # end
  41. end
  42. end

spec/features/pages_spec.rb

63.64% lines covered

11 relevant lines. 7 lines covered and 4 lines missed.
    
  1. 1 require 'rails_helper'
  2. 1 describe 'Pages' do
  3. 1 fixtures :users
  4. 1 before(:each) do
  5. @admin = users(:captain_janeway)
  6. login_as(@admin, :scope => :user)
  7. end
  8. 1 context 'without any pages' do
  9. # it 'can create a new homepage' do
  10. # click_link 'New Homepage'
  11. # fill_in 'Page Title', with: 'Voyager Home'
  12. # fill_in 'Breadcrumb', with: 'Home'
  13. # click_button 'Create Page'
  14. # within 'table#pages' do
  15. # expect(page).to have_selector 'tbody tr', count: 1
  16. # expect(page).to have_link 'Voyager Home'
  17. # expect(page).to have_link 'Add Child'
  18. # expect(page).to have_link 'Normal Page'
  19. # expect(page).to have_link 'File Not Found'
  20. # expect(page).to have_link 'Remove'
  21. # end
  22. # end
  23. end
  24. 1 context 'with only a homepage' do
  25. 1 before(:each) do
  26. Page.create!(title: 'Voyager Home', breadcrumb: 'Home', slug: '/')
  27. visit '/admin/pages'
  28. end
  29. # it 'lets you edit the homepage' do
  30. # click_link 'Voyager Home'
  31. # expect(page).to have_field 'Page Title', with: 'Voyager Home'
  32. # expect(page).to have_button 'Save Changes'
  33. # expect(page).to have_content 'Last Updated by Kathryn Janeway'
  34. # end
  35. # it 'lets you remove the homepage' do
  36. # click_link 'Remove'
  37. # expect(page).to have_content 'Are you sure you want to permanently remove the following Page?'
  38. # click_button 'Delete Page'
  39. # expect(page).to have_content 'No Pages'
  40. # expect(page).to have_link 'New Homepage'
  41. # end
  42. end
  43. end

spec/models/layout_spec.rb

36.84% lines covered

19 relevant lines. 7 lines covered and 12 lines missed.
    
  1. 1 require 'spec_helper'
  2. 1 describe Layout do
  3. 1 let(:layout){ FactoryBot.build(:layout) }
  4. 1 describe 'name' do
  5. 1 it 'is invalid when blank' do
  6. layout = FactoryBot.build(:layout, name: '')
  7. layout.valid?
  8. expect(layout.errors[:name]).to include("This field is required.")
  9. end
  10. 1 it 'should validate uniqueness of' do
  11. layout = FactoryBot.build(:layout, name: 'Normal', content: "Content!")
  12. layout.save!
  13. other = FactoryBot.build(:layout, name: 'Normal', content: "Content!")
  14. expect{other.save!}.to raise_error(ActiveRecord::RecordInvalid)
  15. end
  16. 1 it 'should validate length of' do
  17. layout = FactoryBot.build(:layout, name: 'x' * 100)
  18. expect(layout.errors[:name]).to be_blank
  19. layout = FactoryBot.build(:layout, name: 'x' * 101)
  20. expect{layout.save!}.to raise_error(ActiveRecord::RecordInvalid)
  21. expect(layout.errors[:name]).to include("This must not be longer than 100 characters")
  22. end
  23. end
  24. end

spec/rails_helper.rb

70.83% lines covered

24 relevant lines. 17 lines covered and 7 lines missed.
    
  1. # This file is copied to spec/ when you run 'rails generate rspec:install'
  2. 1 ENV["RAILS_ENV"] ||= 'test'
  3. 1 require 'spec_helper'
  4. 1 require 'rspec/rails'
  5. 1 require 'capybara/rails'
  6. 1 require 'capybara/poltergeist'
  7. 1 Capybara.javascript_driver = :poltergeist
  8. 1 Capybara.register_driver :poltergeist do |app|
  9. Capybara::Poltergeist::Driver.new(app, timeout: 60)
  10. end
  11. 1 require 'database_cleaner'
  12. 1 DatabaseCleaner.strategy = :truncation, {except: %w[config]}
  13. # Requires supporting ruby files with custom matchers and macros, etc, in
  14. # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
  15. # run as spec files by default. This means that files in spec/support that end
  16. # in _spec.rb will both be required and run as specs, causing the specs to be
  17. # run twice. It is recommended that you do not name files matching this glob to
  18. # end with _spec.rb. You can configure this pattern with with the --pattern
  19. # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
  20. 1 Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
  21. 1 RSpec.configure do |config|
  22. # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  23. 1 config.fixture_path = "#{::TRUSTY_CMS_ROOT}/spec/fixtures"
  24. # If you're not using ActiveRecord, or you'd prefer not to run each of your
  25. # examples within a transaction, remove the following line or assign false
  26. # instead of true.
  27. 1 config.use_transactional_fixtures = false
  28. # RSpec Rails can automatically mix in different behaviours to your tests
  29. # based on their file location, for example enabling you to call `get` and
  30. # `post` in specs under `spec/controllers`.
  31. #
  32. # You can disable this behaviour by removing the line below, and instead
  33. # explicitly tag your specs with their type, e.g.:
  34. #
  35. # RSpec.describe UsersController, :type => :controller do
  36. # # ...
  37. # end
  38. #
  39. # The different available types are documented in the features, such as in
  40. # https://relishapp.com/rspec/rspec-rails/docs
  41. 1 config.infer_spec_type_from_file_location!
  42. 1 config.before(:suite) do
  43. 1 TrustyCms::Config.initialize_cache
  44. configs = [
  45. ['admin.title', 'TrustyCMS'],
  46. ['admin.subtitle', 'Publishing for Small Teams'],
  47. ['defaults.page.parts', 'body, extended'],
  48. ['defaults.page.status', 'Draft'],
  49. ['defaults.page.filter', nil],
  50. ['defaults.page.fields', 'Keywords, Description'],
  51. ['defaults.snippet.filter', nil],
  52. ['session_timeout', '1209600'], # 2.weeks.to_s ????
  53. ['default_locale', 'en'],
  54. ]
  55. configs.each do |key, value|
  56. c = TrustyCms::Config.find_or_initialize_by(key: key)
  57. c.value = value
  58. c.save
  59. end
  60. end
  61. 1 config.after(:each) do
  62. DatabaseCleaner.clean
  63. end
  64. end

spec/support/custom_actions.rb

20.0% lines covered

5 relevant lines. 1 lines covered and 4 lines missed.
    
  1. # Commonly occurring user actions in tests.
  2. # This takes a username and by default assumes the password is 'password'.
  3. 1 def log_in_as(login, plaintext_password = 'password')
  4. visit '/'
  5. fill_in 'username_or_email', with: login
  6. fill_in 'password', with: plaintext_password
  7. click_on 'Login'
  8. end