|
| 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) do |
| 26 | + create(:customer, urn: '123', name: 'Active Customer One', postcode: 'AB1 2CD', sector: :central_government) |
| 27 | + end |
| 28 | + let!(:deleted_customer) do |
| 29 | + create(:customer, urn: '456', name: 'Deleted Customer', postcode: 'IJ5 6KL', sector: :wider_public_sector, |
| 30 | + deleted: true) |
| 31 | + end |
| 32 | + |
| 33 | + it 'returns a CSV file with active customers' do |
| 34 | + get download_admin_urns_path |
| 35 | + |
| 36 | + expect(response).to have_http_status(:ok) |
| 37 | + expect(response.headers['Content-Type']).to include('text/csv') |
| 38 | + expect(response.headers['Content-Disposition']).to include("filename=\"customer_urns_#{Time.zone.today}.csv\"") |
| 39 | + |
| 40 | + csv = CSV.parse(response.body, headers: true) |
| 41 | + expect(csv.headers).to eq(['URN', 'CustomerName', 'PostCode', 'Sector', 'Published']) |
| 42 | + expect(csv.length).to eq(1) |
| 43 | + |
| 44 | + expect(csv[0]['URN']).to include('123') |
| 45 | + expect(csv[0]['CustomerName']).to eq(active_customer.name) |
| 46 | + expect(csv[0]['PostCode']).to eq(active_customer.postcode) |
| 47 | + expect(csv[0]['Sector']).to eq(active_customer.sector) |
| 48 | + |
| 49 | + # Ensure deleted customer is not included |
| 50 | + csv.each do |row| |
| 51 | + expect(row['URN']).not_to include('456') |
| 52 | + expect(row['CustomerName']).not_to eq(deleted_customer.name) |
| 53 | + expect(row['PostCode']).not_to eq(deleted_customer.postcode) |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | +end |
0 commit comments