require File.dirname(__FILE__) + '/spec_helper.rb' describe Enigma do describe "when generating an API key" do it "should be a string" do Enigma.generate_key.should be_a(String) end it "should be 32 digits long" do Enigma.generate_key.length.should == 32 end end describe "when generating an API secret" do it "should be a string" do Enigma.generate_secret.should be_a(String) end it "should be 64 digits long" do Enigma.generate_secret.length.should == 32 end end describe "when authenticating" do before :each do @key = Enigma.generate_key # Sigh, has to be global to be seen below... @secret = $secret = Enigma.generate_secret @params = { :p1 => 1, :p2 => 2 } @signed_params = Enigma.encode_params(@secret, @params) end context "with a valid key/secret pair" do before :each do # Let's just return the secret, straight up. Enigma.find_secret do |key| $secret end end it "should return the decoded parameters" do Enigma.authenticate(:key => @key, :params => @signed_params).should == @params end end context "with an invalid key/secret pair" do before :each do # And now, let's return the wrong one. Enigma.find_secret do |key| Enigma.generate_secret end end it "should not be able to decode the params using the invalid secret" do Enigma.decode_params(Enigma.generate_secret, @signed_params).should be_nil end it "should return nil" do Enigma.authenticate(:key => @key, :params => @signed_params).should be_nil end end end describe "when encoding parameters" do before :each do @secret = Enigma.generate_secret @params = { 'param1' => '1', 'param2' => 2 } end it "should be able to decode them" do Enigma.decode_params(@secret, Enigma.encode_params(@secret, @params)).should == @params end end end describe Enigma::Client do describe "get" do before :each do @key = Enigma.generate_key @secret = Enigma.generate_secret @client = Enigma::Client.new(@key, @secret) @params = { 'p1' => 'value' } @signed_params = Enigma.encode_params(@secret, @params) end it "should send a GET request with the key and signed params" do RestClient.should_receive(:get).with("http://some.service?params=#{@signed_params}&key=#{@key}") @client.get("http://some.service", @params) end end end =begin get '/' do user = User.find_by_key(params[:key]) Enigma.authenticate(params[:key], user.secret, params[:params]) end Enigma.generate_key Enigma.generate_secret =end