From e0bdb42d41ed2a2f273fffd96ee0dce8fcc4ec79 Mon Sep 17 00:00:00 2001 From: Pedro Igor Date: Thu, 25 Apr 2024 17:14:38 -0300 Subject: [PATCH] adding test and minor updates to cover inviting existing users Signed-off-by: Pedro Igor --- .../resource/OrganizationMembersResource.java | 5 ++ .../inviteorg/InviteOrgActionToken.java | 2 +- .../InviteOrgActionTokenHandler.java | 5 -- .../resource/OrganizationMemberResource.java | 13 +-- .../keycloak/testsuite/pages/InfoPage.java | 7 ++ .../admin/OrganizationInvitationLinkTest.java | 87 +++++++++++++++++++ .../theme/base/email/text/org-invite.ftl | 2 + 7 files changed, 110 insertions(+), 11 deletions(-) create mode 100755 testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/organization/admin/OrganizationInvitationLinkTest.java create mode 100644 themes/src/main/resources/theme/base/email/text/org-invite.ftl diff --git a/integration/admin-client-jee/src/main/java/org/keycloak/admin/client/resource/OrganizationMembersResource.java b/integration/admin-client-jee/src/main/java/org/keycloak/admin/client/resource/OrganizationMembersResource.java index ec5199ee77..a3b6ab6fb7 100644 --- a/integration/admin-client-jee/src/main/java/org/keycloak/admin/client/resource/OrganizationMembersResource.java +++ b/integration/admin-client-jee/src/main/java/org/keycloak/admin/client/resource/OrganizationMembersResource.java @@ -73,4 +73,9 @@ public interface OrganizationMembersResource { @Path("{id}") OrganizationMemberResource member(@PathParam("id") String id); + + @POST + @Path("invite") + @Consumes(MediaType.APPLICATION_JSON) + Response inviteMember(UserRepresentation rep); } diff --git a/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionToken.java b/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionToken.java index b09daa8aca..d93163672d 100644 --- a/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionToken.java +++ b/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionToken.java @@ -27,7 +27,7 @@ import org.keycloak.models.Constants; */ public class InviteOrgActionToken extends DefaultActionToken { - public static final String TOKEN_TYPE = "invite-org"; + public static final String TOKEN_TYPE = "ORGIVT"; private static final String JSON_FIELD_REDIRECT_URI = "reduri"; private static final String JSON_ORG_ID = "org_id"; diff --git a/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionTokenHandler.java b/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionTokenHandler.java index b07b63afe8..50ebc2276e 100644 --- a/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionTokenHandler.java +++ b/services/src/main/java/org/keycloak/authentication/actiontoken/inviteorg/InviteOrgActionTokenHandler.java @@ -133,9 +133,4 @@ public class InviteOrgActionTokenHandler extends AbstractActionTokenHandler smtpConfig = testRealm.getSmtpServer(); + super.configureTestRealm(testRealm); + testRealm.setSmtpServer(smtpConfig); + } + + @Test + public void testInviteExistingUser() throws IOException { + UserRepresentation user = UserBuilder.create() + .username("invited") + .email("invited@myemail.com") + .password("password") + .enabled(true) + .build(); + try (Response response = testRealm().users().create(user)) { + user.setId(ApiUtil.getCreatedId(response)); + } + + OrganizationResource organization = testRealm().organizations().get(createOrganization().getId()); + + organization.members().inviteMember(user).close(); + + MimeMessage message = greenMail.getLastReceivedMessage(); + Assert.assertNotNull(message); + String link = MailUtils.getPasswordResetEmailLink(message); + driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.DAYS); + driver.navigate().to(link.trim()); + Assert.assertFalse(organization.members().getAll().stream().anyMatch(actual -> user.getId().equals(actual.getId()))); + infoPage.clickToContinue(); + assertThat(infoPage.getInfo(), containsString("Your account has been updated.")); + Assert.assertTrue(organization.members().getAll().stream().anyMatch(actual -> user.getId().equals(actual.getId()))); + } +} \ No newline at end of file diff --git a/themes/src/main/resources/theme/base/email/text/org-invite.ftl b/themes/src/main/resources/theme/base/email/text/org-invite.ftl new file mode 100644 index 0000000000..afc32a7230 --- /dev/null +++ b/themes/src/main/resources/theme/base/email/text/org-invite.ftl @@ -0,0 +1,2 @@ +<#ftl output_format="plainText"> +${kcSanitize(msg("orgInviteBodyHtml", link, linkExpiration, realmName, linkExpirationFormatter(linkExpiration)))}