Search in sources :

Example 1 with EndpointPage

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

the class EndpointResourceTest method testSearchWithType.

@Test
void testSearchWithType() {
    String tenant = "search-type";
    String orgId = "search-type2";
    String userName = "user";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    addEndpoints(10, identityHeader);
    Response response = given().header(identityHeader).queryParam("limit", "20").queryParam("offset", "0").queryParam("name", "2").queryParam("type", "WEBHOOK").when().get("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    EndpointPage endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    assertEquals(1, endpointPage.getMeta().getCount());
    assertEquals(1, endpointPage.getData().size());
    assertEquals("Endpoint 2", endpointPage.getData().get(0).getName());
    response = given().header(identityHeader).queryParam("limit", "20").queryParam("offset", "0").queryParam("name", "foo").queryParam("type", "WEBHOOK").when().get("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    assertEquals(0, endpointPage.getMeta().getCount());
    assertEquals(0, endpointPage.getData().size());
}
Also used : Response(io.restassured.response.Response) EndpointPage(com.redhat.cloud.notifications.routers.models.EndpointPage) Header(io.restassured.http.Header) 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 2 with EndpointPage

use of com.redhat.cloud.notifications.routers.models.EndpointPage 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 3 with EndpointPage

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

the class EndpointResourceTest method testSortingOrder.

@Test
void testSortingOrder() {
    String tenant = "testSortingOrder";
    String orgId = "testSortingOrder2";
    String userName = "user";
    String identityHeaderValue = TestHelpers.encodeRHIdentityInfo(tenant, orgId, userName);
    Header identityHeader = TestHelpers.createRHIdentityHeader(identityHeaderValue);
    MockServerConfig.addMockRbacAccess(identityHeaderValue, MockServerConfig.RbacAccess.FULL_ACCESS);
    // Create 50 test-ones with sanely sortable name & enabled & disabled & type
    int[] stats = helpers.createTestEndpoints(tenant, 50);
    int disableCount = stats[1];
    int webhookCount = stats[2];
    Response response = given().header(identityHeader).when().get("/endpoints?limit=100").then().statusCode(200).contentType(JSON).extract().response();
    EndpointPage endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    Endpoint[] endpoints = endpointPage.getData().toArray(new Endpoint[0]);
    assertEquals(stats[0], endpoints.length);
    response = given().header(identityHeader).queryParam("sort_by", "enabled").when().get("/endpoints?limit=100").then().statusCode(200).contentType(JSON).extract().response();
    endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    endpoints = endpointPage.getData().toArray(new Endpoint[0]);
    assertFalse(endpoints[0].isEnabled());
    assertFalse(endpoints[disableCount - 1].isEnabled());
    assertTrue(endpoints[disableCount].isEnabled());
    assertTrue(endpoints[stats[0] - 1].isEnabled());
    response = given().header(identityHeader).queryParam("sort_by", "name:desc").queryParam("limit", "50").queryParam("offset", stats[0] - 20).when().get("/endpoints?limit=100").then().statusCode(200).contentType(JSON).extract().response();
    endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    endpoints = endpointPage.getData().toArray(new Endpoint[0]);
    assertEquals(20, endpoints.length);
    assertEquals("Endpoint 1", endpoints[endpoints.length - 1].getName());
    assertEquals("Endpoint 10", endpoints[endpoints.length - 2].getName());
    assertEquals("Endpoint 27", endpoints[0].getName());
}
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) 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 4 with EndpointPage

use of com.redhat.cloud.notifications.routers.models.EndpointPage 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 5 with EndpointPage

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

the class EndpointResourceTest method testEndpointLimiter.

@Test
void testEndpointLimiter() {
    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);
    addEndpoints(29, identityHeader);
    // Fetch the list, page 1
    Response response = given().header(identityHeader).queryParam("limit", "10").queryParam("offset", "0").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(10, endpoints.size());
    assertEquals(29, endpointPage.getMeta().getCount());
    // Fetch the list, page 3
    response = given().header(identityHeader).queryParam("limit", "10").queryParam("pageNumber", "2").when().get("/endpoints").then().statusCode(200).contentType(JSON).extract().response();
    endpointPage = Json.decodeValue(response.getBody().asString(), EndpointPage.class);
    endpoints = endpointPage.getData();
    assertEquals(9, endpoints.size());
    assertEquals(29, endpointPage.getMeta().getCount());
}
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) 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

EndpointPage (com.redhat.cloud.notifications.routers.models.EndpointPage)8 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 Endpoint (com.redhat.cloud.notifications.models.Endpoint)6 QuarkusTest (io.quarkus.test.junit.QuarkusTest)6 Test (org.junit.jupiter.api.Test)6 JsonObject (io.vertx.core.json.JsonObject)4 WebhookProperties (com.redhat.cloud.notifications.models.WebhookProperties)3 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)2 Constants (com.redhat.cloud.notifications.Constants)1 ConsoleIdentityProvider (com.redhat.cloud.notifications.auth.ConsoleIdentityProvider)1 RhIdPrincipal (com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal)1 RbacGroupValidator (com.redhat.cloud.notifications.auth.rbac.RbacGroupValidator)1 Query (com.redhat.cloud.notifications.db.Query)1 ApplicationRepository (com.redhat.cloud.notifications.db.repositories.ApplicationRepository)1 EmailSubscriptionRepository (com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository)1 EndpointRepository (com.redhat.cloud.notifications.db.repositories.EndpointRepository)1 NotificationRepository (com.redhat.cloud.notifications.db.repositories.NotificationRepository)1