| 1 | $:.unshift File.dirname(__FILE__) |
|---|
| 2 | $:.unshift File.dirname(__FILE__) + '/../lib' |
|---|
| 3 | |
|---|
| 4 | require 'test_helper' |
|---|
| 5 | require 'httpauth/digest' |
|---|
| 6 | |
|---|
| 7 | class DigestCredentialsTest < Test::Unit::TestCase |
|---|
| 8 | fixtures :credentials |
|---|
| 9 | |
|---|
| 10 | def setup |
|---|
| 11 | remove_tmpdir |
|---|
| 12 | create_tmpdir |
|---|
| 13 | end |
|---|
| 14 | |
|---|
| 15 | def test_empty_initialization_from_header |
|---|
| 16 | assert_raise HTTPAuth::UnwellformedHeader do |
|---|
| 17 | HTTPAuth::Digest::Credentials.from_header nil, :tmpdir => tmpdir |
|---|
| 18 | end |
|---|
| 19 | assert_raise HTTPAuth::UnwellformedHeader do |
|---|
| 20 | HTTPAuth::Digest::Credentials.from_header '', :tmpdir => tmpdir |
|---|
| 21 | end |
|---|
| 22 | end |
|---|
| 23 | |
|---|
| 24 | def test_initialization_from_header |
|---|
| 25 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_marcel], :credentials) |
|---|
| 26 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 27 | assert_equal @@credentials[:from_marcel][:username], credentials.username |
|---|
| 28 | assert_equal @@credentials[:from_marcel][:realm], credentials.realm |
|---|
| 29 | end |
|---|
| 30 | |
|---|
| 31 | def test_validate |
|---|
| 32 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_safari2], :credentials) |
|---|
| 33 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 34 | assert credentials.validate(:method => 'GET', :password => 'secret') |
|---|
| 35 | |
|---|
| 36 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_safari2], :credentials) |
|---|
| 37 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 38 | assert credentials.validate(:method => 'GET', :digest => '659ac260760c38dce4d67663b74a71d2') |
|---|
| 39 | |
|---|
| 40 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_safari2], :credentials) |
|---|
| 41 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 42 | assert !credentials.validate(:method => 'GET', :digest => '659ac260760c38dce4d67663b74') |
|---|
| 43 | |
|---|
| 44 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_thijs], :credentials) |
|---|
| 45 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 46 | assert !credentials.validate(:method => 'GET', :password => 'secret') |
|---|
| 47 | |
|---|
| 48 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_thijs], :credentials) |
|---|
| 49 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 50 | assert credentials.validate(:method => 'GET', :password => 'wrong') |
|---|
| 51 | |
|---|
| 52 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_safari], :credentials) |
|---|
| 53 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 54 | assert !credentials.validate(:method => 'GET', :password => 'wrong') |
|---|
| 55 | |
|---|
| 56 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_safari], :credentials) |
|---|
| 57 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 58 | assert credentials.validate(:method => 'GET', :password => 'secret') |
|---|
| 59 | |
|---|
| 60 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_mustafa], :credentials) |
|---|
| 61 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 62 | assert credentials.validate(:method => 'GET', :password => 'Circle Of Life') |
|---|
| 63 | |
|---|
| 64 | directives = HTTPAuth::Digest::Utils.encode_directives(@@credentials[:from_safari], :credentials) |
|---|
| 65 | credentials = HTTPAuth::Digest::Credentials.from_header directives, :tmpdir => tmpdir |
|---|
| 66 | assert credentials.validate(:method => 'GET', :password => 'secret') |
|---|
| 67 | end |
|---|
| 68 | |
|---|
| 69 | def test_from_blank |
|---|
| 70 | credentials = HTTPAuth::Digest::Credentials.new :nc=>1, |
|---|
| 71 | :uri=>"/", |
|---|
| 72 | :opaque=>"0cf3b80a175d023ce40e7ad878dd4a2b", |
|---|
| 73 | :realm=>"Admin pages", |
|---|
| 74 | :nonce=>"MjAwNi0wOS0wNCAxNDoxNTo1Nzo2MjcyNDA6NDJmZDNjY2NiNzQ3ZjU1MDlhNTIyYTI1MWI1MTkzZm", |
|---|
| 75 | :algorithm=>"MD5", |
|---|
| 76 | :username=>"admin", |
|---|
| 77 | :digest=>"659ac260760c38dce4d67663b74a71d2", |
|---|
| 78 | :method=>"GET", |
|---|
| 79 | :response=>"5e7bbe24dac88a1936edf1a89cae6168", |
|---|
| 80 | :cnonce=>"30b49be53eab919d", |
|---|
| 81 | :qop=>"auth" |
|---|
| 82 | assert credentials.validate({}) |
|---|
| 83 | end |
|---|
| 84 | end |
|---|