modules/avs/spec/requests/avs/v0/avs_spec.rb
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'V0::Avs', type: :request do
  before do
    sign_in_as(current_user)
  end

  describe 'GET `index`' do
    let(:current_user) { build(:user, :loa3, :legacy_icn) }

    it 'returns error when stationNo is not given' do
      get '/avs/v0/avs/search?stationNo=&appointmentIen=123456'
      expect(response).to have_http_status(:bad_request)
    end

    it 'returns error when appointmentIen is not given' do
      get '/avs/v0/avs/search?stationNo=500&appointmentIen='
      expect(response).to have_http_status(:bad_request)
    end

    it 'returns error when stationNo does not have the correct format' do
      get '/avs/v0/avs/search?stationNo=a5c&appointmentIen=123456'
      expect(response).to have_http_status(:bad_request)
    end

    it 'returns error when appointmentIen does not have the correct format' do
      get '/avs/v0/avs/search?stationNo=500&appointmentIen=123abc'
      expect(response).to have_http_status(:bad_request)
    end

    it 'returns empty response found when AVS not found for appointment' do
      VCR.use_cassette('avs/search/empty') do
        get '/avs/v0/avs/search?stationNo=500&appointmentIen=10000'
        expect(response).to have_http_status(:ok)
        expect(JSON.parse(response.body)).to eq({})
      end
    end

    it 'returns 401 when ICN does not match' do
      VCR.use_cassette('avs/search/unauthorized') do
        get '/avs/v0/avs/search?stationNo=500&appointmentIen=9876543'
        expect(response).to have_http_status(:unauthorized)
      end
    end

    it 'returns 200 when ICN in response does not have checksum' do
      VCR.use_cassette('avs/search/icn_short') do
        get '/avs/v0/avs/search?stationNo=500&appointmentIen=9876543'
        expect(response).to have_http_status(:ok)
      end
    end

    it 'returns 200 when AVS found for appointment' do
      VCR.use_cassette('avs/search/9876543') do
        get '/avs/v0/avs/search?stationNo=500&appointmentIen=9876543'
        expect(response).to have_http_status(:ok)
        expect(JSON.parse(response.body)).to eq(
          {
            'path' => '/my-health/medical-records/summaries-and-notes/visit-summary/9A7AF40B2BC2471EA116891839113252'
          }
        )
      end
    end
  end

  describe 'GET `show`' do
    let(:current_user) { build(:user, :loa3, :legacy_icn) }

    it 'returns error when sid format is incorrect' do
      get '/avs/v0/avs/1234567890'
      expect(response).to have_http_status(:bad_request)
    end

    it 'returns error when sid is too short' do
      get '/avs/v0/avs/3e8535e743ce63ed333ebbbb67626'
      expect(response).to have_http_status(:bad_request)
    end

    it 'returns error when sid is too long' do
      get '/avs/v0/avs/5e68a88a158a70da31e48c0bf02243545e68a88a1'
      expect(response).to have_http_status(:bad_request)
    end

    it 'returns error when sid is not in hex format' do
      get '/avs/v0/avs/5e68a88a158a70da31e48c0bf0224354z'
      expect(response).to have_http_status(:bad_request)
    end

    it 'returns empty response found when AVS not found for sid' do
      VCR.use_cassette('avs/show/not_found') do
        get '/avs/v0/avs/9A7AF40B2BC2471EA116891839113253'
        expect(response).to have_http_status(:not_found)
      end
    end

    it 'returns 401 when ICN does not match' do
      VCR.use_cassette('avs/show/unauthorized') do
        get '/avs/v0/avs/9A7AF40B2BC2471EA116891839113252'
        expect(response).to have_http_status(:unauthorized)
      end
    end

    it 'returns 401 when ICN is null in source file' do
      VCR.use_cassette('avs/show/icn_null') do
        get '/avs/v0/avs/9A7AF40B2BC2471EA116891839113252'
        expect(response).to have_http_status(:unauthorized)
      end
    end

    it 'returns 200 when ICN in AVS does not have checksum' do
      VCR.use_cassette('avs/show/icn_short') do
        get '/avs/v0/avs/9A7AF40B2BC2471EA116891839113252'
        expect(response).to have_http_status(:ok)
      end
    end

    it 'returns 200 when AVS found for appointment' do
      sid = '9A7AF40B2BC2471EA116891839113252'
      VCR.use_cassette("avs/show/#{sid}") do
        get "/avs/v0/avs/#{sid}"
        expect(response).to have_http_status(:ok)
        parsed = JSON.parse(response.body)
        expect(parsed['data']['id']).to eq(sid)
      end
    end
  end
end
