<file_sep>require 'omniauth/strategies/plato'

describe OmniAuth::Strategies::Plato do
  subject { OmniAuth::Strategies::Plato.new({}) }

  describe "client options" do
    it "has correct site" do
      subject.options.client_options.site.should eq("http://subscriptions.teachtci.com")
    end

    it "has correct authorize url" do
      subject.options.client_options.authorize_url.should eq("http://subscriptions.teachtci.com/oauth/authorize")
    end
  end

  describe "name" do
    context "teacher" do
      it "should return first and last name from raw_info if available" do
        subject.stub!(:raw_info).and_return({ "user" => { "teacher" => {"first_name" => "John", "last_name" => "Kelly" }}})
        subject.info.should == {:name => "<NAME>", :user_type => "teacher", :customer_number => nil}
      end
    end

    context "coordinator" do
      it "should return first and last name from raw_info if available" do
        subject.stub!(:raw_info).and_return({ "user" => { "coordinator" => {"first_name" => "John", "last_name" => "Kelly" }}})
        subject.info.should == {:name => "<NAME>", :user_type => "coordinator", :customer_number => nil}
      end
    end

    context "admin" do
      it "should return first and last name from raw_info if available" do
        subject.stub!(:raw_info).and_return({ "user" => { "admin" => {"first_name" => "John", "last_name" => "Kelly" }}})
        subject.info.should == {:name => "<NAME>", :user_type => "admin", :customer_number => nil}
      end
    end
  end

  describe "uid" do
    context "teacher" do
      it "should return uid from raw_info if available" do
       subject.stub!(:raw_info).and_return({ "user" => { "teacher" => {"id" => "9" }}})
       subject.uid.should == "9"
     end
    end

    context "coordinator" do
      it "should return uid from raw_info if available" do
       subject.stub!(:raw_info).and_return({ "user" => { "coordinator" => {"id" => "9"}}})
       subject.uid.should == "9"
     end
    end

    context "admin" do
      it "should return uid from raw_info if available" do
       subject.stub!(:raw_info).and_return({ "user" => { "admin" => {"id" => "9" }}})
       subject.uid.should == "9"
     end
    end
  end

  describe "customer_number" do
    it "should return the customer number from the raw_info" do
      subject.stub!(:raw_info).and_return({ "user" => { "teacher" => { "id" => "9" }}, "customer_number" => "QWERTY12345" })
      subject.info[:customer_number].should == "QWERTY12345"
    end
  end
end<file_sep>require 'omniauth-oauth2'

module OmniAuth
  module Strategies
    class Plato < OmniAuth::Strategies::OAuth2
      option :name, :plato

      option :client_options, {
          :site => "http://subscriptions.teachtci.com",
          :authorize_url => "http://subscriptions.teachtci.com/oauth/authorize"
      }

      uid { raw_info["user"][staffer_type]["id"] }

      info do
        {
            :name => "#{staffer["first_name"]} #{staffer["last_name"]}",
            :user_type => staffer_type,
            :customer_number => raw_info["customer_number"]
        }
      end

      def staffer
        raw_info["user"][staffer_type]
      end

      def raw_info
        @raw_info ||= access_token.get('/api/v1/user.json').parsed
      end

      def staffer_type
        raw_info["user"].keys.detect{|key| staffer_types.include?(key) }
      end

      private

      def staffer_types
        %w[teacher coordinator admin sysadmin]
      end
    end
  end
end