View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.api.jexl;
20  
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.UUID;
31  import org.apache.commons.jexl3.MapContext;
32  import org.apache.commons.lang3.SerializationUtils;
33  import org.apache.syncope.common.lib.Attr;
34  import org.apache.syncope.common.lib.to.MembershipTO;
35  import org.apache.syncope.common.lib.to.UserTO;
36  import org.apache.syncope.core.provisioning.api.AbstractTest;
37  import org.junit.jupiter.api.Test;
38  
39  public class MailTemplateTest extends AbstractTest {
40  
41      private static final String CONFIRM_PASSWORD_RESET_TEMPLATE =
42              "<html><body>"
43              + "<p>Hi,<br/> we are happy to inform you that the password request was successfully executed for "
44              + "your account.</p>  <p>Best regards.</p> </body> </html>";
45  
46      private static final String REQUEST_PASSWORD_RESET_TEMPLATE = "Hi, a password reset was requested for "
47              + "${user.getUsername()}.  In order to complete this request, you need to visit this link: "
48              + "http://localhost:9080/syncope-enduser/app/#!/confirmpasswordreset?token="
49              + "${input.get(0).replaceAll(' ', '%20')}"
50              + "If you did not request this reset, just ignore the present e-mail.  Best regards.";
51  
52      private static final String OPTIN_TEMPLATE =
53              "<html> <body> <h3>Hi ${user.getPlainAttr(\"firstname\").get().values[0]} "
54              + "${user.getPlainAttr(\"surname\").get().values[0]}, welcome to Syncope!</h3>"
55              + "<p> Your username is ${user.username}.<br/>"
56              + "Your email address is ${user.getPlainAttr(\"email\").get().values[0]}."
57              + "Your email address inside a <a href=\"http://localhost/?email="
58              + "${user.getPlainAttr(\"email\").get().values[0].replace('@', '%40')}\">link</a>.</p>"
59              + "<p>This message was sent to the following recipients: <ul>\n $$ for (recipient: recipients) {\n"
60              + "   <li>${recipient.getPlainAttr(\"email\").get().values[0]}</li>\n $$ }\n </ul>\n"
61              + "  because one of the following events occurred: <ul>\n $$ for (event: events) {\n"
62              + "   <li>${event}</li>\n $$ }\n </ul>\n </p> \n $$ if (!empty(user.memberships)) {\n"
63              + " You have been provided with the following groups:\n <ul>\n"
64              + " $$ for(membership : user.memberships) {\n   <li>${membership.groupName}</li>\n $$ }\n"
65              + " </ul>\n $$ }\n </body> </html>";
66  
67      @Test
68      public void confirmPasswordReset() throws IOException {
69          String htmlBody = JexlUtils.evaluateTemplate(CONFIRM_PASSWORD_RESET_TEMPLATE, new MapContext());
70          assertNotNull(htmlBody);
71      }
72  
73      @Test
74      public void requestPasswordReset() throws IOException {
75          Map<String, Object> ctx = new HashMap<>();
76  
77          String username = "test" + UUID.randomUUID().toString();
78          UserTO user = new UserTO();
79          user.setUsername(username);
80          ctx.put("user", user);
81  
82          String token = "token " + UUID.randomUUID().toString();
83          List<String> input = new ArrayList<>();
84          input.add(token);
85          ctx.put("input", input);
86  
87          String textBody = JexlUtils.evaluateTemplate(REQUEST_PASSWORD_RESET_TEMPLATE, new MapContext(ctx));
88  
89          assertNotNull(textBody);
90          assertTrue(textBody.contains("a password reset was requested for " + username + "."));
91          assertFalse(textBody.contains(
92                  "http://localhost:9080/syncope-enduser/app/#!/confirmpasswordreset?token="
93                  + token));
94          assertTrue(textBody.contains(
95                  "http://localhost:9080/syncope-enduser/app/#!/confirmpasswordreset?token="
96                  + token.replaceAll(" ", "%20")));
97      }
98  
99      @Test
100     public void optin() throws IOException {
101         Map<String, Object> ctx = new HashMap<>();
102 
103         String username = "test" + UUID.randomUUID().toString();
104         UserTO user = new UserTO();
105         user.setUsername(username);
106         user.getPlainAttrs().add(new Attr.Builder("firstname").value("John").build());
107         user.getPlainAttrs().add(new Attr.Builder("surname").value("Doe").build());
108         user.getPlainAttrs().add(new Attr.Builder("email").value("john.doe@syncope.apache.org").build());
109         user.getMemberships().add(new MembershipTO.Builder(UUID.randomUUID().toString()).groupName("a group").build());
110         ctx.put("user", user);
111 
112         String token = "token " + UUID.randomUUID().toString();
113         List<String> input = new ArrayList<>();
114         input.add(token);
115         ctx.put("input", input);
116 
117         UserTO recipient = SerializationUtils.clone(user);
118         recipient.getPlainAttr("email").get().getValues().set(0, "another@syncope.apache.org");
119         ctx.put("recipients", List.of(recipient));
120 
121         ctx.put("events", List.of("event1"));
122 
123         String htmlBody = JexlUtils.evaluateTemplate(OPTIN_TEMPLATE, new MapContext(ctx));
124 
125         assertNotNull(htmlBody);
126 
127         assertTrue(htmlBody.contains("Hi John Doe,"));
128         assertTrue(htmlBody.contains("Your email address is john.doe@syncope.apache.org."));
129         assertTrue(htmlBody.contains("<li>another@syncope.apache.org</li>"));
130         assertTrue(htmlBody.contains("<li>a group</li>"));
131         assertTrue(htmlBody.contains("<li>event1</li>"));
132     }
133 }