diff --git a/.copilot/config.yml b/.copilot/config.yml index c72dda0ec..c4ae3d5ce 100644 --- a/.copilot/config.yml +++ b/.copilot/config.yml @@ -1,4 +1,4 @@ repository: export-opportunities builder: name: paketobuildpacks/builder-jammy-full - version: 0.3.397 + version: 0.3.397 \ No newline at end of file diff --git a/.copilot/image_build_run.sh b/.copilot/image_build_run.sh index 4e2bfeec8..6eb6a0bf4 100755 --- a/.copilot/image_build_run.sh +++ b/.copilot/image_build_run.sh @@ -7,6 +7,8 @@ set -e rm ./config/application.yml +cp -r /workspace/db/ /workspace/db-copy/ + rm -rf /workspace/tmp rm -rf /workspace/db rm -rf /workspace/log diff --git a/Procfile b/Procfile index 329785eea..593dabf6e 100644 --- a/Procfile +++ b/Procfile @@ -1,3 +1,3 @@ web: bundle exec rake cf:on_first_instance db:migrate && bundle exec puma -p ${PORT:-3000} -app: bundle exec rake cf:on_first_instance db:migrate && bundle exec rails assets:precompile && bundle exec puma -p ${PORT:-3000} +app: cp -r /workspace/db-copy/* /workspace/db/ && bundle exec rake cf:on_first_instance db:migrate && bundle exec rails assets:precompile && bundle exec puma -p ${PORT:-3000} worker: bin/start-stunnel bundle exec sidekiq -C config/sidekiq.yml diff --git a/app/controllers/api/activity_stream_controller.rb b/app/controllers/api/activity_stream_controller.rb index 5d8f13543..b065bfe30 100644 --- a/app/controllers/api/activity_stream_controller.rb +++ b/app/controllers/api/activity_stream_controller.rb @@ -316,11 +316,13 @@ def to_search_after(object, method) end def authenticate(request) - remote_ips = request.headers['X-Forwarded-For'].gsub(/\s+/, '').split(',') - return [false, 'Connecting from unauthorized IP'] unless remote_ips.length >= 2 - - authorized_ip_addresses = Figaro.env.ACTIVITY_STREAM_IP_WHITELIST.split(',') - return [false, 'Connecting from unauthorized IP'] unless authorized_ip_addresses.include?(remote_ips[-2]) + unless ENV['COPILOT_ENVIRONMENT_NAME'] # DBT Platform + remote_ips = request.headers['X-Forwarded-For'].gsub(/\s+/, '').split(',') + return [false, 'Connecting from unauthorized IP'] unless remote_ips.length >= 2 + + authorized_ip_addresses = Figaro.env.ACTIVITY_STREAM_IP_WHITELIST.split(',') + return [false, 'Connecting from unauthorized IP'] unless authorized_ip_addresses.include?(remote_ips[-2]) + end return [false, 'Authorization header is missing'] unless request.headers.key?('Authorization') diff --git a/spec/controllers/api/activity_stream_spec.rb b/spec/controllers/api/activity_stream_spec.rb index da8bbb450..ab0a9768f 100644 --- a/spec/controllers/api/activity_stream_spec.rb +++ b/spec/controllers/api/activity_stream_spec.rb @@ -7,9 +7,9 @@ def auth_header(ts, key_id, secret_key, uri, payload) credentials = { id: key_id, key: secret_key, - algorithm: 'sha256', + algorithm: 'sha256' } - return Hawk::Client.build_authorization_header( + Hawk::Client.build_authorization_header( credentials: credentials, ts: ts, method: 'GET', @@ -17,7 +17,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) host: 'test.host', port: '443', content_type: '', - payload: payload, + payload: payload ) end @@ -27,7 +27,6 @@ def auth_header(ts, key_id, secret_key, uri, payload) end describe 'GET #enquiries' do - it 'if activity_stream is not enabled responds with a 403 error' do allow(Figaro.env).to receive('ACTIVITY_STREAM_ENABLED').and_return(nil) get :enquiries, params: { format: :json } @@ -35,8 +34,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) expect(response.body).to eq(%({"message":"Activity Stream is disabled"})) end - describe "authorization" do - + describe 'authorization' do it 'responds with a 401 error if connecting from unauthorized IP' do # The whitelist is 0.0.0.0, and we reject all requests that don't have # 0.0.0.0 as the second-to-last IP in X-Fowarded-For, as this isn't @@ -44,19 +42,15 @@ def auth_header(ts, key_id, secret_key, uri, payload) @request.headers['X-Forwarded-For'] = '1.2.3.4' get :enquiries, params: { format: :json } expect(response.body).to eq(%({"message":"Connecting from unauthorized IP"})) - @request.headers['X-Forwarded-For'] = '0.0.0.0' get :enquiries, params: { format: :json } expect(response.body).to eq(%({"message":"Connecting from unauthorized IP"})) - @request.headers['X-Forwarded-For'] = '1.2.3.4, 0.0.0.0' get :enquiries, params: { format: :json } expect(response.body).to eq(%({"message":"Connecting from unauthorized IP"})) - @request.headers['X-Forwarded-For'] = '0.0.0.0, 1.2.3.4, 123.123.123' get :enquiries, params: { format: :json } expect(response.body).to eq(%({"message":"Connecting from unauthorized IP"})) - @request.headers['X-Forwarded-For'] = '1.2.3.4, 123.123.123, 0.0.0.0' get :enquiries, params: { format: :json } expect(response.body).to eq(%({"message":"Connecting from unauthorized IP"})) @@ -71,12 +65,12 @@ def auth_header(ts, key_id, secret_key, uri, payload) it 'responds with a 401 if Authorization header in invalid format' do @request.headers['X-Forwarded-For'] = '0.0.0.0, 1.2.3.4' - @request.headers['Authorization'] = 'Hawk' # Should have a space after + @request.headers['Authorization'] = 'Hawk' # Should have a space after get :enquiries, params: { format: :json } expect(response.status).to eq(401) expect(response.body).to eq(%({"message":"Invalid header"})) - @request.headers['Authorization'] = 'Hawk ' # Should not have two spaces after + @request.headers['Authorization'] = 'Hawk ' # Should not have two spaces after get :enquiries, params: { format: :json } expect(response.status).to eq(401) expect(response.body).to eq(%({"message":"Invalid header"})) @@ -96,27 +90,27 @@ def auth_header(ts, key_id, secret_key, uri, payload) expect(response.status).to eq(401) expect(response.body).to eq(%({"message":"Invalid header"})) - @request.headers['Authorization'] = 'Hawk b="a" c="d"' # Should have commas + @request.headers['Authorization'] = 'Hawk b="a" c="d"' # Should have commas get :enquiries, params: { format: :json } expect(response.status).to eq(401) expect(response.body).to eq(%({"message":"Invalid header"})) - @request.headers['Authorization'] = 'Hawk, b="a", c="d"' # Should not have comma after Hawk + @request.headers['Authorization'] = 'Hawk, b="a", c="d"' # Should not have comma after Hawk get :enquiries, params: { format: :json } expect(response.status).to eq(401) expect(response.body).to eq(%({"message":"Invalid header"})) - @request.headers['Authorization'] = 'Hawk b="a",c="d"' # Should have space after comma + @request.headers['Authorization'] = 'Hawk b="a",c="d"' # Should have space after comma get :enquiries, params: { format: :json } expect(response.status).to eq(401) expect(response.body).to eq(%({"message":"Invalid header"})) - @request.headers['Authorization'] = 'Hawk b="a", c="d" ' # Should not have trailing space + @request.headers['Authorization'] = 'Hawk b="a", c="d" ' # Should not have trailing space get :enquiries, params: { format: :json } expect(response.status).to eq(401) expect(response.body).to eq(%({"message":"Invalid header"})) - @request.headers['Authorization'] = 'Hawk B="a"' # Keys must be lower case + @request.headers['Authorization'] = 'Hawk B="a"' # Keys must be lower case get :enquiries, params: { format: :json } expect(response.status).to eq(401) expect(response.body).to eq(%({"message":"Invalid header"})) @@ -126,7 +120,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ).sub('Hawk ', 'AWS ') get :enquiries, params: { format: :json } @@ -138,8 +132,8 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', - ).sub('Hawk ', ' Hawk ') # Should not have leading space + '' + ).sub('Hawk ', ' Hawk ') # Should not have leading space get :enquiries, params: { format: :json } expect(response.status).to eq(401) @@ -150,7 +144,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ).sub('Hawk ', '') get :enquiries, params: { format: :json } @@ -162,7 +156,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ).sub('Hawk ', ', ') get :enquiries, params: { format: :json } @@ -174,7 +168,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ).sub('Hawk ', '", ') get :enquiries, params: { format: :json } @@ -189,7 +183,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } @@ -203,7 +197,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing ts") + expect(response.body).to include('Missing ts') end it 'responds with a 401 if Authorization header has non integer ts' do @@ -212,7 +206,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Invalid ts") + expect(response.body).to include('Invalid ts') end it 'responds with a 401 if Authorization header has empty ts' do @@ -221,7 +215,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing ts") + expect(response.body).to include('Missing ts') end it 'responds with a 401 if Authorization header misses mac' do @@ -230,7 +224,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing mac") + expect(response.body).to include('Missing mac') end it 'responds with a 401 if Authorization header has empty mac' do @@ -239,7 +233,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing mac") + expect(response.body).to include('Missing mac') end it 'responds with a 401 if Authorization header misses hash' do @@ -248,7 +242,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing hash") + expect(response.body).to include('Missing hash') end it 'responds with a 401 if Authorization header has empty hash' do @@ -257,7 +251,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing hash") + expect(response.body).to include('Missing hash') end it 'responds with a 401 if Authorization header misses nonce' do @@ -266,7 +260,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing hash") + expect(response.body).to include('Missing hash') end it 'responds with a 401 if Authorization header has empty nonce' do @@ -275,7 +269,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing hash") + expect(response.body).to include('Missing hash') end it 'responds with a 401 if Authorization header misses id' do @@ -284,7 +278,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing id") + expect(response.body).to include('Missing id') end it 'responds with a 401 if Authorization header has empty id' do @@ -293,7 +287,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Missing id") + expect(response.body).to include('Missing id') end it 'responds with a 401 if Authorization header uses incorrect key ID' do @@ -303,7 +297,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID + 'something-incorrect', Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } @@ -318,12 +312,12 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY + 'something-incorrect', activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Invalid mac") + expect(response.body).to include('Invalid mac') end it 'responds with a 401 if Authorization header uses incorrect payload' do @@ -333,12 +327,12 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - 'something-incorrect', + 'something-incorrect' ) get :enquiries, params: { format: :json } expect(response.status).to eq(401) - expect(response.body).to include("Invalid hash") + expect(response.body).to include('Invalid hash') end it 'responds with a 401 if header is reused' do @@ -348,7 +342,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } @@ -372,13 +366,13 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) begin get :enquiries, params: { format: :json } - rescue Redis::CannotConnectError => ex + rescue Redis::CannotConnectError => e end - expect(ex.backtrace.to_s).to include('/redis/') + expect(e.backtrace.to_s).to include('/redis/') end it 'responds with no items if Authorization header is set and correct' do @@ -388,18 +382,16 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } expect(JSON.parse(response.body)['orderedItems']).to eq([]) expect(response.headers['Content-Type']).to eq('application/json; charset=utf-8') end - end - describe "content" do - + describe 'content' do it 'does not have any entry elements if an enquiry made without a company number' do create(:enquiry, company_house_number: nil) @@ -409,7 +401,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } @@ -425,7 +417,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } @@ -447,7 +439,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } feed_hash = JSON.parse(response.body) @@ -455,7 +447,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) items = feed_hash['orderedItems'] expect(items.length).to eq(1) - item = items[0] + item = items[0] expect(item['id']).to eq("dit:exportOpportunities:Enquiry:#{enquiry.id}:Create") expect(item['type']).to eq('Create') expect(item['object']['published']).to eq('2008-09-01T12:01:02+00:00') @@ -486,7 +478,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } feed_hash = JSON.parse(response.body) @@ -497,7 +489,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) elastic_search_bulk_1 = items[0] expect(elastic_search_bulk_1['object']['published']).to eq('2008-09-01T12:01:02+00:00') - elastic_search_bulk_2 = items[1] + elastic_search_bulk_2 = items[1] expect(elastic_search_bulk_2['object']['published']).to eq('2008-09-01T12:01:03+00:00') end @@ -518,7 +510,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } feed_hash = JSON.parse(response.body) @@ -528,7 +520,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) elastic_search_bulk_1 = items[0] expect(elastic_search_bulk_1['actor'][0]['dit:companiesHouseNumber']).to eq('124') - elastic_search_bulk_2 = items[1] + elastic_search_bulk_2 = items[1] expect(elastic_search_bulk_2['actor'][0]['dit:companiesHouseNumber']).to eq('123') end @@ -537,10 +529,10 @@ def auth_header(ts, key_id, secret_key, uri, payload) country_2 = create(:country) # Creating records takes quite a while. Stub for a quicker test - stub_const("MAX_PER_PAGE", 20) - Timecop.freeze(Time.utc(2008, 9, 1, 12, 1, 2, 344590)) do - for i in 1..21 do - enquiry = create(:enquiry, company_house_number: i.to_s, id:(2923 + i)) + stub_const('MAX_PER_PAGE', 20) + Timecop.freeze(Time.utc(2008, 9, 1, 12, 1, 2, 344_590)) do + (1..21).each do |i| + enquiry = create(:enquiry, company_house_number: i.to_s, id: (2923 + i)) enquiry.opportunity.countries = [country_1, country_2] end end @@ -551,7 +543,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_enquiries_path, - '', + '' ) get :enquiries, params: { format: :json } feed_hash_1 = JSON.parse(response.body) @@ -566,7 +558,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, "#{activity_stream_enquiries_path}?search_after=1220270462.344590_2943", - '', + '' ) get :enquiries, params: { format: :json, search_after: '1220270462.344590_2943' } feed_hash_2 = JSON.parse(response.body) @@ -578,7 +570,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, "#{activity_stream_enquiries_path}?search_after=1220270462.344590_2944", - '', + '' ) get :enquiries, params: { format: :json, search_after: '1220270462.344590_2944' } feed_hash_3 = JSON.parse(response.body) @@ -589,9 +581,7 @@ def auth_header(ts, key_id, secret_key, uri, payload) end describe 'GET #opportunities' do - - describe "content" do - + describe 'content' do before do Timecop.freeze(Time.utc(2008, 9, 1, 12, 1, 2)) end @@ -600,31 +590,31 @@ def auth_header(ts, key_id, secret_key, uri, payload) Timecop.return end - def create_opportunity(state=:published, options={}) + def create_opportunity(state = :published, options = {}) options = { - title: options[:title] || "2x4 Wood", - slug: options[:slug] ||"2x4-wood", - buyer_name: options[:buyer_name] || "Xi Weng Manufacturing", - buyer_address: options[:buyer_address] || - "2002 Jiabin Rd, RenMin NanLu, Luohu Qu, Shenzhen Shi, Guangdong Sheng, China, 518011", + title: options[:title] || '2x4 Wood', + slug: options[:slug] || '2x4-wood', + buyer_name: options[:buyer_name] || 'Xi Weng Manufacturing', + buyer_address: options[:buyer_address] || + '2002 Jiabin Rd, RenMin NanLu, Luohu Qu, Shenzhen Shi, Guangdong Sheng, China, 518011', first_published_at: options[:first_published_at] || Time.utc(2001, 9, 3), - response_due_on: options[:response_due_on] || Time.utc(2008, 12, 1), - teaser: options[:teaser] || "Looking for 50kg of 2x4 Oak Wood Blocks", - description: options[:description] || - "We are proud to announce the tender for our annual wood block requirement. - We are looking for 50kg of 2x4 Oak Wood Blocks" + response_due_on: options[:response_due_on] || Time.utc(2008, 12, 1), + teaser: options[:teaser] || 'Looking for 50kg of 2x4 Oak Wood Blocks', + description: options[:description] || + "We are proud to announce the tender for our annual wood block requirement. + We are looking for 50kg of 2x4 Oak Wood Blocks" } create(:opportunity, state, options) end - def get_feed(path, params={}) + def get_feed(path, params = {}) @request.headers['X-Forwarded-For'] = '0.0.0.0, 1.2.3.4' @request.headers['Authorization'] = auth_header( Time.now.getutc.to_i, Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, path, - '', + '' ) get :opportunities, params: { format: :json }.merge(params) @@ -635,7 +625,7 @@ def get_feed(path, params={}) opportunity = create_opportunity(:published) items = get_feed(activity_stream_opportunities_path) - uri = URI.parse(ENV["DOMAIN"]) + uri = URI.parse(ENV['DOMAIN']) domain = "#{uri.scheme}://#{uri.host}" expect(items.length).to eq(1) @@ -650,8 +640,8 @@ def get_feed(path, params={}) expect(item['object']['published']).to eq('2001-09-03T00:00:00+00:00') expect(item['object']['endTime']).to eq('2008-12-01T00:00:00+00:00') expect(item['object']['summary']).to eq('Looking for 50kg of 2x4 Oak Wood Blocks') - expect(item['object']['content']).to eq('We are proud to announce the tender for our annual wood block requirement. - We are looking for 50kg of 2x4 Oak Wood Blocks') + expect(item['object']['content']).to eq('We are proud to announce the tender for our annual wood block requirement. + We are looking for 50kg of 2x4 Oak Wood Blocks') expect(item['object']['attributedTo'][0]['name']).to eq('post') end @@ -664,14 +654,14 @@ def get_feed(path, params={}) it 'returns opportunities in date order' do # Create two an opportunities - for i in 0..1 do + (0..1).each do |i| op = create_opportunity(:published, { - title: "2x4 Wood #{i}", - slug: "2x4-wood-#{i}" - }) + title: "2x4 Wood #{i}", + slug: "2x4-wood-#{i}" + }) op.update_column(:updated_at, Time.now + i.days) end - + items = get_feed(activity_stream_opportunities_path) expect(items.length).to eq(2) @@ -682,11 +672,11 @@ def get_feed(path, params={}) it 'in ID order if two opportunities are made at the same time' do Opportunity.destroy_all - for i in 0..1 do + (0..1).each do |i| op = create_opportunity(:published, { - title: "2x4 Wood #{i}", - slug: "2x4-wood-#{i}", - }) + title: "2x4 Wood #{i}", + slug: "2x4-wood-#{i}" + }) op.update_column(:updated_at, Time.now) end @@ -699,21 +689,20 @@ def get_feed(path, params={}) it 'is paginated with a link element if there are MAX_PER_PAGE opportunities' do Opportunity.destroy_all - ServiceProvider.create(name: "British Embassy") + ServiceProvider.create(name: 'British Embassy') - stub_const("MAX_PER_PAGE", 20) - Timecop.freeze(Time.utc(2008, 9, 1, 12, 1, 2, 344590)) do - for i in 1..21 do + stub_const('MAX_PER_PAGE', 20) + Timecop.freeze(Time.utc(2008, 9, 1, 12, 1, 2, 344_590)) do + (1..21).each do |i| op = create_opportunity(:published, { - title: "2x4 Wood #{i}", - slug: "2x4-wood-#{i}", - service_provider: ServiceProvider.find(ServiceProvider.ids.shuffle.first) - }) + title: "2x4 Wood #{i}", + slug: "2x4-wood-#{i}", + service_provider: ServiceProvider.find(ServiceProvider.ids.sample) + }) op.update_column(:updated_at, Time.now + i.days) end end - # Fetch data for search_after() to help generate 'next' URL twenty = Opportunity.order(:updated_at)[19] id_20 = twenty.id @@ -728,7 +717,7 @@ def get_feed(path, params={}) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_opportunities_path, - '', + '' ) get :opportunities, params: { format: :json } feed_hash_1 = JSON.parse(response.body) @@ -743,11 +732,11 @@ def get_feed(path, params={}) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, "#{activity_stream_opportunities_path}?search_after=#{timestamp_20}_#{id_20}", - '', + '' ) get :opportunities, params: { format: :json, search_after: "#{timestamp_20}_#{id_20}" } feed_hash_2 = JSON.parse(response.body) - + expect(feed_hash_2.key?('next')).to eq(true) expect(feed_hash_2['orderedItems'][0]['id']).to eq("dit:exportOpportunities:Opportunity:#{id_21}:Create") @@ -756,29 +745,26 @@ def get_feed(path, params={}) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, "#{activity_stream_opportunities_path}?search_after=#{timestamp_21}_#{id_21}", - '', + '' ) get :opportunities, params: { format: :json, search_after: "#{timestamp_21}_#{id_21}" } feed_hash_3 = JSON.parse(response.body) expect(feed_hash_3.key?('next')).to eq(false) expect(feed_hash_3['orderedItems']).to eq([]) end - end - describe "authorization" do - + describe 'authorization' do it 'responds with a 401 error if Authorization header is not set' do @request.headers['X-Forwarded-For'] = '0.0.0.0, 1.2.3.4' get :opportunities, params: { format: :json } expect(response.status).to eq(401) expect(response.body).to eq(%({"message":"Authorization header is missing"})) end - end end - describe 'GET #csat_feedback' do + describe 'GET #csat_feedback' do describe 'content' do before do Timecop.freeze(Time.utc(2008, 9, 1, 12, 1, 2)) @@ -788,14 +774,14 @@ def get_feed(path, params={}) Timecop.return end - def get_feed(path, params={}) + def get_feed(path, params = {}) @request.headers['X-Forwarded-For'] = '0.0.0.0, 1.2.3.4' @request.headers['Authorization'] = auth_header( Time.now.getutc.to_i, Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, path, - '', + '' ) get :csat_feedback, params: { format: :json }.merge(params) @@ -807,12 +793,12 @@ def get_feed(path, params={}) items = get_feed(activity_stream_csat_feedback_path) - uri = URI.parse(ENV["DOMAIN"]) + uri = URI.parse(ENV['DOMAIN']) domain = "#{uri.scheme}://#{uri.host}" expect(items.length).to eq(1) item = items[0] - item_object = item["object"] + item_object = item['object'] expect(item['id']).to eq("dit:exportOpportunities:HCSATFeedbackData:#{feedback.id}:Update") expect(item['type']).to eq('Update') @@ -831,14 +817,14 @@ def get_feed(path, params={}) it 'is paginated with a link element if there are MAX_PER_PAGE csat feedback responses' do CustomerSatisfactionFeedback.destroy_all - stub_const("MAX_PER_PAGE", 20) - Timecop.freeze(Time.utc(2008, 9, 1, 12, 1, 2, 344590)) do - for i in 1..21 do + stub_const('MAX_PER_PAGE', 20) + Timecop.freeze(Time.utc(2008, 9, 1, 12, 1, 2, 344_590)) do + (1..21).each do |i| feedback = create(:csat_feedback) feedback.update_column(:created_at, Time.now + i.days) end end - + # Fetch data for search_after() to help generate 'next' URL twenty = CustomerSatisfactionFeedback.order(:created_at)[19] id_20 = twenty.id @@ -853,7 +839,7 @@ def get_feed(path, params={}) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, activity_stream_csat_feedback_path, - '', + '' ) get :csat_feedback, params: { format: :json } feed_hash_1 = JSON.parse(response.body) @@ -868,11 +854,11 @@ def get_feed(path, params={}) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, "#{activity_stream_csat_feedback_path}?search_after=#{timestamp_20}_#{id_20}", - '', + '' ) get :csat_feedback, params: { format: :json, search_after: "#{timestamp_20}_#{id_20}" } feed_hash_2 = JSON.parse(response.body) - + expect(feed_hash_2.key?('next')).to eq(true) expect(feed_hash_2['orderedItems'][0]['id']).to eq("dit:exportOpportunities:HCSATFeedbackData:#{id_21}:Update") @@ -881,7 +867,7 @@ def get_feed(path, params={}) Figaro.env.ACTIVITY_STREAM_ACCESS_KEY_ID, Figaro.env.ACTIVITY_STREAM_SECRET_ACCESS_KEY, "#{activity_stream_csat_feedback_path}?search_after=#{timestamp_21}_#{id_21}", - '', + '' ) get :csat_feedback, params: { format: :json, search_after: "#{timestamp_21}_#{id_21}" } feed_hash_3 = JSON.parse(response.body) @@ -889,9 +875,9 @@ def get_feed(path, params={}) expect(feed_hash_3['orderedItems']).to eq([]) end end - - describe "authorization" do + + describe 'authorization' do it 'responds with a 401 error if Authorization header is not set' do @request.headers['X-Forwarded-For'] = '0.0.0.0, 1.2.3.4' get :csat_feedback, params: { format: :json }