@Test public void authInfoWithAbsentUserShouldFailAuthentication(final TestContext context) { JsonObject authInfo = new JsonObject().put("username", "foo").put("password", "foo"); tomlAuth.authenticate( authInfo, context.asyncAssertFailure(th -> context.assertEquals("User not found", th.getMessage()))); }
@Test public void passwordMismatchShouldFailAuthentication(final TestContext context) { JsonObject authInfo = new JsonObject().put("username", "userA").put("password", "foo"); tomlAuth.authenticate( authInfo, context.asyncAssertFailure( th -> context.assertEquals("Invalid password", th.getMessage()))); }
@Test public void authInfoWithoutPasswordShouldFailAuthentication(final TestContext context) { JsonObject authInfo = new JsonObject().put("username", "foo"); tomlAuth.authenticate( authInfo, context.asyncAssertFailure( th -> context.assertEquals("No password provided", th.getMessage()))); }
@Test public void authInfoWithoutUsernameShouldFailAuthentication(final TestContext context) { JsonObject authInfo = new JsonObject().put("password", "foo"); tomlAuth.authenticate( authInfo, context.asyncAssertFailure( th -> context.assertEquals("No username provided", th.getMessage()))); }
@Test public void testConnectInvalidPassword(TestContext ctx) { Async async = ctx.async(); options.setPassword("incorrect"); connector.accept(ctx.asyncAssertFailure(conn -> { ctx.assertEquals("password authentication failed for user \"postgres\"", conn.getMessage()); async.complete(); })); }
@Test public void testConnectInvalidDatabase(TestContext ctx) { Async async = ctx.async(); options.setDatabase("blah_db"); connector.accept(ctx.asyncAssertFailure(conn -> { ctx.assertEquals("database \"blah_db\" does not exist", conn.getMessage()); async.complete(); })); }
@Test public void testConnectNonSSLServer(TestContext ctx) { Async async = ctx.async(); options.setSslMode(SslMode.REQUIRE).setTrustAll(true); connector.accept(ctx.asyncAssertFailure(err -> { ctx.assertEquals("Postgres Server does not handle SSL connection", err.getMessage()); async.complete(); })); }
private void testValidationError(TestContext ctx, BiConsumer<PgConnection, Handler<Throwable>> test) { Async async = ctx.async(); PgClient.connect(vertx, options(), ctx.asyncAssertSuccess(conn -> { test.accept(conn, failure -> { ctx.assertEquals(Util.buildInvalidArgsError(Stream.of("invalid-id"), Stream.of(Number.class)), failure.getMessage()); async.complete(); }); conn.preparedQuery("SELECT * FROM Fortune WHERE id=$1", Tuple.of("invalid-id"), ctx.asyncAssertFailure(failure -> { })); })); }
@Test public void testPreparedQueryValidationError(TestContext ctx) { testValidationError(ctx, (conn, cont) -> { conn.preparedQuery("SELECT * FROM Fortune WHERE id=$1", Tuple.of("invalid-id"), ctx.asyncAssertFailure(cont)); }); }
@Test public void testSslModeVerifyFullConf(TestContext ctx) { PgConnectOptions options = new PgConnectOptions(TLSTest.options) .setSslMode(SslMode.VERIFY_FULL); PgClient.connect(vertx, new PgConnectOptions(options), ctx.asyncAssertFailure(error -> { ctx.assertEquals("Host verification algorithm must be specified under verify-full sslmode", error.getMessage()); })); } }
@Test public void testTLSInvalidCertificate(TestContext ctx) { Async async = ctx.async(); PgClient.connect(vertx, new PgConnectOptions(options).setSslMode(SslMode.REQUIRE), ctx.asyncAssertFailure(err -> { ctx.assertEquals(err.getClass(), VertxException.class); ctx.assertEquals(err.getMessage(), "SSL handshake failed"); async.complete(); })); }
@Test public void testConnectInvalidUsername(TestContext ctx) { Async async = ctx.async(); options.setUser("vertx"); connector.accept(ctx.asyncAssertFailure(err -> { PgException ex = (PgException) err; // Class 28 — Invalid Authorization Specification ctx.assertEquals(ex.getCode().substring(0, 2), "28"); ctx.assertEquals(ex.getSeverity(), "FATAL"); async.complete(); })); }
@Test public void testPrepareBatchValidationError(TestContext ctx) { testValidationError(ctx, (conn, cont) -> { conn.prepare("SELECT * FROM Fortune WHERE id=$1", ctx.asyncAssertSuccess(ps -> { ps.batch(Collections.singletonList(Tuple.of("invalid-id")), ctx.asyncAssertFailure(cont)); })); }); }
@Test public void testReleaseConnectionOnSetRollback(TestContext ctx) { Async async = ctx.async(); connector.accept(ctx.asyncAssertSuccess(conn -> { conn.query("SELECT whatever from DOES_NOT_EXIST", ctx.asyncAssertFailure(result -> { // Try acquire a connection pool.getConnection(ctx.asyncAssertSuccess(v2 -> { async.complete(); })); })); })); }
@Test public void parseFailureWithIOExceptionShouldFailAuthentication(final TestContext context) { tomlAuth = new TomlAuth(vertx, new TomlAuthOptions().setTomlPath("invalid_path")); tomlAuth.authenticate( validAuthInfo, context.asyncAssertFailure( th -> { context.assertEquals(th.getClass(), NoSuchFileException.class); })); }
@Test public void timeout(TestContext tc) { SlowHttpServer slowConsul = new SlowHttpServer(vertx, 10000); ConsulClient client = ctx.createClient(new ConsulClientOptions().setPort(slowConsul.port()).setTimeout(1000)); client.agentInfo(tc.asyncAssertFailure(t -> { ctx.closeClient(client); slowConsul.close(); tc.assertTrue(t.getMessage().contains("The timeout period of 1000ms")); })); }
@Test public void testPrepareExecuteValidationError(TestContext ctx) { testValidationError(ctx, (conn, cont) -> { conn.prepare("SELECT * FROM Fortune WHERE id=$1", ctx.asyncAssertSuccess(ps -> { ps.execute(Tuple.of("invalid-id"), ctx.asyncAssertFailure(cont)); })); }); }
/** * Verifies that the base service fails for an incomplete message that does not contain mandatory fields. * * @param ctx The vert.x test context. */ @Test public void testAddFailsForIncompleteMessage(final TestContext ctx) { final EventBusMessage msg = EventBusMessage.forOperation(TenantConstants.TenantAction.add.toString()); tenantService.processRequest(msg).setHandler(ctx.asyncAssertFailure(t -> { ctx.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, ((ServiceInvocationException) t).getErrorCode()); })); }
@Test public void testAuthFailure(TestContext ctx) { Async async = ctx.async(); PgPool pool = createPool(new PgConnectOptions(options).setPassword("wrong"), 1); pool.query("SELECT id, randomnumber from WORLD", ctx.asyncAssertFailure(v2 -> { async.complete(); })); }
@Test public void testQueryParseError(TestContext ctx) { Async async = ctx.async(); PgClient.connect(vertx, options(), ctx.asyncAssertSuccess(conn -> { conn.prepare("invalid", ctx.asyncAssertFailure(err -> { PgException pgErr = (PgException) err; ctx.assertEquals(ErrorCodes.syntax_error, pgErr.getCode()); async.complete(); })); })); }