1+ require 'rails_helper'
2+ require 'csv'
3+
4+ RSpec . describe 'Admin URNs' , type : :request do
5+ include SingleSignOnHelpers
6+
7+ before do
8+ stub_govuk_bank_holidays_request
9+ mock_sso_with ( email : 'admin@example.com' )
10+ get '/auth/google_oauth2/callback'
11+ end
12+
13+ describe 'GET /admin/urns' do
14+ it 'renders the URN search page' do
15+ get admin_urns_path
16+
17+ expect ( response ) . to have_http_status ( :ok )
18+ expect ( response . body ) . to include ( 'Active URN list' )
19+ expect ( response . body ) . to include ( 'Search' )
20+ expect ( response . body ) . to include ( 'Download Active URN list' )
21+ end
22+ end
23+
24+ describe 'GET /admin/urns/download' do
25+ let! ( :active_customer ) { create ( :customer , urn : '123' , name : 'Active Customer One' , postcode : 'AB1 2CD' , sector : :central_government ) }
26+ let! ( :deleted_customer ) { create ( :customer , urn : '456' , name : 'Deleted Customer' , postcode : 'IJ5 6KL' , sector : :wider_public_sector , deleted : true ) }
27+
28+ it 'returns a CSV file with active customers' do
29+ get download_admin_urns_path
30+
31+ expect ( response ) . to have_http_status ( :ok )
32+ expect ( response . headers [ 'Content-Type' ] ) . to include ( 'text/csv' )
33+ expect ( response . headers [ 'Content-Disposition' ] ) . to include ( "attachment; filename=\" customer_urns_#{ Time . zone . today } .csv\" " )
34+
35+ csv = CSV . parse ( response . body , headers : true )
36+ expect ( csv . headers ) . to eq ( [ 'URN' , 'CustomerName' , 'PostCode' , 'Sector' , 'Published' ] )
37+ expect ( csv . length ) . to eq ( 1 )
38+
39+ expect ( csv [ 0 ] [ 'URN' ] ) . to include ( '123' )
40+ expect ( csv [ 0 ] [ 'CustomerName' ] ) . to eq ( active_customer . name )
41+ expect ( csv [ 0 ] [ 'PostCode' ] ) . to eq ( active_customer . postcode )
42+ expect ( csv [ 0 ] [ 'Sector' ] ) . to eq ( active_customer . sector )
43+
44+ # Ensure deleted customer is not included
45+ csv . each do |row |
46+ expect ( row [ 'URN' ] ) . not_to include ( '456' )
47+ expect ( row [ 'CustomerName' ] ) . not_to eq ( deleted_customer . name )
48+ expect ( row [ 'PostCode' ] ) . not_to eq ( deleted_customer . postcode )
49+ end
50+ end
51+ end
52+ end
0 commit comments