Search in sources :

Example 21 with Endpoint

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

the class EmailTest method testEmailSubscriptionInstant.

@Test
@Disabled
void testEmailSubscriptionInstant() {
    mockGetUsers(8);
    final String tenant = "instant-email-tenant";
    final String[] usernames = { "username-1", "username-2", "username-4" };
    String bundle = "rhel";
    String application = "policies";
    for (String username : usernames) {
        subscribe(tenant, username, bundle, application);
    }
    final List<String> bodyRequests = new ArrayList<>();
    ExpectationResponseCallback verifyEmptyRequest = req -> {
        assertEquals(BOP_TOKEN, req.getHeader(EmailSender.BOP_APITOKEN_HEADER).get(0));
        assertEquals(BOP_CLIENT_ID, req.getHeader(EmailSender.BOP_CLIENT_ID_HEADER).get(0));
        assertEquals(BOP_ENV, req.getHeader(EmailSender.BOP_ENV_HEADER).get(0));
        bodyRequests.add(req.getBodyAsString());
        return response().withStatusCode(200);
    };
    HttpRequest postReq = getMockHttpRequest(verifyEmptyRequest);
    Action emailActionMessage = TestHelpers.createPoliciesAction(tenant, bundle, application, "My test machine");
    Event event = new Event();
    event.setAction(emailActionMessage);
    EmailSubscriptionProperties properties = new EmailSubscriptionProperties();
    Endpoint ep = new Endpoint();
    ep.setType(EndpointType.EMAIL_SUBSCRIPTION);
    ep.setName("positive feeling");
    ep.setDescription("needle in the haystack");
    ep.setEnabled(true);
    ep.setProperties(properties);
    try {
        List<NotificationHistory> historyEntries = statelessSessionFactory.withSession(statelessSession -> {
            return emailProcessor.process(event, List.of(ep));
        });
        NotificationHistory history = historyEntries.get(0);
        assertTrue(history.isInvocationResult());
        assertEquals(3, bodyRequests.size());
        List<JsonObject> emailRequests = emailRequestIsOK(bodyRequests, usernames);
        for (int i = 0; i < usernames.length; ++i) {
            JsonObject body = emailRequests.get(i);
            JsonArray emails = body.getJsonArray("emails");
            assertNotNull(emails);
            assertEquals(1, emails.size());
            JsonObject firstEmail = emails.getJsonObject(0);
            JsonArray recipients = firstEmail.getJsonArray("recipients");
            assertEquals(1, recipients.size());
            assertEquals(usernames[i], recipients.getString(0));
            JsonArray bccList = firstEmail.getJsonArray("bccList");
            assertEquals(0, bccList.size());
            String bodyRequest = body.toString();
            assertTrue(bodyRequest.contains(TestHelpers.policyId1), "Body should contain policy id" + TestHelpers.policyId1);
            assertTrue(bodyRequest.contains(TestHelpers.policyName1), "Body should contain policy name" + TestHelpers.policyName1);
            assertTrue(bodyRequest.contains(TestHelpers.policyId2), "Body should contain policy id" + TestHelpers.policyId2);
            assertTrue(bodyRequest.contains(TestHelpers.policyName2), "Body should contain policy name" + TestHelpers.policyName2);
            // Display name
            assertTrue(bodyRequest.contains("My test machine"), "Body should contain the display_name");
            // Formatted date
            assertTrue(bodyRequest.contains("03 Aug 2020 15:22 UTC"));
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e);
    } finally {
        // Remove expectations
        MockServerLifecycleManager.getClient().clear(postReq);
    }
    clearSubscriptions();
}
Also used : INSTANT(com.redhat.cloud.notifications.models.EmailSubscriptionType.INSTANT) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) HttpRequest(org.mockserver.model.HttpRequest) Endpoint(com.redhat.cloud.notifications.models.Endpoint) ITUserResponse(com.redhat.cloud.notifications.recipients.itservice.pojo.response.ITUserResponse) LocalDateTime(java.time.LocalDateTime) Disabled(org.junit.jupiter.api.Disabled) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) MockServerLifecycleManager(com.redhat.cloud.notifications.MockServerLifecycleManager) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) InjectSpy(io.quarkus.test.junit.mockito.InjectSpy) ITUserRequest(com.redhat.cloud.notifications.recipients.itservice.pojo.request.ITUserRequest) Event(com.redhat.cloud.notifications.models.Event) JsonObject(io.vertx.core.json.JsonObject) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Payload(com.redhat.cloud.notifications.ingress.Payload) Context(com.redhat.cloud.notifications.ingress.Context) InjectMock(io.quarkus.test.junit.mockito.InjectMock) RestClient(org.eclipse.microprofile.rest.client.inject.RestClient) Transactional(javax.transaction.Transactional) ExpectationResponseCallback(org.mockserver.mock.action.ExpectationResponseCallback) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) ITUserService(com.redhat.cloud.notifications.recipients.itservice.ITUserService) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) Mockito(org.mockito.Mockito) List(java.util.List) Authentication(com.redhat.cloud.notifications.recipients.itservice.pojo.response.Authentication) EndpointType(com.redhat.cloud.notifications.models.EndpointType) StatelessSessionFactory(com.redhat.cloud.notifications.db.StatelessSessionFactory) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AccountRelationship(com.redhat.cloud.notifications.recipients.itservice.pojo.response.AccountRelationship) TestHelpers(com.redhat.cloud.notifications.TestHelpers) Metadata(com.redhat.cloud.notifications.ingress.Metadata) Action(com.redhat.cloud.notifications.ingress.Action) Comparator(java.util.Comparator) HttpResponse.response(org.mockserver.model.HttpResponse.response) PersonalInformation(com.redhat.cloud.notifications.recipients.itservice.pojo.response.PersonalInformation) HttpRequest(org.mockserver.model.HttpRequest) Action(com.redhat.cloud.notifications.ingress.Action) ExpectationResponseCallback(org.mockserver.mock.action.ExpectationResponseCallback) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) ArrayList(java.util.ArrayList) JsonObject(io.vertx.core.json.JsonObject) Endpoint(com.redhat.cloud.notifications.models.Endpoint) JsonArray(io.vertx.core.json.JsonArray) Endpoint(com.redhat.cloud.notifications.models.Endpoint) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) Event(com.redhat.cloud.notifications.models.Event) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 22 with Endpoint

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

the class BehaviorGroupRepositoryTest method testAddAndDeleteBehaviorGroupAction.

@Test
void testAddAndDeleteBehaviorGroupAction() {
    Bundle bundle = resourceHelpers.createBundle();
    BehaviorGroup behaviorGroup1 = resourceHelpers.createBehaviorGroup(DEFAULT_ACCOUNT_ID, "Behavior group 1", bundle.getId());
    BehaviorGroup behaviorGroup2 = resourceHelpers.createBehaviorGroup(DEFAULT_ACCOUNT_ID, "Behavior group 2", bundle.getId());
    Endpoint endpoint1 = resourceHelpers.createEndpoint(DEFAULT_ACCOUNT_ID, WEBHOOK);
    Endpoint endpoint2 = resourceHelpers.createEndpoint(DEFAULT_ACCOUNT_ID, WEBHOOK);
    Endpoint endpoint3 = resourceHelpers.createEndpoint(DEFAULT_ACCOUNT_ID, WEBHOOK);
    // At the beginning of the test, endpoint1 shouldn't be linked with any behavior group.
    findBehaviorGroupsByEndpointId(endpoint1.getId());
    updateAndCheckBehaviorGroupActions(DEFAULT_ACCOUNT_ID, bundle.getId(), behaviorGroup1.getId(), OK, endpoint1.getId());
    updateAndCheckBehaviorGroupActions(DEFAULT_ACCOUNT_ID, bundle.getId(), behaviorGroup1.getId(), OK, endpoint1.getId());
    updateAndCheckBehaviorGroupActions(DEFAULT_ACCOUNT_ID, bundle.getId(), behaviorGroup1.getId(), OK, endpoint1.getId(), endpoint2.getId());
    // Now, endpoint1 should be linked with behaviorGroup1.
    findBehaviorGroupsByEndpointId(endpoint1.getId(), behaviorGroup1.getId());
    updateAndCheckBehaviorGroupActions(DEFAULT_ACCOUNT_ID, bundle.getId(), behaviorGroup2.getId(), OK, endpoint1.getId());
    // Then, endpoint1 should be linked with both behavior groups.
    findBehaviorGroupsByEndpointId(endpoint1.getId(), behaviorGroup1.getId(), behaviorGroup2.getId());
    updateAndCheckBehaviorGroupActions(DEFAULT_ACCOUNT_ID, bundle.getId(), behaviorGroup1.getId(), OK, endpoint2.getId());
    updateAndCheckBehaviorGroupActions(DEFAULT_ACCOUNT_ID, bundle.getId(), behaviorGroup1.getId(), OK, endpoint3.getId(), endpoint2.getId(), endpoint1.getId());
    updateAndCheckBehaviorGroupActions(DEFAULT_ACCOUNT_ID, bundle.getId(), behaviorGroup1.getId(), OK);
    // The link between endpoint1 and behaviorGroup1 was removed. Let's check it is still linked with behaviorGroup2.
    findBehaviorGroupsByEndpointId(endpoint1.getId(), behaviorGroup2.getId());
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) Bundle(com.redhat.cloud.notifications.models.Bundle) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) Test(org.junit.jupiter.api.Test)

Example 23 with Endpoint

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

the class BehaviorGroupRepositoryTest method testAddMultipleEmailSubscriptionBehaviorGroupActions.

@Test
void testAddMultipleEmailSubscriptionBehaviorGroupActions() {
    Bundle bundle = resourceHelpers.createBundle();
    BehaviorGroup behaviorGroup = resourceHelpers.createBehaviorGroup(DEFAULT_ACCOUNT_ID, "displayName", bundle.getId());
    Endpoint endpoint1 = resourceHelpers.createEndpoint(DEFAULT_ACCOUNT_ID, EMAIL_SUBSCRIPTION);
    Endpoint endpoint2 = resourceHelpers.createEndpoint(DEFAULT_ACCOUNT_ID, EMAIL_SUBSCRIPTION);
    updateAndCheckBehaviorGroupActions(DEFAULT_ACCOUNT_ID, bundle.getId(), behaviorGroup.getId(), OK, endpoint1.getId(), endpoint2.getId());
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) Bundle(com.redhat.cloud.notifications.models.Bundle) BehaviorGroup(com.redhat.cloud.notifications.models.BehaviorGroup) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) Test(org.junit.jupiter.api.Test)

Example 24 with Endpoint

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

the class LifecycleITest method createWebhookEndpoint.

private String createWebhookEndpoint(Header identityHeader, String secretToken) {
    WebhookProperties properties = new WebhookProperties();
    properties.setMethod(HttpType.POST);
    properties.setDisableSslVerification(true);
    properties.setSecretToken(secretToken);
    properties.setUrl(getMockServerUrl() + WEBHOOK_MOCK_PATH);
    Endpoint endpoint = new Endpoint();
    endpoint.setType(EndpointType.WEBHOOK);
    endpoint.setName("endpoint");
    endpoint.setDescription("Endpoint");
    endpoint.setEnabled(true);
    endpoint.setProperties(properties);
    String responseBody = given().basePath(API_INTEGRATIONS_V_1_0).header(identityHeader).contentType(JSON).body(Json.encode(endpoint)).when().post("/endpoints").then().statusCode(200).contentType(JSON).extract().body().asString();
    JsonObject jsonEndpoint = new JsonObject(responseBody);
    jsonEndpoint.mapTo(Endpoint.class);
    assertNotNull(jsonEndpoint.getString("id"));
    assertNull(jsonEndpoint.getString("accountId"));
    assertNull(jsonEndpoint.getString("orgId"));
    assertEquals(endpoint.getName(), jsonEndpoint.getString("name"));
    assertEquals(endpoint.getDescription(), jsonEndpoint.getString("description"));
    assertTrue(jsonEndpoint.getBoolean("enabled"));
    assertEquals(EndpointType.WEBHOOK.name().toLowerCase(), jsonEndpoint.getString("type"));
    JsonObject jsonWebhookProperties = jsonEndpoint.getJsonObject("properties");
    jsonWebhookProperties.mapTo(WebhookProperties.class);
    assertEquals(properties.getMethod().name(), jsonWebhookProperties.getString("method"));
    assertEquals(properties.getDisableSslVerification(), jsonWebhookProperties.getBoolean("disable_ssl_verification"));
    if (properties.getSecretToken() != null) {
        assertEquals(properties.getSecretToken(), jsonWebhookProperties.getString("secret_token"));
    }
    assertEquals(properties.getUrl(), jsonWebhookProperties.getString("url"));
    return jsonEndpoint.getString("id");
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) WebhookProperties(com.redhat.cloud.notifications.models.WebhookProperties) JsonObject(io.vertx.core.json.JsonObject)

Example 25 with Endpoint

use of com.redhat.cloud.notifications.models.Endpoint 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)

Aggregations

Endpoint (com.redhat.cloud.notifications.models.Endpoint)57 Test (org.junit.jupiter.api.Test)23 JsonObject (io.vertx.core.json.JsonObject)22 QuarkusTest (io.quarkus.test.junit.QuarkusTest)20 WebhookProperties (com.redhat.cloud.notifications.models.WebhookProperties)18 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)15 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)14 NotificationHistory (com.redhat.cloud.notifications.models.NotificationHistory)14 Header (io.restassured.http.Header)13 List (java.util.List)13 Transactional (javax.transaction.Transactional)13 Inject (javax.inject.Inject)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 EndpointType (com.redhat.cloud.notifications.models.EndpointType)10 Collectors (java.util.stream.Collectors)10 EmailSubscriptionProperties (com.redhat.cloud.notifications.models.EmailSubscriptionProperties)9 Event (com.redhat.cloud.notifications.models.Event)9 Response (io.restassured.response.Response)9 Map (java.util.Map)9 UUID (java.util.UUID)9