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