Skip to content

Commit

Permalink
Add test suite with CI (#816)
Browse files Browse the repository at this point in the history
* Add initial system tests

This covers current functionality. Asserting redirects isn't available in system tests so we'll need to create integration tests instead for that assertion.

* Switch to headless_chrome

This avoids bringing up the actual browser when running tests and should also just work in CI too.

* Disable admin user fixtures

These cause errors since they have database constraints and we'd need to fill these out. For now we'll just have a helper to create a default admin user.

* Add simplecov gem setup

* Add tests CI workflow

* Fix Rails secrets deprecation from Devise

Known issue and waiting on PR merge and new release. In the meantime, we can apply this simple fix.

```
DEPRECATION WARNING: `Rails.application.secrets` is deprecated in favor of `Rails.application.credentials` and will be removed in Rails 7.2. (called from <top (required)> at /home/runner/work/demo.activeadmin.info/demo.activeadmin.info/config/environment.rb:5)
```

heartcombo/devise#5645 (comment)
  • Loading branch information
javierjulio committed Jan 6, 2024
1 parent 612fc85 commit cd9193d
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 4 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Tests

on:
pull_request:
push:
branches:
- main

concurrency:
group: ${{ github.repository }}-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

env:
COVERAGE_RETENTION_DAYS: 3

jobs:
run_tests:
name: Run tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
bundler-cache: true
- name: Run tests
env:
COVERAGE: true
RAILS_ENV: test
run: bin/rails db:setup test:all
- name: Upload screenshots from failed system tests
uses: actions/upload-artifact@v4
if: failure()
with:
name: screenshots
path: ${{ github.workspace }}/tmp/screenshots
if-no-files-found: ignore
- uses: actions/upload-artifact@v4
with:
name: rails-coverage
path: coverage
if-no-files-found: error
retention-days: ${{ env.COVERAGE_RETENTION_DAYS }}

upload_coverage:
name: Upload Coverage
runs-on: ubuntu-latest
needs:
- run_tests
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: rails-coverage
- uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.xml
fail_ci_if_error: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@
!/app/assets/builds/.keep

/node_modules
/coverage
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"
gem "simplecov", require: false
gem "simplecov-cobertura"
end
12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ GEM
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
docile (1.4.0)
drb (2.2.0)
ruby2_keywords
erubi (1.12.0)
Expand Down Expand Up @@ -253,6 +254,15 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-cobertura (2.1.0)
rexml
simplecov (~> 0.19)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand Down Expand Up @@ -294,6 +304,8 @@ DEPENDENCIES
puma
rails (~> 7.1.2)
selenium-webdriver
simplecov
simplecov-cobertura
sprockets-rails
sqlite3
tzinfo-data
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# confirmation, reset password and unlock tokens in the database.
# Devise will use the `secret_key_base` as its `secret_key`
# by default. You can change it below and use your own secret key.
# config.secret_key = 'd0e4b1dd2d7c805cdd767c13ca2d7ac1e0f669575727596a66c5c8fff12f7ef6988c463f34268ca27915107b70674e093d81e612bff8427f6e40dd54ed5a9c71'
config.secret_key = Rails.application.secret_key_base

# ==> Controller configuration
# Configure the parent class to the devise controllers.
Expand Down
2 changes: 1 addition & 1 deletion test/application_system_test_case.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]
end
4 changes: 2 additions & 2 deletions test/fixtures/admin_users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# one: {}
# column: value
#
two: {}
# two: {}
# column: value
45 changes: 45 additions & 0 deletions test/system/active_admin/admin_users_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "application_system_test_case"

class AdminUsersTest < ApplicationSystemTestCase
test "visiting the index" do
sign_in default_admin_user

visit admin_admin_users_path

assert_text "Admin Users"
assert_text "Showing 1 of 1"
assert_text "admin@example.com"
end

test "visiting the show" do
sign_in default_admin_user

visit admin_admin_user_path(default_admin_user)

assert_text "admin@example.com"
assert_selector "a", text: "Edit Admin User"
assert_selector "a", text: "Delete Admin User"
end

test "visiting the new and submitting" do
sign_in default_admin_user

visit new_admin_admin_user_path

fill_in "Email", with: "test@test.com"
fill_in "Password", with: "password", id: "admin_user_password"
fill_in "Password confirmation", with: "password"
click_on "Create Admin user"

assert_text "Admin user was successfully created."
assert_text "test@test.com"
end

test "visiting the edit" do
sign_in default_admin_user

visit edit_admin_admin_user_path(default_admin_user)

assert_text "admin@example.com"
end
end
21 changes: 21 additions & 0 deletions test/system/active_admin/dashboard_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "application_system_test_case"

class AdminUsersTest < ApplicationSystemTestCase
test "visiting root redirects to admin root" do
sign_in default_admin_user
visit root_path
assert_text "Welcome to ActiveAdmin"
end

test "visiting the admin root renders dashboard" do
sign_in default_admin_user
visit admin_root_path
assert_text "Welcome to ActiveAdmin"
end

test "visiting the admin dashboard" do
sign_in default_admin_user
visit admin_dashboard_path
assert_text "Welcome to ActiveAdmin"
end
end
22 changes: 22 additions & 0 deletions test/system/active_admin/sessions_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "application_system_test_case"

class SessionsTest < ApplicationSystemTestCase
test "visiting the root redirects to admin login" do
visit root_path
# assert_redirected_to new_admin_user_session_path
assert_text "Active Admin Demo Sign In"
end

test "submitting the login form successfully" do
default_admin_user

visit new_admin_user_session_path

fill_in "Email", with: "admin@example.com"
fill_in "Password", with: "password"
click_on "Sign In"

# assert_redirected_to admin_dashboard_path
assert_text "Welcome to ActiveAdmin"
end
end
20 changes: 20 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
if ENV.fetch("COVERAGE", false)
require "simplecov"
require "simplecov-cobertura"
SimpleCov.start do
add_filter %r{^/test/}
minimum_coverage 98
maximum_coverage_drop 0.2
formatter SimpleCov::Formatter::CoberturaFormatter
end
end

ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"
Expand All @@ -11,5 +22,14 @@ class TestCase
fixtures :all

# Add more helper methods to be used by all tests here...
include Devise::Test::IntegrationHelpers

def default_admin_user
@default_admin_user ||= AdminUser.create!(
email: "admin@example.com",
password: "password",
password_confirmation: "password"
)
end
end
end

0 comments on commit cd9193d

Please sign in to comment.