<commit_before>require 'test_helper'
require 'api_test'

class ProductTestsControllerTest < ActionController::TestCase
  include Devise::TestHelpers
  include ActiveJob::TestHelper
  include ApiTest

  setup do
    collection_fixtures('vendors', 'products', 'product_tests', 'users', 'roles', 'bundles', 'measures')
    @vendor = Vendor.find(EHR1)
    @product = @vendor.products.first
    @test = @product.product_tests.first
    @test['_type'] = MeasureTest # each measure test should have a _type of MeasureTest and cms_id
    @test['cms_id'] = 'CMS001'
    @test.save!
  end

  test 'should be able to download zip file of patients' do
    product = Product.create!(vendor: Vendor.first, name: 'Product 1', c1_test: true, bundle_id: '4fdb62e01d41c820f6000001',
                              measure_ids: ['8A4D92B2-35FB-4AA7-0136-5A26000D30BD'])
    product_test = product.product_tests.build({ name: 'mtest', measure_ids: ['8A4D92B2-35FB-4AA7-0136-5A26000D30BD'],
                                                 bundle_id: '4fdb62e01d41c820f6000001' }, MeasureTest)
    product_test.save!
    for_each_logged_in_user([ADMIN, ATL, OWNER, VENDOR]) do
      get :patients, :id => product_test.id, :format => :format_does_not_matter
      assert_response :success, "#{<EMAIL>} should have access. response was #{response.status}"
      assert_not_nil assigns(:product_test)
      assert_equal 'application/zip', response.headers['Content-Type']
    end
  end

  test 'should restrict access to download zip' do
    product_test = Product.first.product_tests.build({ name: 'mtest', measure_ids: ['8A4D92B2-35FB-4AA7-0136-5A26000D30BD'],
                                                       bundle_id: '4fdb62e01d41c820f6000001' }, MeasureTest)
    product_test.save!
    for_each_logged_in_user([OTHER_VENDOR]) do
      get :patients, :id => product_test.id, :format => :format_does_not_matter
      assert_response 401
    end
  end

  test 'should not be able to download zip file of patients if invalid product_test id' do
    for_each_logged_in_user([ADMIN, ATL, OWNER, VENDOR]) do
      get :patients, :format => :format_does_not_matter, :id => 'bad_id'
      assert_response 404, 'response should be Not Found on patients if bad id'
      assert_equal '', response.body
    end
  end
  # need negative tests for user that does not have owner or vendor access

  # # # # # # #
  #   A P I   #
  # # # # # # #

  # json

  test 'should get index with json request' do
    for_each_logged_in_user([ADMIN, ATL, OWNER, VENDOR]) do
      get :index, :format => :json, :product_id => @product.id
      assert_response 200, 'response should be OK on index'
      assert_equal @product.product_tests.count, JSON.parse(response.body).count
    end
  end<commit_msg>potential fix for broken test on travis<commit_after>require 'test_helper'
require 'api_test'

class ProductTestsControllerTest < ActionController::TestCase
  include Devise::TestHelpers
  include ActiveJob::TestHelper
  include ApiTest

  setup do
    collection_fixtures('vendors', 'products', 'product_tests', 'users', 'roles', 'bundles', 'measures')
    @vendor = Vendor.find(EHR1)
    @product = @vendor.products.first
    @test = @product.product_tests.first
    @test['_type'] = MeasureTest # each measure test should have a _type of MeasureTest and cms_id
    @test['cms_id'] = 'CMS001'
    @test.save!
  end

  test 'should be able to download zip file of patients' do
    product = Product.create!(vendor: @vendor, name: 'Product 1', c1_test: true, bundle_id: '4fdb62e01d41c820f6000001',
                              measure_ids: ['8A4D92B2-35FB-4AA7-0136-5A26000D30BD'])
    product_test = product.product_tests.build({ name: 'mtest', measure_ids: ['8A4D92B2-35FB-4AA7-0136-5A26000D30BD'],
                                                 bundle_id: '4fdb62e01d41c820f6000001' }, MeasureTest)
    product_test.save!
    for_each_logged_in_user([ADMIN, ATL, OWNER, VENDOR]) do
      get :patients, :id => product_test.id, :format => :format_does_not_matter
      assert_response :success, "#{@user.email} should have access. response was #{response.status}"
      assert_not_nil assigns(:product_test)
      assert_equal 'application/zip', response.headers['Content-Type']
    end
  end

  test 'should restrict access to download zip' do
    product_test = @product.product_tests.build({ name: 'mtest', measure_ids: ['8A4D92B2-35FB-4AA7-0136-5A26000D30BD'],
                                                  bundle_id: '4fdb62e01d41c820f6000001' }, MeasureTest)
    product_test.save!
    for_each_logged_in_user([OTHER_VENDOR]) do
      get :patients, :id => product_test.id, :format => :format_does_not_matter
      assert_response 401
    end
  end

  test 'should not be able to download zip file of patients if invalid product_test id' do
    for_each_logged_in_user([ADMIN, ATL, OWNER, VENDOR]) do
      get :patients, :format => :format_does_not_matter, :id => 'bad_id'
      assert_response 404, 'response should be Not Found on patients if bad id'
      assert_equal '', response.body
    end
  end
  # need negative tests for user that does not have owner or vendor access

  # # # # # # #
  #   A P I   #
  # # # # # # #

  # json

  test 'should get index with json request' do
    for_each_logged_in_user([ADMIN, ATL, OWNER, VENDOR]) do
      get :index, :format => :json, :product_id => @product.id
      assert_response 200, 'response should be OK on index'
      assert_equal @product.product_tests.count, JSON.parse(response.body).count
    end
  end
{'fraction_non_alphanumeric': 0.10607974021288111, 'fraction_numerical': 0.04654519213422335, 'mean_word_length': 3.3278688524590163, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7551842', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1790, 'n_words': 459}