From 3c206e7b377575891ab918a3ae0d050c60772a86 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Tue, 14 Oct 2014 11:00:35 -0300 Subject: [PATCH] Rewrite HTTP caching tests as integration tests --- features/http_caching.feature | 69 --------------------------------------------------------------------- features/step_definitions/http_caching_steps.rb | 21 --------------------- test/integration/http_caching_test.rb | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+), 90 deletions(-) delete mode 100644 features/http_caching.feature delete mode 100644 features/step_definitions/http_caching_steps.rb create mode 100644 test/integration/http_caching_test.rb diff --git a/features/http_caching.feature b/features/http_caching.feature deleted file mode 100644 index 0f7a0b8..0000000 --- a/features/http_caching.feature +++ /dev/null @@ -1,69 +0,0 @@ -Feature: HTTP caching - - As a sysdamin - I want Noosfero to provide appropriate cache headers - So that Varnish can serve content from the cache, everything works faster and everyone is happy - - Background: - Given the following user - | login | name | - | joao | João Silva | - - Scenario: home page, default configuration - When I go to the homepage - Then the response should be valid for 5 minutes - And the cache should be public - - Scenario: home page, custom configuration - Given the following environment configuration - | home_cache_in_minutes | 10 | - When I go to the homepage - Then the response should be valid for 10 minutes - - Scenario: search results, default configuration - Given I am on the search page - When I fill in "query" with "anything" - And I press "Search" - Then the response should be valid for 15 minutes - - Scenario: search results, custom configuration - Given the following environment configuration - | general_cache_in_minutes | 90 | - When I go to the search page - And I fill in "query" with "anything" - And I press "Search" - Then the response should be valid for 90 minutes - - Scenario: profile pages, default configuaration - When I go to joao's homepage - Then the response should be valid for 15 minutes - - Scenario: profile pages, custom configuration - Given the following environment configuration - | profile_cache_in_minutes | 90 | - When I go to joao's homepage - Then the response should be valid for 90 minutes - - Scenario: account controller should not be cached at all - When I go to /account/login - Then there must be no cache at all - - Scenario: profile administration - Given I am logged in as "joao" - When I go to joao's control panel - Then there must be no cache at all - - Scenario: environment administration - Given I am logged in as admin - When I go to /admin - Then there must be no cache at all - - Scenario: logged in user in the homepage - Given I am logged in as "joao" - When I go to the homepage - Then there must be no cache at all - - Scenario: logged in user in a profile page - Given I am logged in as "joao" - When I go to /joao - Then there must be no cache at all diff --git a/features/step_definitions/http_caching_steps.rb b/features/step_definitions/http_caching_steps.rb deleted file mode 100644 index 8f45273..0000000 --- a/features/step_definitions/http_caching_steps.rb +++ /dev/null @@ -1,21 +0,0 @@ -Then /^the response should be valid for (.+) minutes$/ do |n| - page.response_headers['Cache-Control'].split(/,\s*/).should include("max-age=#{n.to_i * 60}") -end - -Then /^the cache should be public/ do - page.response_headers['Cache-Control'].split(/,\s*/).should include("public") -end - -Then /^there must be no cache at all$/ do - parts = page.response_headers['Cache-Control'].split(/,\s*/) - parts.should include('must-revalidate') - parts.should include('max-age=0') -end - -Then 'there must be no cookies' do - cookies.to_hash.should == {} -end - -Then /^there must be a cookie "(.+)"$/ do |cookie_name| - cookies.keys.should include(cookie_name) -end diff --git a/test/integration/http_caching_test.rb b/test/integration/http_caching_test.rb new file mode 100644 index 0000000..5116c3c --- /dev/null +++ b/test/integration/http_caching_test.rb @@ -0,0 +1,112 @@ +require 'test_helper' + +class HttpCachingTest < ActionController::IntegrationTest + + def setup + create_user('joao', password: 'test', password_confirmation: 'test').activate + end + + test 'home page, default configuration' do + get '/' + assert_cache(5.minutes) + end + + test 'home page, custom config' do + set_env_config(home_cache_in_minutes: 10) + get '/' + assert_cache(10.minutes) + end + + test 'search results, default config' do + get '/search', query: 'anything' + assert_cache(15.minutes) + end + + test 'search results, custom config' do + set_env_config(general_cache_in_minutes: 30) + get '/search', query: 'anything' + assert_cache(30.minutes) + end + + test 'profile page, default config' do + get "/profile/joao" + assert_cache(15.minutes) + end + + test 'profile page, custom config' do + set_env_config(profile_cache_in_minutes: 60) + get "/profile/joao" + assert_cache(60.minutes) + end + + test 'account controller' do + get '/account/login' + assert_no_cache + end + + test 'profile admin' do + login 'joao', 'test' + get "/myprofile/joao" + assert_no_cache + end + + test 'environment admin' do + Environment.default.add_admin(Profile['joao']) + get '/admin' + assert_no_cache + end + + test 'logged in, home page' do + login 'joao', 'test' + get '/' + assert_no_cache + end + + test 'logged in, profile home' do + login 'joao', 'test' + get '/joao' + assert_no_cache + end + + test 'logged in, profile page' do + login 'joao', 'test' + get '/profile/joao' + assert_no_cache + end + + protected + + def set_env_config(data) + env = Environment.default + data.each do |key, value| + env.send("#{key}=", value) + end + env.save! + end + + def assert_no_cache + assert(cache_parts == ['max-age=0', 'must-revalidate', 'private'] || cache_parts == ['no-cache'], "should not set cache headers (found #{cache_parts.inspect})") + end + + def assert_public_cache + assert_includes cache_parts, 'public' + end + + def cache_parts + @cache_parts ||= response.headers['Cache-Control'].split(/\s*,\s*/).sort + end + + def assert_cache(valid_for) + assert_includes cache_parts, "max-age=#{valid_for}" + end + + def assert_no_cookies + assert_equal({}, response.cookies.to_hash) + end + + def assert_cookie(cookie_name) + assert_includes response.cookies.keys, cookie_name + end + +end + -- libgit2 0.21.2