Permalink
Browse files

added password parser

  • Loading branch information...
1 parent d05aab6 commit b1a724fe420d8abbe29232eb2094f791a4059141 @peppyheppy committed Jan 25, 2012
Showing with 85 additions and 14 deletions.
  1. +26 −10 lib/janrain/capture/client/entity.rb
  2. +14 −3 lib/janrain/capture/user.rb
  3. +45 −1 spec/lib/janrain/capture/user_spec.rb
@@ -9,18 +9,20 @@ class Entity
# https://janraincapture.com/docs/api_entity.html
def self.by_token(token, options={})
- get("/entity", headers: { 'Authorization' => "OAuth #{token}" })
+ logres(get("/entity", headers: { 'Authorization' => "OAuth #{token}" }))
end
# https://janraincapture.com/docs/api_entity.html
def self.by_id(id, overrides={})
attrs = defaults.merge(overrides)
- get("/entity", query: attrs.merge(id: id))
+ logreq(attrs.merge(id: id))
+ logres(get("/entity", query: attrs.merge(id: id)))
end
# https://janraincapture.com/docs/api_entity.count.html
def self.count(overrides={})
- get("/entity.count", query: defaults.merge(overrides))
+ logreq(defaults.merge(overrides))
+ logres(get("/entity.count", query: defaults.merge(overrides)))
end
# https://janraincapture.com/docs/api_entity.create.html
@@ -30,23 +32,25 @@ def self.create(attrs={})
if type_name = attrs.delete(:type_name)
params = defaults.merge(type_name: type_name)
end
- post("/entity.create", query: params.merge(attributes: attrs.to_json))
+ logreq(params.merge(attributes: attrs.to_json))
+ logres(post("/entity.create", query: params.merge(attributes: attrs.to_json)))
end
# https://janraincapture.com/docs/api_entity.bulkCreate.html
# returns an array of ids and/or errors
def self.bulk_create(new_entities=[], overrides={})
- post("/entity.bulkCreate", query: defaults.
- merge(overrides).
- merge(all_attributes: new_entities.to_json))
+ query = defaults.merge(overrides).merge(all_attributes: new_entities.to_json)
+ logreq(query)
+ logres(post("/entity.bulkCreate", query: query))
end
# https://janraincapture.com/docs/api_entity.delete.html
# XXX: does not support plural deletion, nor created/lastUpdated date
# checks. Its a sharp delete -- don't hurt yourself!
def self.delete(id, overrides={})
attrs = defaults.merge(overrides)
- post("/entity.delete", query: attrs.merge(id: id))
+ logreq(attrs.merge(id: id))
+ logres(post("/entity.delete", query: attrs.merge(id: id)))
end
# https://janraincapture.com/docs/api_entity.update.html
@@ -56,7 +60,9 @@ def self.update(id, attrs={})
if type_name = attrs.delete(:type_name)
params = defaults.merge(type_name: type_name)
end
- post("/entity.update", query: params.merge(id: id, attributes: attrs.to_json))
+ query = params.merge(id: id, attributes: attrs.to_json)
+ logreq(query)
+ logres(post("/entity.update", query: query))
end
# XXX: not supporting replace.... don't have a real case for it yet
@@ -65,11 +71,21 @@ def self.update(id, attrs={})
# https://janraincapture.com/docs/api_entity.find.html
def self.find(filter, options={})
params = defaults.merge(filter: filter).merge(options)
- get('/entity.find', query: params)
+ logreq(params)
+ logres(get('/entity.find', query: params))
end
private
+ def self.logreq(attrs)
+ Rails.logger.info("Janrain::Entity Request: (#{caller.first.split(':in ').last}) #{attrs.inspect}")
+ end
+
+ def self.logres(response)
+ Rails.logger.info("Janrain::Entity Response: (#{caller.first.split(':in ').last}) #{response.body}")
+ response
+ end
+
def self.defaults
{
type_name: Config.capture.entity['schema_type_name'],
@@ -20,6 +20,19 @@ def authenticate(code, options={})
end
false
end
+
+ def password_parser(stuff)
+ parsed_password = {}
+ begin
+ password = JSON.load(stuff) rescue YAML.load(stuff) rescue {}
+ if password.is_a? Hash
+ parsed_password = password
+ end
+ rescue => e
+ parsed_password = {}
+ end
+ parsed_password
+ end
end
def to_capture(only_changes=false)
@@ -37,9 +50,7 @@ def to_capture(only_changes=false)
all
end
- if capture_id.blank?
- attrs.delete_if { |a,v| a == 'id' || a == 'capture_id'}
- end
+ attrs.delete_if { |a,v| a == 'id' || a == 'capture_id'}
attrs
end
@@ -25,6 +25,50 @@
}
end
+ context "password parser" do
+ it "should return an empty hash if password is null" do
+ pass = TestUser.password_parser(nil)
+ pass.should be_a Hash
+ pass.should be_blank
+ end
+
+ it "should return an empty hash if password is empty string" do
+ pass = TestUser.password_parser('')
+ pass.should be_a Hash
+ pass.should be_blank
+ end
+
+ it "should return an empty hash if password is non json/yaml string" do
+ pass = TestUser.password_parser('hello hello')
+ pass.should be_a Hash
+ pass.should be_blank
+ end
+
+ it "should return an empty hash if password is not valid json/yaml" do
+ pass = TestUser.password_parser('{ :dddd => :qqqq }')
+
+ puts "YYYYY: #{pass.inspect}"
+
+ pass.should be_a Hash
+ pass.should be_blank
+ end
+
+ it "should parse valid json" do
+ pass = TestUser.password_parser('{ "a": "b"}')
+ pass.should be_a Hash
+ pass.should_not be_blank
+ pass['a'].should == 'b'
+ end
+
+ it "should parse valid yaml" do
+ pass = TestUser.password_parser("--- \nvalue: p8ssw0rd\ntype: password-rr\nsalt: lee\n")
+ pass.should be_a Hash
+ pass.should_not be_blank
+ pass['value'].should == 'p8ssw0rd'
+ pass['type'].should == 'password-rr'
+ end
+ end
+
context "capture persistence" do
context "new capture user" do
@@ -108,7 +152,7 @@
@attrs.should be_a Hash
@attrs.should have_key 'email'
@attrs['email'].should == @user_params[:email]
- @attrs.should have_key 'id'
+ # @attrs.should have_key 'id'
end
it "should exclude locally cached values" do

0 comments on commit b1a724f

Please sign in to comment.