Fix handling of keystore creation

Put the keystore management code in a `before` block, so as to stop abusing mocha, and make sure keystore is created before we try to use it in a test.