require "rails_helper"

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

  context "#index" do
    let(:registrant_email) { "donald@trump.com"}
    let(:upgrade_src) { "maga"}
    it "returns an upgrade src based on registered users email" do

      registrant   = create(:registrant, email: registrant_email)
      webinar      = create(:webinar, url: upgrade_src)
      registration = create(:registration, registrant_id: registrant.id, webinar_id: webinar.id)
      get :index, params: {email: registrant_email, api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"]}

      expect(JSON.parse(response.body)).to eq({"upgrade_src" => "#{upgrade_src}-play"})
    end

    it "returns 'Missing email param' message if email missing" do
      get :index, params: {api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"]}

      expect(JSON.parse(response.body)).to eq({"error" => "Missing email param"})
    end

    it "returns proper error message if registrant not found with email" do
      get :index, params: {email: registrant_email, api_key: ENV["TEACHABLE_CROSS_PLATFORM_API_KEY"]}

      expect(JSON.parse(response.body)).to eq({"error" => "Unable to find registrant with email: #{registrant_email}"})
    end
  end

end
