Commit 3c206e7b377575891ab918a3ae0d050c60772a86

Authored by Antonio Terceiro
1 parent d54e2a0d

Rewrite HTTP caching tests as integration tests

Total runtime drops from ~21s to ~7s
features/http_caching.feature
... ... @@ -1,69 +0,0 @@
1   -Feature: HTTP caching
2   -
3   - As a sysdamin
4   - I want Noosfero to provide appropriate cache headers
5   - So that Varnish can serve content from the cache, everything works faster and everyone is happy
6   -
7   - Background:
8   - Given the following user
9   - | login | name |
10   - | joao | João Silva |
11   -
12   - Scenario: home page, default configuration
13   - When I go to the homepage
14   - Then the response should be valid for 5 minutes
15   - And the cache should be public
16   -
17   - Scenario: home page, custom configuration
18   - Given the following environment configuration
19   - | home_cache_in_minutes | 10 |
20   - When I go to the homepage
21   - Then the response should be valid for 10 minutes
22   -
23   - Scenario: search results, default configuration
24   - Given I am on the search page
25   - When I fill in "query" with "anything"
26   - And I press "Search"
27   - Then the response should be valid for 15 minutes
28   -
29   - Scenario: search results, custom configuration
30   - Given the following environment configuration
31   - | general_cache_in_minutes | 90 |
32   - When I go to the search page
33   - And I fill in "query" with "anything"
34   - And I press "Search"
35   - Then the response should be valid for 90 minutes
36   -
37   - Scenario: profile pages, default configuaration
38   - When I go to joao's homepage
39   - Then the response should be valid for 15 minutes
40   -
41   - Scenario: profile pages, custom configuration
42   - Given the following environment configuration
43   - | profile_cache_in_minutes | 90 |
44   - When I go to joao's homepage
45   - Then the response should be valid for 90 minutes
46   -
47   - Scenario: account controller should not be cached at all
48   - When I go to /account/login
49   - Then there must be no cache at all
50   -
51   - Scenario: profile administration
52   - Given I am logged in as "joao"
53   - When I go to joao's control panel
54   - Then there must be no cache at all
55   -
56   - Scenario: environment administration
57   - Given I am logged in as admin
58   - When I go to /admin
59   - Then there must be no cache at all
60   -
61   - Scenario: logged in user in the homepage
62   - Given I am logged in as "joao"
63   - When I go to the homepage
64   - Then there must be no cache at all
65   -
66   - Scenario: logged in user in a profile page
67   - Given I am logged in as "joao"
68   - When I go to /joao
69   - Then there must be no cache at all
features/step_definitions/http_caching_steps.rb
... ... @@ -1,21 +0,0 @@
1   -Then /^the response should be valid for (.+) minutes$/ do |n|
2   - page.response_headers['Cache-Control'].split(/,\s*/).should include("max-age=#{n.to_i * 60}")
3   -end
4   -
5   -Then /^the cache should be public/ do
6   - page.response_headers['Cache-Control'].split(/,\s*/).should include("public")
7   -end
8   -
9   -Then /^there must be no cache at all$/ do
10   - parts = page.response_headers['Cache-Control'].split(/,\s*/)
11   - parts.should include('must-revalidate')
12   - parts.should include('max-age=0')
13   -end
14   -
15   -Then 'there must be no cookies' do
16   - cookies.to_hash.should == {}
17   -end
18   -
19   -Then /^there must be a cookie "(.+)"$/ do |cookie_name|
20   - cookies.keys.should include(cookie_name)
21   -end
test/integration/http_caching_test.rb 0 → 100644
... ... @@ -0,0 +1,112 @@
  1 +require 'test_helper'
  2 +
  3 +class HttpCachingTest < ActionController::IntegrationTest
  4 +
  5 + def setup
  6 + create_user('joao', password: 'test', password_confirmation: 'test').activate
  7 + end
  8 +
  9 + test 'home page, default configuration' do
  10 + get '/'
  11 + assert_cache(5.minutes)
  12 + end
  13 +
  14 + test 'home page, custom config' do
  15 + set_env_config(home_cache_in_minutes: 10)
  16 + get '/'
  17 + assert_cache(10.minutes)
  18 + end
  19 +
  20 + test 'search results, default config' do
  21 + get '/search', query: 'anything'
  22 + assert_cache(15.minutes)
  23 + end
  24 +
  25 + test 'search results, custom config' do
  26 + set_env_config(general_cache_in_minutes: 30)
  27 + get '/search', query: 'anything'
  28 + assert_cache(30.minutes)
  29 + end
  30 +
  31 + test 'profile page, default config' do
  32 + get "/profile/joao"
  33 + assert_cache(15.minutes)
  34 + end
  35 +
  36 + test 'profile page, custom config' do
  37 + set_env_config(profile_cache_in_minutes: 60)
  38 + get "/profile/joao"
  39 + assert_cache(60.minutes)
  40 + end
  41 +
  42 + test 'account controller' do
  43 + get '/account/login'
  44 + assert_no_cache
  45 + end
  46 +
  47 + test 'profile admin' do
  48 + login 'joao', 'test'
  49 + get "/myprofile/joao"
  50 + assert_no_cache
  51 + end
  52 +
  53 + test 'environment admin' do
  54 + Environment.default.add_admin(Profile['joao'])
  55 + get '/admin'
  56 + assert_no_cache
  57 + end
  58 +
  59 + test 'logged in, home page' do
  60 + login 'joao', 'test'
  61 + get '/'
  62 + assert_no_cache
  63 + end
  64 +
  65 + test 'logged in, profile home' do
  66 + login 'joao', 'test'
  67 + get '/joao'
  68 + assert_no_cache
  69 + end
  70 +
  71 + test 'logged in, profile page' do
  72 + login 'joao', 'test'
  73 + get '/profile/joao'
  74 + assert_no_cache
  75 + end
  76 +
  77 + protected
  78 +
  79 + def set_env_config(data)
  80 + env = Environment.default
  81 + data.each do |key, value|
  82 + env.send("#{key}=", value)
  83 + end
  84 + env.save!
  85 + end
  86 +
  87 + def assert_no_cache
  88 + assert(cache_parts == ['max-age=0', 'must-revalidate', 'private'] || cache_parts == ['no-cache'], "should not set cache headers (found #{cache_parts.inspect})")
  89 + end
  90 +
  91 + def assert_public_cache
  92 + assert_includes cache_parts, 'public'
  93 + end
  94 +
  95 + def cache_parts
  96 + @cache_parts ||= response.headers['Cache-Control'].split(/\s*,\s*/).sort
  97 + end
  98 +
  99 + def assert_cache(valid_for)
  100 + assert_includes cache_parts, "max-age=#{valid_for}"
  101 + end
  102 +
  103 + def assert_no_cookies
  104 + assert_equal({}, response.cookies.to_hash)
  105 + end
  106 +
  107 + def assert_cookie(cookie_name)
  108 + assert_includes response.cookies.keys, cookie_name
  109 + end
  110 +
  111 +end
  112 +
... ...