<commit_before>require 'test_helper'

class CertificatesControllerTest < ActionController::TestCase
  include Devise::TestHelpers

  test "published certificates can be shown" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id}
    assert_response :success
  end

  test "unpublished certificates can't be shown" do
    cert = FactoryGirl.create(:certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id}
    assert_response 404
  end

  test "unpublished certificates can be shown to their creator" do
    user = FactoryGirl.create(:user)
    sign_in user

    cert = FactoryGirl.create(:certificate_with_dataset)
    cert.response_set.assign_to_user! user

    get :show, {dataset_id: cert.dataset.id, id: cert.id}

    assert_response :success
  end

  test "Requesting a JSON version of a certificate returns JSON" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    cert.attained_level = "basic"
    cert.save
    get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}
    assert_response :success
    assert_equal "application/json", response.content_type
  end

  test "Requesting legacy url performs a redirect" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    cert.attained_level = "basic"
    cert.save
    get :legacy_show, {id: cert.id}
    assert_redirected_to dataset_certificate_url dataset_id: cert.dataset.id, id: cert.id
  end

  test "Requesting legacy url for badge performs a redirect" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    cert.attained_level = "basic"
    cert.save
    get :legacy_show, {id: cert.id, type: "badge", format: "png"}

    assert_redirected_to "http://test.host/datasets/1/certificates/1/badge.png"
  end

  test "Requesting a JSON version of a certificate returns the correct level" do
    levels = {
        "basic" => "raw",
        "pilot" => "pilot",
        "standard" => "standard",
        "exemplar" => "expert"
      }

    levels.each do |level, actual|
      cert = FactoryGirl.create(:"published_#{level}_certificate_with_dataset")
      get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}

      json = JSON.parse(response.body)

      assert_equal actual, json["certificate"]["level"]
    end
  end

  test "Requesting a JSON version of a certificate returns the correct juristiction and status" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}

    json = JSON.parse(response.body)

    assert_equal "Simple survey", json["certificate"]["jurisdiction"]
    assert_equal "alpha", json["certificate"]["status"]
  end

  test "Requesting a JSON version of a certificate returns the correct badge urls" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}

    json = JSON.parse(response.body)

    assert_equal "http://test.host/datasets/1/certificates/1/badge.js", json["certificate"]["badges"]["application/javascript"]
    assert_equal "http://test.host/datasets/1/certificates/1/badge.html", json["certificate"]["badges"]["text/html"]
    assert_equal "http://test.host/datasets/1/certificates/1/badge.png", json["certificate"]["badges"]["image/png"]
  end

  test "Requesting a JSON version of a certificate in production returns https urls" do
    Rails.env.stubs :production? => true
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}

    json = JSON.parse(response.body)

    assert_match /https:\/\//, json["certificate"]["uri"]
    assert_match /https:\/\//, json["certificate"]["badges"]["application/javascript"]
    assert_match /https:\/\//, json["certificate"]["badges"]["text/html"]
    assert_match /https:\/\//, json["certificate"]["badges"]["image/png"]
    assert_match /https:\/\//, json["certificate"]["dataset"]["uri"]
  end

  test "mark certificate as valid" do
    cert = FactoryGirl.create(:certificate_with_dataset)
    user = FactoryGirl.create(:user)
    sign_in user

    assert_difference ->{cert.verifications.count}, 1 do
      post :verify, {dataset_id: cert.dataset.id, id: cert.id}
    end
  end

  test "sign in to mark certificate as valid" do
    cert = FactoryGirl.create(:certificate_with_dataset)
    user = FactoryGirl.create(:user)

    assert_no_difference ->{cert.verifications.count} do
      post :verify, {dataset_id: cert.dataset.id, id: cert.id}
    end

    assert_redirected_to dataset_certificate_url dataset_id: cert.dataset.id, id: cert.id

  end

  test "undo validation" do
    cv = FactoryGirl.create :verification
    cert = cv.certificate
    sign_in cv.user

    assert_difference ->{cert.verifications.count}, -1 do
      post :verify, {dataset_id: cert.dataset.id, id: cert.id, undo: true}
    end
  end

  test "Non-admin tries to mark a certificate as audited" do
    cert = FactoryGirl.create(:certificate_with_dataset)
    user = FactoryGirl.create(:user)<commit_msg>Create certificate with dataset for verification<commit_after>require 'test_helper'

class CertificatesControllerTest < ActionController::TestCase
  include Devise::TestHelpers

  test "published certificates can be shown" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id}
    assert_response :success
  end

  test "unpublished certificates can't be shown" do
    cert = FactoryGirl.create(:certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id}
    assert_response 404
  end

  test "unpublished certificates can be shown to their creator" do
    user = FactoryGirl.create(:user)
    sign_in user

    cert = FactoryGirl.create(:certificate_with_dataset)
    cert.response_set.assign_to_user! user

    get :show, {dataset_id: cert.dataset.id, id: cert.id}

    assert_response :success
  end

  test "Requesting a JSON version of a certificate returns JSON" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    cert.attained_level = "basic"
    cert.save
    get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}
    assert_response :success
    assert_equal "application/json", response.content_type
  end

  test "Requesting legacy url performs a redirect" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    cert.attained_level = "basic"
    cert.save
    get :legacy_show, {id: cert.id}
    assert_redirected_to dataset_certificate_url dataset_id: cert.dataset.id, id: cert.id
  end

  test "Requesting legacy url for badge performs a redirect" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    cert.attained_level = "basic"
    cert.save
    get :legacy_show, {id: cert.id, type: "badge", format: "png"}

    assert_redirected_to "http://test.host/datasets/1/certificates/1/badge.png"
  end

  test "Requesting a JSON version of a certificate returns the correct level" do
    levels = {
        "basic" => "raw",
        "pilot" => "pilot",
        "standard" => "standard",
        "exemplar" => "expert"
      }

    levels.each do |level, actual|
      cert = FactoryGirl.create(:"published_#{level}_certificate_with_dataset")
      get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}

      json = JSON.parse(response.body)

      assert_equal actual, json["certificate"]["level"]
    end
  end

  test "Requesting a JSON version of a certificate returns the correct juristiction and status" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}

    json = JSON.parse(response.body)

    assert_equal "Simple survey", json["certificate"]["jurisdiction"]
    assert_equal "alpha", json["certificate"]["status"]
  end

  test "Requesting a JSON version of a certificate returns the correct badge urls" do
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}

    json = JSON.parse(response.body)

    assert_equal "http://test.host/datasets/1/certificates/1/badge.js", json["certificate"]["badges"]["application/javascript"]
    assert_equal "http://test.host/datasets/1/certificates/1/badge.html", json["certificate"]["badges"]["text/html"]
    assert_equal "http://test.host/datasets/1/certificates/1/badge.png", json["certificate"]["badges"]["image/png"]
  end

  test "Requesting a JSON version of a certificate in production returns https urls" do
    Rails.env.stubs :production? => true
    cert = FactoryGirl.create(:published_certificate_with_dataset)
    get :show, {dataset_id: cert.dataset.id, id: cert.id, format: "json"}

    json = JSON.parse(response.body)

    assert_match /https:\/\//, json["certificate"]["uri"]
    assert_match /https:\/\//, json["certificate"]["badges"]["application/javascript"]
    assert_match /https:\/\//, json["certificate"]["badges"]["text/html"]
    assert_match /https:\/\//, json["certificate"]["badges"]["image/png"]
    assert_match /https:\/\//, json["certificate"]["dataset"]["uri"]
  end

  test "mark certificate as valid" do
    cert = FactoryGirl.create(:certificate_with_dataset)
    user = FactoryGirl.create(:user)
    sign_in user

    assert_difference ->{cert.verifications.count}, 1 do
      post :verify, {dataset_id: cert.dataset.id, id: cert.id}
    end
  end

  test "sign in to mark certificate as valid" do
    cert = FactoryGirl.create(:certificate_with_dataset)
    user = FactoryGirl.create(:user)

    assert_no_difference ->{cert.verifications.count} do
      post :verify, {dataset_id: cert.dataset.id, id: cert.id}
    end

    assert_redirected_to dataset_certificate_url dataset_id: cert.dataset.id, id: cert.id

  end

  test "undo validation" do
    cv = FactoryGirl.create(:verification, certificate: FactoryGirl.create(:certificate_with_dataset))
    cert = cv.certificate
    sign_in cv.user

    binding.pry

    assert_difference ->{cert.verifications.count}, -1 do
      post :verify, {dataset_id: cert.dataset.id, id: cert.id, undo: true}
    end
  end

  test "Non-admin tries to mark a certificate as audited" do
    cert = FactoryGirl.create(:certificate_with_dataset)
    user = FactoryGirl.create(:user)
{'fraction_non_alphanumeric': 0.1205727923627685, 'fraction_numerical': 0.0024821002386634845, 'mean_word_length': 3.9909480705097664, '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': '6712401', 'n_tokens_mistral': 3305, 'n_tokens_neox': 3241, 'n_words': 810}