@ -252,24 +252,36 @@ RSpec.describe LettingsLogsController, type: :request do
end
it " does have organisation values " do
get " /lettings-logs " , headers : headers , params : { }
get " /lettings-logs " , headers : , params : { }
expect ( page ) . to have_content ( " Owned by " )
expect ( page ) . to have_content ( " Managed by " )
end
it " shows lettings logs for all organisations " do
get " /lettings-logs " , headers : headers , params : { }
get " /lettings-logs " , headers : , params : { }
expect ( page ) . to have_content ( " LC783 " )
expect ( page ) . to have_content ( " UA984 " )
end
it " displays CSV download links with the correct paths " do
get " /lettings-logs " , headers : , params : { }
expect ( page ) . to have_link ( " Download (CSV) " , href : " /lettings-logs/csv-download?codes_only=false " )
expect ( page ) . to have_link ( " Download (CSV, codes only) " , href : " /lettings-logs/csv-download?codes_only=true " )
end
context " when there are no logs in the database " do
before do
LettingsLog . destroy_all
end
it " does not display CSV download links " do
get " /lettings-logs " , headers : , params : { }
expect ( page ) . not_to have_link ( " Download (CSV) " )
expect ( page ) . not_to have_link ( " Download (CSV, codes only) " )
end
it " page has correct title " do
get " /lettings-logs " , headers : headers , params : { }
get " /lettings-logs " , headers : , params : { }
expect ( page ) . to have_title ( " Logs - Submit social housing lettings and sales data (CORE) - GOV.UK " )
end
end
@ -292,29 +304,29 @@ RSpec.describe LettingsLogsController, type: :request do
end
it " shows lettings logs for multiple selected statuses " do
get " /lettings-logs?status[]=in_progress&status[]=completed " , headers : headers , params : { }
get " /lettings-logs?status[]=in_progress&status[]=completed " , headers : , params : { }
expect ( page ) . to have_link ( in_progress_lettings_log . id . to_s )
expect ( page ) . to have_link ( completed_lettings_log . id . to_s )
end
it " shows lettings logs for one selected status " do
get " /lettings-logs?status[]=in_progress " , headers : headers , params : { }
get " /lettings-logs?status[]=in_progress " , headers : , params : { }
expect ( page ) . to have_link ( in_progress_lettings_log . id . to_s )
expect ( page ) . not_to have_link ( completed_lettings_log . id . to_s )
end
it " filters on organisation " do
get " /lettings-logs?organisation[]= #{ organisation_2 . id } " , headers : headers , params : { }
get " /lettings-logs?organisation[]= #{ organisation_2 . id } " , headers : , params : { }
expect ( page ) . to have_link ( completed_lettings_log . id . to_s )
expect ( page ) . not_to have_link ( in_progress_lettings_log . id . to_s )
end
it " does not reset the filters " do
get " /lettings-logs?status[]=in_progress " , headers : headers , params : { }
get " /lettings-logs?status[]=in_progress " , headers : , params : { }
expect ( page ) . to have_link ( in_progress_lettings_log . id . to_s )
expect ( page ) . not_to have_link ( completed_lettings_log . id . to_s )
get " /lettings-logs " , headers : headers , params : { }
get " /lettings-logs " , headers : , params : { }
expect ( page ) . to have_link ( in_progress_lettings_log . id . to_s )
expect ( page ) . not_to have_link ( completed_lettings_log . id . to_s )
end
@ -338,13 +350,13 @@ RSpec.describe LettingsLogsController, type: :request do
end
it " shows lettings logs for multiple selected years " do
get " /lettings-logs?years[]=2021&years[]=2022 " , headers : headers , params : { }
get " /lettings-logs?years[]=2021&years[]=2022 " , headers : , params : { }
expect ( page ) . to have_link ( lettings_log_2021 . id . to_s )
expect ( page ) . to have_link ( lettings_log_2022 . id . to_s )
end
it " shows lettings logs for one selected year " do
get " /lettings-logs?years[]=2021 " , headers : headers , params : { }
get " /lettings-logs?years[]=2021 " , headers : , params : { }
expect ( page ) . to have_link ( lettings_log_2021 . id . to_s )
expect ( page ) . not_to have_link ( lettings_log_2022 . id . to_s )
end
@ -387,14 +399,14 @@ RSpec.describe LettingsLogsController, type: :request do
end
it " shows lettings logs for multiple selected statuses and years " do
get " /lettings-logs?years[]=2021&years[]=2022&status[]=in_progress&status[]=completed " , headers : headers , params : { }
get " /lettings-logs?years[]=2021&years[]=2022&status[]=in_progress&status[]=completed " , headers : , params : { }
expect ( page ) . to have_link ( lettings_log_2021 . id . to_s )
expect ( page ) . to have_link ( lettings_log_2022 . id . to_s )
expect ( page ) . to have_link ( lettings_log_2022_in_progress . id . to_s )
end
it " shows lettings logs for one selected status " do
get " /lettings-logs?years[]=2022&status[]=in_progress " , headers : headers , params : { }
get " /lettings-logs?years[]=2022&status[]=in_progress " , headers : , params : { }
expect ( page ) . to have_link ( lettings_log_2022_in_progress . id . to_s )
expect ( page ) . not_to have_link ( lettings_log_2021 . id . to_s )
expect ( page ) . not_to have_link ( lettings_log_2022 . id . to_s )
@ -512,23 +524,36 @@ RSpec.describe LettingsLogsController, type: :request do
end
it " does not have organisation columns " do
get " /lettings-logs " , headers : headers , params : { }
get " /lettings-logs " , headers : , params : { }
expect ( page ) . not_to have_content ( " Owning organisation " )
expect ( page ) . not_to have_content ( " Managing organisation " )
end
it " displays standard CSV download link only, with the correct path " do
get " /lettings-logs " , headers : , params : { }
expect ( page ) . to have_link ( " Download (CSV) " , href : " /lettings-logs/csv-download?codes_only=false " )
expect ( page ) . not_to have_link ( " Download (CSV, codes only) " )
end
it " does not display CSV download links if there are no logs " do
LettingsLog . destroy_all
get " /lettings-logs " , headers : , params : { }
expect ( page ) . not_to have_link ( " Download (CSV) " )
expect ( page ) . not_to have_link ( " Download (CSV, codes only) " )
end
context " when using a search query " do
let ( :logs ) { FactoryBot . create_list ( :lettings_log , 3 , :completed , owning_organisation : user . organisation , created_by : user ) }
let ( :log_to_search ) { FactoryBot . create ( :lettings_log , :completed , owning_organisation : user . organisation , created_by : user ) }
let ( :log_total_count ) { LettingsLog . where ( owning_organisation : user . organisation ) . count }
it " has search results in the title " do
get " /lettings-logs?search= #{ log_to_search . id } " , headers : headers , params : { }
get " /lettings-logs?search= #{ log_to_search . id } " , headers : , params : { }
expect ( page ) . to have_title ( " Logs (1 logs matching ‘ #{ log_to_search . id } ’) - Submit social housing lettings and sales data (CORE) - GOV.UK " )
end
it " shows lettings logs matching the id " do
get " /lettings-logs?search= #{ log_to_search . id } " , headers : headers , params : { }
get " /lettings-logs?search= #{ log_to_search . id } " , headers : , params : { }
expect ( page ) . to have_link ( log_to_search . id . to_s )
logs . each do | log |
expect ( page ) . not_to have_link ( log . id . to_s )
@ -536,7 +561,7 @@ RSpec.describe LettingsLogsController, type: :request do
end
it " shows lettings logs matching the tenant code " do
get " /lettings-logs?search= #{ log_to_search . tenancycode } " , headers : headers , params : { }
get " /lettings-logs?search= #{ log_to_search . tenancycode } " , headers : , params : { }
expect ( page ) . to have_link ( log_to_search . id . to_s )
logs . each do | log |
expect ( page ) . not_to have_link ( log . id . to_s )
@ -544,7 +569,7 @@ RSpec.describe LettingsLogsController, type: :request do
end
it " shows lettings logs matching the property reference " do
get " /lettings-logs?search= #{ log_to_search . propcode } " , headers : headers , params : { }
get " /lettings-logs?search= #{ log_to_search . propcode } " , headers : , params : { }
expect ( page ) . to have_link ( log_to_search . id . to_s )
logs . each do | log |
expect ( page ) . not_to have_link ( log . id . to_s )
@ -552,25 +577,27 @@ RSpec.describe LettingsLogsController, type: :request do
end
it " shows lettings logs matching the property postcode " do
get " /lettings-logs?search= #{ log_to_search . postcode_full } " , headers : headers , params : { }
get " /lettings-logs?search= #{ log_to_search . postcode_full } " , headers : , params : { }
expect ( page ) . to have_link ( log_to_search . id . to_s )
logs . each do | log |
expect ( page ) . not_to have_link ( log . id . to_s )
end
end
it " includes the search on the CSV link " do
it " includes the search on the CSV links " do
search_term = " foo "
FactoryBot . create ( :lettings_log , created_by : user , owning_organisation : user . organisation , tenancycode : " foo " )
get " /lettings-logs?search= #{ search_term } " , headers : headers , params : { }
expect ( page ) . to have_link ( " Download (CSV) " , href : " /lettings-logs/csv-download?search= #{ search_term } " )
get " /lettings-logs?search= #{ search_term } " , headers : , params : { }
download_link = page . find_link ( " Download (CSV) " )
download_link_params = CGI . parse ( URI . parse ( download_link [ :href ] ) . query )
expect ( download_link_params ) . to include ( " search " = > [ search_term ] )
end
context " when more than one results with matching postcode " do
let! ( :matching_postcode_log ) { FactoryBot . create ( :lettings_log , :completed , owning_organisation : user . organisation , postcode_full : log_to_search . postcode_full ) }
it " displays all matching logs " do
get " /lettings-logs?search= #{ log_to_search . postcode_full } " , headers : headers , params : { }
get " /lettings-logs?search= #{ log_to_search . postcode_full } " , headers : , params : { }
expect ( page ) . to have_link ( log_to_search . id . to_s )
expect ( page ) . to have_link ( matching_postcode_log . id . to_s )
logs . each do | log |
@ -585,12 +612,12 @@ RSpec.describe LettingsLogsController, type: :request do
let ( :log_total_count ) { LettingsLog . where ( owning_organisation : user . organisation ) . count }
it " has title with pagination details for page 1 " do
get " /lettings-logs?search= #{ logs [ 0 ] . postcode_full } " , headers : headers , params : { }
get " /lettings-logs?search= #{ logs [ 0 ] . postcode_full } " , headers : , params : { }
expect ( page ) . to have_title ( " Logs ( #{ logs . count } logs matching ‘ #{ postcode } ’) (page 1 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK " )
end
it " has title with pagination details for page 2 " do
get " /lettings-logs?search= #{ logs [ 0 ] . postcode_full } &page=2 " , headers : headers , params : { }
get " /lettings-logs?search= #{ logs [ 0 ] . postcode_full } &page=2 " , headers : , params : { }
expect ( page ) . to have_title ( " Logs ( #{ logs . count } logs matching ‘ #{ postcode } ’) (page 2 of 2) - Submit social housing lettings and sales data (CORE) - GOV.UK " )
end
end
@ -621,7 +648,7 @@ RSpec.describe LettingsLogsController, type: :request do
let! ( :log_matching_filter_and_search ) { FactoryBot . create ( :lettings_log , :in_progress , owning_organisation : user . organisation , postcode_full : matching_postcode , created_by : user ) }
it " shows only logs matching both search and filters " do
get " /lettings-logs?search= #{ matching_postcode } &status[]= #{ matching_status } " , headers : headers , params : { }
get " /lettings-logs?search= #{ matching_postcode } &status[]= #{ matching_status } " , headers : , params : { }
expect ( page ) . to have_link ( log_matching_filter_and_search . id . to_s )
expect ( page ) . not_to have_link ( log_to_search . id . to_s )
logs . each do | log |
@ -631,7 +658,7 @@ RSpec.describe LettingsLogsController, type: :request do
end
end
context " when there are less than 20 logs " do
context " when there are fewer than 20 logs " do
before do
get " /lettings-logs " , headers : , params : { }
end
@ -675,7 +702,7 @@ RSpec.describe LettingsLogsController, type: :request do
end
it " shows the CSV download link " do
expect ( page ) . to have_link ( " Download (CSV) " , href : " /lettings-logs/csv-download " )
expect ( page ) . to have_link ( " Download (CSV) " , href : " /lettings-logs/csv-download?codes_only=false " )
end
it " does not show the organisation filter " do
@ -809,7 +836,7 @@ RSpec.describe LettingsLogsController, type: :request do
context " with a user that is not signed in " do
it " does not let the user get lettings log tasklist pages they don't have access to " do
get " /lettings-logs/ #{ lettings_log . id } " , headers : headers , params : { }
get " /lettings-logs/ #{ lettings_log . id } " , headers : , params : { }
expect ( response ) . to redirect_to ( " /account/sign-in " )
end
end
@ -975,7 +1002,7 @@ RSpec.describe LettingsLogsController, type: :request do
before do
sign_in user
get " /lettings-logs/csv-download?search= #{ search_term } " , headers :
get " /lettings-logs/csv-download?search= #{ search_term } &codes_only=false " , headers :
end
it " returns http success " do
@ -1246,8 +1273,91 @@ RSpec.describe LettingsLogsController, type: :request do
end
end
describe " GET # csv-download " do
let ( :page ) { Capybara :: Node :: Simple . new ( response . body ) }
let ( :user ) { FactoryBot . create ( :user ) }
let ( :headers ) { { " Accept " = > " text/html " } }
before do
allow ( user ) . to receive ( :need_two_factor_authentication? ) . and_return ( false )
sign_in user
end
it " renders a page with the correct header " do
get " /lettings-logs/csv-download?codes_only=false " , headers : , params : { }
header = page . find_css ( " h1 " )
expect ( header . text ) . to include ( " Download CSV " )
end
it " renders a form with the correct target containing a button with the correct text " do
get " /lettings-logs/csv-download?codes_only=false " , headers : , params : { }
form = page . find ( " form.button_to " )
expect ( form [ :method ] ) . to eq ( " post " )
expect ( form [ :action ] ) . to eq ( " /lettings-logs/email-csv " )
expect ( form ) . to have_button ( " Send email " )
end
it " when query string contains search parameter, form contains hidden field with correct value " do
search_term = " blam "
get " /lettings-logs/csv-download?codes_only=false&search= #{ search_term } " , headers : , params : { }
hidden_field = page . find ( " form.button_to " ) . find_field ( " search " , type : " hidden " )
expect ( hidden_field . value ) . to eq ( search_term )
end
context " when the user is a data coordinator " do
let ( :user ) { FactoryBot . create ( :user , :data_coordinator ) }
it " when codes_only query parameter is false, form contains hidden field with correct value " do
codes_only = false
get " /lettings-logs/csv-download?codes_only= #{ codes_only } " , headers : , params : { }
hidden_field = page . find ( " form.button_to " ) . find_field ( " codes_only " , type : " hidden " )
expect ( hidden_field . value ) . to eq ( codes_only . to_s )
end
it " when codes_only query parameter is true, user is not authorized " do
codes_only = true
get " /lettings-logs/csv-download?codes_only= #{ codes_only } " , headers : , params : { }
expect ( response ) . to have_http_status ( :unauthorized )
end
end
context " when the user is a data provider " do
it " when codes_only query parameter is false, form contains hidden field with correct value " do
codes_only = false
get " /lettings-logs/csv-download?codes_only= #{ codes_only } " , headers : , params : { }
hidden_field = page . find ( " form.button_to " ) . find_field ( " codes_only " , type : " hidden " )
expect ( hidden_field . value ) . to eq ( codes_only . to_s )
end
it " when codes_only query parameter is true, user is not authorized " do
codes_only = true
get " /lettings-logs/csv-download?codes_only= #{ codes_only } " , headers : , params : { }
expect ( response ) . to have_http_status ( :unauthorized )
end
end
context " when the user is a support user " do
let ( :user ) { FactoryBot . create ( :user , :support ) }
it " when codes_only query parameter is false, form contains hidden field with correct value " do
codes_only = false
get " /lettings-logs/csv-download?codes_only= #{ codes_only } " , headers : , params : { }
hidden_field = page . find ( " form.button_to " ) . find_field ( " codes_only " , type : " hidden " )
expect ( hidden_field . value ) . to eq ( codes_only . to_s )
end
it " when codes_only query parameter is true, form contains hidden field with correct value " do
codes_only = true
get " /lettings-logs/csv-download?codes_only= #{ codes_only } " , headers : , params : { }
hidden_field = page . find ( " form.button_to " ) . find_field ( " codes_only " , type : " hidden " )
expect ( hidden_field . value ) . to eq ( codes_only . to_s )
end
end
end
describe " POST # email-csv " do
let ( :other_organisation ) { FactoryBot . create ( :organisation ) }
let ( :user ) { FactoryBot . create ( :user , :support ) }
context " when a log exists " do
let! ( :lettings_log ) do
@ -1259,6 +1369,7 @@ RSpec.describe LettingsLogsController, type: :request do
end
before do
allow ( user ) . to receive ( :need_two_factor_authentication? ) . and_return ( false )
sign_in user
FactoryBot . create ( :lettings_log )
FactoryBot . create ( :lettings_log ,
@ -1270,33 +1381,59 @@ RSpec.describe LettingsLogsController, type: :request do
it " creates an E-mail job " do
expect {
post " /lettings-logs/email-csv " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , nil , { } , false )
post " /lettings-logs/email-csv?codes_only=true " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , nil , { } , false , nil , true )
end
it " redirects to the confirmation page " do
post " /lettings-logs/email-csv " , headers : , params : { }
post " /lettings-logs/email-csv?codes_only=true " , headers : , params : { }
expect ( response ) . to redirect_to ( csv_confirmation_lettings_logs_path )
end
it " passes the search term " do
expect {
post " /lettings-logs/email-csv?search= #{ lettings_log . id } " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , lettings_log . id . to_s , { } , false )
post " /lettings-logs/email-csv?search= #{ lettings_log . id } &codes_only=false " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , lettings_log . id . to_s , { } , false , nil , false )
end
it " passes filter parameters " do
expect {
post " /lettings-logs/email-csv?status[]=completed " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , nil , { " status " = > %w[ completed ] } , false )
post " /lettings-logs/email-csv?status[]=completed&codes_only=true " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , nil , { " status " = > %w[ completed ] } , false , nil , true )
end
it " passes export type flag " do
expect {
post " /lettings-logs/email-csv?codes_only=true " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , nil , { } , false , nil , true )
expect {
post " /lettings-logs/email-csv?codes_only=false " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , nil , { } , false , nil , false )
end
it " passes a combination of search term and filter parameters " do
it " passes a combination of search term, export type and filter parameters " do
postcode = " XX1 1TG "
expect {
post " /lettings-logs/email-csv?status[]=completed&search= #{ postcode } " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , postcode , { " status " = > %w[ completed ] } , false )
post " /lettings-logs/email-csv?status[]=completed&search= #{ postcode } &codes_only=false " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , postcode , { " status " = > %w[ completed ] } , false , nil , false )
end
context " when the user is not a support user " do
let ( :user ) { FactoryBot . create ( :user , :data_coordinator ) }
it " has permission to download human readable csv " do
codes_only_export = false
expect {
post " /lettings-logs/email-csv?codes_only= #{ codes_only_export } " , headers : , params : { }
} . to enqueue_job ( EmailCsvJob ) . with ( user , nil , { } , false , nil , false )
end
it " is not authorized to download codes only csv " do
codes_only_export = true
post " /lettings-logs/email-csv?codes_only= #{ codes_only_export } " , headers : , params : { }
expect ( response ) . to have_http_status ( :unauthorized )
end
end
end
end