PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test-hgweb-auth.py

https://bitbucket.org/mirror/mercurial/
Python | 107 lines | 92 code | 15 blank | 0 comment | 13 complexity | a817b002aa0806b09855709e2eb53a8d MD5 | raw file
Possible License(s): GPL-2.0
  1. from mercurial import demandimport; demandimport.enable()
  2. import urllib2
  3. from mercurial import ui, util
  4. from mercurial import url
  5. from mercurial.error import Abort
  6. class myui(ui.ui):
  7. def interactive(self):
  8. return False
  9. origui = myui()
  10. def writeauth(items):
  11. ui = origui.copy()
  12. for name, value in items.iteritems():
  13. ui.setconfig('auth', name, value)
  14. return ui
  15. def dumpdict(dict):
  16. return '{' + ', '.join(['%s: %s' % (k, dict[k])
  17. for k in sorted(dict.iterkeys())]) + '}'
  18. def test(auth, urls=None):
  19. print 'CFG:', dumpdict(auth)
  20. prefixes = set()
  21. for k in auth:
  22. prefixes.add(k.split('.', 1)[0])
  23. for p in prefixes:
  24. for name in ('.username', '.password'):
  25. if (p + name) not in auth:
  26. auth[p + name] = p
  27. auth = dict((k, v) for k, v in auth.iteritems() if v is not None)
  28. ui = writeauth(auth)
  29. def _test(uri):
  30. print 'URI:', uri
  31. try:
  32. pm = url.passwordmgr(ui)
  33. u, authinfo = util.url(uri).authinfo()
  34. if authinfo is not None:
  35. pm.add_password(*authinfo)
  36. print ' ', pm.find_user_password('test', u)
  37. except Abort:
  38. print 'abort'
  39. if not urls:
  40. urls = [
  41. 'http://example.org/foo',
  42. 'http://example.org/foo/bar',
  43. 'http://example.org/bar',
  44. 'https://example.org/foo',
  45. 'https://example.org/foo/bar',
  46. 'https://example.org/bar',
  47. 'https://x@example.org/bar',
  48. 'https://y@example.org/bar',
  49. ]
  50. for u in urls:
  51. _test(u)
  52. print '\n*** Test in-uri schemes\n'
  53. test({'x.prefix': 'http://example.org'})
  54. test({'x.prefix': 'https://example.org'})
  55. test({'x.prefix': 'http://example.org', 'x.schemes': 'https'})
  56. test({'x.prefix': 'https://example.org', 'x.schemes': 'http'})
  57. print '\n*** Test separately configured schemes\n'
  58. test({'x.prefix': 'example.org', 'x.schemes': 'http'})
  59. test({'x.prefix': 'example.org', 'x.schemes': 'https'})
  60. test({'x.prefix': 'example.org', 'x.schemes': 'http https'})
  61. print '\n*** Test prefix matching\n'
  62. test({'x.prefix': 'http://example.org/foo',
  63. 'y.prefix': 'http://example.org/bar'})
  64. test({'x.prefix': 'http://example.org/foo',
  65. 'y.prefix': 'http://example.org/foo/bar'})
  66. test({'x.prefix': '*', 'y.prefix': 'https://example.org/bar'})
  67. print '\n*** Test user matching\n'
  68. test({'x.prefix': 'http://example.org/foo',
  69. 'x.username': None,
  70. 'x.password': 'xpassword'},
  71. urls=['http://y@example.org/foo'])
  72. test({'x.prefix': 'http://example.org/foo',
  73. 'x.username': None,
  74. 'x.password': 'xpassword',
  75. 'y.prefix': 'http://example.org/foo',
  76. 'y.username': 'y',
  77. 'y.password': 'ypassword'},
  78. urls=['http://y@example.org/foo'])
  79. test({'x.prefix': 'http://example.org/foo/bar',
  80. 'x.username': None,
  81. 'x.password': 'xpassword',
  82. 'y.prefix': 'http://example.org/foo',
  83. 'y.username': 'y',
  84. 'y.password': 'ypassword'},
  85. urls=['http://y@example.org/foo/bar'])
  86. def testauthinfo(fullurl, authurl):
  87. print 'URIs:', fullurl, authurl
  88. pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
  89. pm.add_password(*util.url(fullurl).authinfo()[1])
  90. print pm.find_user_password('test', authurl)
  91. print '\n*** Test urllib2 and util.url\n'
  92. testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')