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();
}
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());
}
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());
}
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");
}
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"));
}
Aggregations