-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #655 from openstax/cache_headers
Started spec for testing caching headers and fixed translation errors.
- Loading branch information
Showing
4 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
--color | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |