require "rails_helper"

RSpec.describe Teachable::Api::V1::DataErasureController do

  context "#delete" do
    let(:email) { "mike+#{SecureRandom.hex}@teachable.com" }
    let(:webinar) { create(:webinar, url: "test", time: Time.now + 2.days, basic_replay_bundle: nil, pro_replay_bundle: nil, basic_play_bundle: nil, pro_play_bundle: nil) }
    let(:registrant) { create(:registrant, email: email) }
    let!(:registration) { create(:registration, webinar: webinar, registrant: registrant) }
    it "removes all of a user's PII data" do

      delete :destroy, params: {email: email, api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"]}

      expect(JSON.parse(response.body)).to eq({ "status" => "PII has been removed for User" })
      expect{ registrant.reload }.to raise_error ActiveRecord::RecordNotFound
      expect{ registration.reload }.to raise_error ActiveRecord::RecordNotFound
    end

    it "returns no_content if the email does not have any PII in the database" do
      delete :destroy, params: {email: "foobar@example.com", api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"]}
      expect(response.status).to eq 204
    end

    it "returns 'Missing email' message if email missing" do
      delete :destroy, params: {api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"]}
      expect(JSON.parse(response.body)).to eq({"error" => "Missing email param"})
    end
  end

end
