Search in sources :

Example 1 with UserConfigPreferences

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

the class UserConfigResource method getPreferences.

@GET
@Path("/notification-preference/{bundleName}/{applicationName}")
@Produces(APPLICATION_JSON)
@Operation(hidden = true)
public UserConfigPreferences getPreferences(@Context SecurityContext sec, @PathParam("bundleName") String bundleName, @PathParam("applicationName") String applicationName) {
    final RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    final String account = principal.getAccount();
    final String name = principal.getName();
    final UserConfigPreferences preferences = new UserConfigPreferences();
    // TODO Get the DAILY and INSTANT subscriptions with a single SQL query and return UserConfigPreferences directly from Hibernate.
    EmailSubscription daily = emailSubscriptionRepository.getEmailSubscription(account, name, bundleName, applicationName, EmailSubscriptionType.DAILY);
    preferences.setDailyEmail(daily != null);
    EmailSubscription instant = emailSubscriptionRepository.getEmailSubscription(account, name, bundleName, applicationName, EmailSubscriptionType.INSTANT);
    preferences.setInstantEmail(instant != null);
    return preferences;
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) UserConfigPreferences(com.redhat.cloud.notifications.routers.models.UserConfigPreferences) EmailSubscription(com.redhat.cloud.notifications.models.EmailSubscription) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(org.eclipse.microprofile.openapi.annotations.Operation)

Example 2 with UserConfigPreferences

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

the class UserConfigResourceTest method testSettings.

@Test
void testSettings() {
    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);
    String bundle = "rhel";
    String application = "policies";
    when(templateEngineClient.isSubscriptionTypeSupported(bundle, application, INSTANT)).thenReturn(TRUE);
    when(templateEngineClient.isSubscriptionTypeSupported(bundle, application, DAILY)).thenReturn(TRUE);
    SettingsValueJsonForm jsonForm = given().header(identityHeader).queryParam("bundleName", bundle).when().get("/user-config/notification-preference").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
    Field rhelPolicy = rhelPolicyForm(jsonForm);
    assertNotNull(rhelPolicy, "RHEL policies not found");
    SettingsValues settingsValues = createSettingsValue(bundle, application, false, false);
    given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValues)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
    jsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
    rhelPolicy = rhelPolicyForm(jsonForm);
    assertNotNull(rhelPolicy, "RHEL policies not found");
    Map<EmailSubscriptionType, Boolean> initialValues = extractNotificationValues(rhelPolicy, bundle, application);
    assertEquals(initialValues, settingsValues.bundles.get(bundle).applications.get(application).notifications);
    UserConfigPreferences preferences = given().header(identityHeader).when().get(String.format("/user-config/notification-preference/%s/%s", bundle, application)).then().statusCode(200).contentType(JSON).extract().body().as(UserConfigPreferences.class);
    assertEquals(false, preferences.getDailyEmail());
    assertEquals(false, preferences.getInstantEmail());
    // Daily to true
    settingsValues = createSettingsValue(bundle, application, true, false);
    given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValues)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
    jsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
    rhelPolicy = rhelPolicyForm(jsonForm);
    assertNotNull(rhelPolicy, "RHEL policies not found");
    initialValues = extractNotificationValues(rhelPolicy, bundle, application);
    assertEquals(initialValues, settingsValues.bundles.get(bundle).applications.get(application).notifications);
    preferences = given().header(identityHeader).when().get(String.format("/user-config/notification-preference/%s/%s", bundle, application)).then().statusCode(200).contentType(JSON).extract().body().as(UserConfigPreferences.class);
    assertEquals(true, preferences.getDailyEmail());
    assertEquals(false, preferences.getInstantEmail());
    // Instant to true
    settingsValues = createSettingsValue(bundle, application, false, true);
    given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValues)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
    jsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
    rhelPolicy = rhelPolicyForm(jsonForm);
    assertNotNull(rhelPolicy, "RHEL policies not found");
    initialValues = extractNotificationValues(rhelPolicy, bundle, application);
    assertEquals(initialValues, settingsValues.bundles.get(bundle).applications.get(application).notifications);
    preferences = given().header(identityHeader).when().get(String.format("/user-config/notification-preference/%s/%s", bundle, application)).then().statusCode(200).contentType(JSON).extract().body().as(UserConfigPreferences.class);
    assertEquals(false, preferences.getDailyEmail());
    assertEquals(true, preferences.getInstantEmail());
    // Both to true
    settingsValues = createSettingsValue(bundle, application, true, true);
    given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValues)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
    jsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
    rhelPolicy = rhelPolicyForm(jsonForm);
    assertNotNull(rhelPolicy, "RHEL policies not found");
    initialValues = extractNotificationValues(rhelPolicy, bundle, application);
    assertEquals(initialValues, settingsValues.bundles.get(bundle).applications.get(application).notifications);
    preferences = given().header(identityHeader).when().get(String.format("/user-config/notification-preference/%s/%s", bundle, application)).then().statusCode(200).contentType(JSON).extract().body().as(UserConfigPreferences.class);
    assertEquals(true, preferences.getDailyEmail());
    assertEquals(true, preferences.getInstantEmail());
    // does not fail if we have unknown apps in our bundle's settings
    emailSubscriptionRepository.subscribe(tenant, username, bundle, "not-found-app", DAILY);
    given().header(identityHeader).when().queryParam("bundleName", bundle).get("/user-config/notification-preference").then().statusCode(200).contentType(JSON);
    emailSubscriptionRepository.unsubscribe(tenant, username, "not-found-bundle", "not-found-app", DAILY);
    // Fails if we don't specify the bundleName
    given().header(identityHeader).when().get("/user-config/notification-preference").then().statusCode(400).contentType(JSON);
    // does not add if we try to create unknown bundle/apps
    SettingsValues settingsValue = createSettingsValue("not-found-bundle-2", "not-found-app-2", true, true);
    given().header(identityHeader).when().contentType(JSON).body(Json.encode(settingsValue)).post("/user-config/notification-preference").then().statusCode(200).contentType(TEXT);
    assertNull(emailSubscriptionRepository.getEmailSubscription(tenant, username, "not-found-bundle-2", "not-found-app-2", DAILY));
    assertNull(emailSubscriptionRepository.getEmailSubscription(tenant, username, "not-found-bundle", "not-found-app", INSTANT));
    // Does not add event type if is not supported by the templates
    when(templateEngineClient.isSubscriptionTypeSupported(bundle, application, DAILY)).thenReturn(FALSE);
    SettingsValueJsonForm settingsValueJsonForm = given().header(identityHeader).when().get("/user-config/notification-preference?bundleName=rhel").then().statusCode(200).contentType(JSON).extract().body().as(SettingsValueJsonForm.class);
    Field rhelPolicy2 = rhelPolicyForm(settingsValueJsonForm);
    assertNotNull(rhelPolicy2, "RHEL policies not found");
    assertEquals(1, rhelPolicy2.fields.get(0).fields.size());
    assertEquals("bundles[rhel].applications[policies].notifications[INSTANT]", rhelPolicy2.fields.get(0).fields.get(0).name);
}
Also used : Field(com.redhat.cloud.notifications.routers.models.SettingsValueJsonForm.Field) Header(io.restassured.http.Header) SettingsValues(com.redhat.cloud.notifications.routers.models.SettingsValues) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) SettingsValueJsonForm(com.redhat.cloud.notifications.routers.models.SettingsValueJsonForm) UserConfigPreferences(com.redhat.cloud.notifications.routers.models.UserConfigPreferences) QuarkusTest(io.quarkus.test.junit.QuarkusTest) DbIsolatedTest(com.redhat.cloud.notifications.db.DbIsolatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

UserConfigPreferences (com.redhat.cloud.notifications.routers.models.UserConfigPreferences)2 RhIdPrincipal (com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal)1 DbIsolatedTest (com.redhat.cloud.notifications.db.DbIsolatedTest)1 EmailSubscription (com.redhat.cloud.notifications.models.EmailSubscription)1 EmailSubscriptionType (com.redhat.cloud.notifications.models.EmailSubscriptionType)1 SettingsValueJsonForm (com.redhat.cloud.notifications.routers.models.SettingsValueJsonForm)1 Field (com.redhat.cloud.notifications.routers.models.SettingsValueJsonForm.Field)1 SettingsValues (com.redhat.cloud.notifications.routers.models.SettingsValues)1 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 Header (io.restassured.http.Header)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 Operation (org.eclipse.microprofile.openapi.annotations.Operation)1 Test (org.junit.jupiter.api.Test)1