Search in sources :

Example 6 with WebhookProperties

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

the class EndpointResourceTest method testWebhookAttributes.

@Test
void testWebhookAttributes() {
    String tenant = "testWebhookAttributes";
    String orgId = "testWebhookAttributes2";
    String userName = "user";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    // Add new endpoints
    WebhookProperties properties = new WebhookProperties();
    properties.setMethod(HttpType.POST);
    properties.setDisableSslVerification(false);
    properties.setSecretToken("my-super-secret-token");
    properties.setBasicAuthentication(new BasicAuthentication("myuser", "mypassword"));
    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 single endpoint also and verify
    JsonObject responsePointSingle = fetchSingle(responsePoint.getString("id"), identityHeader);
    assertNotNull(responsePoint.getJsonObject("properties"));
    assertTrue(responsePointSingle.getBoolean("enabled"));
    assertNotNull(responsePointSingle.getJsonObject("properties"));
    assertNotNull(responsePointSingle.getJsonObject("properties").getString("secret_token"));
    JsonObject attr = responsePointSingle.getJsonObject("properties");
    attr.mapTo(WebhookProperties.class);
    assertNotNull(attr.getJsonObject("basic_authentication"));
    assertEquals("mypassword", attr.getJsonObject("basic_authentication").getString("password"));
}
Also used : Response(io.restassured.response.Response) Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) BasicAuthentication(com.redhat.cloud.notifications.models.BasicAuthentication) 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 7 with WebhookProperties

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

the class EndpointResourceTest method addEndpoints.

private void addEndpoints(int count, Header identityHeader) {
    for (int i = 0; i < count; i++) {
        // Add new endpoints
        WebhookProperties properties = new WebhookProperties();
        properties.setMethod(HttpType.POST);
        properties.setDisableSslVerification(false);
        properties.setSecretToken("my-super-secret-token");
        properties.setUrl(getMockServerUrl() + "/" + i);
        Endpoint ep = new Endpoint();
        ep.setType(EndpointType.WEBHOOK);
        ep.setName(String.format("Endpoint %d", i));
        ep.setDescription("Try to find me!");
        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"));
    }
}
Also used : Response(io.restassured.response.Response) Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) JsonObject(io.vertx.core.json.JsonObject) Endpoint(com.redhat.cloud.notifications.models.Endpoint)

Example 8 with WebhookProperties

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

the class EndpointResourceTest method testEndpointUpdates.

@Test
void testEndpointUpdates() {
    String tenant = "updates";
    String orgId = "updates2";
    String userName = "testEndpointUpdates";
    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).contentType(JSON).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"));
    // Update the endpoint
    responsePointSingle.put("name", "endpoint found");
    JsonObject attrSingle = responsePointSingle.getJsonObject("properties");
    attrSingle.mapTo(WebhookProperties.class);
    attrSingle.put("secret_token", "not-so-secret-anymore");
    // Update without payload
    given().header(identityHeader).contentType(JSON).when().put(String.format("/endpoints/%s", responsePointSingle.getString("id"))).then().statusCode(400);
    // With payload
    given().header(identityHeader).contentType(JSON).when().body(responsePointSingle.encode()).put(String.format("/endpoints/%s", responsePointSingle.getString("id"))).then().statusCode(200).contentType(TEXT);
    // Fetch single one again to see that the updates were done
    JsonObject updatedEndpoint = fetchSingle(responsePointSingle.getString("id"), identityHeader);
    JsonObject attrSingleUpdated = updatedEndpoint.getJsonObject("properties");
    attrSingleUpdated.mapTo(WebhookProperties.class);
    assertEquals("endpoint found", updatedEndpoint.getString("name"));
    assertEquals("not-so-secret-anymore", attrSingleUpdated.getString("secret_token"));
}
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 9 with WebhookProperties

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

the class EndpointResourceTest method testConnectionCount.

@Test
void testConnectionCount() {
    String tenant = "count";
    String orgId = "count2";
    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}}"));
    for (int i = 0; i < 200; i++) {
        // 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" + i);
        ep.setDescription("needle in the haystack" + i);
        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
        given().header(identityHeader).when().get("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    }
}
Also used : Response(io.restassured.response.Response) 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) Endpoint(com.redhat.cloud.notifications.models.Endpoint) 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 10 with WebhookProperties

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

the class EndpointResourceTest method testAddEndpointEmailSubscription.

@Test
void testAddEndpointEmailSubscription() {
    String tenant = "adding-email-subscription";
    String orgId = "adding-email-subscription2";
    String userName = "user";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    // EmailSubscription can't be created
    EmailSubscriptionProperties properties = new EmailSubscriptionProperties();
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.EMAIL_SUBSCRIPTION);
    ep.setName("Endpoint: EmailSubscription");
    ep.setDescription("Subscribe!");
    ep.setEnabled(true);
    ep.setProperties(properties);
    String stringResponse = given().header(identityHeader).when().contentType(JSON).body(Json.encode(ep)).post("/endpoints").then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    RequestEmailSubscriptionProperties requestProps = new RequestEmailSubscriptionProperties();
    // EmailSubscription can be fetch from the properties
    Response response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(requestProps)).post("/endpoints/system/email_subscription").then().statusCode(200).contentType(JSON).extract().response();
    JsonObject responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertNotNull(responsePoint.getString("id"));
    // It is always enabled
    assertEquals(true, responsePoint.getBoolean("enabled"));
    // Calling again yields the same endpoint id
    String defaultEndpointId = responsePoint.getString("id");
    response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(requestProps)).post("/endpoints/system/email_subscription").then().statusCode(200).contentType(JSON).extract().response();
    responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertEquals(defaultEndpointId, responsePoint.getString("id"));
    // Different properties are different endpoints
    Set<String> endpointIds = new HashSet<>();
    endpointIds.add(defaultEndpointId);
    requestProps.setOnlyAdmins(true);
    response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(requestProps)).post("/endpoints/system/email_subscription").then().statusCode(200).contentType(JSON).extract().response();
    responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertFalse(endpointIds.contains(responsePoint.getString("id")));
    endpointIds.add(responsePoint.getString("id"));
    response = given().header(identityHeader).when().contentType(JSON).body(Json.encode(requestProps)).post("/endpoints/system/email_subscription").then().statusCode(200).contentType(JSON).extract().response();
    responsePoint = new JsonObject(response.getBody().asString());
    responsePoint.mapTo(Endpoint.class);
    assertTrue(endpointIds.contains(responsePoint.getString("id")));
    // It is not possible to delete it
    stringResponse = given().header(identityHeader).when().delete("/endpoints/" + defaultEndpointId).then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    // It is not possible to disable or enable it
    stringResponse = given().header(identityHeader).when().delete("/endpoints/" + defaultEndpointId + "/enable").then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    stringResponse = given().header(identityHeader).when().put("/endpoints/" + defaultEndpointId + "/enable").then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    // It is not possible to update it
    stringResponse = given().header(identityHeader).contentType(JSON).body(Json.encode(ep)).when().put("/endpoints/" + defaultEndpointId).then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
    // It is not possible to update it to other type
    ep.setType(EndpointType.WEBHOOK);
    WebhookProperties webhookProperties = new WebhookProperties();
    webhookProperties.setMethod(HttpType.POST);
    webhookProperties.setDisableSslVerification(false);
    webhookProperties.setSecretToken("my-super-secret-token");
    webhookProperties.setUrl(getMockServerUrl());
    ep.setProperties(webhookProperties);
    stringResponse = given().header(identityHeader).contentType(JSON).body(Json.encode(ep)).when().put("/endpoints/" + defaultEndpointId).then().statusCode(400).extract().asString();
    assertSystemEndpointTypeError(stringResponse, EndpointType.EMAIL_SUBSCRIPTION);
}
Also used : Response(io.restassured.response.Response) RequestEmailSubscriptionProperties(com.redhat.cloud.notifications.routers.models.RequestEmailSubscriptionProperties) Header(io.restassured.http.Header) Endpoint(com.redhat.cloud.notifications.models.Endpoint) RequestEmailSubscriptionProperties(com.redhat.cloud.notifications.routers.models.RequestEmailSubscriptionProperties) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) JsonObject(io.vertx.core.json.JsonObject) HashSet(java.util.HashSet) 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)

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