diff --git a/Gemfile b/Gemfile index 9bfe6c1..4b7caf5 100644 --- a/Gemfile +++ b/Gemfile @@ -69,6 +69,10 @@ group :development, :test do gem 'byebug', platform: :mri end +group :test do + gem "minitest-rails-capybara" +end + # TODO… https://github.com/galetahub/simple-captcha # Captcha for anonymous reports # gem "galetahub-simple_captcha", :require => "simple_captcha" diff --git a/Gemfile.lock b/Gemfile.lock index 188a09b..694b278 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -44,6 +44,8 @@ GEM i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) + addressable (2.5.2) + public_suffix (>= 2.0.2, < 4.0) ansi (1.5.0) arel (7.1.4) babel-source (5.8.35) @@ -61,6 +63,13 @@ GEM builder (3.2.3) byebug (10.0.2) callsite (0.0.11) + capybara (2.18.0) + addressable + mini_mime (>= 0.1.3) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (>= 2.0, < 4.0) choice (0.2.0) climate_control (0.2.0) coderay (1.1.2) @@ -151,9 +160,20 @@ GEM mini_mime (1.0.0) mini_portile2 (2.3.0) minitest (5.11.3) + minitest-capybara (0.8.2) + capybara (~> 2.2) + minitest (~> 5.0) + rake + minitest-metadata (0.6.0) + minitest (>= 4.7, < 6.0) minitest-rails (3.0.0) minitest (~> 5.8) railties (~> 5.0) + minitest-rails-capybara (3.0.1) + capybara (~> 2.7) + minitest-capybara (~> 0.8) + minitest-metadata (~> 0.6) + minitest-rails (~> 3.0) minitest-reporters (1.2.0) ansi builder @@ -209,6 +229,7 @@ GEM pry (0.11.3) coderay (~> 1.1.0) method_source (~> 0.9.0) + public_suffix (3.0.2) puma (3.11.4) rack (2.0.5) rack-contrib (2.0.1) @@ -321,6 +342,8 @@ GEM will_paginate (3.1.6) will_paginate-foundation (6.2.1) will_paginate (>= 3.0.3) + xpath (3.1.0) + nokogiri (~> 1.8) PLATFORMS ruby @@ -343,6 +366,7 @@ DEPENDENCIES listen (~> 3.0.5) meta_request minitest-rails + minitest-rails-capybara minitest-reporters omniauth omniauth-amazon diff --git a/Guardfile b/Guardfile index bee49e7..51ac9e1 100644 --- a/Guardfile +++ b/Guardfile @@ -50,6 +50,6 @@ guard :minitest do watch(%r{^app/controllers/(.+)_controller\.rb$}) { |m| "test/integration/#{m[1]}_test.rb" } watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" } watch(%r{^lib/(.+)\.rb$}) { |m| "test/lib/#{m[1]}_test.rb" } - watch(%r{^test/.+_test\.rb$}) + watch(%r{^test/.+?/.+_test\.rb$}) watch(%r{^test/test_helper\.rb$}) { 'test' } end diff --git a/test/controllers/admin_controller_test.rb b/test/controllers/admin_controller_test.rb index 7e268c0..78b0b3b 100644 --- a/test/controllers/admin_controller_test.rb +++ b/test/controllers/admin_controller_test.rb @@ -1,9 +1,21 @@ require "test_helper" class AdminControllerTest < ActionController::TestCase - test "should get status" do + test "user must login" do get :status - assert_response :redirect + assert_redirected_to user_session_path + end + + test "normal user must not see admin status" do + sign_in users(:normal) + get :status + assert_response :forbidden + end + + test "admin can see status" do + sign_in users(:admin) + get :status + assert_response :success end end diff --git a/test/fixtures/packages.yml b/test/fixtures/packages.yml index 66bc7e5..0073d32 100644 --- a/test/fixtures/packages.yml +++ b/test/fixtures/packages.yml @@ -1,27 +1,3 @@ -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html - -# one: -# name: MyText -# description: MyString -# long_description: MyString -# section: MyString -# maintainer: MyString -# maintainer_email: MyString -# homepage: MyString -# version: MyString -# origin: MyString - -# two: -# name: MyText -# description: MyString -# long_description: MyString -# section: MyString -# maintainer: MyString -# maintainer_email: MyString -# homepage: MyString -# version: MyString -# origin: MyString - firefox: name: firefox description: A web browser @@ -32,3 +8,43 @@ firefox: homepage: http://www.mozilla.org/ version: 10.01alpha-7 origin: Debian + +vim: + name: vim + description: Vi IMproved - enhanced vi editor + long_description: | + Vim is an almost compatible version of the UNIX editor Vi. + . + Many new features have been added: multi level undo, syntax + highlighting, command line history, on-line help, filename + completion, block operations, folding, Unicode support, etc. + . + This package contains a version of vim compiled with a rather + standard set of features. This package does not provide a GUI + version of Vim. See the other vim-* packages if you need more + (or less). + section: editors + maintainer: Debian Vim Maintainers + maintainer_email: team+vim@tracker.debian.org + homepage: https://vim.sourceforge.io/ + version: 2:8.1.0229-1 + origin: Ubuntu + +<% 100.times do |n| %> +package_<%= n %>: + name: package-<%= n %> + description: Description of package <%= n %> + long_description: | + Long description of package <%= n%> that may be so long that + it spans several lines of text. + . + There might also be several paragraphs in the description text. + . + Like this one. + section: <%= %W(admin cli-mono database debug devel doc editors education games).sample %> + maintainer: Maintainer of package-<%= n %> + maintainer_email: maintainer-<%= n %>@debian.cc + homepage: https://packages.debian.org/package-<%= n %> + version: <%= (rand*10).round(2) %> + origin: <%= %W(ubuntu debian mint opensuse).sample %> +<% end %> diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..f22935b --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,20 @@ +admin: + name: Super Sayajin + created_at: 2018-07-01 + updated_at: 2018-07-01 + email: admin@debian.ork + sign_in_count: 5 + provider: local + admin: 1 + encrypted_password: <%= Devise::Encryptor.digest(User, 'adminsecret') %> + +normal: + name: Norman Normal + created_at: 2018-06-01 + updated_at: 2018-06-01 + email: mortal@debian.ork + sign_in_count: 3 + provider: local + admin: 0 + encrypted_password: <%= Devise::Encryptor.digest(User, 'normalsecret') %> + \ No newline at end of file diff --git a/test/integration/browse_test.rb b/test/integration/browse_test.rb new file mode 100644 index 0000000..024cfa4 --- /dev/null +++ b/test/integration/browse_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class BrowserBrowseTest < ActionDispatch::IntegrationTest + test "user can see home page" do + visit root_path + page.must_have_content 'This website lets you browse screenshots' + end + + test "user can browse packages" do + visit packages_grid_path + page.must_have_content /Previous/ + page.must_have_link href: packages_grid_path(page: 2) + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 743dda7..df46a2c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,10 +10,12 @@ Minitest::Reporters.use! # To add Capybara feature tests add `gem "minitest-rails-capybara"` # to the test group in the Gemfile and uncomment the following: -# require "minitest/rails/capybara" +require "minitest/rails/capybara" +require 'capybara/rails' +require 'capybara/minitest' # Uncomment for awesome colorful output -# require "minitest/pride" +require "minitest/pride" class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. @@ -22,4 +24,19 @@ end class ActionController::TestCase include Devise::Test::ControllerHelpers -end \ No newline at end of file +end + +# Capybara-based tests +class ActionDispatch::IntegrationTest + # Make the Capybara DSL available in all integration tests + include Capybara::DSL + # Make `assert_*` methods behave like Minitest assertions + include Capybara::Minitest::Assertions + + # Reset sessions and driver between tests + # Use super wherever this method is redefined in your individual test classes + def teardown + Capybara.reset_sessions! + Capybara.use_default_driver + end +end