Updates from May, 2015 Toggle Comment Threads | Keyboard Shortcuts

  • ThomasPowell 7:06 pm on May 22, 2015 Permalink
    Tags: cucumber, ladle, , ,   

    Hooking in an LDAP Test Server to Cucumber Tests 

    I managed to get a custom Devise strategy with LDAP working, but had no clear way of automating tests. I wanted to validate if I still had to keep the password fresh in the database, and needed to be able to write scenarios around that in case someone attempted to refactor out the code.

    After trying to incorporate the spec strategy used in the development devise_ldap_authenticatable and failing, I found a ruby wrapper of ApacheDS called ladle that looked like it would serve my purposes.

    I included in gem in my test group in my Gemfile:

      gem 'ladle'
    

    At the top of my features/env.rb file for configuring cucumber, I turned off admin binding (wanted the connection as simple as possible):

    ::Devise.ldap_use_admin_to_bind = false
    

    I then created an @ldap tag for my LDAP-dependent features than would start and stop the LDAP server in those instances. (Again, in my features/env.rb… probably need to clean that up.)

    Around('@ldap') do |scenario, block|
      $ladle ||= Ladle::Server.new(
        :ldif => "spec/ldap/test_users.ldif",
        :domain => "dc=example,dc=org",
        :quiet => true
      )
      $ladle.start
      block.call
      $ladle.stop
    end
    

    I then created an the spec/ldap/test_users.ldif (from following the example in the ladle project).

    version: 1
    
    dn: ou=people,dc=example,dc=org
    objectClass: top
    objectClass: organizationalUnit
    ou: people
    
    dn: uid=eadmin,ou=people,dc=example,dc=org
    objectClass: top
    objectClass: person
    objectClass: organizationalPerson
    objectClass: inetOrgPerson
    cn: Example Admin
    sn: Admin
    givenName: Example
    mail: [email protected]
    uid: eadmin
    # Password is "[email protected]!"
    userpassword: {SHA}Aedq5WHQSxglvJSfpX0kgdGRdHk=
    

    I generated the password with:

      slappasswd -h {SHA} -s [email protected]!
    

    One stupid mistake that I did in the process was kicking off two Ladle servers (with slightly different parameters). In one iteration, I couldn’t bind to the user. Another, the server using the test file failed to start. Be aware that Ladle will run happily with default parameters, but that they won’t be much use to you.

    If you want to test your configuration file:

    require 'net/ldap'
    require 'ladle'
    
    $ladle ||= Ladle::Server.new(
      :ldif => "spec/ldap/test_users.ldif",
      :domain => "dc=example,dc=org",
      :quiet => true
    )
    $ladle.start
    
    ldap = Net::LDAP.new(host: 'localhost',
        :port => 3897,
    )
    filter = Net::LDAP::Filter.eq('mail', '[email protected]') # or ('uid', 'eadmin') 
    
    ldap.search(:base => 'ou=people,dc=example,dc=org', :filter => filter) do |entry|
      ldap.auth(entry.dn, '[email protected]!') # or whatever your password is
    
      entry.each do |attribute, values|
        puts "   #{attribute}:"
        values.each do |value|
          puts "      --->#{value}"
        end
      end
    end
    
     
  • ThomasPowell 1:59 pm on February 27, 2015 Permalink
    Tags: , extensions,   

    devise_security_extension undefined method authenticate for nil:NilClass on Rspec Tests for Controller That Skips Authentication 

    After installing the Devise Security Extension to add password expiration to my Devise models, I started getting the following error on an RSpec test for a controller that does not perform authentication of the user:

         Failure/Error: get :index
         NoMethodError:
           undefined method `authenticate?' for nil:NilClass
    

    After a bit of digging, I found that the helpers.rb in the gem includes an additional before_filter that needs to be skipped:

    module DeviseSecurityExtension
      module Controllers
        module Helpers
          extend ActiveSupport::Concern
    
          included do
            before_filter :handle_password_change
          end
    

    So while I’m skipping authenticate_user! in my controller, I still needed an additional:

      skip_before_filter :handle_password_change
    

    Interestingly enough, the controller itself doesn’t break, just the tests. The downside is that I’m referencing two different Devise filters/actions just to not use them.

     
  • ThomasPowell 5:42 pm on January 22, 2015 Permalink
    Tags:   

    Ruby Keyword Argument Implementation Differences Between 2.1 and 2.2 

    I’m currently reading through Ruby Under a Microscope: An Illustrated Guide to Ruby Internals, and when I tried to test the “hidden hash” in ruby 2.2, I got different results from the book.

    In Ruby 2.2, it appears that the default arguments are used as one expects, without regard to the Hash monkey patch:

    ### CODE ###
    class Hash
      def key?(val)
        false
      end
    end
    
    def blah(a: 2, b: 5)
      puts a + b
    end
    blah
    blah(a:3)
    blah(b:3)
    blah(a:2000, b:2000)
    
    ### DISASSEMBLY ###
    == disasm: @>==========
    0000 trace            1                                               (   1)
    0002 putspecialobject 3
    0004 putnil
    0005 defineclass      :Hash, , 0
    0009 pop
    0010 trace            1                                               (   7)
    0012 putspecialobject 1
    0014 putspecialobject 2
    0016 putobject        :blah
    0018 putiseq          blah
    0020 opt_send_without_block 
    0022 pop
    0023 trace            1                                               (  10)
    0025 putself
    0026 opt_send_without_block 
    0028 pop
    0029 trace            1                                               (  11)
    0031 putself
    0032 putobject        3
    0034 opt_send_without_block 
    0036 pop
    0037 trace            1                                               (  12)
    0039 putself
    0040 putobject        3
    0042 opt_send_without_block 
    0044 pop
    0045 trace            1                                               (  13)
    0047 putself
    0048 putobject        2000
    0050 putobject        2000
    0052 opt_send_without_block 
    0054 leave
    == disasm: @>========
    0000 trace            2                                               (   1)
    0002 trace            1                                               (   2)
    0004 putspecialobject 1
    0006 putspecialobject 2
    0008 putobject        :key?
    0010 putiseq          key?
    0012 opt_send_without_block 
    0014 trace            4                                               (   5)
    0016 leave                                                            (   2)
    == disasm: >================
    local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, kw: [email protected], kwrest: -1])
    [ 2] val
    0000 trace            8                                               (   2)
    0002 trace            1                                               (   3)
    0004 putobject        false
    0006 trace            16                                              (   4)
    0008 leave                                                            (   3)
    == disasm: >================
    local table (size: 4, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: [email protected], kwrest: -1])
    [ 4] a          [ 3] b          [ 2] ?
    0000 trace            8                                               (   7)
    0002 trace            1                                               (   8)
    0004 putself
    0005 getlocal_OP__WC__0 4
    0007 getlocal_OP__WC__0 3
    0009 opt_plus         
    0011 opt_send_without_block 
    0013 trace            16                                              (   9)
    0015 leave                                                            (   8)
    
    ### RESULTS ###
    7
    8
    5
    4000
    

    If I reset back to ruby 2.1, I get the unconditional usage of the default arguments:

    ### CODE ###
    class Hash
      def key?(val)
        false
      end
    end
    
    def blah(a: 2, b: 5)
      puts a + b
    end
    blah
    blah(a:3)
    blah(b:3)
    blah(a:2000, b:2000)
    
    ### DISASSEMBLY ###
    == disasm: @>==========
    0000 trace            1                                               (   1)
    0002 putspecialobject 3
    0004 putnil
    0005 defineclass      :Hash, , 0
    0009 pop
    0010 trace            1                                               (   7)
    0012 putspecialobject 1
    0014 putspecialobject 2
    0016 putobject        :blah
    0018 putiseq          blah
    0020 opt_send_simple  
    0022 pop
    0023 trace            1                                               (  10)
    0025 putself
    0026 opt_send_simple  
    0028 pop
    0029 trace            1                                               (  11)
    0031 putself
    0032 putspecialobject 1
    0034 putobject        [:a, 3]
    0036 opt_send_simple  
    0038 opt_send_simple  
    0040 pop
    0041 trace            1                                               (  12)
    0043 putself
    0044 putspecialobject 1
    0046 putobject        [:b, 3]
    0048 opt_send_simple  
    0050 opt_send_simple  
    0052 pop
    0053 trace            1                                               (  13)
    0055 putself
    0056 putspecialobject 1
    0058 putobject        [:a, 2000, :b, 2000]
    0060 opt_send_simple  
    0062 opt_send_simple  
    0064 leave
    == disasm: @>========
    0000 trace            2                                               (   1)
    0002 trace            1                                               (   2)
    0004 putspecialobject 1
    0006 putspecialobject 2
    0008 putobject        :key?
    0010 putiseq          key?
    0012 opt_send_simple  
    0014 trace            4                                               (   5)
    0016 leave                                                            (   2)
    == disasm: >================
    local table (size: 2, argc: 1 [opts: 0, rest: -1, post: 0, block: -1, keyword: [email protected]] s1)
    [ 2] val
    0000 trace            8                                               (   2)
    0002 trace            1                                               (   3)
    0004 putobject        false
    0006 trace            16                                              (   4)
    0008 leave                                                            (   3)
    == disasm: >================
    local table (size: 4, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, keyword: [email protected]] s0)
    [ 4] a          [ 3] b          [ 2] ?
    0000 getlocal_OP__WC__0 2                                             (   7)
    0002 dup
    0003 putobject        :a
    0005 opt_send_simple  
    0007 branchunless     18
    0009 dup
    0010 putobject        :a
    0012 opt_send_simple  
    0014 setlocal_OP__WC__0 4
    0016 jump             22
    0018 putobject        2
    0020 setlocal_OP__WC__0 4
    0022 dup
    0023 putobject        :b
    0025 opt_send_simple  
    0027 branchunless     38
    0029 dup
    0030 putobject        :b
    0032 opt_send_simple  
    0034 setlocal_OP__WC__0 3
    0036 jump             42
    0038 putobject        5
    0040 setlocal_OP__WC__0 3
    0042 pop
    0043 trace            8
    0045 trace            1                                               (   8)
    0047 putself
    0048 getlocal_OP__WC__0 4
    0050 getlocal_OP__WC__0 3
    0052 opt_plus         
    0054 opt_send_simple  
    0056 trace            16                                              (   9)
    0058 leave                                                            (   8)
    
    ### RESULTS ###
    7
    7
    7
    7
    
     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
%d bloggers like this: