diff options
author | Christopher Baines <mail@cbaines.net> | 2018-04-03 07:52:00 +0100 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2018-04-03 21:58:55 +0100 |
commit | 9e622112a80f493eab53420d7dafedb9dd10a612 (patch) | |
tree | 6a21d19f6ff7ae72db8510f8e0ae813bb8a49045 /test | |
parent | a29946a593988003cbd4637293c5b4cc1aee7eaa (diff) | |
download | govuk-mini-environment-admin-9e622112a80f493eab53420d7dafedb9dd10a612.tar govuk-mini-environment-admin-9e622112a80f493eab53420d7dafedb9dd10a612.tar.gz |
Improve the test suite
Add some tests, and setup the helpers. For some reason, directly
running the test files requires the ugly `ENV['RAILS_ENV'] = 'test'`
line at the top of the test_helper.rb file.
Diffstat (limited to 'test')
-rw-r--r-- | test/controllers/backends/terraform_aws_controller_test.rb | 85 | ||||
-rw-r--r-- | test/controllers/backends/terraform_libvirt_controller_test.rb | 77 | ||||
-rw-r--r-- | test/controllers/govuk_guix/revisions_controller_test.rb | 32 | ||||
-rw-r--r-- | test/controllers/que_jobs_controller_test.rb | 21 | ||||
-rw-r--r-- | test/controllers/setup_controller_test.rb | 12 | ||||
-rw-r--r-- | test/integration_test_helper.rb | 7 | ||||
-rw-r--r-- | test/models/backends/terraform_aws_test.rb | 45 | ||||
-rw-r--r-- | test/models/backends/terraform_libvirt_test.rb | 45 | ||||
-rw-r--r-- | test/test_helper.rb | 10 |
9 files changed, 305 insertions, 29 deletions
diff --git a/test/controllers/backends/terraform_aws_controller_test.rb b/test/controllers/backends/terraform_aws_controller_test.rb index 0c574fa..41373b8 100644 --- a/test/controllers/backends/terraform_aws_controller_test.rb +++ b/test/controllers/backends/terraform_aws_controller_test.rb @@ -1,14 +1,89 @@ -require 'test_helper' +require 'integration_test_helper' class Backends::TerraformAwsControllerTest < ActionDispatch::IntegrationTest - test "should get new" do - get backends_terraform_aws_new_url + setup do + login_as User.new(name: 'Test', email: 'test@example.com') + end + + test 'new' do + get new_terraform_aws_backend_path + assert_response :success + end + + test 'create' do + attributes = { + label: 'Test Backend', + aws_region: 'test-region', + aws_access_key_id: 'test-access-key-id', + aws_secret_access_key: 'test-secret-access-key' + } + + post( + terraform_aws_backends_path, + params: { + backends_terraform_aws: attributes + } + ) + + assert_response :redirect + follow_redirect! + assert_response :success + + backend = Backends::TerraformAws.last + attributes.each do |(key, value)| + assert_equal backend.send(key), value + end + end + + test 'update' do + backend = Backends::TerraformAws.create( + label: 'Old label', + aws_region: 'old-test-region', + aws_access_key_id: 'old-test-access-key-id', + aws_secret_access_key: 'old-test-secret-access-key' + ) + + new_attributes = { + label: 'New label', + aws_access_key_id: 'new-test-access-key-id', + aws_secret_access_key: 'new-test-secret-access-key' + } + + put( + terraform_aws_backend_path(backend), + params: { + backends_terraform_aws: new_attributes + } + ) + assert_response :success + + backend.reload end - test "should get show" do - get backends_terraform_aws_show_url + test 'show' do + backend = Backends::TerraformAws.create( + label: 'Test Backend', + aws_region: 'old-test-region', + aws_access_key_id: 'old-test-access-key-id', + aws_secret_access_key: 'old-test-secret-access-key' + ) + + get terraform_aws_backend_path(backend) + assert_response :success end + test 'destroy' do + backend = Backends::TerraformAws.create( + label: 'Test Backend', + aws_region: 'old-test-region', + aws_access_key_id: 'old-test-access-key-id', + aws_secret_access_key: 'old-test-secret-access-key' + ) + + delete terraform_aws_backend_path(backend) + + assert_response :redirect + end end diff --git a/test/controllers/backends/terraform_libvirt_controller_test.rb b/test/controllers/backends/terraform_libvirt_controller_test.rb index 376d753..b34a8ac 100644 --- a/test/controllers/backends/terraform_libvirt_controller_test.rb +++ b/test/controllers/backends/terraform_libvirt_controller_test.rb @@ -1,14 +1,81 @@ -require 'test_helper' +require 'integration_test_helper' class Backends::TerraformLibvirtControllerTest < ActionDispatch::IntegrationTest - test "should get new" do - get backends_terraform_libvirt_new_url + setup do + login_as User.new(name: 'Test', email: 'test@example.com') + end + + test 'new' do + get new_terraform_aws_backend_path assert_response :success end - test "should get show" do - get backends_terraform_libvirt_show_url + test 'create' do + attributes = { + label: 'Test Backend', + domain: 'test.backend.example.com', + uri: 'test://uri' + } + + post( + terraform_libvirt_backends_path, + params: { + backends_terraform_libvirt: attributes + } + ) + + assert_response :redirect + follow_redirect! assert_response :success + + backend = Backends::TerraformLibvirt.last + attributes.each do |(key, value)| + assert_equal backend.send(key), value + end end + test 'update' do + backend = Backends::TerraformLibvirt.create( + label: 'Old label', + domain: 'old.domain.example.com', + uri: 'old://uri' + ) + + new_attributes = { + label: 'New label' + } + + put( + terraform_libvirt_backend_path(backend), + params: { + backends_terraform_libvirt: new_attributes + } + ) + + assert_response :success + end + + test 'show' do + backend = Backends::TerraformLibvirt.create( + label: 'Test Backend', + domain: 'test.backend.example.com', + uri: 'test://uri' + ) + + get terraform_libvirt_backend_path(backend) + + assert_response :success + end + + test 'destroy' do + backend = Backends::TerraformLibvirt.create( + label: 'Test Backend', + domain: 'test.backend.example.com', + uri: 'test://uri' + ) + + delete terraform_libvirt_backend_path(backend) + + assert_response :redirect + end end diff --git a/test/controllers/govuk_guix/revisions_controller_test.rb b/test/controllers/govuk_guix/revisions_controller_test.rb index ae93202..51ca048 100644 --- a/test/controllers/govuk_guix/revisions_controller_test.rb +++ b/test/controllers/govuk_guix/revisions_controller_test.rb @@ -1,9 +1,35 @@ -require 'test_helper' +require 'integration_test_helper' class GovukGuix::RevisionsControllerTest < ActionDispatch::IntegrationTest - test "should get index" do - get govuk_guix_revisions_index_url + setup do + login_as User.new(name: 'Test', email: 'test@example.com') + end + + test 'show' do + revision = GovukGuix::Revision.create( + commit_hash: 'test-commit-hash', + store_path: 'test-store-path' + ) + + get govuk_guix_revision_path(revision) + assert_response :success end + test 'enqueue_fetch_revision' do + revision = 'test-revision' + + GovukGuix::FetchRevisionJob.expects(:enqueue).with(revision) + + post( + enqueue_fetch_govuk_guix_revision_path, + params: { + revision: revision + } + ) + + assert_response :redirect + follow_redirect! + assert_response :success + end end diff --git a/test/controllers/que_jobs_controller_test.rb b/test/controllers/que_jobs_controller_test.rb index 691d0f3..f5821e3 100644 --- a/test/controllers/que_jobs_controller_test.rb +++ b/test/controllers/que_jobs_controller_test.rb @@ -1,14 +1,23 @@ -require 'test_helper' +require 'integration_test_helper' class QueJobsControllerTest < ActionDispatch::IntegrationTest - test "should get cancel" do - get que_jobs_cancel_url - assert_response :success + setup do + login_as User.new end - test "should get retry_now" do - get que_jobs_retry_now_url + test 'cancel' do + job = GovukGuix::FetchRevisionJob.enqueue('test-revision') + + post cancel_que_job_path(job.attrs['job_id']) + assert_response :success end + test 'retry now' do + job = GovukGuix::FetchRevisionJob.enqueue('test-revision') + + post retry_now_que_job_path(job.attrs['job_id']) + + assert_response :success + end end diff --git a/test/controllers/setup_controller_test.rb b/test/controllers/setup_controller_test.rb index d350d63..0f8326b 100644 --- a/test/controllers/setup_controller_test.rb +++ b/test/controllers/setup_controller_test.rb @@ -1,7 +1,11 @@ -require 'test_helper' +require 'integration_test_helper' class SetupControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end + test 'show' do + login_as User.new(name: 'Test') + + get setup_path + + assert_response :success + end end diff --git a/test/integration_test_helper.rb b/test/integration_test_helper.rb new file mode 100644 index 0000000..986d8a7 --- /dev/null +++ b/test/integration_test_helper.rb @@ -0,0 +1,7 @@ +require_relative 'test_helper' + +class ActionDispatch::IntegrationTest + def login_as(user) + GDS::SSO.test_user = user + end +end diff --git a/test/models/backends/terraform_aws_test.rb b/test/models/backends/terraform_aws_test.rb index 78cd8de..2337bda 100644 --- a/test/models/backends/terraform_aws_test.rb +++ b/test/models/backends/terraform_aws_test.rb @@ -1,7 +1,46 @@ require 'test_helper' class Backends::TerraformAwsTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + setup do + @backend = Backends::TerraformAws.new + @mini_environment = MiniEnvironment.new( + id: 1, + name: 'Test Mini Environment', + backend_data: { + 'build_output': '/gnu/store/testbuildoutput', + } + ) + end + + test "type_and_id" do + @backend.type_and_id + end + + test "build" do + GovukGuix::BuildJob.expects(:enqueue) + + @backend.build(@mini_environment) + end + + test "start" do + RubyTerraform.stubs(:init) + RubyTerraform.expects(:apply) + + @backend.start(@mini_environment) + end + + test "destroy" do + RubyTerraform.stubs(:init) + RubyTerraform.expects(:destroy) + + @backend.destroy(@mini_environment) + end + + test "signon_url" do + @backend.signon_url(@mini_environment) + end + + test "terraform_variables" do + @backend.terraform_variables(@mini_environment) + end end diff --git a/test/models/backends/terraform_libvirt_test.rb b/test/models/backends/terraform_libvirt_test.rb index b7d3c35..e509b03 100644 --- a/test/models/backends/terraform_libvirt_test.rb +++ b/test/models/backends/terraform_libvirt_test.rb @@ -1,7 +1,46 @@ require 'test_helper' class Backends::TerraformLibvirtTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + setup do + @backend = Backends::TerraformLibvirt.new + @mini_environment = MiniEnvironment.new( + id: 1, + name: 'Test Mini Environment', + backend_data: { + 'build_output': '/gnu/store/testbuildoutput', + } + ) + end + + test "type_and_id" do + @backend.type_and_id + end + + test "build" do + GovukGuix::BuildJob.expects(:enqueue) + + @backend.build(@mini_environment) + end + + test "start" do + RubyTerraform.stubs(:init) + RubyTerraform.expects(:apply) + + @backend.start(@mini_environment) + end + + test "destroy" do + RubyTerraform.stubs(:init) + RubyTerraform.expects(:destroy) + + @backend.destroy(@mini_environment) + end + + test "signon_url" do + @backend.signon_url(@mini_environment) + end + + test "terraform_variables" do + @backend.terraform_variables(@mini_environment) + end end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..5bbb0fa --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,10 @@ +ENV['RAILS_ENV'] = 'test' + +require File.expand_path('../../config/environment', __FILE__) +require 'rails/test_help' + +gem 'mocha' +require 'mocha/mini_test' + +class ActiveSupport::TestCase +end |