spec/authy/api_spec.rb in authy-0.0.7 vs spec/authy/api_spec.rb in authy-0.0.8

- old
+ new

@@ -1,46 +1,77 @@ require 'spec_helper' describe "Authy::API" do - it "should find or create a user" do - user = Authy::API.register_user(:email => generate_email, :cellphone => generate_cellphone, :country_code => 1) - user.should be_kind_of(Authy::Response) + describe "Registering users" do - user.should be_kind_of(Authy::User) - user.should_not be_nil - user.id.should_not be_nil - user.id.should be_kind_of(Integer) - end + it "should find or create a user" do + user = Authy::API.register_user(:email => generate_email, :cellphone => generate_cellphone, :country_code => 1) + user.should be_kind_of(Authy::Response) - it "should validate a given token" do - user = Authy::API.register_user(:email => generate_email, :cellphone => generate_cellphone, :country_code => 1) - response = Authy::API.verify(:token => 'invalid_token', :id => user['id']) + user.should be_kind_of(Authy::User) + user.should_not be_nil + user.id.should_not be_nil + user.id.should be_kind_of(Integer) + end + + it "should return the error messages as a hash" do + user = Authy::API.register_user(:email => generate_email, :cellphone => "abc-1234", :country_code => 1) - response.should be_kind_of(Authy::Response) - response.ok?.should be_true - response['token'].should == 'is valid' - end + user.errors.should be_kind_of(Hash) + user.errors['cellphone'].should == 'must be a valid cellphone number.' + end - it "should fail to validate a given token when force=true is given" do - user = Authy::API.register_user(:email => generate_email, :cellphone => generate_cellphone, :country_code => 1) - response = Authy::API.verify(:token => 'invalid_token', :id => user['id'], :force => true) + it "should allow to override the API key" do + user = Authy::API.register_user(:email => generate_email, :cellphone => generate_cellphone, :country_code => 1, :api_key => "invalid_api_key") - response.should be_kind_of(Authy::Response) - response.ok?.should be_false - response.errors['token'].should == 'is invalid' + user.should_not be_ok + user.errors['message'].should =~ /invalid api key/i + end end - it "should return the error messages as a hash" do - user = Authy::API.register_user(:email => generate_email, :cellphone => "abc-1234", :country_code => 1) + describe "verificating tokens" do + before do + @user = Authy::API.register_user(:email => generate_email, :cellphone => generate_cellphone, :country_code => 1) + @user.should be_ok + end - user.errors.should be_kind_of(Hash) - user.errors['cellphone'].should == 'must be a valid cellphone number.' + it "should validate a given token if the user is not registered when the verification is not forced" do + response = Authy::API.verify(:token => 'invalid_token', :id => @user['id']) + + response.should be_kind_of(Authy::Response) + response.ok?.should be_true + end + + it "should fail to validate a given token if the user is not registered when force=true is given" do + response = Authy::API.verify(:token => 'invalid_token', :id => @user['id'], :force => true) + + response.should be_kind_of(Authy::Response) + response.ok?.should be_false + response.errors['token'].should == 'is invalid' + end + + it "should allow to override the API key" do + response = Authy::API.verify(:token => 'invalid_token', :id => @user['id'], :force => true, :api_key => "invalid_api_key") + + response.should_not be_ok + response.errors['message'].should =~ /invalid api key/i + end end - it "should request a SMS token" do - user = Authy::API.register_user(:email => generate_email, :cellphone => generate_cellphone, :country_code => 1) - user.should be_ok + describe "Requesting SMS" do + before do + @user = Authy::API.register_user(:email => generate_email, :cellphone => generate_cellphone, :country_code => 1) + @user.should be_ok + end - response = Authy::API.request_sms(:id => user.id) - response.should be_ok + it "should request a SMS token" do + response = Authy::API.request_sms(:id => @user.id) + response.should be_ok + end + + it "should allow to override the API key" do + response = Authy::API.request_sms(:id => @user.id, :api_key => "invalid_api_key") + response.should_not be_ok + response.errors['message'].should =~ /invalid api key/i + end end end