Skip to content

Commit

Permalink
Merge pull request #655 from openstax/cache_headers
Browse files Browse the repository at this point in the history
Started spec for testing caching headers and fixed translation errors.
  • Loading branch information
jpslav authored Jun 25, 2019
2 parents a321fa8 + 01bd323 commit a3b3b18
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
1 change: 0 additions & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
--color
--require spec_helper
10 changes: 10 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ class Application < Rails::Application
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.i18n.default_locale = :en
config.i18n.fallbacks = {
en_us: :en,
en_ca: :en,
en_uk: :en,
fr_lu: :fr,
fr_ca: :fr,
fr: :en,
es: :en,
}

config.accounts = ActiveSupport::OrderedOptions.new
# configure how long a login token is valid for
Expand Down
50 changes: 50 additions & 0 deletions spec/features/cache_headers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'rails_helper'

RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.around(:each, :caching) do |example|
caching = ActionController::Base.perform_caching
ActionController::Base.perform_caching = example.metadata[:caching]
example.run
ActionController::Base.perform_caching = caching
end
end

describe "Cache-Control headers", :type => :request do

let(:verified_emails) { ["[email protected]"] }
let(:unverified_emails) { [] }
let(:user) {
create_user('user').tap do |user|
verified_emails.each do |verified_email|
create_email_address_for(user, verified_email)
end

unverified_emails.each do |unverified_email|
create_email_address_for(user, unverified_email, SecureRandom.hex(32))
end
end
}
# test cached stuff
before(:each) do
mock_current_user(user)
end

context "Requests should get cached headers after a while" do
it 'status code is 200' do
get '/profile'
assert_response 200
expect(response.headers["Cache-Control"]).to eq("no-cache, no-store")
end

it 'should not allow caching' do
get '/api/user'
expect(response.headers["Cache-Control"]).to eq("no-cache")
end

it 'should not cache assets' do
get '/assets/bg-login.jpg'
expect(response.headers["Cache-Control"]).to match /public/
end
end
end
37 changes: 37 additions & 0 deletions spec/requests/api/v1/cache_header_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'rails_helper'

# Moved multiple request specs here from the controller spec

RSpec.describe 'API cache header verification',
type: :request, api: true, version: :v1 do

let!(:untrusted_application) { FactoryGirl.create :doorkeeper_application }
let!(:trusted_application) { FactoryGirl.create :doorkeeper_application, :trusted }
let!(:user_2) do
FactoryGirl.create :user_with_emails,
first_name: 'Bob',
last_name: 'Michaels'
end

let!(:user_2_token) do
FactoryGirl.create :doorkeeper_access_token,
application: untrusted_application,
resource_owner_id: user_2.id
end

let!(:untrusted_application_token) do
FactoryGirl.create :doorkeeper_access_token,
application: untrusted_application,
resource_owner_id: nil
end
let!(:trusted_application_token) do
FactoryGirl.create :doorkeeper_access_token,
application: trusted_application,
resource_owner_id: nil
end

it "should have headers that prevent caching in AWS" do
api_get "/api/user", user_2_token
expect(response.headers["Cache-Control"].split(',').each(&:strip!)).to include("max-age=0", "private")
end
end

0 comments on commit a3b3b18

Please sign in to comment.