<commit_before>require "#{File.dirname(__FILE__)}/../test_helper"

class PrivacyTest < ActionController::IntegrationTest
  fixtures :people, :families
  
  def test_help_for_parents_with_hidden_children
    sign_in_as people(:jeremy)
    assert_select '#sidebar', /Where are my kids\?/
    assert_select '#sidebar a[href=?]', /\/help\/safeguarding_children/
    assert_select '#sidebar tr.family-member', 2 # not 3 (should not see child)
  end
  
  def test_children_without_consent_hidden_on_family_profiles
    sign_in_as people(:peter)
    get "/people/#{people(:tim).id}" # view Tim's profile
    assert_response :success
    assert_template 'people/show'
    assert_select '#sidebar tr.family-member', 2 # not 3 (should not see child)
  end
  
  def test_children_without_consent_profiles_hidden
    sign_in_as people(:peter)
    get "/people/#{people(:mac).id}" # view Mac's profile (child)
    assert_response :missing
  end
  
  def test_children_without_consent_hidden_in_search_results
    sign_in_as people(:peter)
    get "/directory/search", :name => 'Mac'
    assert_template 'directory/search'
    assert_select 'body', /Your search didn't match any people\./
  end
  
  def test_children_cannot_sign_in_without_consent
    post_sign_in_form people(:mac).email
    assert_redirected_to '/help/unauthorized'
    people(:mac).update_attribute :parental_consent, 'consent statement goes here' # not nil means this child has consent
    sign_in_as people(:mac)  # sign_in_as() will do assertions
  end
  
  def test_parent_give_parental_consent
    sign_in_as people(:jeremy)
    get '/'
    assert_response :redirect
    follow_redirect!
    assert_template 'people/show'
    assert_select '#sidebar tr.family-member', 2 # not 3 (should not see child)
    get "people/#{people(:jeremy).id}/privacy"
    assert_response :success
    assert_template 'privacies/show'
    assert_select 'p.alert', :minimum => 1, :text => /you have not given consent/
    assert_select 'li', :minimum => 1, :text => /Privacy Policy/
    assert_select 'input[type=submit][value=I Agree]', 1
    put "/people/#{people(:megan).id}/privacy", :agree => 'I Agree.'
    assert_response :redirect
    follow_redirect!
    assert_template 'privacies/show'
    assert_select 'div#notice', /Agreement saved\./
    people(:megan).reload
    assert people(:megan).parental_consent # not nil
    assert people(:megan).parental_consent.include?("#{people(:jeremy).name} \(#{people(:jeremy).id}\)")
    assert_select 'p.highlight', :minimum => 1, :text => /This child's profile has parental consent/
    get "/people/#{people(:megan).id}"
    assert_response :success<commit_msg>Updated test to match movement in code.<commit_after>require "#{File.dirname(__FILE__)}/../test_helper"

class PrivacyTest < ActionController::IntegrationTest
  fixtures :people, :families
  
  def test_help_for_parents_with_hidden_children
    sign_in_as people(:jeremy)
    assert_select '#sidebar', /Where are my kids\?/
    assert_select '#sidebar a[href=?]', /\/help\/safeguarding_children/
    assert_select '#sidebar tr.family-member', 2 # not 3 (should not see child)
  end
  
  def test_children_without_consent_hidden_on_family_profiles
    sign_in_as people(:peter)
    get "/people/#{people(:tim).id}" # view Tim's profile
    assert_response :success
    assert_template 'people/show'
    assert_select '#sidebar tr.family-member', 2 # not 3 (should not see child)
  end
  
  def test_children_without_consent_profiles_hidden
    sign_in_as people(:peter)
    get "/people/#{people(:mac).id}" # view Mac's profile (child)
    assert_response :missing
  end
  
  def test_children_without_consent_hidden_in_search_results
    sign_in_as people(:peter)
    get "/directory/search", :name => 'Mac'
    assert_template 'directory/search'
    assert_select 'body', /Your search didn't match any people\./
  end
  
  def test_children_cannot_sign_in_without_consent
    post_sign_in_form people(:mac).email
    assert_redirected_to '/help/unauthorized'
    people(:mac).update_attribute :parental_consent, 'consent statement goes here' # not nil means this child has consent
    sign_in_as people(:mac)  # sign_in_as() will do assertions
  end
  
  def test_parent_give_parental_consent
    sign_in_as people(:jeremy)
    get '/'
    assert_response :redirect
    follow_redirect!
    assert_template 'people/show'
    assert_select '#sidebar tr.family-member', 2 # not 3 (should not see child)
    get "people/#{people(:jeremy).id}/privacy/edit"
    assert_response :success
    assert_template 'privacies/edit'
    assert_select 'p.alert', :minimum => 1, :text => /you have not given consent/
    assert_select 'li', :minimum => 1, :text => /Privacy Policy/
    assert_select 'input[type=submit][value=I Agree]', 1
    put "/people/#{people(:megan).id}/privacy", :agree => 'I Agree.'
    assert_response :redirect
    follow_redirect!
    assert_template 'privacies/edit'
    assert_select 'div#notice', /Agreement saved\./
    people(:megan).reload
    assert people(:megan).parental_consent # not nil
    assert people(:megan).parental_consent.include?("#{people(:jeremy).name} \(#{people(:jeremy).id}\)")
    assert_select 'p.highlight', :minimum => 1, :text => /This child's profile has parental consent/
    get "/people/#{people(:megan).id}"
    assert_response :success
{'fraction_non_alphanumeric': 0.1179996253980146, 'fraction_numerical': 0.0037460198539052258, 'mean_word_length': 4.240431795878312, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6591584', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1682, 'n_words': 403}