Add tests for basic_authenticate() and fix base64

* Add tests for the success branch and two different failure
  branches of basic_authenticate().
* It transpires that .encode('base64') doesn't work in Python
  3, so (after much grief) replace that with a method capable
  of encoding and decoding base64 in both Python 2 and 3.