Search in sources :

Example 11 with WebhookProperties

use of com.redhat.cloud.notifications.models.WebhookProperties in project notifications-backend by RedHatInsights.

the class WebhookTest method buildWebhookEndpoint.

private static Endpoint buildWebhookEndpoint(String url) {
    WebhookProperties properties = new WebhookProperties();
    properties.setMethod(HttpType.POST);
    properties.setUrl(url);
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.WEBHOOK);
    ep.setName("positive feeling");
    ep.setDescription("needle in the haystack");
    ep.setEnabled(true);
    ep.setProperties(properties);
    return ep;
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties)

Example 12 with WebhookProperties

use of com.redhat.cloud.notifications.models.WebhookProperties in project notifications-backend by RedHatInsights.

the class EndpointResourceTest method testEndpointValidation.

@Test
void testEndpointValidation() {
    String tenant = "validation";
    String orgId = "validation2";
    String userName = "testEndpointValidation";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    // Add new endpoint without properties
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.WEBHOOK);
    ep.setName("endpoint with missing properties");
    ep.setDescription("Destined to fail");
    ep.setEnabled(true);
    expectReturn400(identityHeader, ep);
    WebhookProperties properties = new WebhookProperties();
    properties.setMethod(HttpType.POST);
    properties.setDisableSslVerification(false);
    properties.setSecretToken("my-super-secret-token");
    properties.setUrl(getMockServerUrl());
    // Test with properties, but without endpoint type
    ep.setProperties(properties);
    ep.setType(null);
    expectReturn400(identityHeader, ep);
    // Test with incorrect webhook properties
    ep.setType(EndpointType.WEBHOOK);
    ep.setName("endpoint with incorrect webhook properties");
    properties.setMethod(null);
    expectReturn400(identityHeader, ep);
    // Type and attributes don't match
    properties.setMethod(HttpType.POST);
    ep.setType(EndpointType.EMAIL_SUBSCRIPTION);
    expectReturn400(identityHeader, ep);
    ep.setName("endpoint with subtype too long");
    ep.setType(EndpointType.CAMEL);
    ep.setSubType("something-longer-than-20-chars");
    expectReturn400(identityHeader, ep);
}
Also used : Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with WebhookProperties

use of com.redhat.cloud.notifications.models.WebhookProperties in project notifications-backend by RedHatInsights.

the class EndpointResourceTest method testEndpointAdding.

@Test
void testEndpointAdding() {
    String tenant = "empty";
    String orgId = "empty";
    String userName = "user";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    // Test empty tenant
    given().header(identityHeader).when().get("/endpoints").then().statusCode(// TODO Maybe 204 here instead?
    200).contentType(JSON).body(is("{\"data\":[],\"links\":{},\"meta\":{\"count\":0}}"));
    // Add new endpoints
    WebhookProperties properties = new WebhookProperties();
    properties.setMethod(HttpType.POST);
    properties.setDisableSslVerification(false);
    properties.setSecretToken("my-super-secret-token");
    properties.setUrl(getMockServerUrl());
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.WEBHOOK);
    ep.setName("endpoint to find");
    ep.setDescription("needle in the haystack");
    ep.setEnabled(true);
    ep.setProperties(properties);
    Response response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    JsonObject responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertNotNull(responsePoint.getString("id"));
    // Fetch the list
    response = given().header(identityHeader).when().get("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    EndpointPage endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    List<Endpoint> endpoints = endpointPage.getData();
    assertEquals(1, endpoints.size());
    // Fetch single endpoint also and verify
    JsonObject responsePointSingle = fetchSingle(responsePoint.getString("id"), identityHeader);
    assertNotNull(responsePoint.getJsonObject("properties"));
    assertTrue(responsePointSingle.getBoolean("enabled"));
    // Disable and fetch
    String body = given().header(identityHeader).when().delete("/endpoints/" + responsePoint.getString("id") + "/enable").then().statusCode(204).extract().body().asString();
    assertEquals(0, body.length());
    responsePointSingle = fetchSingle(responsePoint.getString("id"), identityHeader);
    assertNotNull(responsePoint.getJsonObject("properties"));
    assertFalse(responsePointSingle.getBoolean("enabled"));
    // Enable and fetch
    given().header(identityHeader).when().put("/endpoints/" + responsePoint.getString("id") + "/enable").then().statusCode(200).contentType(TEXT).contentType(ContentType.TEXT);
    responsePointSingle = fetchSingle(responsePoint.getString("id"), identityHeader);
    assertNotNull(responsePoint.getJsonObject("properties"));
    assertTrue(responsePointSingle.getBoolean("enabled"));
    // Delete
    body = given().header(identityHeader).when().delete("/endpoints/" + responsePoint.getString("id")).then().statusCode(204).extract().body().asString();
    assertEquals(0, body.length());
    // Fetch single
    given().header(identityHeader).when().get("/endpoints/" + responsePoint.getString("id")).then().statusCode(404).contentType(JSON);
    // Fetch all, nothing should be left
    given().header(identityHeader).when().get("/endpoints").then().statusCode(200).contentType(JSON).body(is("{\"data\":[],\"links\":{},\"meta\":{\"count\":0}}"));
}
Also used : Response(io.restassured.response.Response) EndpointPage(com.redhat.cloud.notifications.routers.models.EndpointPage) Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with WebhookProperties

use of com.redhat.cloud.notifications.models.WebhookProperties in project notifications-backend by RedHatInsights.

the class EndpointResourceTest method testEndpointTypeQuery.

@ParameterizedTest
@MethodSource
void testEndpointTypeQuery(Set<EndpointType> types) {
    String tenant = "limiter";
    String orgId = "limiter2";
    String userName = "user";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    // Add webhook
    WebhookProperties properties = new WebhookProperties();
    properties.setMethod(HttpType.POST);
    properties.setDisableSslVerification(false);
    properties.setSecretToken("my-super-secret-token");
    properties.setUrl(getMockServerUrl());
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.WEBHOOK);
    ep.setName("endpoint to find");
    ep.setDescription("needle in the haystack");
    ep.setEnabled(true);
    ep.setProperties(properties);
    Response response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    JsonObject responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertNotNull(responsePoint.getString("id"));
    // Add Camel
    CamelProperties camelProperties = new CamelProperties();
    camelProperties.setDisableSslVerification(false);
    camelProperties.setSecretToken("my-super-secret-token");
    camelProperties.setUrl(getMockServerUrl());
    camelProperties.setExtras(new HashMap<>());
    Endpoint camelEp = new Endpoint();
    camelEp.setType(EndpointType.CAMEL);
    camelEp.setSubType("demo");
    camelEp.setName("endpoint to find");
    camelEp.setDescription("needle in the haystack");
    camelEp.setEnabled(true);
    camelEp.setProperties(camelProperties);
    response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(camelEp)).post("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertNotNull(responsePoint.getString("id"));
    // Fetch the list to ensure everything was inserted correctly.
    response = given().header(identityHeader).when().get("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    EndpointPage endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    List<Endpoint> endpoints = endpointPage.getData();
    assertEquals(2, endpoints.size());
    // Fetch the list with types
    response = given().header(identityHeader).queryParam("type", types).when().get("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    endpoints = endpointPage.getData();
    // Ensure there is only the requested types
    assertEquals(types, endpoints.stream().map(Endpoint::getType).collect(Collectors.toSet()));
}
Also used : Response(io.restassured.response.Response) EndpointPage(com.redhat.cloud.notifications.routers.models.EndpointPage) Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) JsonObject(io.vertx.core.json.JsonObject) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 15 with WebhookProperties

use of com.redhat.cloud.notifications.models.WebhookProperties in project notifications-backend by RedHatInsights.

the class EndpointRepository method updateEndpoint.

@Transactional
public boolean updateEndpoint(Endpoint endpoint) {
    // TODO Update could fail because the item did not exist, throw 404 in that case?
    // TODO Fix transaction so that we don't end up with half the updates applied
    String endpointQuery = "UPDATE Endpoint SET name = :name, description = :description, enabled = :enabled " + "WHERE accountId = :accountId AND id = :id";
    String webhookQuery = "UPDATE WebhookProperties SET url = :url, method = :method, " + "disableSslVerification = :disableSslVerification, secretToken = :secretToken WHERE endpoint.id = :endpointId";
    String camelQuery = "UPDATE CamelProperties SET url = :url, extras = :extras, " + "basicAuthentication = :basicAuthentication, " + "disableSslVerification = :disableSslVerification, secretToken = :secretToken WHERE endpoint.id = :endpointId";
    if (endpoint.getType() == EndpointType.EMAIL_SUBSCRIPTION) {
        throw new RuntimeException("Unable to update an endpoint of type EMAIL_SUBSCRIPTION");
    }
    int endpointRowCount = entityManager.createQuery(endpointQuery).setParameter("name", endpoint.getName()).setParameter("description", endpoint.getDescription()).setParameter("enabled", endpoint.isEnabled()).setParameter("accountId", endpoint.getAccountId()).setParameter("id", endpoint.getId()).executeUpdate();
    if (endpointRowCount == 0) {
        return false;
    } else if (endpoint.getProperties() == null) {
        return true;
    } else {
        switch(endpoint.getType()) {
            case WEBHOOK:
                WebhookProperties properties = endpoint.getProperties(WebhookProperties.class);
                return entityManager.createQuery(webhookQuery).setParameter("url", properties.getUrl()).setParameter("method", properties.getMethod()).setParameter("disableSslVerification", properties.getDisableSslVerification()).setParameter("secretToken", properties.getSecretToken()).setParameter("endpointId", endpoint.getId()).executeUpdate() > 0;
            case CAMEL:
                CamelProperties cAttr = (CamelProperties) endpoint.getProperties();
                return entityManager.createQuery(camelQuery).setParameter("url", cAttr.getUrl()).setParameter("disableSslVerification", cAttr.getDisableSslVerification()).setParameter("secretToken", cAttr.getSecretToken()).setParameter("endpointId", endpoint.getId()).setParameter("extras", cAttr.getExtras()).setParameter("basicAuthentication", cAttr.getBasicAuthentication()).executeUpdate() > 0;
            default:
                return true;
        }
    }
}
Also used : WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) Endpoint(com.redhat.cloud.notifications.models.Endpoint) Transactional(javax.transaction.Transactional)

Aggregations

WebhookProperties (com.redhat.cloud.notifications.models.WebhookProperties)15 Endpoint (com.redhat.cloud.notifications.models.Endpoint)13 JsonObject (io.vertx.core.json.JsonObject)9 Header (io.restassured.http.Header)7 Response (io.restassured.response.Response)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)6 QuarkusTest (io.quarkus.test.junit.QuarkusTest)6 Test (org.junit.jupiter.api.Test)6 EndpointPage (com.redhat.cloud.notifications.routers.models.EndpointPage)3 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)2 BasicAuthentication (com.redhat.cloud.notifications.models.BasicAuthentication)1 EmailSubscriptionProperties (com.redhat.cloud.notifications.models.EmailSubscriptionProperties)1 RequestEmailSubscriptionProperties (com.redhat.cloud.notifications.routers.models.RequestEmailSubscriptionProperties)1 Buffer (io.vertx.mutiny.core.buffer.Buffer)1 HashSet (java.util.HashSet)1 Transactional (javax.transaction.Transactional)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1